Merged with 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
46 void
47 elapsed_time_to_str (char *buf, uint32_t seconds)
48
49 {
50         uint32_t days;
51         uint32_t hours;
52         uint32_t minutes;
53         uint32_t s;
54
55         s = seconds;
56         days = s / (3600 * 24);
57         s -= (days * 3600 * 24);
58         hours = s / 3600;
59         s -= (hours * 3600);
60         minutes = s / 60;
61         s -= minutes * 60;
62         
63         if (days) {
64                 snprintf (buf, sizeof (buf), "%" PRIu32 " day%s %" PRIu32 " hour%s", 
65                          days, 
66                          days > 1 ? "s" : "",
67                          hours,
68                          hours > 1 ? "s" : "");
69         } else if (hours) {
70                 snprintf (buf, sizeof (buf), "%" PRIu32 " hour%s %" PRIu32 " minute%s", 
71                          hours, 
72                          hours > 1 ? "s" : "",
73                          minutes,
74                          minutes > 1 ? "s" : "");
75         } else if (minutes) {
76                 snprintf (buf, sizeof (buf), "%" PRIu32 " minute%s", 
77                          minutes,
78                          minutes > 1 ? "s" : "");
79         } else if (s) {
80                 snprintf (buf, sizeof (buf), "%" PRIu32 " second%s", 
81                          seconds,
82                          seconds > 1 ? "s" : "");
83         } else {
84                 snprintf (buf, sizeof (buf), "no time");
85         }
86 }
87
88 string 
89 legalize_for_path (string str)
90 {
91         string::size_type pos;
92         string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
93         string legal;
94
95         legal = str;
96         pos = 0;
97
98         while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
99                 legal.replace (pos, 1, "_");
100                 pos += 1;
101         }
102
103         return legal;
104 }
105
106 ostream&
107 operator<< (ostream& o, const BBT_Time& bbt)
108 {
109         o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
110         return o;
111 }
112
113 XMLNode *
114 find_named_node (const XMLNode& node, string name)
115 {
116         XMLNodeList nlist;
117         XMLNodeConstIterator niter;
118         XMLNode* child;
119
120         nlist = node.children();
121
122         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
123
124                 child = *niter;
125
126                 if (child->name() == name) {
127                         return child;
128                 }
129         }
130
131         return 0;
132 }
133
134 int
135 cmp_nocase (const string& s, const string& s2)
136 {
137         string::const_iterator p = s.begin();
138         string::const_iterator p2 = s2.begin();
139         
140         while (p != s.end() && p2 != s2.end()) {
141                 if (toupper(*p) != toupper(*p2)) {
142                         return (toupper(*p) < toupper(*p2)) ? -1 : 1;
143                 }
144                 ++p;
145                 ++p2;
146         }
147         
148         return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
149 }
150
151 int
152 tokenize_fullpath (string fullpath, string& path, string& name)
153 {
154         string::size_type m = fullpath.find_last_of("/");
155         
156         if (m == string::npos) {
157                 path = fullpath;
158                 name = fullpath;
159                 return 1;
160         }
161
162         // does it look like just a directory?
163         if (m == fullpath.length()-1) {
164                 return -1;
165         }
166         path = fullpath.substr(0, m+1);
167         
168         string::size_type n = fullpath.find(".ardour", m);
169         // no .ardour?
170         if (n == string::npos) {
171                 return -1;
172         }
173         name = fullpath.substr(m+1, n - m - 1);
174         return 1;
175 }
176
177 int
178 touch_file (string path)
179 {
180         int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
181         if (fd >= 0) {
182                 close (fd);
183                 return 0;
184         }
185         return 1;
186 }
187
188 uint32_t long
189 get_uid()
190 {
191         struct timeval tv;
192         gettimeofday(&tv, 0);
193
194         return (uint32_t long) tv.tv_sec * 1000000 + tv.tv_usec;
195 }
196
197 string
198 placement_as_string (Placement p)
199 {
200         switch (p) {
201         case PreFader:
202                 return _("pre");
203         default: /* to get g++ to realize we have all the cases covered */
204         case PostFader:
205                 return _("post");
206         }
207 }
208
209 string
210 region_name_from_path (string path)
211 {
212         string::size_type pos;
213
214         /* remove filename suffixes etc. */
215         
216         if ((pos = path.find_last_of ('.')) != string::npos) {
217                 path = path.substr (0, pos);
218         }
219
220         /* remove any "?R", "?L" or "?[a-z]" channel identifier */
221         
222         string::size_type len = path.length();
223         
224         if (len > 3 && (path[len-2] == '%' || path[len-2] == '?') && 
225             (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
226                 
227                 path = path.substr (0, path.length() - 2);
228         }
229
230         return path;
231 }       
232
233 string
234 path_expand (string path)
235 {
236 #ifdef HAVE_WORDEXP
237         /* Handle tilde and environment variable expansion in session path */
238         string ret = path;
239
240         wordexp_t expansion;
241         switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
242         case 0:
243                 break;
244         default:
245                 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
246                 goto out;
247         }
248
249         if (expansion.we_wordc > 1) {
250                 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
251                 goto out;
252         }
253
254         ret = expansion.we_wordv[0];
255   out:
256         wordfree (&expansion);
257         return ret;
258
259 #else 
260         return path;
261 #endif
262 }
263