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

Peter Kümmel syntheticpp at gmx.net
Mon Jul 26 18:36:54 CEST 2010


SVN commit 1154987 by kuemmel:

AsciiPlugin: minimize macro usage

 M  +6 -4      ascii.pro  
 M  +8 -49     asciisource.cpp  
 M  +37 -2     kst_atof.cpp  
 M  +26 -1     kst_atof.h  


--- branches/work/kst/portto4/kst/src/datasources/ascii/ascii.pro #1154986:1154987
@@ -8,16 +8,18 @@
 SOURCES += \
   asciisource.cpp \
   asciisourceconfig.cpp \
-  asciiplugin.cpp
+  asciiplugin.cpp \
+  kst_atof.cpp
 
-win32:SOURCES += kst_atof.cpp
-
 HEADERS += \
   asciisource.h \
   asciisourceconfig.h \
   asciiplugin.h \
   namedparameter.h \
+  kst_atof.h  \
   ../../libkst/kst_inf.h
 
-win32:HEADERS += kst_atof.h
 FORMS += asciiconfig.ui
+
+win32:DEFINES += KST_USE_KST_ATOF  
+
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.cpp #1154986:1154987
@@ -22,28 +22,17 @@
 #include "math_kst.h"
 #include "kst_inf.h"
 #include "kst_i18n.h"
+#include "kst_atof.h"
 #include "measuretime.h"
 
 #include <QFile>
-#include <QLocale>
 
 #include <assert.h>
 #include <ctype.h>
-#include <math.h>
 #include <stdlib.h>
 
 
-#ifdef Q_OS_WIN
-#define USE_KST_ATOF
-#endif
 
-#ifdef USE_KST_ATOF
-#include "kst_atof.h"
-#define atof(X, Y) kst_atof(X, Y)
-#else
-#define atof(X, Y) atof(X)
-#endif
-
 using namespace Kst;
 
 
@@ -338,44 +327,14 @@
 */
 
 
