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

Peter Kümmel syntheticpp at gmx.net
Sun Oct 14 12:02:55 UTC 2012


SVN commit 1320469 by kuemmel:

split out character traits

 M  +20 -15    asciidatareader.cpp  
 M  +8 -109    asciidatareader.h  


--- branches/work/kst/portto4/kst/src/datasources/ascii/asciidatareader.cpp #1320468:1320469
@@ -13,6 +13,7 @@
 #include "asciidatareader.h"
 
 #include "asciisourceconfig.h"
+
 #include "math_kst.h"
 #include "kst_inf.h"
 #include "kst_i18n.h"
@@ -36,6 +37,9 @@
 #endif
 
 
+using namespace AsciiCharacterTraits;
+
+
 AsciiDataReader::AsciiDataReader(AsciiSourceConfig& c) : _config(c)
 {
 }
@@ -49,8 +53,9 @@
 
 
 
+
 //-------------------------------------------------------------------------------------------
-AsciiDataReader::LineEndingType AsciiDataReader::detectLineEndingType(QFile& file) const
+LineEndingType AsciiDataReader::detectLineEndingType(QFile& file) const
 {
   QByteArray line;
   int line_size = 0;
@@ -140,16 +145,16 @@
   } else if (_config._columnType == AsciiSourceConfig::Custom) {
     if (_config._columnDelimiter.value().size() == 1) {
       MeasureTime t("AsciiSource::readField: 1 custom column delimiter");
-      const AsciiDataReader::IsCharacter column_del(_config._columnDelimiter.value()[0].toLatin1());
+      const IsCharacter column_del(_config._columnDelimiter.value()[0].toLatin1());
       return readColumns(v, _fileBuffer->constData(), bufstart, bufread, col, s, n, _lineending, column_del);
     } if (_config._columnDelimiter.value().size() > 1) {
       MeasureTime t(QString("AsciiSource::readField: %1 custom column delimiters").arg(_config._columnDelimiter.value().size()));
-      const AsciiDataReader::IsInString column_del(_config._columnDelimiter.value());
+      const IsInString column_del(_config._columnDelimiter.value());
       return readColumns(v, _fileBuffer->constData(), bufstart, bufread, col, s, n, _lineending, column_del);
     }
   } else if (_config._columnType == AsciiSourceConfig::Whitespace) {
     MeasureTime t("AsciiSource::readField: whitespace separated columns");
-    const AsciiDataReader::IsWhiteSpace column_del;
+    const IsWhiteSpace column_del;
     return readColumns(v, _fileBuffer->constData(), bufstart, bufread, col, s, n, _lineending, column_del);
   }
 
@@ -159,7 +164,7 @@
 
 bool AsciiDataReader::findDataRows(int& numFrames, bool read_completely, QFile& file, int _byteLength)
 {
-  AsciiDataReader::LineEndingType lineending = detectLineEndingType(file);
+  LineEndingType lineending = detectLineEndingType(file);
 
   bool new_data = false;
 
@@ -174,25 +179,25 @@
     buf._bufferedN = readFromFile(file, buf, buf._bufferedS, _byteLength - buf._bufferedS, AsciiFileBuffer::Prealloc - 1);
 
     if (_config._delimiters.value().size() == 0) {
-      const AsciiDataReader::NoDelimiter comment_del;
+      const NoDelimiter comment_del;
       if (lineending.isLF()) {
-        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, AsciiDataReader::IsLineBreakLF(lineending), comment_del);
+        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, IsLineBreakLF(lineending), comment_del);
       } else {
-        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, AsciiDataReader::IsLineBreakCR(lineending), comment_del);
+        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, IsLineBreakCR(lineending), comment_del);
       }
     } else if (_config._delimiters.value().size() == 1) {
-      const AsciiDataReader::IsCharacter comment_del(_config._delimiters.value()[0].toLatin1());
+      const IsCharacter comment_del(_config._delimiters.value()[0].toLatin1());
       if (lineending.isLF()) {
-        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, AsciiDataReader::IsLineBreakLF(lineending), comment_del);
+        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, IsLineBreakLF(lineending), comment_del);
       } else {
-        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, AsciiDataReader::IsLineBreakCR(lineending), comment_del);
+        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, IsLineBreakCR(lineending), comment_del);
       }
     } else if (_config._delimiters.value().size() > 1) {
-      const AsciiDataReader::IsInString comment_del(_config._delimiters.value());
+      const IsInString comment_del(_config._delimiters.value());
       if (lineending.isLF()) {
-        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedS, AsciiDataReader::IsLineBreakLF(lineending), comment_del);
+        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedS, IsLineBreakLF(lineending), comment_del);
       } else {
-        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, AsciiDataReader::IsLineBreakCR(lineending), comment_del);
+        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, IsLineBreakCR(lineending), comment_del);
       }
     }
   } while (buf._bufferedN == AsciiFileBuffer::Prealloc - 1  && read_completely);
