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