fix a bunch of memory leaks
[ardour.git] / libs / ardour / smf_source.cc
index c504f5000628f67008c0a6e5478194414f952435..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)
@@ -63,9 +65,12 @@ SMFSource::SMFSource (Session& s, const string& path, Source::Flag flags)
 {
        /* note that origin remains empty */
 
-       if (init(_path, false)) {
+       if (init (_path, false)) {
                throw failed_constructor ();
        }
+        assert (!Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
+       existence_check ();
 
        /* file is not opened until write */
 
@@ -73,9 +78,43 @@ SMFSource::SMFSource (Session& s, const string& path, Source::Flag flags)
                return;
        }
 
-       if (open(_path)) {
+       if (open (_path)) {
+               throw failed_constructor ();
+       }
+
+       _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;
 }
 
@@ -93,10 +132,13 @@ SMFSource::SMFSource (Session& s, const XMLNode& node, bool must_exist)
                throw failed_constructor ();
        }
 
-       if (init(_path, true)) {
+       if (init (_path, true)) {
                throw failed_constructor ();
        }
 
+        assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
+       existence_check ();
+
        if (open(_path)) {
                throw failed_constructor ();
        }
@@ -457,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)
 {
@@ -658,3 +709,14 @@ SMFSource::ensure_disk_file ()
        }
 }
 
+void
+SMFSource::prevent_deletion ()
+{
+       /* Unlike the audio case, the MIDI file remains mutable (because we can
+          edit MIDI data)
+       */
+  
+       _flags = Flag (_flags & ~(Removable|RemovableIfEmpty|RemoveAtDestroy));
+}
+               
+