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"
26 #include <cstdio> /* for sprintf */
33 #include <sys/types.h>
40 #include <glibmm/miscutils.h>
41 #include <glibmm/fileutils.h>
47 #include "pbd/error.h"
48 #include "pbd/stacktrace.h"
49 #include "pbd/xml++.h"
50 #include "pbd/basename.h"
51 #include "pbd/strsplit.h"
52 #include "ardour/utils.h"
56 using namespace ARDOUR;
61 legalize_for_path (const string& str)
63 #if OLD_SCHOOL_PROHIBITIVE
64 string::size_type pos;
65 string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
71 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
72 legal.replace (pos, 1, "_");
78 string::size_type pos;
79 string illegal_chars = "/\\"; /* DOS, POSIX. Yes, we're going to ignore HFS */
85 while ((pos = legal.find_first_of (illegal_chars, pos)) != string::npos) {
86 legal.replace (pos, 1, "_");
95 bump_name_once (const std::string& name, char delimiter)
97 string::size_type delim;
100 if ((delim = name.find_last_of (delimiter)) == string::npos) {
102 newname += delimiter;
106 const char *last_element = name.c_str() + delim + 1;
107 for (size_t i = 0; i < strlen(last_element); i++) {
108 if (!isdigit(last_element[i])) {
115 int32_t version = strtol (name.c_str()+delim+1, (char **)NULL, 10);
117 if (isnumber == 0 || errno != 0) {
118 // last_element is not a number, or is too large
120 newname += delimiter;
125 snprintf (buf, sizeof(buf), "%d", version+1);
127 newname = name.substr (0, delim+1);
137 could_be_a_valid_path (const string& path)
139 vector<string> posix_dirs;
140 vector<string> dos_dirs;
143 split (path, posix_dirs, '/');
144 split (path, dos_dirs, '\\');
146 /* remove the last component of each */
148 posix_dirs.erase (--posix_dirs.end());
149 dos_dirs.erase (--dos_dirs.end());
151 if (G_DIR_SEPARATOR == '/') {
152 for (vector<string>::iterator x = posix_dirs.begin(); x != posix_dirs.end(); ++x) {
153 testpath = Glib::build_filename (testpath, *x);
154 cerr << "Testing " << testpath << endl;
155 if (!Glib::file_test (testpath, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
161 if (G_DIR_SEPARATOR == '\\') {
163 for (vector<string>::iterator x = dos_dirs.begin(); x != dos_dirs.end(); ++x) {
164 testpath = Glib::build_filename (testpath, *x);
165 cerr << "Testing " << testpath << endl;
166 if (!Glib::file_test (testpath, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
177 find_named_node (const XMLNode& node, string name)
180 XMLNodeConstIterator niter;
183 nlist = node.children();
185 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
189 if (child->name() == name) {
198 cmp_nocase (const string& s, const string& s2)
200 string::const_iterator p = s.begin();
201 string::const_iterator p2 = s2.begin();
203 while (p != s.end() && p2 != s2.end()) {
204 if (toupper(*p) != toupper(*p2)) {
205 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
211 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
215 touch_file (string path)
217 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
226 region_name_from_path (string path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
228 path = PBD::basename_nosuffix (path);
230 if (strip_channels) {
232 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
234 string::size_type len = path.length();
236 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
237 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
239 path = path.substr (0, path.length() - 2);
243 if (add_channel_suffix) {
248 path += (char) ('a' + this_one);
250 path += (char) (this_one == 0 ? 'L' : 'R');
258 path_is_paired (string path, string& pair_base)
260 string::size_type pos;
262 /* remove any leading path */
264 if ((pos = path.find_last_of (G_DIR_SEPARATOR)) != string::npos) {
265 path = path.substr(pos+1);
268 /* remove filename suffixes etc. */
270 if ((pos = path.find_last_of ('.')) != string::npos) {
271 path = path.substr (0, pos);
274 string::size_type len = path.length();
276 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
278 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
279 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
281 pair_base = path.substr (0, len-2);
290 path_expand (string path)
297 /* Handle tilde and environment variable expansion in session path */
301 switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
305 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
309 if (expansion.we_wordc > 1) {
310 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
314 ret = expansion.we_wordv[0];
316 wordfree (&expansion);
326 CFStringRefToStdString(CFStringRef stringRef)
329 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
330 kCFStringEncodingUTF8);
331 char *buf = new char[size];
335 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
344 compute_equal_power_fades (framecnt_t nframes, float* in, float* out)
348 step = 1.0/(nframes-1);
352 for (framecnt_t i = 1; i < nframes - 1; ++i) {
353 in[i] = in[i-1] + step;
358 const float pan_law_attenuation = -3.0f;
359 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
361 for (framecnt_t n = 0; n < nframes; ++n) {
363 float outVal = 1 - inVal;
364 out[n] = outVal * (scale * outVal + 1.0f - scale);
365 in[n] = inVal * (scale * inVal + 1.0f - scale);
370 string_to_edit_mode (string str)
372 if (str == _("Splice")) {
374 } else if (str == _("Slide")) {
376 } else if (str == _("Lock")) {
379 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
385 edit_mode_to_string (EditMode mode)
401 string_to_sync_source (string str)
403 if (str == _("MIDI Timecode") || str == _("MTC")) {
407 if (str == _("MIDI Clock")) {
411 if (str == _("JACK")) {
415 fatal << string_compose (_("programming error: unknown sync source string \"%1\""), str) << endmsg;
420 /** @param sh Return a short version of the string */
422 sync_source_to_string (SyncSource src, bool sh)
432 return _("MIDI Timecode");
436 return _("MIDI Clock");
438 /* GRRRR .... stupid, stupid gcc - you can't get here from there, all enum values are handled */
443 meter_falloff_to_float (MeterFalloff falloff)
446 case MeterFalloffOff:
447 return METER_FALLOFF_OFF;
448 case MeterFalloffSlowest:
449 return METER_FALLOFF_SLOWEST;
450 case MeterFalloffSlow:
451 return METER_FALLOFF_SLOW;
452 case MeterFalloffMedium:
453 return METER_FALLOFF_MEDIUM;
454 case MeterFalloffFast:
455 return METER_FALLOFF_FAST;
456 case MeterFalloffFaster:
457 return METER_FALLOFF_FASTER;
458 case MeterFalloffFastest:
459 return METER_FALLOFF_FASTEST;
461 return METER_FALLOFF_FAST;
466 meter_falloff_from_float (float val)
468 if (val == METER_FALLOFF_OFF) {
469 return MeterFalloffOff;
471 else if (val <= METER_FALLOFF_SLOWEST) {
472 return MeterFalloffSlowest;
474 else if (val <= METER_FALLOFF_SLOW) {
475 return MeterFalloffSlow;
477 else if (val <= METER_FALLOFF_MEDIUM) {
478 return MeterFalloffMedium;
480 else if (val <= METER_FALLOFF_FAST) {
481 return MeterFalloffFast;
483 else if (val <= METER_FALLOFF_FASTER) {
484 return MeterFalloffFaster;
487 return MeterFalloffFastest;
492 ARDOUR::string_to_auto_state (std::string str)
494 if (str == X_("Off")) {
496 } else if (str == X_("Play")) {
498 } else if (str == X_("Write")) {
500 } else if (str == X_("Touch")) {
504 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
510 ARDOUR::auto_state_to_string (AutoState as)
512 /* to be used only for XML serialization, no i18n done */
528 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
534 ARDOUR::string_to_auto_style (std::string str)
536 if (str == X_("Absolute")) {
538 } else if (str == X_("Trim")) {
542 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
548 ARDOUR::auto_style_to_string (AutoStyle as)
550 /* to be used only for XML serialization, no i18n done */
554 return X_("Absolute");
561 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
567 bool_as_string (bool yn)
569 return (yn ? "yes" : "no");
573 string_is_affirmative (const std::string& str)
575 /* to be used only with XML data - not intended to handle user input */
577 return str == "1" || str == "y" || str == "Y" || (!g_strncasecmp(str.c_str(), "yes", str.length()));
581 native_header_format_extension (HeaderFormat hf, const DataType& type)
583 if (type == DataType::MIDI) {
604 fatal << string_compose (_("programming error: unknown native header format: %1"), hf);
610 matching_unsuffixed_filename_exists_in (const string& dir, const string& path)
612 string bws = basename_nosuffix (path);
613 struct dirent* dentry;
618 if ((dead = ::opendir (dir.c_str())) == 0) {
619 error << string_compose (_("cannot open directory %1 (%2)"), dir, strerror (errno)) << endl;
623 while ((dentry = ::readdir (dead)) != 0) {
625 /* avoid '.' and '..' */
627 if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
628 (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
632 string fullpath = Glib::build_filename (dir, dentry->d_name);
634 if (::stat (fullpath.c_str(), &statbuf)) {
638 if (!S_ISREG (statbuf.st_mode)) {
642 string bws2 = basename_nosuffix (dentry->d_name);
655 void c_stacktrace() { stacktrace (cerr); }