[Kst] branches/work/kst/portto4/kst/src/datasources/ascii

Peter Kümmel syntheticpp at gmx.net
Fri Jan 21 15:02:27 CET 2011


SVN commit 1216126 by kuemmel:

AsciiDatasource: split out common column search code

 M  +32 -33    asciisource.cpp  
 M  +1 -0      asciisource.h  


--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.cpp #1216125:1216126
@@ -394,11 +394,20 @@
 
 
 //-------------------------------------------------------------------------------------------
-int AsciiSource::readField(double *v, const QString& field, int s, int n) 
+namespace 
 {
-  LexicalCast lexc;
-  lexc.setDecimalSeparator(_config._useDot, _config._localSeparator);
+  bool isWhiteSpace(char c) { 
+    return isspace((unsigned char)c); 
+  }
 
+  char columnDelimiter;
+  bool isColumnDelimiter(char c) {
+    return columnDelimiter == c;
+  }
+}
+
+int AsciiSource::readField(double *v, const QString& field, int s, int n) 
+{
   if (n < 0) {
     n = 1; /* n < 0 means read one sample, not frame - irrelevent here */
   }
@@ -436,38 +445,33 @@
 
 
   if (_config._columnType == AsciiSourceConfig::Fixed) {
+    LexicalCast lexc;
+    lexc.setDecimalSeparator(_config._useDot, _config._localSeparator);
     for (int i = 0; i < n; ++i, ++s) {
       // Read appropriate column and convert to double
       v[i] = lexc.toDouble(&buffer[0] + _rowIndex[i] - _rowIndex[0] + _config._columnWidth * (col - 1));
     }
   } else if (_config._columnType == AsciiSourceConfig::Custom) {
-    const QString delimiters = _config._delimiters.value();
-    const QString columnDelimiter = _config._columnDelimiter.value();
-    for (int i = 0; i < n; ++i, ++s) {
-      bool incol = false;
-      int i_col = 0;
-
-      v[i] = Kst::NOPOINT;
-      for (int ch = _rowIndex[s] - bufstart; ch < bufread; ++ch) {
-        if (buffer[ch] == '\n' || buffer[ch] == '\r') {
-          break;
-        } else if (columnDelimiter.contains(buffer[ch])) { //<- check for column start
-          incol = false;
-        } else if (delimiters.contains(buffer[ch])) {
-          break;
+    if (_config._columnDelimiter.value().isEmpty()) {
+      return 0;
+    }
+    columnDelimiter = _config._columnDelimiter.value().toAscii().constData()[0];
+    readColumns(v, buffer, bufstart, bufread, col, s, n, &isColumnDelimiter);
+  } else if (_config._columnType == AsciiSourceConfig::Whitespace) {
+    readColumns(v, buffer, bufstart, bufread, col, s, n, &isWhiteSpace);
         } else {
-          if (!incol) {
-            incol = true;
-            ++i_col;
-            if (i_col == col) {
-              toDouble(lexc, buffer, bufread, ch, &v[i], i);
-              break;
+    return 0;
             }
+
+  return n;
           }
-        }
-      }
-    }
-  } else if (_config._columnType == AsciiSourceConfig::Whitespace) {
+
+
+
+void AsciiSource::readColumns(double* v, const char* buffer, int bufstart, int bufread, int col, int s, int n, bool (*isColumnDelemiterFunction)(char))
+{
+    LexicalCast lexc;
+    lexc.setDecimalSeparator(_config._useDot, _config._localSeparator);
     const QString delimiters = _config._delimiters.value();
     for (int i = 0; i < n; i++, s++) {
       bool incol = false;
@@ -478,7 +482,7 @@
       for (ch = _rowIndex[s] - bufstart; ch < bufread; ++ch) {
         if (buffer[ch] == '\n' || buffer[ch] == '\r') {
           break;
-        } else if (isspace((unsigned char)buffer[ch])) { //<- check for column start
+        } else if (isColumnDelemiterFunction(buffer[ch])) { //<- check for column start
             incol = false;
         } else if (delimiters.contains(buffer[ch])) {
           break;
@@ -494,14 +498,9 @@
         }
       }     
     }
-  } else {
-    return 0;
   }
 
-  return n;
-}
 
-
 void AsciiSource::toDouble(const LexicalCast& lexc, const char* buffer, int bufread, int ch, double* v, int row)
 {
   const char* here = &buffer[ch];
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.h #1216125:1216126
@@ -102,6 +102,7 @@
     template<class T>
     int readFromFile(QFile&, T& buffer, int start, int numberOfBytes, int maximalBytes = -1);
 
+    void readColumns(double* v, const char* buffer, int bufstart, int bufread, int col, int s, int n, bool (*isColumnDelemiterFunction)(char));
     void toDouble(const LexicalCast& lexc, const char* buffer, int bufread, int ch, double* v, int row);
 
     // TODO remove


More information about the Kst mailing list