diff runner/JAMSFeatureWriter.cpp @ 169:859d8ec60e06 jams

Add setNofM logic which the JSON writer can use to write a list when sending multiple files' worth to a single target
author Chris Cannam
date Wed, 15 Oct 2014 16:05:15 +0100
parents 3e30dbb68ca2
children 3536342ac088
line wrap: on
line diff
--- a/runner/JAMSFeatureWriter.cpp	Wed Oct 15 15:20:16 2014 +0100
+++ b/runner/JAMSFeatureWriter.cpp	Wed Oct 15 16:05:15 2014 +0100
@@ -33,7 +33,9 @@
 		      SupportStdOut,
                       "json"),
     m_network(false),
-    m_networkRetrieved(false)
+    m_networkRetrieved(false),
+    m_n(1),
+    m_m(1)
 {
 }
 
@@ -142,7 +144,13 @@
         if (f.values.size() > 0) {
             d += QString(", \"value\": [ ");
             for (int j = 0; j < int(f.values.size()); ++j) {
-                d += QString("%1 ").arg(f.values[i]);
+                if (isnan(f.values[j])) {
+                    d += "\"NaN\" ";
+                } else if (isinf(f.values[j])) {
+                    d += "\"Inf\" ";
+                } else {
+                    d += QString("%1 ").arg(f.values[j]);
+                }
             }
             d += "]";
         }
@@ -154,6 +162,18 @@
 }
 
 void
+JAMSFeatureWriter::setNofM(int n, int m)
+{
+    if (m_singleFileName != "" || m_stdout) {
+        m_n = n;
+        m_m = m;
+    } else {
+        m_n = 1;
+        m_m = 1;
+    }
+}
+
+void
 JAMSFeatureWriter::finish()
 {
     for (FileStreamMap::const_iterator stri = m_streams.begin();
@@ -162,10 +182,6 @@
         QTextStream *sptr = stri->second;
         QTextStream &stream = *sptr;
 
-        if (m_streamTracks[sptr].size() > 1) {
-            stream << "[\n";
-        }
-
         bool firstInStream = true;
 
         for (TrackIds::const_iterator tri = m_streamTracks[sptr].begin();
@@ -173,7 +189,13 @@
 
             TrackId trackId = *tri;
 
-            if (!firstInStream) {
+            if (firstInStream) {
+                if (m_streamTracks[sptr].size() > 1 || (m_m > 1 && m_n == 1)) {
+                    stream << "[\n";
+                }
+            }
+
+            if (!firstInStream || (m_m > 1 && m_n > 1)) {
                 stream << ",\n";
             }
 
@@ -253,10 +275,12 @@
             firstInStream = false;
         }
 
-        if (m_streamTracks[sptr].size() > 1) {
-            stream << "\n]";
+        if (!firstInStream) {
+            if (m_streamTracks[sptr].size() > 1 || (m_m > 1 && m_n == m_m)) {
+                stream << "\n]";
+            }
+            stream << "\n";
         }
-        stream << "\n";
     }
         
     m_streamTracks.clear();