003e1c92a3495f18f55ab86e2d36893a7b4e8fca
[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 #include <pbd/error.h>
34 #include <pbd/xml++.h>
35 #include <ardour/utils.h>
36
37 #include "i18n.h"
38
39 using namespace ARDOUR;
40 using namespace std;
41
42 void
43 elapsed_time_to_str (char *buf, uint32_t seconds)
44
45 {
46         uint32_t days;
47         uint32_t hours;
48         uint32_t minutes;
49         uint32_t s;
50
51         s = seconds;
52         days = s / (3600 * 24);
53         s -= (days * 3600 * 24);
54         hours = s / 3600;
55         s -= (hours * 3600);
56         minutes = s / 60;
57         s -= minutes * 60;
58         
59         if (days) {
60                 snprintf (buf, sizeof (buf), "%" PRIu32 " day%s %" PRIu32 " hour%s", 
61                          days, 
62                          days > 1 ? "s" : "",
63                          hours,
64                          hours > 1 ? "s" : "");
65         } else if (hours) {
66                 snprintf (buf, sizeof (buf), "%" PRIu32 " hour%s %" PRIu32 " minute%s", 
67                          hours, 
68                          hours > 1 ? "s" : "",
69                          minutes,
70                          minutes > 1 ? "s" : "");
71         } else if (minutes) {
72                 snprintf (buf, sizeof (buf), "%" PRIu32 " minute%s", 
73                          minutes,
74                          minutes > 1 ? "s" : "");
75         } else if (s) {
76                 snprintf (buf, sizeof (buf), "%" PRIu32 " second%s", 
77                          seconds,
78                          seconds > 1 ? "s" : "");
79         } else {
80                 snprintf (buf, sizeof (buf), "no time");
81         }
82 }
83
84 string 
85 legalize_for_path (string str)
86 {
87         string::size_type pos;
88         string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
89         string legal;
90
91         legal = str;
92         pos = 0;
93
94         while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
95                 legal.replace (pos, 1, "_");
96                 pos += 1;
97         }
98
99         return legal;
100 }
101
102 ostream&
103 operator<< (ostream& o, const BBT_Time& bbt)
104 {
105         o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
106         return o;
107 }
108
109 XMLNode *
110 find_named_node (const XMLNode& node, string name)
111 {
112         XMLNodeList nlist;
113         XMLNodeConstIterator niter;
114         XMLNode* child;
115
116         nlist = node.children();
117
118         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
119
120                 child = *niter;
121
122                 if (child->name() == name) {
123                         return child;
124                 }
125         }
126
127         return 0;
128 }
129
130 int
131 cmp_nocase (const string& s, const string& s2)
132 {
133         string::const_iterator p = s.begin();
134         string::const_iterator p2 = s2.begin();
135         
136         while (p != s.end() && p2 != s2.end()) {
137                 if (toupper(*p) != toupper(*p2)) {
138                         return (toupper(*p) < toupper(*p2)) ? -1 : 1;
139                 }
140                 ++p;
141                 ++p2;
142         }
143         
144         return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
145 }
146
147 int
148 tokenize_fullpath (string fullpath, string& path, string& name)
149 {
150         string::size_type m = fullpath.find_last_of("/");
151         
152         if (m == string::npos) {
153                 path = fullpath;
154                 name = fullpath;
155                 return 1;
156         }
157
158         // does it look like just a directory?
159         if (m == fullpath.length()-1) {
160                 return -1;
161         }
162         path = fullpath.substr(0, m+1);
163         
164         string::size_type n = fullpath.find(".ardour", m);
165         // no .ardour?
166         if (n == string::npos) {
167                 return -1;
168         }
169         name = fullpath.substr(m+1, n - m - 1);
170         return 1;
171 }
172
173 int
174 touch_file(string path)
175 {
176         FILE* file = fopen(path.c_str(), "a");
177         fclose(file);
178         return 1;
179 }
180
181 uint32_t long
182 get_uid()
183 {
184         struct timeval tv;
185         gettimeofday(&tv, 0);
186
187         return (uint32_t long) tv.tv_sec * 1000000 + tv.tv_usec;
188 }
189
190 string
191 placement_as_string (Placement p)
192 {
193         switch (p) {
194         case PreFader:
195                 return _("pre");
196         default: /* to get g++ to realize we have all the cases covered */
197         case PostFader:
198                 return _("post");
199         }
200 }
201
202 string
203 region_name_from_path (string path)
204 {
205         string::size_type pos;
206
207         /* remove filename suffixes etc. */
208         
209         if ((pos = path.find_last_of ('.')) != string::npos) {
210                 path = path.substr (0, pos);
211         }
212
213         /* remove any "?R", "?L" or "?[a-z]" channel identifier */
214         
215         string::size_type len = path.length();
216         
217         if (len > 3 && (path[len-2] == '%' || path[len-2] == '?') && 
218             (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
219                 
220                 path = path.substr (0, path.length() - 2);
221         }
222
223         return path;
224 }