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 "libardour-config.h"
24 #define __STDC_FORMAT_MACROS 1
27 #include <cstdio> /* for sprintf */
34 #include <sys/types.h>
41 #include <glibmm/miscutils.h>
42 #include <glibmm/fileutils.h>
48 #include "pbd/error.h"
49 #include "pbd/stacktrace.h"
50 #include "pbd/xml++.h"
51 #include "pbd/basename.h"
52 #include "pbd/strsplit.h"
53 #include "ardour/utils.h"
57 using namespace ARDOUR;
62 legalize_for_path (const string& str)
64 #if OLD_SCHOOL_PROHIBITIVE
65 string::size_type pos;
66 string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
72 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
73 legal.replace (pos, 1, "_");
79 string::size_type pos;
80 string illegal_chars = "/\\"; /* DOS, POSIX. Yes, we're going to ignore HFS */
86 while ((pos = legal.find_first_of (illegal_chars, pos)) != string::npos) {
87 legal.replace (pos, 1, "_");
96 bump_name_once (const std::string& name, char delimiter)
98 string::size_type delim;
101 if ((delim = name.find_last_of (delimiter)) == string::npos) {
103 newname += delimiter;
107 const char *last_element = name.c_str() + delim + 1;
108 for (size_t i = 0; i < strlen(last_element); i++) {
109 if (!isdigit(last_element[i])) {
116 int32_t version = strtol (name.c_str()+delim+1, (char **)NULL, 10);
118 if (isnumber == 0 || errno != 0) {
119 // last_element is not a number, or is too large
121 newname += delimiter;
126 snprintf (buf, sizeof(buf), "%d", version+1);
128 newname = name.substr (0, delim+1);
138 could_be_a_valid_path (const string& path)
140 vector<string> posix_dirs;
141 vector<string> dos_dirs;
144 split (path, posix_dirs, '/');
145 split (path, dos_dirs, '\\');
147 /* remove the last component of each */
149 posix_dirs.erase (--posix_dirs.end());
150 dos_dirs.erase (--dos_dirs.end());
152 if (G_DIR_SEPARATOR == '/') {
153 for (vector<string>::iterator x = posix_dirs.begin(); x != posix_dirs.end(); ++x) {
154 testpath = Glib::build_filename (testpath, *x);
155 cerr << "Testing " << testpath << endl;
156 if (!Glib::file_test (testpath, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
162 if (G_DIR_SEPARATOR == '\\') {
164 for (vector<string>::iterator x = dos_dirs.begin(); x != dos_dirs.end(); ++x) {
165 testpath = Glib::build_filename (testpath, *x);
166 cerr << "Testing " << testpath << endl;
167 if (!Glib::file_test (testpath, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
178 find_named_node (const XMLNode& node, string name)
181 XMLNodeConstIterator niter;
184 nlist = node.children();
186 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
190 if (child->name() == name) {
199 cmp_nocase (const string& s, const string& s2)
201 string::const_iterator p = s.begin();
202 string::const_iterator p2 = s2.begin();
204 while (p != s.end() && p2 != s2.end()) {
205 if (toupper(*p) != toupper(*p2)) {
206 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
212 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
216 touch_file (string path)
218 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
227 region_name_from_path (string path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
229 path = PBD::basename_nosuffix (path);
231 if (strip_channels) {
233 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
235 string::size_type len = path.length();
237 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
238 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
240 path = path.substr (0, path.length() - 2);
244 if (add_channel_suffix) {
249 path += (char) ('a' + this_one);
251 path += (char) (this_one == 0 ? 'L' : 'R');
259 path_is_paired (string path, string& pair_base)
261 string::size_type pos;
263 /* remove any leading path */
265 if ((pos = path.find_last_of (G_DIR_SEPARATOR)) != string::npos) {
266 path = path.substr(pos+1);
269 /* remove filename suffixes etc. */
271 if ((pos = path.find_last_of ('.')) != string::npos) {
272 path = path.substr (0, pos);
275 string::size_type len = path.length();
277 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
279 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
280 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
282 pair_base = path.substr (0, len-2);
291 path_expand (string path)
294 /* Handle tilde and environment variable expansion in session path */
298 switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
302 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
306 if (expansion.we_wordc > 1) {
307 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
311 ret = expansion.we_wordv[0];
313 wordfree (&expansion);
321 #if defined(HAVE_COREAUDIO) || defined(HAVE_AUDIOUNITS)
323 CFStringRefToStdString(CFStringRef stringRef)
326 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
327 kCFStringEncodingUTF8);
328 char *buf = new char[size];
332 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
338 #endif // HAVE_COREAUDIO
341 compute_equal_power_fades (nframes_t nframes, float* in, float* out)
345 step = 1.0/(nframes-1);
349 for (nframes_t i = 1; i < nframes - 1; ++i) {
350 in[i] = in[i-1] + step;
355 const float pan_law_attenuation = -3.0f;
356 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
358 for (nframes_t n = 0; n < nframes; ++n) {
360 float outVal = 1 - inVal;
361 out[n] = outVal * (scale * outVal + 1.0f - scale);
362 in[n] = inVal * (scale * inVal + 1.0f - scale);
367 string_to_edit_mode (string str)
369 if (str == _("Splice")) {
371 } else if (str == _("Slide")) {
373 } else if (str == _("Lock")) {
376 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
382 edit_mode_to_string (EditMode mode)
398 string_to_sync_source (string str)
400 if (str == _("MIDI Timecode")) {
404 if (str == _("MIDI Clock")) {
408 if (str == _("JACK")) {
412 fatal << string_compose (_("programming error: unknown sync source string \"%1\""), str) << endmsg;
418 sync_source_to_string (SyncSource src)
425 return _("MIDI Timecode");
428 return _("MIDI Clock");
430 /* GRRRR .... stupid, stupid gcc - you can't get here from there, all enum values are handled */
435 meter_falloff_to_float (MeterFalloff falloff)
438 case MeterFalloffOff:
439 return METER_FALLOFF_OFF;
440 case MeterFalloffSlowest:
441 return METER_FALLOFF_SLOWEST;
442 case MeterFalloffSlow:
443 return METER_FALLOFF_SLOW;
444 case MeterFalloffMedium:
445 return METER_FALLOFF_MEDIUM;
446 case MeterFalloffFast:
447 return METER_FALLOFF_FAST;
448 case MeterFalloffFaster:
449 return METER_FALLOFF_FASTER;
450 case MeterFalloffFastest:
451 return METER_FALLOFF_FASTEST;
453 return METER_FALLOFF_FAST;
458 meter_falloff_from_float (float val)
460 if (val == METER_FALLOFF_OFF) {
461 return MeterFalloffOff;
463 else if (val <= METER_FALLOFF_SLOWEST) {
464 return MeterFalloffSlowest;
466 else if (val <= METER_FALLOFF_SLOW) {
467 return MeterFalloffSlow;
469 else if (val <= METER_FALLOFF_MEDIUM) {
470 return MeterFalloffMedium;
472 else if (val <= METER_FALLOFF_FAST) {
473 return MeterFalloffFast;
475 else if (val <= METER_FALLOFF_FASTER) {
476 return MeterFalloffFaster;
479 return MeterFalloffFastest;
484 ARDOUR::string_to_auto_state (std::string str)
486 if (str == X_("Off")) {
488 } else if (str == X_("Play")) {
490 } else if (str == X_("Write")) {
492 } else if (str == X_("Touch")) {
496 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
502 ARDOUR::auto_state_to_string (AutoState as)
504 /* to be used only for XML serialization, no i18n done */
520 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
526 ARDOUR::string_to_auto_style (std::string str)
528 if (str == X_("Absolute")) {
530 } else if (str == X_("Trim")) {
534 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
540 ARDOUR::auto_style_to_string (AutoStyle as)
542 /* to be used only for XML serialization, no i18n done */
546 return X_("Absolute");
553 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
559 bool_as_string (bool yn)
561 return (yn ? "yes" : "no");
565 string_is_affirmative (const std::string& str)
567 /* to be used only with XML data - not intended to handle user input */
569 return str == "1" || str == "y" || str == "Y" || (!g_strncasecmp(str.c_str(), "yes", str.length()));
573 native_header_format_extension (HeaderFormat hf, const DataType& type)
575 if (type == DataType::MIDI) {
596 fatal << string_compose (_("programming error: unknown native header format: %1"), hf);
602 matching_unsuffixed_filename_exists_in (const string& dir, const string& path)
604 string bws = basename_nosuffix (path);
605 struct dirent* dentry;
610 if ((dead = ::opendir (dir.c_str())) == 0) {
611 error << string_compose (_("cannot open directory %1 (%2)"), dir, strerror (errno)) << endl;
615 while ((dentry = ::readdir (dead)) != 0) {
617 /* avoid '.' and '..' */
619 if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
620 (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
624 string fullpath = Glib::build_filename (dir, dentry->d_name);
626 if (::stat (fullpath.c_str(), &statbuf)) {
630 if (!S_ISREG (statbuf.st_mode)) {
634 string bws2 = basename_nosuffix (dentry->d_name);
647 void c_stacktrace() { stacktrace (cerr); }