remove a bunch of uses of long (mostly replaced by int32_t)
[ArdourMidi.git] / libs / ardour / utils.cc
blobcfa183f4998af066f14d902e8b0b18107134600b
1 /*
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.
20 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
24 #define __STDC_FORMAT_MACROS 1
25 #include <stdint.h>
27 #include <cstdio> /* for sprintf */
28 #include <cstring>
29 #include <cmath>
30 #include <cctype>
31 #include <cstring>
32 #include <cerrno>
33 #include <iostream>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <fcntl.h>
38 #include <dirent.h>
39 #include <errno.h>
41 #include <glibmm/miscutils.h>
43 #ifdef HAVE_WORDEXP
44 #include <wordexp.h>
45 #endif
47 #include "pbd/error.h"
48 #include "pbd/stacktrace.h"
49 #include "pbd/xml++.h"
50 #include "pbd/basename.h"
51 #include "ardour/utils.h"
53 #include "i18n.h"
55 using namespace ARDOUR;
56 using namespace std;
57 using namespace PBD;
58 using Glib::ustring;
60 ustring
61 legalize_for_path (ustring str)
63 ustring::size_type pos;
64 ustring legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
65 ustring legal;
67 legal = str;
68 pos = 0;
70 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
71 legal.replace (pos, 1, "_");
72 pos += 1;
75 return legal;
78 string
79 bump_name_once (const std::string& name, char delimiter)
81 string::size_type delim;
82 string newname;
84 if ((delim = name.find_last_of (delimiter)) == string::npos) {
85 newname = name;
86 newname += delimiter;
87 newname += "1";
88 } else {
89 int isnumber = 1;
90 const char *last_element = name.c_str() + delim + 1;
91 for (size_t i = 0; i < strlen(last_element); i++) {
92 if (!isdigit(last_element[i])) {
93 isnumber = 0;
94 break;
98 errno = 0;
99 int32_t version = strtol (name.c_str()+delim+1, (char **)NULL, 10);
101 if (isnumber == 0 || errno != 0) {
102 // last_element is not a number, or is too large
103 newname = name;
104 newname += delimiter;
105 newname += "1";
106 } else {
107 char buf[32];
109 snprintf (buf, sizeof(buf), "%d", version+1);
111 newname = name.substr (0, delim+1);
112 newname += buf;
116 return newname;
120 XMLNode *
121 find_named_node (const XMLNode& node, string name)
123 XMLNodeList nlist;
124 XMLNodeConstIterator niter;
125 XMLNode* child;
127 nlist = node.children();
129 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
131 child = *niter;
133 if (child->name() == name) {
134 return child;
138 return 0;
142 cmp_nocase (const string& s, const string& s2)
144 string::const_iterator p = s.begin();
145 string::const_iterator p2 = s2.begin();
147 while (p != s.end() && p2 != s2.end()) {
148 if (toupper(*p) != toupper(*p2)) {
149 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
151 ++p;
152 ++p2;
155 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
159 touch_file (ustring path)
161 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
162 if (fd >= 0) {
163 close (fd);
164 return 0;
166 return 1;
169 ustring
170 region_name_from_path (ustring path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
172 path = PBD::basename_nosuffix (path);
174 if (strip_channels) {
176 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
178 ustring::size_type len = path.length();
180 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
181 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
183 path = path.substr (0, path.length() - 2);
187 if (add_channel_suffix) {
189 path += '%';
191 if (total > 2) {
192 path += (char) ('a' + this_one);
193 } else {
194 path += (char) (this_one == 0 ? 'L' : 'R');
198 return path;
201 bool
202 path_is_paired (ustring path, ustring& pair_base)
204 ustring::size_type pos;
206 /* remove any leading path */
208 if ((pos = path.find_last_of ('/')) != string::npos) {
209 path = path.substr(pos+1);
212 /* remove filename suffixes etc. */
214 if ((pos = path.find_last_of ('.')) != string::npos) {
215 path = path.substr (0, pos);
218 ustring::size_type len = path.length();
220 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
222 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
223 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
225 pair_base = path.substr (0, len-2);
226 return true;
230 return false;
233 ustring
234 path_expand (ustring path)
236 #ifdef HAVE_WORDEXP
237 /* Handle tilde and environment variable expansion in session path */
238 string ret = path;
240 wordexp_t expansion;
241 switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
242 case 0:
243 break;
244 default:
245 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
246 goto out;
249 if (expansion.we_wordc > 1) {
250 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
251 goto out;
254 ret = expansion.we_wordv[0];
255 out:
256 wordfree (&expansion);
257 return ret;
259 #else
260 return path;
261 #endif
264 #if defined(HAVE_COREAUDIO) || defined(HAVE_AUDIOUNITS)
265 string
266 CFStringRefToStdString(CFStringRef stringRef)
268 CFIndex size =
269 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
270 kCFStringEncodingUTF8);
271 char *buf = new char[size];
273 std::string result;
275 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
276 result = buf;
278 delete [] buf;
279 return result;
281 #endif // HAVE_COREAUDIO
283 void
284 compute_equal_power_fades (nframes_t nframes, float* in, float* out)
286 double step;
288 step = 1.0/(nframes-1);
290 in[0] = 0.0f;
292 for (nframes_t i = 1; i < nframes - 1; ++i) {
293 in[i] = in[i-1] + step;
296 in[nframes-1] = 1.0;
298 const float pan_law_attenuation = -3.0f;
299 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
301 for (nframes_t n = 0; n < nframes; ++n) {
302 float inVal = in[n];
303 float outVal = 1 - inVal;
304 out[n] = outVal * (scale * outVal + 1.0f - scale);
305 in[n] = inVal * (scale * inVal + 1.0f - scale);
309 EditMode
310 string_to_edit_mode (string str)
312 if (str == _("Splice Edit")) {
313 return Splice;
314 } else if (str == _("Slide Edit")) {
315 return Slide;
316 } else if (str == _("Lock Edit")) {
317 return Lock;
319 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
320 /*NOTREACHED*/
321 return Slide;
324 const char*
325 edit_mode_to_string (EditMode mode)
327 switch (mode) {
328 case Slide:
329 return _("Slide Edit");
331 case Lock:
332 return _("Lock Edit");
334 default:
335 case Splice:
336 return _("Splice Edit");
340 SyncSource
341 string_to_sync_source (string str)
343 if (str == _("MIDI Timecode")) {
344 return MTC;
347 if (str == _("MIDI Clock")) {
348 return MIDIClock;
351 if (str == _("JACK")) {
352 return JACK;
355 fatal << string_compose (_("programming error: unknown sync source string \"%1\""), str) << endmsg;
356 /*NOTREACHED*/
357 return JACK;
360 const char*
361 sync_source_to_string (SyncSource src)
363 switch (src) {
364 case JACK:
365 return _("JACK");
367 case MTC:
368 return _("MIDI Timecode");
370 case MIDIClock:
371 return _("MIDI Clock");
373 /* GRRRR .... stupid, stupid gcc - you can't get here from there, all enum values are handled */
374 return _("JACK");
377 float
378 meter_falloff_to_float (MeterFalloff falloff)
380 switch (falloff) {
381 case MeterFalloffOff:
382 return METER_FALLOFF_OFF;
383 case MeterFalloffSlowest:
384 return METER_FALLOFF_SLOWEST;
385 case MeterFalloffSlow:
386 return METER_FALLOFF_SLOW;
387 case MeterFalloffMedium:
388 return METER_FALLOFF_MEDIUM;
389 case MeterFalloffFast:
390 return METER_FALLOFF_FAST;
391 case MeterFalloffFaster:
392 return METER_FALLOFF_FASTER;
393 case MeterFalloffFastest:
394 return METER_FALLOFF_FASTEST;
395 default:
396 return METER_FALLOFF_FAST;
400 MeterFalloff
401 meter_falloff_from_float (float val)
403 if (val == METER_FALLOFF_OFF) {
404 return MeterFalloffOff;
406 else if (val <= METER_FALLOFF_SLOWEST) {
407 return MeterFalloffSlowest;
409 else if (val <= METER_FALLOFF_SLOW) {
410 return MeterFalloffSlow;
412 else if (val <= METER_FALLOFF_MEDIUM) {
413 return MeterFalloffMedium;
415 else if (val <= METER_FALLOFF_FAST) {
416 return MeterFalloffFast;
418 else if (val <= METER_FALLOFF_FASTER) {
419 return MeterFalloffFaster;
421 else {
422 return MeterFalloffFastest;
426 AutoState
427 ARDOUR::string_to_auto_state (std::string str)
429 if (str == X_("Off")) {
430 return Off;
431 } else if (str == X_("Play")) {
432 return Play;
433 } else if (str == X_("Write")) {
434 return Write;
435 } else if (str == X_("Touch")) {
436 return Touch;
439 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
440 /*NOTREACHED*/
441 return Touch;
444 string
445 ARDOUR::auto_state_to_string (AutoState as)
447 /* to be used only for XML serialization, no i18n done */
449 switch (as) {
450 case Off:
451 return X_("Off");
452 break;
453 case Play:
454 return X_("Play");
455 break;
456 case Write:
457 return X_("Write");
458 break;
459 case Touch:
460 return X_("Touch");
463 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
464 /*NOTREACHED*/
465 return "";
468 AutoStyle
469 ARDOUR::string_to_auto_style (std::string str)
471 if (str == X_("Absolute")) {
472 return Absolute;
473 } else if (str == X_("Trim")) {
474 return Trim;
477 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
478 /*NOTREACHED*/
479 return Trim;
482 string
483 ARDOUR::auto_style_to_string (AutoStyle as)
485 /* to be used only for XML serialization, no i18n done */
487 switch (as) {
488 case Absolute:
489 return X_("Absolute");
490 break;
491 case Trim:
492 return X_("Trim");
493 break;
496 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
497 /*NOTREACHED*/
498 return "";
501 std::string
502 bool_as_string (bool yn)
504 return (yn ? "yes" : "no");
507 bool
508 string_is_affirmative (const std::string& str)
510 /* to be used only with XML data - not intended to handle user input */
512 return str == "1" || str == "y" || str == "Y" || (!g_strncasecmp(str.c_str(), "yes", str.length()));
515 const char*
516 native_header_format_extension (HeaderFormat hf, const DataType& type)
518 if (type == DataType::MIDI) {
519 return ".mid";
522 switch (hf) {
523 case BWF:
524 return ".wav";
525 case WAVE:
526 return ".wav";
527 case WAVE64:
528 return ".w64";
529 case CAF:
530 return ".caf";
531 case AIFF:
532 return ".aif";
533 case iXML:
534 return ".ixml";
535 case RF64:
536 return ".rf64";
539 fatal << string_compose (_("programming error: unknown native header format: %1"), hf);
540 /*NOTREACHED*/
541 return ".wav";
544 bool
545 matching_unsuffixed_filename_exists_in (const string& dir, const string& path)
547 string bws = basename_nosuffix (path);
548 struct dirent* dentry;
549 struct stat statbuf;
550 DIR* dead;
551 bool ret = false;
553 if ((dead = ::opendir (dir.c_str())) == 0) {
554 error << string_compose (_("cannot open directory %1 (%2)"), dir, strerror (errno)) << endl;
555 return false;
558 while ((dentry = ::readdir (dead)) != 0) {
560 /* avoid '.' and '..' */
562 if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
563 (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
564 continue;
567 string fullpath = Glib::build_filename (dir, dentry->d_name);
569 if (::stat (fullpath.c_str(), &statbuf)) {
570 continue;
573 if (!S_ISREG (statbuf.st_mode)) {
574 continue;
577 string bws2 = basename_nosuffix (dentry->d_name);
579 if (bws2 == bws) {
580 ret = true;
581 break;
585 ::closedir (dead);
586 return ret;
589 extern "C" {
590 void c_stacktrace() { stacktrace (cerr); }