further code simplification and rationalization related to MIDI source/file renaming
authorPaul Davis <paul@linuxaudiosystems.com>
Sun, 13 Apr 2014 15:12:22 +0000 (11:12 -0400)
committerPaul Davis <paul@linuxaudiosystems.com>
Mon, 14 Apr 2014 06:17:30 +0000 (02:17 -0400)
libs/ardour/ardour/session.h
libs/ardour/ardour/smf_source.h
libs/ardour/file_source.cc
libs/ardour/midi_diskstream.cc
libs/ardour/session.cc
libs/ardour/smf_source.cc

index ecb47a102a049320b0a4d254c5cb0e950310ebcb..20feb27001fcb4f585a10a78d28a9920c464c6c0 100644 (file)
@@ -194,8 +194,6 @@ class LIBARDOUR_API Session : public PBD::StatefulDestructible, public PBD::Scop
 
        std::string peak_path (std::string) const;
 
-       std::string generate_new_source_path_from_name (std::string oldpath, std::string oldname, std::string newname, bool destructive);
-
        std::string peak_path_from_audio_path (std::string) const;
        std::string new_audio_source_name (const std::string&, uint32_t nchans, uint32_t chan, bool destructive);
        std::string new_midi_source_name (const std::string&);
index 193330ef3688686c3e3c60e358d5a988233a6f7e..9d85f943529d3e4d531d1ca27fe542804f8afe87 100644 (file)
@@ -45,6 +45,15 @@ public:
 
        virtual ~SMFSource ();
 
+       /** Rename the file on disk referenced by this source to \param newname
+        *
+        * This method exists only for MIDI file sources, not for audio, which 
+        * can never be renamed. It exists for MIDI so that we can get
+        * consistent and sane region/source numbering when regions are added
+        * manually (which never happens with audio).
+        */
+       int rename (const std::string& name);
+
         bool safe_file_extension (const std::string& path) const {
                return safe_midi_file_extension(path);
        }
index 6dc6ad9500f73e3bfdfc000298b80a1814915bbc..709915378d8de6896379f5ed6e1306b26902d6d1 100644 (file)
@@ -515,38 +515,6 @@ out:
        return ret;
 }
 
