2 Copyright (C) 2000-2003 Paul Davis
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.
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.
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.
21 #include <cstdio> /* for sprintf */
27 #include <sys/types.h>
37 #include <pbd/error.h>
38 #include <pbd/xml++.h>
39 #include <ardour/utils.h>
43 using namespace ARDOUR;
48 elapsed_time_to_str (char *buf, uint32_t seconds)
57 days = s / (3600 * 24);
58 s -= (days * 3600 * 24);
65 snprintf (buf, sizeof (buf), "%" PRIu32 " day%s %" PRIu32 " hour%s",
69 hours > 1 ? "s" : "");
71 snprintf (buf, sizeof (buf), "%" PRIu32 " hour%s %" PRIu32 " minute%s",
75 minutes > 1 ? "s" : "");
77 snprintf (buf, sizeof (buf), "%" PRIu32 " minute%s",
79 minutes > 1 ? "s" : "");
81 snprintf (buf, sizeof (buf), "%" PRIu32 " second%s",
83 seconds > 1 ? "s" : "");
85 snprintf (buf, sizeof (buf), "no time");
90 legalize_for_path (string str)
92 string::size_type pos;
93 string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
99 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
100 legal.replace (pos, 1, "_");
108 operator<< (ostream& o, const BBT_Time& bbt)
110 o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
115 find_named_node (const XMLNode& node, string name)
118 XMLNodeConstIterator niter;
121 nlist = node.children();
123 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
127 if (child->name() == name) {
136 cmp_nocase (const string& s, const string& s2)
138 string::const_iterator p = s.begin();
139 string::const_iterator p2 = s2.begin();
141 while (p != s.end() && p2 != s2.end()) {
142 if (toupper(*p) != toupper(*p2)) {
143 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
149 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
153 tokenize_fullpath (string fullpath, string& path, string& name)
155 string::size_type m = fullpath.find_last_of("/");
157 if (m == string::npos) {
163 // does it look like just a directory?
164 if (m == fullpath.length()-1) {
167 path = fullpath.substr(0, m+1);
169 string::size_type n = fullpath.find(".ardour", m);
171 if (n == string::npos) {
174 name = fullpath.substr(m+1, n - m - 1);
179 touch_file (string path)
181 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
190 placement_as_string (Placement p)
195 default: /* to get g++ to realize we have all the cases covered */
202 region_name_from_path (string path)
204 string::size_type pos;
206 /* remove filename suffixes etc. */
208 if ((pos = path.find_last_of ('.')) != string::npos) {
209 path = path.substr (0, pos);
212 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
214 string::size_type len = path.length();
216 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?') &&
217 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
219 path = path.substr (0, path.length() - 2);
226 path_expand (string path)
229 /* Handle tilde and environment variable expansion in session path */
233 switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
237 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
241 if (expansion.we_wordc > 1) {
242 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
246 ret = expansion.we_wordv[0];
248 wordfree (&expansion);
256 #ifdef HAVE_COREAUDIO
258 CFStringRefToStdString(CFStringRef stringRef)
261 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
262 kCFStringEncodingUTF8);
263 char *buf = new char[size];
267 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
273 #endif // HAVE_COREAUDIO