forward port 2.X changes up to and including rev 6909
[ardour.git] / libs / ardour / utils.cc
index 9565b4450f6baca8dc4ebab1fe86ff927a711094..a7d5b960f82c2beb31fe05d7fca3a2ab8022ac1e 100644 (file)
 
 */
 
+#ifdef WAF_BUILD
+#include "libardour-config.h"
+#endif
+
 #define __STDC_FORMAT_MACROS 1
 #include <stdint.h>
 
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <fcntl.h>
-#include <unistd.h>
+#include <dirent.h>
+#include <errno.h>
+
+#include <glibmm/miscutils.h>
 
 #ifdef HAVE_WORDEXP
 #include <wordexp.h>
@@ -68,17 +75,19 @@ legalize_for_path (ustring str)
        return legal;
 }
 
-string bump_name_once(std::string name)
+string 
+bump_name_once (const std::string& name, char delimiter)
 {
-       string::size_type period;
+       string::size_type delim;
        string newname;
 
-       if ((period = name.find_last_of ('.')) == string::npos) {
+       if ((delim = name.find_last_of (delimiter)) == string::npos) {
                newname  = name;
-               newname += ".1";
+               newname += delimiter;
+               newname += "1";
        } else {
                int isnumber = 1;
-               const char *last_element = name.c_str() + period + 1;
+               const char *last_element = name.c_str() + delim + 1;
                for (size_t i = 0; i < strlen(last_element); i++) {
                        if (!isdigit(last_element[i])) {
                                isnumber = 0;
@@ -87,18 +96,19 @@ string bump_name_once(std::string name)
                }
 
                errno = 0;
-               long int version = strtol (name.c_str()+period+1, (char **)NULL, 10);
+               int32_t version = strtol (name.c_str()+delim+1, (char **)NULL, 10);
 
                if (isnumber == 0 || errno != 0) {
                        // last_element is not a number, or is too large
                        newname  = name;
-                       newname += ".1";
+                       newname  += delimiter;
+                       newname += "1";
                } else {
                        char buf[32];
 
-                       snprintf (buf, sizeof(buf), "%ld", version+1);
+                       snprintf (buf, sizeof(buf), "%d", version+1);
 
-                       newname  = name.substr (0, period+1);
+                       newname  = name.substr (0, delim+1);
                        newname += buf;
                }
        }
@@ -107,13 +117,6 @@ string bump_name_once(std::string name)
 
 }
 
-ostream&
-operator<< (ostream& o, const BBT_Time& bbt)
-{
-       o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
-       return o;
-}
-
 XMLNode *
 find_named_node (const XMLNode& node, string name)
 {
@@ -202,11 +205,11 @@ path_is_paired (ustring path, ustring& pair_base)
 
        /* remove any leading path */
 
-       if ((pos = path.find_last_of ('/')) != string::npos) {
+       if ((pos = path.find_last_of (G_DIR_SEPARATOR)) != string::npos) {
                path = path.substr(pos+1);
        }
 
-       /* remove filename suffixes etc. */
+       /* remove filename suffixes etc. */
 
        if ((pos = path.find_last_of ('.')) != string::npos) {
                path = path.substr (0, pos);
@@ -306,11 +309,11 @@ compute_equal_power_fades (nframes_t nframes, float* in, float* out)
 EditMode
 string_to_edit_mode (string str)
 {
-       if (str == _("Splice Edit")) {
+       if (str == _("Splice")) {
                return Splice;
-       } else if (str == _("Slide Edit")) {
+       } else if (str == _("Slide")) {
                return Slide;
-       } else if (str == _("Lock Edit")) {
+       } else if (str == _("Lock")) {
                return Lock;
        }
        fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
@@ -323,25 +326,21 @@ edit_mode_to_string (EditMode mode)
 {
        switch (mode) {
        case Slide:
-               return _("Slide Edit");
+               return _("Slide");
 
        case Lock:
-               return _("Lock Edit");
+               return _("Lock");
 
        default:
        case Splice:
-               return _("Splice Edit");
+               return _("Splice");
        }
 }
 
-SlaveSource
-string_to_slave_source (string str)
+SyncSource
+string_to_sync_source (string str)
 {
-       if (str == _("Internal")) {
-               return None;
-       }
-
-       if (str == _("MTC")) {
+       if (str == _("MIDI Timecode")) {
                return MTC;
        }
 
@@ -353,29 +352,26 @@ string_to_slave_source (string str)
                return JACK;
        }
 
-       fatal << string_compose (_("programming error: unknown slave source string \"%1\""), str) << endmsg;
+       fatal << string_compose (_("programming error: unknown sync source string \"%1\""), str) << endmsg;
        /*NOTREACHED*/
-       return None;
+       return JACK;
 }
 
 const char*
-slave_source_to_string (SlaveSource src)
+sync_source_to_string (SyncSource src)
 {
        switch (src) {
        case JACK:
                return _("JACK");
 
        case MTC:
-               return _("MTC");
+               return _("MIDI Timecode");
 
        case MIDIClock:
                return _("MIDI Clock");
-
-       default:
-       case None:
-               return _("Internal");
-
        }
+       /* GRRRR .... stupid, stupid gcc - you can't get here from there, all enum values are handled */
+       return _("JACK");
 }
 
 float
@@ -421,7 +417,7 @@ meter_falloff_from_float (float val)
        }
        else if (val <= METER_FALLOFF_FASTER) {
                return MeterFalloffFaster;
-       }
+       }
        else {
                return MeterFalloffFastest;
        }
@@ -502,6 +498,94 @@ ARDOUR::auto_style_to_string (AutoStyle as)
        return "";
 }
 
+std::string
+bool_as_string (bool yn)
+{
+       return (yn ? "yes" : "no");
+}
+
+bool
+string_is_affirmative (const std::string& str)
+{
+       /* to be used only with XML data - not intended to handle user input */
+
+       return str == "1" || str == "y" || str == "Y" || (!g_strncasecmp(str.c_str(), "yes", str.length()));
+}
+
+const char*
+native_header_format_extension (HeaderFormat hf, const DataType& type)
+{
+        if (type == DataType::MIDI) {
+                return ".mid";
+        }
+        
+        switch (hf) {
+        case BWF:
+                return ".wav";
+        case WAVE:
+                return ".wav";
+        case WAVE64:
+                return ".w64";
+        case CAF:
+                return ".caf";
+        case AIFF:
+                return ".aif";
+        case iXML:
+                return ".ixml";
+        case RF64:
+                return ".rf64";
+        }
+
+        fatal << string_compose (_("programming error: unknown native header format: %1"), hf);
+        /*NOTREACHED*/
+        return ".wav";
+}
+
+bool
+matching_unsuffixed_filename_exists_in (const string& dir, const string& path)
+{
+        string bws = basename_nosuffix (path);
+       struct dirent* dentry;
+       struct stat statbuf;
+       DIR* dead;
+        bool ret = false;
+
+        if ((dead = ::opendir (dir.c_str())) == 0) {
+                error << string_compose (_("cannot open directory %1 (%2)"), dir, strerror (errno)) << endl;
+                return false;
+        }
+        
+        while ((dentry = ::readdir (dead)) != 0) {
+                
+                /* avoid '.' and '..' */
+                
+                if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
+                    (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
+                        continue;
+                }
+        
+                string fullpath = Glib::build_filename (dir, dentry->d_name);
+
+                if (::stat (fullpath.c_str(), &statbuf)) {
+                        continue;
+                }
+                
+                if (!S_ISREG (statbuf.st_mode)) {
+                        continue;
+                }
+
+                string bws2 = basename_nosuffix (dentry->d_name);
+                
+                if (bws2 == bws) {
+                        ret = true;
+                        break;
+                }
+        }
+
+        ::closedir (dead);
+        return ret;
+}
+
 extern "C" {
        void c_stacktrace() { stacktrace (cerr); }
 }