-struct NumberLocale
-{
-  NumberLocale() : use_dot(false) {
-  }
 
-  ~NumberLocale() {
-    if (use_dot) {
-      //printf("original LC_NUMERIC: %s\n", orig.constData());
-      setlocale(LC_NUMERIC, orig.constData());
-    }
-  }
 
-  void useDot() {
-    use_dot = true;
-    orig = QByteArray((const char*) setlocale(LC_NUMERIC, 0));
-    setlocale(LC_NUMERIC, "C");
-  }
 
-private:
-  bool use_dot;
-  QByteArray orig;
-};
+int AsciiSource::readField(double *v, const QString& field, int s, int n) 
+{
+  LexicalCast lexc;
+  lexc.setDecimalSeparator(_config._useDot, _config._localSeparator);
 
-
-int AsciiSource::readField(double *v, const QString& field, int s, int n) {
-
-  NumberLocale dot;
-  char sep = _config._localSeparator;
-
-  if (_config._useDot) {
-#ifdef USE_KST_ATOF
-    sep = '.';
-#else
-    (void) sep;
-    dot.useDot();
-#endif
-  }
-
   if (n < 0) {
     n = 1; /* n < 0 means read one sample, not frame - irrelevent here */
   }
@@ -433,7 +392,7 @@
   if (_config._columnType == AsciiSourceConfig::Fixed) {
     for (int i = 0; i < n; ++i, ++s) {
       // Read appropriate column and convert to double
-      v[i] = atof(_tmpBuf + _rowIndex[i] - _rowIndex[0] + _config._columnWidth * (col - 1), sep);
+      v[i] = lexc.toDouble(_tmpBuf + _rowIndex[i] - _rowIndex[0] + _config._columnWidth * (col - 1));
     }
   } else if (_config._columnType == AsciiSourceConfig::Custom) {
     for (int i = 0; i < n; ++i, ++s) {
@@ -453,7 +412,7 @@
             ++i_col;
             if (i_col == col) {
               if (isdigit(_tmpBuf[ch]) || _tmpBuf[ch] == '-' || _tmpBuf[ch] == '.' || _tmpBuf[ch] == '+') {
-                v[i] = atof(_tmpBuf + ch, sep);
+                v[i] = lexc.toDouble(_tmpBuf + ch);
               } else if (ch + 2 < bufread && tolower(_tmpBuf[ch]) == 'i' &&
                   tolower(_tmpBuf[ch + 1]) == 'n' && tolower(_tmpBuf[ch + 2]) == 'f') {
                 v[i] = INF;
@@ -485,7 +444,7 @@
             ++i_col;
             if (i_col == col) {
               if (isdigit(_tmpBuf[ch]) || _tmpBuf[ch] == '-' || _tmpBuf[ch] == '.' || _tmpBuf[ch] == '+') {
-                v[i] = atof(_tmpBuf + ch, sep);
+                v[i] = lexc.toDouble(_tmpBuf + ch);
               } else if (ch + 2 < bufread && tolower(_tmpBuf[ch]) == 'i' &&
                   tolower(_tmpBuf[ch + 1]) == 'n' && tolower(_tmpBuf[ch + 2]) == 'f') {
                 v[i] = INF;
--- branches/work/kst/portto4/kst/src/datasources/ascii/kst_atof.cpp #1154986:1154987
@@ -1,13 +1,19 @@
 // patched BSD V7 code
 // http://www.bsdlover.cn/study/UnixTree/V7/usr/src/libc/gen/atof.c.html
 
+#include "kst_atof.h"
+
 #include <math.h>
 #include <ctype.h>
 
+#include <QLocale>
+
+
 #define LOGHUGE 39
 
 
-double kst_atof(const char* p, const char sep)
+#ifdef KST_USE_KST_ATOF
+double LexicalCast::toDouble(const char* p) const
 {
 	int c;
 	double fl, flexp, exp5;
@@ -36,7 +42,7 @@
 		nd++;
 	}
 
-	if (c == sep) {
+	if (c == _separator) {
 		while ((c = *p++), isdigit(c)) {
 			if (fl<big) {
 				fl = 10*fl + (c-'0');
@@ -95,5 +101,34 @@
 		fl = -fl;
 	return(fl);
 }
+#endif
 
+
+LexicalCast::LexicalCast() : _useDot(false) 
+{
+}
+
+
+LexicalCast::~LexicalCast() 
+{
+  if (_useDot) {
+    setlocale(LC_NUMERIC, _originalLocal.constData());
+  }
+}
+
+
+void LexicalCast::setDecimalSeparator(bool useDot, char separator)
+{
+  _useDot = useDot;  
+  if (_useDot) {   
+    _separator = '.';
+    _originalLocal = QByteArray((const char*) setlocale(LC_NUMERIC, 0));
+    setlocale(LC_NUMERIC, "C");
+  } else {
+    _separator = separator;
+  }
+}
+
+
+
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/datasources/ascii/kst_atof.h #1154986:1154987
@@ -1,8 +1,33 @@
 #ifndef  KST_ATOF_H
 #define KST_ATOF_H
 
-double kst_atof(const char* p, const char sep);
+#include <QByteArray>
+#include <math.h>
 
+
+struct LexicalCast
+{
+  LexicalCast();
+  ~LexicalCast();
+
+  // use second parameter when useDot is false
+  void setDecimalSeparator(bool useDot, char separator);
+
+#ifdef KST_USE_KST_ATOF
+  double toDouble(const char* p) const;
+#else
+  inline double toDouble(const char* p) const { return atof(p); }
 #endif
 
+private:
+  bool _useDot;  
+  char _separator;
+  QByteArray _originalLocal;
+};
+
+
+
+
+#endif
+
 // vim: ts=2 sw=2 et


More information about the Kst mailing list