-int
-FileSource::set_source_name (const string& newname, bool destructive)
-{
-       Glib::Threads::Mutex::Lock lm (_lock);
-       string oldpath = _path;
-       string newpath = _session.generate_new_source_path_from_name (oldpath, _name, newname, destructive);
-
-       if (newpath.empty()) {
-               error << string_compose (_("programming error: %1"), "cannot generate a changed file path") << endmsg;
-               return -1;
-       }
-
-       // Test whether newpath exists, if yes notify the user but continue.
-       if (Glib::file_test (newpath, Glib::FILE_TEST_EXISTS)) {
-               error << string_compose (_("Programming error! %1 tried to rename a file over another file! It's safe to continue working, but please report this to the developers."), PROGRAM_NAME) << endmsg;
-               return -1;
-       }
-
-       if (Glib::file_test (oldpath.c_str(), Glib::FILE_TEST_EXISTS)) { 
-               /* rename only needed if file exists on disk */
-               if (::rename (oldpath.c_str(), newpath.c_str()) != 0) {
-                       error << string_compose (_("cannot rename file %1 to %2 (%3)"), oldpath, newpath, strerror(errno)) << endmsg;
-                       return -1;
-               }
-       }
-
-       _name = Glib::path_get_basename (newpath);
-       _path = newpath;
-
-       return 0;
-}
-
 void
 FileSource::mark_immutable ()
 {
index fdf155d78b76cdc9795d7928161d13cc07a7d222..4a878c389861b1ae638f47d1f17353a494242284 100644 (file)
@@ -1228,12 +1228,18 @@ MidiDiskstream::steal_write_source_name ()
        /* this will bump the name of the current write source to the next one
         * (e.g. "MIDI 1-1" gets renamed to "MIDI 1-2"), thus leaving the
         * current write source name (e.g. "MIDI 1-1" available). See the
-        * comments in Session::create_midi_source_for_track() about why we do
-        * this.
+        * comments in Session::create_midi_source_by_stealing_name() about why
+        * we do this.
         */
 
-       if (_write_source->set_source_name (name(), false)) {
-               return string();
+       try {
+               string new_name = _session.new_midi_source_name (name());
+               
+               if (_write_source->rename (new_name)) {
+                       return string();
+               }
+       } catch (...) {
+               return string ();
        }
        
        return our_old_name;
index 68ea4c3faf8f880cd591ecaf1edbc82f26faa2ec..f0672b934efc090f5b539d0477bedb7d74082ffa 100644 (file)
@@ -3366,112 +3366,6 @@ Session::count_sources_by_origin (const string& path)
        return cnt;
 }
 
-string
-Session::generate_new_source_path_from_name (string path, string oldname, string newname, bool destructive)
-{
-       string look_for;
-       string old_basename = PBD::basename_nosuffix (oldname);
-       string new_legalized = legalize_for_path (newname);
-
-       /* note: we know (or assume) the old path is already valid */
-
-       if (destructive) {
-
-               /* destructive file sources have a name of the form:
-
-                   /path/to/Tnnnn-NAME(%[LR])?.wav
-
-                   the task here is to replace NAME with the new name.
-               */
-
-               string dir;
-               string prefix;
-               string::size_type dash;
-
-               dir = Glib::path_get_dirname (path);
-               path = Glib::path_get_basename (path);
-
-               /* '-' is not a legal character for the NAME part of the path */
-
-               if ((dash = path.find_last_of ('-')) == string::npos) {
-                       return "";
-               }
-
-               prefix = path.substr (0, dash);
-
-               path += prefix;
-               path += '-';
-               path += new_legalized;
-               path += native_header_format_extension (config.get_native_file_header_format(), DataType::AUDIO);
-               path = Glib::build_filename (dir, path);
-
-       } else {
-
-               /* non-destructive file sources have a name of the form:
-
-                   /path/to/NAME-nnnnn(%[LR])?.ext
-
-                   the task here is to replace NAME with the new name.
-               */
-
-               string dir;
-               string suffix;
-               string::size_type dash;
-               string::size_type postfix;
-
-               dir = Glib::path_get_dirname (path);
-               path = Glib::path_get_basename (path);
-
-               /* '-' is not a legal character for the NAME part of the path */
-
-               if ((dash = path.find_last_of ('-')) == string::npos) {
-                       return "";
-               }
-
-               suffix = path.substr (dash+1);
-
-               // Suffix is now everything after the dash. Now we need to eliminate
-               // the nnnnn part, which is done by either finding a '%' or a '.'
-
-               postfix = suffix.find_last_of ("%");
-               if (postfix == string::npos) {
-                       postfix = suffix.find_last_of ('.');
-               }
-
-               if (postfix != string::npos) {
-                       suffix = suffix.substr (postfix);
-               } else {
-                       error << "Logic error in Session::change_source_path_by_name(), please report" << endl;
-                       return "";
-               }
-
-               const uint32_t limit = 10000;
-               char buf[PATH_MAX+1];
-
-               for (uint32_t cnt = 1; cnt <= limit; ++cnt) {
-
-                       snprintf (buf, sizeof(buf), "%s-%u%s", newname.c_str(), cnt, suffix.c_str());
-
-                       if (!matching_unsuffixed_filename_exists_in (dir, buf)) {
-                               path = Glib::build_filename (dir, buf);
-                               if (!source_by_path (path)) {
-                                       break;
-                               }
-                       }
-
-                       path = "";
-               }
-
-               if (path.empty()) {
-                       fatal << string_compose (_("FATAL ERROR! Could not find a suitable version of %1 for a rename"),
-                                                newname) << endl;
-                       /*NOTREACHED*/
-               }
-       }
-
-       return path;
-}
-
 /** Return the full path (in some session directory) for a new within-session source.
  * \a name must be a session-unique name that does not contain slashes
  *         (e.g. as returned by new_*_source_name)
index 62ecab1397587a2bd0bc5bf3b8ebc5dca79096e7..e15bc9a2382440fc596d6586f581d2d19c96d269 100644 (file)
@@ -668,3 +668,35 @@ SMFSource::prevent_deletion ()
   
        _flags = Flag (_flags & ~(Removable|RemovableIfEmpty|RemoveAtDestroy));
 }
+
+int
+SMFSource::rename (const string& newname)
+{
+       Glib::Threads::Mutex::Lock lm (_lock);
+       string oldpath = _path;
+       string newpath = _session.new_source_path_from_name (DataType::MIDI, newname);
+
+       if (newpath.empty()) {
+               error << string_compose (_("programming error: %1"), "cannot generate a changed file path") << endmsg;
+               return -1;
+       }
+
+       // Test whether newpath exists, if yes notify the user but continue.
+       if (Glib::file_test (newpath, Glib::FILE_TEST_EXISTS)) {
+               error << string_compose (_("Programming error! %1 tried to rename a file over another file! It's safe to continue working, but please report this to the developers."), PROGRAM_NAME) << endmsg;
+               return -1;
+       }
+
+       if (Glib::file_test (oldpath.c_str(), Glib::FILE_TEST_EXISTS)) { 
+               /* rename only needed if file exists on disk */
+               if (::rename (oldpath.c_str(), newpath.c_str()) != 0) {
+                       error << string_compose (_("cannot rename file %1 to %2 (%3)"), oldpath, newpath, strerror(errno)) << endmsg;
+                       return -1;
+               }
+       }
+
+       _name = Glib::path_get_basename (newpath);
+       _path = newpath;
+
+       return 0;
+}