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

Peter Kümmel syntheticpp at gmx.net
Sat Oct 13 17:41:21 UTC 2012


SVN commit 1320357 by kuemmel:

split out file buffer

 M  +6 -31     asciidatareader.cpp  
 M  +7 -47     asciidatareader.h  
 A             asciifilebuffer.cpp   [License: GPL (v2+)]
 A             asciifilebuffer.h   [License: GPL (v2+)]
 M  +1 -1      asciisource.cpp  
 M  +3 -3      asciisource.h  


--- branches/work/kst/portto4/kst/src/datasources/ascii/asciidatareader.cpp #1320356:1320357
@@ -65,33 +65,8 @@
 }
 
 
-//-------------------------------------------------------------------------------------------
-bool AsciiDataReader::FileBuffer::resize(int bytes)
-{ 
-  try {
-    _array->resize(bytes);
-  } catch (const std::bad_alloc&) {
-    // work around Qt bug
-    clearFileBuffer(true);
-    return false;
-  }
-  return true;
-}
 
 //-------------------------------------------------------------------------------------------
-void AsciiDataReader::FileBuffer::clearFileBuffer(bool forceDelete)
-{
-  // force deletion of heap allocated memory if any
-  if (forceDelete || _array->capacity() > AsciiDataReader::FileBuffer::Prealloc) {
-    delete _array;
-    _array = new Array;
-  }
-  _bufferedS = -10;
-  _bufferedN = -10;
-}
-
-
-//-------------------------------------------------------------------------------------------
 void AsciiDataReader::toDouble(const LexicalCast& lexc, const char* buffer, int bufread, int ch, double* v, int)
 {
   if (   isDigit(buffer[ch])
@@ -120,7 +95,7 @@
 }
 
 
-int AsciiDataReader::readFromFile(QFile& file, AsciiDataReader::FileBuffer& buffer, int start, int bytesToRead, int maximalBytes)
+int AsciiDataReader::readFromFile(QFile& file, AsciiFileBuffer& buffer, int start, int bytesToRead, int maximalBytes)
 {    
   if (maximalBytes == -1) {
     if (!buffer.resize(bytesToRead + 1))
@@ -143,7 +118,7 @@
 }
 
 
-int AsciiDataReader::readField(FileBuffer* _fileBuffer, int col, int bufstart, int bufread,
+int AsciiDataReader::readField(AsciiFileBuffer* _fileBuffer, int col, int bufstart, int bufread,
                                double *v, const QString& field, int s, int n) 
   {
 
@@ -184,7 +159,7 @@
 
   bool new_data = false;
 
-  AsciiDataReader::FileBuffer buf;
+  AsciiFileBuffer buf;
   do {
     // Read the tmpbuffer, starting at row_index[_numFrames]
     buf.clear();
@@ -192,7 +167,7 @@
 
     //bufstart += bufread;
     buf._bufferedS = _rowIndex[numFrames]; // always read from the start of a line
-    buf._bufferedN = readFromFile(file, buf, buf._bufferedS, _byteLength - buf._bufferedS, AsciiDataReader::FileBuffer::Prealloc - 1);
+    buf._bufferedN = readFromFile(file, buf, buf._bufferedS, _byteLength - buf._bufferedS, AsciiFileBuffer::Prealloc - 1);
 
     if (_config._delimiters.value().size() == 0) {
       const AsciiDataReader::NoDelimiter comment_del;
@@ -216,7 +191,7 @@
         new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, AsciiDataReader::IsLineBreakCR(lineending), comment_del);
       }
     }
-  } while (buf._bufferedN == AsciiDataReader::FileBuffer::Prealloc - 1  && read_completely);
+  } while (buf._bufferedN == AsciiFileBuffer::Prealloc - 1  && read_completely);
 
   _rowIndex.resize(numFrames + 1);
 
@@ -242,7 +217,7 @@
         _rowIndex[_numFrames] = row_start;
         ++_numFrames;
         if (_numFrames >= _rowIndex.size()) {
-          _rowIndex.resize(_rowIndex.size() + AsciiDataReader::FileBuffer::Prealloc - 1);
+          _rowIndex.resize(_rowIndex.size() + AsciiFileBuffer::Prealloc - 1);
         }
         new_data = true;
         row_start = row_offset+i;
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciidatareader.h #1320356:1320357
@@ -20,11 +20,11 @@
 #define ASCII_DATA_READER_H
 
 #include "asciisourceconfig.h"
-#include "asciidatareader.h"
+#include "asciifilebuffer.h"
+
 #include "math_kst.h"
 #include "kst_inf.h"
 
-#include <QVarLengthArray>
 
 class QFile;
 class DataInterfaceAsciiString;
@@ -40,60 +40,20 @@
     // TODO remove
     mutable AsciiSourceConfig& _config;
 
+    typedef QVarLengthArray<int, AsciiFileBuffer::Prealloc> RowIndex;
+    RowIndex _rowIndex;
 
-    class FileBuffer
-    {
-    public:
+    inline RowIndex& rowIndex() { return _rowIndex; }
 
-      enum SizeOnStack
-      {
-        Prealloc =
-#if defined(__ANDROID__) || defined(__QNX__) // Some mobile systems really do not like you allocating 1MB on the stack.
-        1 * 1024
-#else
-        1 * 1024 * 1024
-#endif
-      };
-
-      typedef QVarLengthArray<char, Prealloc> Array;
-      typedef QVarLengthArray<int, Prealloc> RowIndex;
-
-      inline FileBuffer() : _bufferedS(-10), _bufferedN(-10), _array(new Array) {}
-      inline ~FileBuffer() { delete _array; }
-      
-      int _bufferedS;
-      int _bufferedN;
-
-      inline void clear() { _array->clear(); }
-      inline int size() const { return _array->size(); }
-      inline bool resize(int size);
-      inline int  capacity() const { return _array->capacity(); }
-      inline char* data() { return _array->data(); }
-
-      inline const char* const constPointer() const { return _array->data(); }
-      inline const Array& constArray() const{ return *_array; }
-
       void clearFileBuffer(bool forceDelete = false);
 
-
-    private:
-
-      Array* _array;
-    };
-
-    FileBuffer::RowIndex _rowIndex;
-
-    inline FileBuffer::RowIndex& rowIndex() { return _rowIndex; }
-
-    void clearFileBuffer(bool forceDelete = false);
-
     template<class T>
     bool resizeBuffer(T& buffer, int bytes);
 
     
-    int readFromFile(QFile&, AsciiDataReader::FileBuffer&, int start, int numberOfBytes, int maximalBytes = -1); 
+    int readFromFile(QFile&, AsciiFileBuffer&, int start, int numberOfBytes, int maximalBytes = -1); 
     
-    int readField(FileBuffer* _fileBuffer, int col, int bufstart, int bufread,
+    int readField(AsciiFileBuffer* _fileBuffer, int col, int bufstart, int bufread,
                   double *v, const QString& field, int s, int n);
 
 
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.cpp #1320356:1320357
@@ -60,7 +60,7 @@
 AsciiSource::AsciiSource(Kst::ObjectStore *store, QSettings *cfg, const QString& filename, const QString& type, const QDomElement& e) :
   Kst::DataSource(store, cfg, filename, type),
   r(_config),
-  _fileBuffer(new AsciiDataReader::FileBuffer),
+  _fileBuffer(new AsciiFileBuffer),
   is(new DataInterfaceAsciiString(*this)),
   iv(new DataInterfaceAsciiVector(*this))
 {
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.h #1320356:1320357
@@ -24,11 +24,11 @@
 #include "asciisourceconfig.h"
 #include "asciidatareader.h"
 
-#include <QVarLengthArray>
+
 #include <QFile>
-#include <QMap>
 
 
+
 class DataInterfaceAsciiString;
 class DataInterfaceAsciiVector;
 struct LexicalCast;
@@ -81,7 +81,7 @@
 
   private:
     AsciiDataReader r;
-    AsciiDataReader::FileBuffer* _fileBuffer;
+    AsciiFileBuffer* _fileBuffer;
     
 
     friend class ConfigWidgetAscii;


More information about the Kst mailing list