forward port 2.X changes up to and including rev 6909
[ardour.git] / libs / ardour / utils.cc
index abdba63803f8270dfbaa4ca5e24a53991360ee6a..a7d5b960f82c2beb31fe05d7fca3a2ab8022ac1e 100644 (file)
 #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>
@@ -93,7 +96,7 @@ bump_name_once (const std::string& name, char delimiter)
                }
 
                errno = 0;
-               long int version = strtol (name.c_str()+delim+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
@@ -103,7 +106,7 @@ bump_name_once (const std::string& name, char delimiter)
                } else {
                        char buf[32];
 
-                       snprintf (buf, sizeof(buf), "%ld", version+1);
+                       snprintf (buf, sizeof(buf), "%d", version+1);
 
                        newname  = name.substr (0, delim+1);
                        newname += buf;
@@ -202,7 +205,7 @@ 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);
        }
 
@@ -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,14 +326,14 @@ 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");
        }
 }
 
@@ -538,6 +541,51 @@ native_header_format_extension (HeaderFormat hf, const DataType& type)
         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); }
 }