Merging from trunk
[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 uint32_t long
190 get_uid()
191 {
192         struct timeval tv;
193         gettimeofday(&tv, 0);
194
195         return (uint32_t long) tv.tv_sec * 1000000 + tv.tv_usec;
196 }
197
198 string
199 placement_as_string (Placement p)
200 {
201         switch (p) {
202         case PreFader:
203                 return _("pre");
204         default: /* to get g++ to realize we have all the cases covered */
205         case PostFader:
206                 return _("post");
207         }
208 }
209
210 string
211 region_name_from_path (string path)
212 {
213         string::size_type pos;
214
215         /* remove filename suffixes etc. */
216         
217         if ((pos = path.find_last_of ('.')) != string::npos) {
218                 path = path.substr (0, pos);
219         }
220
221         /* remove any "?R", "?L" or "?[a-z]" channel identifier */
222         
223         string::size_type len = path.length();
224         
225         if (len > 3 && (path[len-2] == '%' || path[len-2] == '?') && 
226             (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
227                 
228                 path = path.substr (0, path.length() - 2);
229         }
230
231         return path;
232 }       
233
234 string
235 path_expand (string path)
236 {
237 #ifdef HAVE_WORDEXP
238         /* Handle tilde and environment variable expansion in session path */
239         string ret = path;
240
241         wordexp_t expansion;
242         switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
243         case 0:
244                 break;
245         default:
246                 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
247                 goto out;
248         }
249
250         if (expansion.we_wordc > 1) {
251                 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
252                 goto out;
253         }
254
255         ret = expansion.we_wordv[0];
256   out:
257         wordfree (&expansion);
258         return ret;
259
260 #else 
261         return path;
262 #endif
263 }
264