[Kst] extragear/graphics/kst/kst/datasources/ascii

Ted Kisner tskisner.public at gmail.com
Tue Jan 31 03:04:46 CET 2006


SVN commit 504007 by tskisner:

This patch implements a temporary fix that allows the ascii datasource to read files with varying numbers of columns in a single file.  Reviewed/approved on the mailing list.

 M  +48 -16    ascii.cpp  


--- trunk/extragear/graphics/kst/kst/datasources/ascii/ascii.cpp #504006:504007
@@ -32,7 +32,7 @@
 #include <qstylesheet.h>
 
 #include <kcombobox.h>
-#include <kdebug.h>
+#include <ksdebug.h>
 
 #include <kstmath.h>
 #include "ascii.h"
@@ -605,7 +605,6 @@
   return _numFrames < 1;
 }
 
-
 QStringList AsciiSource::fieldListFor(const QString& filename, AsciiSource::Config *cfg) {
   QStringList rc;
   QFile file(filename);
@@ -651,16 +650,59 @@
   bool done = false;
   QString line;
   int skip = cfg->_dataLine;
-  while (!file.atEnd() && !done) {
+  //FIXME This is a hack which should eventually be fixed by specifying 
+  // the starting frame of the data when calling KstDataSource::fieldListForSource
+  // and KstDataSource::fieldList.  If the skip value is not specified, then 
+  // we scan a few lines and take the maximum number of fields that we find.
+  int maxcnt;
+  if (skip > 0) {
+    maxcnt = -1;
+  } else {
+    maxcnt = 0;
+  }
+  int cnt;
+  int nextscan = 0;
+  int curscan = 0;
+  while (!file.atEnd() && !done && (nextscan < 200)) {
     int r = readFullLine(file, line);
-    if (skip > 0) {
+    if (skip > 0) { //keep skipping until desired line
       --skip;
       if (r < 0) {
         return rc;
       }
       continue;
     }
-    if (r > 1 && !re.exactMatch(line)) {
+    if (maxcnt >= 0) { //original skip value == 0, so scan some lines
+      if (curscan >= nextscan) {
+        if (r > 1 && !re.exactMatch(line)) {
+          line = line.stripWhiteSpace();
+          if (cfg->_columnType == AsciiSource::Config::Custom && !cfg->_columnDelimiter.isEmpty()) {
+            cnt = QStringList::split(QRegExp(QString("[%1]").arg(QRegExp::escape(cfg->_columnDelimiter))), line, false).count();
+          } else if (cfg->_columnType == AsciiSource::Config::Fixed) {
+            cnt = line.length() / cfg->_columnWidth;
+          } else {
+            cnt = QStringList::split(QRegExp("\\s"), line, false).count();
+          }
+          if (cnt > maxcnt) {
+            maxcnt = cnt;
+          }
+        } else if (r < 0) {
+          return rc;
+        }
+        nextscan += nextscan + 1;
+      }
+      curscan++;
+      continue;
+    }
+    if (r > 1 && !re.exactMatch(line)) { //at desired line, find count
+      line = line.stripWhiteSpace();
+      if (cfg->_columnType == AsciiSource::Config::Custom && !cfg->_columnDelimiter.isEmpty()) {
+        maxcnt = QStringList::split(QRegExp(QString("[%1]").arg(QRegExp::escape(cfg->_columnDelimiter))), line, false).count();
+      } else if (cfg->_columnType == AsciiSource::Config::Fixed) {
+        maxcnt = line.length() / cfg->_columnWidth;
+      } else {
+        maxcnt = QStringList::split(QRegExp("\\s"), line, false).count();
+      }
       done = true;
     } else if (r < 0) {
       return rc;
@@ -668,17 +710,7 @@
   }
 
   file.close();
-
-  int cnt;
-  line = line.stripWhiteSpace();
-  if (cfg->_columnType == AsciiSource::Config::Custom && !cfg->_columnDelimiter.isEmpty()) {
-    cnt = QStringList::split(QRegExp(QString("[%1]").arg(QRegExp::escape(cfg->_columnDelimiter))), line, false).count();
-  } else if (cfg->_columnType == AsciiSource::Config::Fixed) {
-    cnt = line.length() / cfg->_columnWidth;
-  } else {
-    cnt = QStringList::split(QRegExp("\\s"), line, false).count();
-  }
-  for (int i = 1; i <= cnt; ++i) {
+  for (int i = 1; i <= maxcnt; ++i) {
     rc += QString::number(i);
   }
 


More information about the Kst mailing list