Merged up to trunk R732
[ardour.git] / libs / ardour / utils.cc
1 /*
2     Copyright (C) 2000-2003 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #include <cstdio> /* for sprintf */
22 #include <cmath>
23 #include <cctype>
24 #include <string>
25 #include <cerrno>
26 #include <iostream>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/time.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32
33 #ifdef HAVE_WORDEXP
34 #include <wordexp.h>
35 #endif
36
37 #include <pbd/error.h>
38 #include <pbd/xml++.h>
39 #include <ardour/utils.h>
40
41 #include "i18n.h"
42
43 using namespace ARDOUR;
44 using namespace std;
45 using namespace PBD;
46
47 void
48 elapsed_time_to_str (char *buf, uint32_t seconds)
49
50 {
51         uint32_t days;
52         uint32_t hours;
53         uint32_t minutes;
54         uint32_t s;
55
56         s = seconds;
57         days = s / (3600 * 24);
58         s -= (days * 3600 * 24);
59         hours = s / 3600;
60         s -= (hours * 3600);
61         minutes = s / 60;
62         s -= minutes * 60;
63         
64         if (days) {
65                 snprintf (buf, sizeof (buf), "%" PRIu32 " day%s %" PRIu32 " hour%s", 
66                          days, 
67                          days > 1 ? "s" : "",
68                          hours,
69                          hours > 1 ? "s" : "");
70         } else if (hours) {
71                 snprintf (buf, sizeof (buf), "%" PRIu32 " hour%s %" PRIu32 " minute%s", 
72                          hours, 
73                          hours > 1 ? "s" : "",
74                          minutes,
75                          minutes > 1 ? "s" : "");
76         } else if (minutes) {
77                 snprintf (buf, sizeof (buf), "%" PRIu32 " minute%s", 
78                          minutes,
79                          minutes > 1 ? "s" : "");
80         } else if (s) {
81                 snprintf (buf, sizeof (buf), "%" PRIu32 " second%s", 
82                          seconds,
83                          seconds > 1 ? "s" : "");
84         } else {
85                 snprintf (buf, sizeof (buf), "no time");
86         }
87 }
88
89 string 
90 legalize_for_path (string str)
91 {
92         string::size_type pos;
93         string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
94         string legal;
95
96         legal = str;
97         pos = 0;
98
99         while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
100                 legal.replace (pos, 1, "_");
101                 pos += 1;
102         }
103
104         return legal;
105 }
106
107 ostream&
108 operator<< (ostream& o, const BBT_Time& bbt)
109 {
110         o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
111         return o;
112 }
113
114 XMLNode *
115 find_named_node (const XMLNode& node, string name)
116 {
117         XMLNodeList nlist;
118         XMLNodeConstIterator niter;
119         XMLNode* child;
120
121         nlist = node.children();
122
123         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
124
125                 child = *niter;
126
127                 if (child->name() == name) {
128                         return child;
129                 }
130         }
131
132         return 0;
133 }
134
135 int
136 cmp_nocase (const string& s, const string& s2)
137 {
138         string::const_iterator p = s.begin();
139         string::const_iterator p2 = s2.begin();
140         
141         while (p != s.end() && p2 != s2.end()) {
142                 if (toupper(*p) != toupper(*p2)) {
143                         return (toupper(*p) < toupper(*p2)) ? -1 : 1;
144                 }
145                 ++p;
146                 ++p2;
147         }
148         
149         return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
150 }
151
152 int
153 tokenize_fullpath (string fullpath, string& path, string& name)
154 {
155         string::size_type m = fullpath.find_last_of("/");
156         
157         if (m == string::npos) {
158                 path = fullpath;
159                 name = fullpath;
160                 return 1;
161         }
162
163         // does it look like just a directory?
164         if (m == fullpath.length()-1) {
165                 return -1;
166         }
167         path = fullpath.substr(0, m+1);
168         
169         string::size_type n = fullpath.find(".ardour", m);
170         // no .ardour?
171         if (n == string::npos) {
172                 return -1;
173         }
174         name = fullpath.substr(m+1, n - m - 1);
175         return 1;
176 }
177
178 int
179 touch_file (string path)
180 {
181         int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
182         if (fd >= 0) {
183                 close (fd);
184                 return 0;
185         }
186         return 1;
187 }
188
189 string
190 placement_as_string (Placement p)
191 {
192         switch (p) {
193         case PreFader:
194                 return _("pre");
195         default: /* to get g++ to realize we have all the cases covered */
196         case PostFader:
197                 return _("post");
198         }
199 }
200
201 string
202 region_name_from_path (string path)
203 {
204         string::size_type pos;
205
206         /* remove filename suffixes etc. */
207         
208         if ((pos = path.find_last_of ('.')) != string::npos) {
209                 path = path.substr (0, pos);
210         }
211
212         /* remove any "?R", "?L" or "?[a-z]" channel identifier */
213         
214         string::size_type len = path.length();
215         
216         if (len > 3 && (path[len-2] == '%' || path[len-2] == '?') && 
217             (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
218                 
219                 path = path.substr (0, path.length() - 2);
220         }
221
222         return path;
223 }       
224
225 string
226 path_expand (string path)
227 {
228 #ifdef HAVE_WORDEXP
229         /* Handle tilde and environment variable expansion in session path */
230         string ret = path;
231
232         wordexp_t expansion;
233         switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
234         case 0:
235                 break;
236         default:
237                 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
238                 goto out;
239         }
240
241         if (expansion.we_wordc > 1) {
242                 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
243                 goto out;
244         }
245
246         ret = expansion.we_wordv[0];
247   out:
248         wordfree (&expansion);
249         return ret;
250
251 #else 
252         return path;
253 #endif
254 }
255
256 #ifdef HAVE_COREAUDIO
257 string 
258 CFStringRefToStdString(CFStringRef stringRef)
259 {
260         CFIndex size = 
261                 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) , 
262                 kCFStringEncodingUTF8);
263             char *buf = new char[size];
264         
265         std::string result;
266
267         if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
268             result = buf;
269         }
270         delete [] buf;
271         return result;
272 }
273 #endif // HAVE_COREAUDIO