changeset 625:608b4dc5ff34

* Improve behaviour when output file write fails (i.e. fail earlier)
author Chris Cannam
date Fri, 21 May 2010 10:21:18 +0000
parents 076dcd2ce209
children 686fc1703a33
files transform/FeatureWriter.h transform/FileFeatureWriter.cpp transform/FileFeatureWriter.h
diffstat 3 files changed, 41 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/transform/FeatureWriter.h	Fri May 21 10:20:52 2010 +0000
+++ b/transform/FeatureWriter.h	Fri May 21 10:21:18 2010 +0000
@@ -85,6 +85,16 @@
                        const Vamp::Plugin::FeatureList &features,
                        std::string summaryType = "") = 0;
 
+    /**
+     * Throw FailedToOpenOutputStream if we can already tell that we
+     * will be unable to write to the output file.  This is called to
+     * test the output stream before processing begins.  The writer
+     * may legitimately succeed here but still fail later -- this is
+     * really an optimisation to ensure that easy-to-recognise failure
+     * cases fail early.
+     */
+    virtual void testOutputFile(QString trackId, TransformId transformId) { }
+
     virtual void flush() { } // whatever the last stream was
 
     virtual void finish() = 0;
--- a/transform/FileFeatureWriter.cpp	Fri May 21 10:20:52 2010 +0000
+++ b/transform/FileFeatureWriter.cpp	Fri May 21 10:21:18 2010 +0000
@@ -59,9 +59,11 @@
         m_streams.erase(m_streams.begin());
     }
     while (!m_files.empty()) {
-        cerr << "FileFeatureWriter::~FileFeatureWriter: NOTE: Closing feature file \""
-             << m_files.begin()->second->fileName().toStdString() << "\"" << endl;
-        delete m_files.begin()->second;
+        if (m_files.begin()->second) {
+            cerr << "FileFeatureWriter::~FileFeatureWriter: NOTE: Closing feature file \""
+                 << m_files.begin()->second->fileName().toStdString() << "\"" << endl;
+            delete m_files.begin()->second;
+        }
         m_files.erase(m_files.begin());
     }
 }
@@ -153,8 +155,9 @@
     }
 }
 
-QString FileFeatureWriter::getOutputFilename(QString trackId,
-                                             TransformId transformId)
+QString
+FileFeatureWriter::getOutputFilename(QString trackId,
+                                     TransformId transformId)
 {
     if (m_singleFileName != "") {
         if (QFileInfo(m_singleFileName).exists() && !(m_force || m_append)) {
@@ -181,14 +184,14 @@
         infilename = scheme + ":" + infilename; // DOS drive!
     }
 
-    cerr << "trackId = " << trackId.toStdString() << ", url = " << url.toString().toStdString() << ", infilename = "
-         << infilename.toStdString() << ", basename = " << basename.toStdString() << ", m_baseDir = " << m_baseDir.toStdString() << endl;
+//    cerr << "trackId = " << trackId.toStdString() << ", url = " << url.toString().toStdString() << ", infilename = "
+//         << infilename.toStdString() << ", basename = " << basename.toStdString() << ", m_baseDir = " << m_baseDir.toStdString() << endl;
 
     if (m_baseDir != "") dirname = QFileInfo(m_baseDir).absoluteFilePath();
     else if (local) dirname = QFileInfo(infilename).absolutePath();
     else dirname = QDir::currentPath();
 
-    cerr << "dirname = " << dirname.toStdString() << endl;
+//    cerr << "dirname = " << dirname.toStdString() << endl;
 
     QString filename;
 
@@ -211,9 +214,20 @@
     return filename;
 }
 
+void
+FileFeatureWriter::testOutputFile(QString trackId,
+                                  TransformId transformId)
+{
+    if (m_stdout) return;
+    QString filename = getOutputFilename(trackId, transformId);
+    if (filename == "") {
+        throw FailedToOpenOutputStream(trackId, transformId);
+    }
+}
 
-QFile *FileFeatureWriter::getOutputFile(QString trackId,
-                                        TransformId transformId)
+QFile *
+FileFeatureWriter::getOutputFile(QString trackId,
+                                 TransformId transformId)
 {
     pair<QString, TransformId> key;
 
@@ -229,7 +243,7 @@
 
         QString filename = getOutputFilename(trackId, transformId);
 
-        if (filename == "") { // stdout
+        if (filename == "") { // stdout or failure
             return 0;
         }
 
@@ -309,9 +323,11 @@
         m_streams.erase(m_streams.begin());
     }
     while (!m_files.empty()) {
-        cerr << "FileFeatureWriter::finish: NOTE: Closing feature file \""
-             << m_files.begin()->second->fileName().toStdString() << "\"" << endl;
-        delete m_files.begin()->second;
+        if (m_files.begin()->second) {
+            cerr << "FileFeatureWriter::finish: NOTE: Closing feature file \""
+                 << m_files.begin()->second->fileName().toStdString() << "\"" << endl;
+            delete m_files.begin()->second;
+        }
         m_files.erase(m_files.begin());
     }
     m_prevstream = 0;
--- a/transform/FileFeatureWriter.h	Fri May 21 10:20:52 2010 +0000
+++ b/transform/FileFeatureWriter.h	Fri May 21 10:21:18 2010 +0000
@@ -42,6 +42,7 @@
     virtual ParameterList getSupportedParameters() const;
     virtual void setParameters(map<string, string> &params);
 
+    virtual void testOutputFile(QString trackId, TransformId transformId);
     virtual void flush();
     virtual void finish();