@@ -205,7 +210,7 @@
 template<class Buffer, typename IsLineBreak, typename CommentDelimiter>
 bool AsciiDataReader::findDataRows(int& _numFrames, const Buffer& buffer, int bufstart, int bufread, const IsLineBreak& isLineBreak, const CommentDelimiter& comment_del)
 {
-  const AsciiDataReader::IsWhiteSpace isWhiteSpace;
+  const IsWhiteSpace isWhiteSpace;
 
   bool new_data = false;
   bool row_has_data = false;
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciidatareader.h #1320468:1320469
@@ -14,9 +14,10 @@
 #define ASCII_DATA_READER_H
 
 #include "asciifilebuffer.h"
+#include "asciicharactertraits.h"
 
 class QFile;
-class LexicalCast;
+struct LexicalCast;
 class AsciiSourceConfig;
 
 class AsciiDataReader
@@ -38,123 +39,21 @@
     template<class T>
     bool resizeBuffer(T& buffer, int bytes);
 
+    AsciiCharacterTraits::LineEndingType detectLineEndingType(QFile& file) const;
+    AsciiCharacterTraits::LineEndingType _lineending;
     
     int readFromFile(QFile&, AsciiFileBuffer&, int start, int numberOfBytes, int maximalBytes = -1); 
     
     int readField(AsciiFileBuffer* _fileBuffer, int col, int bufstart, int bufread,
                   double *v, const QString& field, int s, int n);
 
-
-    struct LineEndingType {
-      bool is_crlf;
-      char character;
-      bool isCR() const { return character == '\r'; }
-      bool isLF() const { return character == '\n'; }
-    };
-    LineEndingType detectLineEndingType(QFile& file) const;
-    LineEndingType _lineending;
-
-    // column and comment delimiter functions
-
-    struct AlwaysTrue {
-      AlwaysTrue() {
-      }
-      inline bool operator()() const {
-        return true;
-      }
-    };
-
-    struct AlwaysFalse {
-      AlwaysFalse() {
-      }
-      inline bool operator()() const {
-        return false;
-      }
-    };
-
-    struct NoDelimiter {
-      NoDelimiter() {
-      }
-      inline bool operator()(const char) const {
-        return false;
-      }
-    };
-
-    struct  IsWhiteSpace {
-      IsWhiteSpace() {
-      }
-      inline bool operator()(const char c) const {
-        return c == ' ' || c == '\t';
-      }
-    };
-
-    struct IsDigit {
-      IsDigit() {
-      }
-      inline bool operator()(const char c) const {
-        return (c >= 48) && (c <= 57) ? true : false;
-      }
-    };
-
-    struct IsCharacter {
-      IsCharacter(char c) : character(c) {
-      }
-      const char character;
-      inline bool operator()(const char c) const {
-        return character == c;
-      }
-    };
-
-    struct IsInString {
-      IsInString(const QString& s) : str(s), chars(s.size()) {
-        QByteArray ascii = str.toLatin1();
-        for (int i = 0; i < 6 && i < chars; i++) {
-          ch[i] = ascii[i];
-        }
-      }
-      const QString str;
-      const int chars;
-      char ch[6];
-      inline bool operator()(const char c) const {
-        switch (chars) {
-          case 0: return false;
-          case 1: return ch[0]==c;
-          case 2: return ch[0]==c || ch[1]==c;
-          case 3: return ch[0]==c || ch[1]==c || ch[2]==c;
-          case 4: return ch[0]==c || ch[1]==c || ch[2]==c || ch[3]==c;
-          case 5: return ch[0]==c || ch[1]==c || ch[2]==c || ch[3]==c || ch[4]==c;
-          case 6: return ch[0]==c || ch[1]==c || ch[2]==c || ch[3]==c || ch[4]==c || ch[5]==c;
-          default: return str.contains(c);
-        }
-      }
-    };
-
-    struct IsLineBreakLF {
-      IsLineBreakLF(const LineEndingType&) : size(1) {
-      }
-      const int size;
-      inline bool operator()(const char c) const {
-        return c == '\n';
-      }
-    };
-
-    struct IsLineBreakCR {
-      IsLineBreakCR(const LineEndingType& t) : size( t.is_crlf ? 2 : 1 ) {
-      }
-      const int size;
-      inline bool operator()(const char c) const {
-        return c == '\r';
-      }
-    };
-
-
     template<class Buffer, typename ColumnDelimiter>
     int readColumns(double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
-                    const LineEndingType&, const ColumnDelimiter&);
+                    const AsciiCharacterTraits::LineEndingType&, const ColumnDelimiter&);
 
     template<class Buffer, typename ColumnDelimiter, typename CommentDelimiter>
     int readColumns(double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
-                    const LineEndingType&, const ColumnDelimiter&, const CommentDelimiter&);
+                    const AsciiCharacterTraits::LineEndingType&, const ColumnDelimiter&, const CommentDelimiter&);
 
     template<class Buffer, typename IsLineBreak, typename ColumnDelimiter, typename CommentDelimiter, typename ColumnWidthsAreConst>
     int readColumns(double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
@@ -167,8 +66,8 @@
 
     void toDouble(const LexicalCast& lexc, const char* buffer, int bufread, int ch, double* v, int row);
 
-    const IsDigit isDigit;
-    const IsWhiteSpace isWhiteSpace;
+    const AsciiCharacterTraits::IsDigit isDigit;
+    const AsciiCharacterTraits::IsWhiteSpace isWhiteSpace;
 };
 
 #endif


More information about the Kst mailing list