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

Peter Kümmel syntheticpp at gmx.net
Wed Oct 17 18:09:17 UTC 2012


SVN commit 1320971 by kuemmel:

don't change location while reading with threads

 M  +2 -12     asciidatareader.cpp  
 M  +1 -1      asciidatareader.h  
 M  +6 -0      asciisource.cpp  
 M  +14 -10    kst_atof.cpp  
 M  +17 -6     kst_atof.h  


--- branches/work/kst/portto4/kst/src/datasources/ascii/asciidatareader.cpp #1320970:1320971
@@ -203,8 +203,7 @@
 {
   if (_config._columnType == AsciiSourceConfig::Fixed) {
     //MeasureTime t("AsciiSource::readField: same width for all columns");
-    LexicalCast lexc;
-    lexc.setDecimalSeparator(_config._useDot);
+    const LexicalCast& lexc = LexicalCast::instance();
     // &buffer[0] points to first row at _rowIndex[0] , so if we wanna find
     // the column in row i by adding _rowIndex[i] we have to start at:
     const char* col_start = &buf.constData()[0] - _rowIndex[0] + _config._columnWidth * (col - 1);
@@ -281,12 +280,7 @@
                                  const ColumnDelimiter& column_del, const CommentDelimiter& comment_del,
                                  const ColumnWidthsAreConst& are_column_widths_const) const
 {
-  LexicalCast& lexc(*new LexicalCast);
-  {
-    // TODO move
-    QMutexLocker lock(&_localeMutex);
-    lexc.setDecimalSeparator(_config._useDot);
-  }
+  const LexicalCast& lexc = LexicalCast::instance();
 
   const QString delimiters = _config._delimiters.value();
 
@@ -336,10 +330,6 @@
     }
   }
 
-  {
-    QMutexLocker lock(&_localeMutex);
-    delete &lexc;
-  }
   return n;
 }
 
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciidatareader.h #1320970:1320971
@@ -20,7 +20,7 @@
 #include <QMutex>
 
 class QFile;
-struct LexicalCast;
+class LexicalCast;
 class AsciiSourceConfig;
 
 
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.cpp #1320970:1320971
@@ -268,6 +268,8 @@
       return 0;
     }
 
+    // prepare file buffer
+
     _fileBuffer.setFile(file);
 
     int numThreads;
@@ -295,6 +297,10 @@
     _reader.detectLineEndingType(*file);
   }
   
+  // now start reading
+
+  LexicalCast::AutoDot useDot(_config._useDot);
+
   int sampleRead = 0;
   QVector<QVector<AsciiFileData> >& slidingWindow = _fileBuffer.fileData();
   if (_config._useThreads) {
--- branches/work/kst/portto4/kst/src/datasources/ascii/kst_atof.cpp #1320970:1320971
@@ -15,7 +15,6 @@
 
 #include "kst_atof.h"
 
-
 #include <math.h>
 #include <ctype.h>
 
@@ -24,10 +23,8 @@
 
 #define LOGHUGE 39
 
-inline bool LexicalCast::isDigit(const char c) const {
-  return (c >= 48) && (c <= 57) ? true : false;
-}
 
+//-------------------------------------------------------------------------------------------
 #ifdef KST_USE_KST_ATOF
 double LexicalCast::toDouble(const char* signedp) const
 {
@@ -121,27 +118,34 @@
 #endif
 
 
+//-------------------------------------------------------------------------------------------
+LexicalCast& LexicalCast::instance()
+{
+  static LexicalCast lexcInstance;
+  return lexcInstance;
+}
+
+//-------------------------------------------------------------------------------------------
 LexicalCast::LexicalCast()
 {
 }
 
-
+//-------------------------------------------------------------------------------------------
 LexicalCast::~LexicalCast() 
 {
   resetLocal();
-};
+}
 
-
+//-------------------------------------------------------------------------------------------
 void LexicalCast::resetLocal() 
 {
-  
   if (!_originalLocal.isEmpty()) {
     setlocale(LC_NUMERIC, _originalLocal.constData());
     _originalLocal.clear();
   }
 }
 
-
+//-------------------------------------------------------------------------------------------
 void LexicalCast::setDecimalSeparator(bool useDot)
 {
   useDot ? _separator = '.' : _separator = ',';
@@ -158,7 +162,7 @@
   }
 }
 
-
+//-------------------------------------------------------------------------------------------
 char LexicalCast::localSeparator() const
 {
   return *setlocale(LC_NUMERIC, 0);
--- branches/work/kst/portto4/kst/src/datasources/ascii/kst_atof.h #1320970:1320971
@@ -17,16 +17,21 @@
 #include <stdlib.h>
 
 
-struct LexicalCast
+class LexicalCast
 {
-  LexicalCast();
-  ~LexicalCast();
+public:
+  static LexicalCast& instance();
 
-  char localSeparator() const;
+  struct AutoDot {
+    inline AutoDot(bool useDot) { instance().setDecimalSeparator(useDot); }
+    inline ~AutoDot() { instance().resetLocal(); }
+  };
 
   // use second parameter when useDot is false
   void setDecimalSeparator(bool useDot);
 
+  char localSeparator() const;
+
 #ifdef KST_USE_KST_ATOF
   double toDouble(const char* p) const;
 #else
@@ -34,13 +39,19 @@
 #endif
 
 private:
+  LexicalCast();
+  ~LexicalCast();
+
   char _separator;
   QByteArray _originalLocal;
+
   void resetLocal();
-  bool isDigit(const char) const;
-};
 
+  inline bool isDigit(const char c) const {
+    return (c >= 48) && (c <= 57) ? true : false;
+  }
 
+};
 
 
 #endif


More information about the Kst mailing list