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

Peter Kümmel syntheticpp at gmx.net
Tue Jul 27 10:03:34 CEST 2010


SVN commit 1155288 by kuemmel:

AsciiPlugin: split out reading from file

 M  +14 -24    asciisource.cpp  
 M  +19 -1     asciisource.h  


--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.cpp #1155287:1155288
@@ -201,6 +201,8 @@
 }
 
 
+
+
 #define MAXBUFREADLEN 32768
 Kst::Object::UpdateType AsciiSource::internalDataSourceUpdate() 
 {
@@ -231,28 +233,23 @@
   }
 
 
-  int bufstart, bufread;
+  int bufread;
   bool new_data = false;
-  char tmpbuf[MAXBUFREADLEN+1];
+  bool first_read = (_numFrames == 0);
+
   QByteArray delbytes = _config._delimiters.value().toLatin1();
   const char *del = delbytes.constData();
 
-  bool first_read = (_numFrames==0);
   do {
-    /* Read the tmpbuffer, starting at row_index[_numFrames] */
-    if (_byteLength - _rowIndex[_numFrames] > MAXBUFREADLEN) {
-      bufread = MAXBUFREADLEN;
-    } else {
-      bufread = _byteLength - _rowIndex[_numFrames];
-    }
-
-    bufstart = _rowIndex[_numFrames];
-    file.seek(bufstart); // expensive?
-    file.read(tmpbuf, bufread);
+    // Read the tmpbuffer, starting at row_index[_numFrames]
+    QVarLengthArray<char, MAXBUFREADLEN + 1> tmpbuf;
+    tmpbuf.resize(tmpbuf.capacity());
+    int bufstart = _rowIndex[_numFrames];
+    bufread = readFromFile(file, tmpbuf, bufstart, _byteLength - bufstart, MAXBUFREADLEN);
     tmpbuf[bufread] = '\0';
 
     bool is_comment = false, has_dat = false;
-    char *comment = strpbrk(tmpbuf, del);
+    char *comment = strpbrk(tmpbuf.data(), del);
     for (int i = 0; i < bufread; i++) {
       if (comment == &(tmpbuf[i])) {
         is_comment = true;
@@ -273,8 +270,8 @@
         if (comment && comment < &(tmpbuf[i])) {
           comment = strpbrk(&(tmpbuf[i]), del);
         }
-      } else if (!is_comment && !isspace(tmpbuf[i])) {  // FIXME: this breaks
-                                                        // custom delimiters
+      } else if (!is_comment && !isspace(tmpbuf[i])) {  
+        // FIXME: this breaks custom delimiters
         has_dat = true;
       }
     }
@@ -348,15 +345,8 @@
   if (!openValidFile(file)) {
     return 0;
   }
+  readFromFile(file, _tmpBuffer, bufstart, bufread);    
     
-  _tmpBuffer.resize(bufread);
-  if(_tmpBuffer.size() < bufread) {
-    return -1;
-  }
-
-  file.seek(bufstart);    
-  file.read(&_tmpBuffer[0], bufread);
-
   if (_config._columnType == AsciiSourceConfig::Fixed) {
     for (int i = 0; i < n; ++i, ++s) {
       // Read appropriate column and convert to double
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.h #1155287:1155288
@@ -24,8 +24,8 @@
 #include "asciisourceconfig.h"
 
 #include <QVarLengthArray>
+#include <QFile>
 
-class QFile;
 class DataInterfaceAsciiVector;
 
 class AsciiSource : public Kst::DataSource
@@ -92,11 +92,29 @@
 
     bool openValidFile(QFile &file);
     static bool openFile(QFile &file);
+    template<class T>
+    int readFromFile(QFile&, T& buffer, int start, int numberOfBytes, int maximalBytes = -1);
 
     // TODO remove
     friend class DataInterfaceAsciiVector;
 };
 
 
+template<class T>
+int AsciiSource::readFromFile(QFile& file, T& buffer, int start, int bytesToRead, int maximalBytes)
+{    
+  if (maximalBytes == -1) {
+    buffer.resize(bytesToRead);
+  } else {
+    bytesToRead = qMin(bytesToRead, maximalBytes);
+    if (buffer.size() < bytesToRead) {
+      buffer.resize(bytesToRead);
+    }
+  }
+  file.seek(start); // expensive?
+  return file.read(buffer.data(), bytesToRead);
+}
+
+
 #endif
 // vim: ts=2 sw=2 et


More information about the Kst mailing list