fix a bunch of memory leaks
[ardour.git] / libs / ardour / smf_source.cc
index 812e06c27b08d30252630d1bb776fc4178567a8c..6ec7c5a78ce8318afc8890ea14df380769b91bbf 100644 (file)
@@ -26,7 +26,7 @@
 #include <errno.h>
 #include <regex.h>
 
-#include "pbd/pathscanner.h"
+#include "pbd/file_utils.h"
 #include "pbd/stl_delete.h"
 #include "pbd/strsplit.h"
 
@@ -35,6 +35,7 @@
 #include <glibmm/fileutils.h>
 
 #include "evoral/Control.hpp"
+#include "evoral/SMF.hpp"
 
 #include "ardour/event_type_map.h"
 #include "ardour/midi_model.h"
@@ -49,6 +50,7 @@
 using namespace ARDOUR;
 using namespace Glib;
 using namespace PBD;
+using namespace Evoral;
 
 /** Constructor used for new internal-to-session files.  File cannot exist. */
 SMFSource::SMFSource (Session& s, const string& path, Source::Flag flags)
@@ -83,6 +85,39 @@ SMFSource::SMFSource (Session& s, const string& path, Source::Flag flags)
        _open = true;
 }
 
+/** Constructor used for external-to-session files.  File must exist. */
+SMFSource::SMFSource (Session& s, const string& path)
+       : Source(s, DataType::MIDI, path, Source::Flag (0))
+       , MidiSource(s, path, Source::Flag (0))
+       , FileSource(s, DataType::MIDI, path, string(), Source::Flag (0))
+       , Evoral::SMF()
+       , _last_ev_time_beats(0.0)
+       , _last_ev_time_frames(0)
+       , _smf_last_read_end (0)
+       , _smf_last_read_time (0)
+{
+       /* note that origin remains empty */
+
+       if (init (_path, false)) {
+               throw failed_constructor ();
+       }
+        assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
+       existence_check ();
+
+       /* file is not opened until write */
+
+       if (_flags & Writable) {
+               return;
+       }
+
+       if (open (_path)) {
+               throw failed_constructor ();
+       }
+
+       _open = true;
+}
+
 /** Constructor used for existing internal-to-session files. */
 SMFSource::SMFSource (Session& s, const XMLNode& node, bool must_exist)
        : Source(s, node)
@@ -464,6 +499,15 @@ SMFSource::mark_midi_streaming_write_completed (Evoral::Sequence<Evoral::Musical
        mark_nonremovable ();
 }
 
+bool
+SMFSource::valid_midi_file (const string& file)
+{
+       if (safe_midi_file_extension (file) ) {
+               return (SMF::test (file) );
+       }
+       return false;
+}
+
 bool
 SMFSource::safe_midi_file_extension (const string& file)
 {
@@ -674,35 +718,5 @@ 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;
-}
+               
+