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

Peter Kümmel syntheticpp at gmx.net
Mon Oct 22 10:11:10 UTC 2012


SVN commit 1321913 by kuemmel:

add support for reading QDateTime readable times

 M  +41 -19    asciiconfig.ui  
 M  +8 -2      asciiconfigwidget.cpp  
 M  +5 -1      asciisource.cpp  
 M  +13 -3     asciisourceconfig.cpp  
 M  +5 -2      asciisourceconfig.h  
 M  +53 -3     kst_atof.cpp  
 M  +13 -5     kst_atof.h  


--- branches/work/kst/portto4/kst/src/datasources/ascii/asciiconfig.ui #1321912:1321913
@@ -6,17 +6,11 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>839</width>
-    <height>619</height>
+    <width>932</width>
+    <height>677</height>
    </rect>
   </property>
-  <layout class="QGridLayout" name="mainGridLayout" columnstretch="2,0,0" columnminimumwidth="300,0,0">
-   <property name="margin">
-    <number>0</number>
-   </property>
-   <property name="spacing">
-    <number>9</number>
-   </property>
+  <layout class="QGridLayout" name="gridLayout_7">
    <item row="0" column="0">
     <widget class="QLabel" name="_labelBeginning">
      <property name="text">
@@ -24,6 +18,13 @@
      </property>
     </widget>
    </item>
+   <item row="0" column="1" rowspan="2">
+    <widget class="Line" name="verticalLine">
+     <property name="orientation">
+      <enum>Qt::Vertical</enum>
+     </property>
+    </widget>
+   </item>
    <item row="0" column="2" rowspan="2">
     <layout class="QVBoxLayout" name="verticalLayout">
      <item>
@@ -287,7 +288,7 @@
           </property>
          </widget>
         </item>
-        <item row="0" column="1">
+        <item row="0" column="1" colspan="2">
          <widget class="QLineEdit" name="_delimiters">
           <property name="toolTip">
            <string>You can use more than one symbol here, like "#!C" for instance, but it will slow down reading ASCII files</string>
@@ -304,10 +305,10 @@
           </property>
          </widget>
         </item>
-        <item row="1" column="1">
+        <item row="1" column="1" colspan="2">
          <widget class="QLineEdit" name="_fileNamePattern"/>
         </item>
-        <item row="2" column="0" colspan="2">
+        <item row="2" column="0" colspan="3">
          <layout class="QHBoxLayout" name="horizontalLayout">
           <item>
            <widget class="QLabel" name="_indexVectorLabel">
@@ -368,10 +369,38 @@
               <string>Seconds</string>
              </property>
             </item>
+            <item>
+             <property name="text">
+              <string>Formated Time</string>
+             </property>
+            </item>
            </widget>
           </item>
          </layout>
         </item>
+        <item row="3" column="0" colspan="2">
+         <widget class="QLabel" name="_timeFormatLabel">
+          <property name="text">
+           <string>Time format (see QDateTime)</string>
+          </property>
+          <property name="wordWrap">
+           <bool>false</bool>
+          </property>
+          <property name="buddy">
+           <cstring>_indexTimeFormat</cstring>
+          </property>
+         </widget>
+        </item>
+        <item row="3" column="2">
+         <widget class="QLineEdit" name="_indexTimeFormat">
+          <property name="toolTip">
+           <string>You can use more than one symbol here, like "#!C" for instance, but it will slow down reading ASCII files</string>
+          </property>
+          <property name="text">
+           <string>hh:mm:ss.zzz</string>
+          </property>
+         </widget>
+        </item>
        </layout>
       </widget>
      </item>
@@ -434,13 +463,6 @@
      </property>
     </widget>
    </item>
-   <item row="1" column="1">
-    <widget class="Line" name="verticalLine">
-     <property name="orientation">
-      <enum>Qt::Vertical</enum>
-     </property>
-    </widget>
-   </item>
    <item row="2" column="0" colspan="3">
     <widget class="Line" name="line">
      <property name="orientation">
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciiconfigwidget.cpp #1321912:1321913
@@ -111,6 +111,7 @@
 {
   AsciiSourceConfig config;
   config._fileNamePattern = _fileNamePattern->text();
+  config._indexVector = _indexVector->currentText();
   config._indexInterpretation = (AsciiSourceConfig::Interpretation) (1 + _indexType->currentIndex());
   config._delimiters = _delimiters->text();
   
@@ -141,6 +142,7 @@
   }
 
   config._useThreads =_useThreads->isChecked();
+  config._indexTimeFormat = _indexTimeFormat->text();
 
   return config;
 }
@@ -184,6 +186,7 @@
   updateFrameBuffer(config._limitFileBuffer);
 
   _useThreads->setChecked(config._useThreads);
+  _indexTimeFormat->setText(config._indexTimeFormat);
 }
 
 
@@ -220,9 +223,12 @@
   if (hasInstance()) {
     Kst::SharedPtr<AsciiSource> src = Kst::kst_cast<AsciiSource>(instance());
     _ac->_indexVector->addItems(src->vector().list());
-    _ac->_indexVector->setCurrentIndex(src->_config._indexInterpretation - 1);
+    _ac->_indexType->setCurrentIndex(src->_config._indexInterpretation - 1);
     if (src->vector().list().contains(src->_config._indexVector)) {
-      _ac->_indexVector->setEditText(src->_config._indexVector);
+      int idx = _ac->_indexVector->findText(src->_config._indexVector);
+      if (idx == -1)
+        idx = _ac->_indexVector->findText("INDEX");
+      _ac->_indexVector->setCurrentIndex(idx == -1 ? 0 : idx);
     }
   } else {
     _ac->_indexVector->addItem("INDEX");
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.cpp #1321912:1321913
@@ -317,8 +317,12 @@
 
   // now start reading
 
-  LexicalCast::AutoDot useDot(_config._useDot);
+  LexicalCast::AutoReset useDot(_config._useDot);
 
+  if (field == _config._indexVector && _config._indexInterpretation == AsciiSourceConfig::FormatedTime) {
+    LexicalCast::instance().setTimeFormat(_config._indexTimeFormat);
+  }
+
   QVector<QVector<AsciiFileData> >& slidingWindow = _fileBuffer.fileData();
   int sampleRead = 0;
   for (int i = 0; i < slidingWindow.size(); i++) {
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisourceconfig.cpp #1321912:1321913
@@ -31,6 +31,8 @@
 const char AsciiSourceConfig::Tag_indexVector[] ="vector";
 const char AsciiSourceConfig::Key_indexInterpretation[] = "Default INDEX Interpretation";
 const char AsciiSourceConfig::Tag_indexInterpretation[] = "interpretation";
+const char AsciiSourceConfig::Key_indexTimeFormat[] = "Time format";
+const char AsciiSourceConfig::Tag_indexTimeFormat[] = "timeFormat";
 const char AsciiSourceConfig::Key_columnType[] = "Column Type";
 const char AsciiSourceConfig::Tag_columnType[] = "columntype";
 const char AsciiSourceConfig::Key_columnDelimiter[] = "Column Delimiter";
@@ -61,8 +63,9 @@
 AsciiSourceConfig::AsciiSourceConfig() :
   _delimiters(DEFAULT_COMMENT_DELIMITERS),
   _indexVector("INDEX"),
+  _indexInterpretation(Unknown),
+  _indexTimeFormat("hh:mm:ss.zzz"),
   _fileNamePattern(""),
-  _indexInterpretation(Unknown),
   _columnType(Whitespace),
   _columnDelimiter(","),
   _columnWidth(DEFAULT_COLUMN_WIDTH),
@@ -99,6 +102,7 @@
   _limitFileBuffer >> cfg;
   _limitFileBufferSize >> cfg;
   _useThreads >> cfg;
+  _indexTimeFormat >> cfg;
 }
 
 
@@ -133,6 +137,7 @@
   _limitFileBuffer << cfg;
   _limitFileBufferSize << cfg;
   _useThreads << cfg;
+  _indexTimeFormat << cfg;
 }
 
 
@@ -174,6 +179,7 @@
   _limitFileBuffer >> s;
   _limitFileBufferSize >> s;
   _useThreads >> s;
+  _indexTimeFormat >> s;
 
   s.writeEndElement();
 }
@@ -197,6 +203,7 @@
   _limitFileBuffer << attributes;
   _limitFileBufferSize << attributes;
   _useThreads << attributes;
+  _indexTimeFormat << attributes;
 }
 
 
@@ -223,6 +230,7 @@
         _limitFileBuffer << elem;
         _limitFileBufferSize << elem;
         _useThreads << elem;
+        _indexTimeFormat << elem;
       }
     }
     n = n.nextSibling();
@@ -248,7 +256,8 @@
       _unitsLine == rhs._unitsLine &&
       _limitFileBuffer == rhs._limitFileBuffer &&
       _limitFileBufferSize == rhs._limitFileBufferSize &&
-      _useThreads == rhs._useThreads;
+      _useThreads == rhs._useThreads &&
+      _indexTimeFormat == rhs._indexTimeFormat;
 }
 
 bool AsciiSourceConfig::operator!=(const AsciiSourceConfig& rhs) const
@@ -271,7 +280,8 @@
       _fieldsLine != rhs._fieldsLine ||
       _columnWidthIsConst != rhs._columnWidthIsConst ||
       _readUnits != rhs._readUnits ||
-      _unitsLine != rhs._unitsLine ;
+      _unitsLine != rhs._unitsLine ||
+      _indexTimeFormat != rhs._indexTimeFormat;
 }
 
 
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisourceconfig.h #1321912:1321913
@@ -29,6 +29,8 @@
     static const char Tag_indexVector[];
     static const char Key_indexInterpretation[];
     static const char Tag_indexInterpretation[];
+    static const char Key_indexTimeFormat[];
+    static const char Tag_indexTimeFormat[];
     static const char Key_columnType[];
     static const char Tag_columnType[];
     static const char Key_columnDelimiter[];
@@ -71,13 +73,14 @@
 
     void load(const QDomElement& e);
 
-    enum Interpretation { Unknown = 0, INDEX, CTime, Seconds, IntEnd = 0xffff };
+    enum Interpretation { Unknown = 0, INDEX, CTime, Seconds, FormatedTime, IntEnd = 0xffff };
     enum ColumnType { Whitespace = 0, Fixed, Custom, ColEnd = 0xffff };
 
     NamedParameter<QString, Key_delimiters, Tag_delimiters> _delimiters;
     NamedParameter<QString, Key_indexVector, Tag_indexVector> _indexVector;
+    NamedParameter<int, Key_indexInterpretation, Tag_indexInterpretation> _indexInterpretation;
+    NamedParameter<QString, Key_indexTimeFormat, Tag_indexTimeFormat> _indexTimeFormat;
     NamedParameter<QString, Key_fileNamePattern, Tag_fileNamePattern> _fileNamePattern;
-    NamedParameter<int, Key_indexInterpretation, Tag_indexInterpretation> _indexInterpretation;
     NamedParameter<int, Key_columnType, Tag_columnType> _columnType;
     NamedParameter<QString, Key_columnDelimiter, Tag_columnDelimiter> _columnDelimiter;
     NamedParameter<int, Key_columnWidth, Tag_columnWidth> _columnWidth;
--- branches/work/kst/portto4/kst/src/datasources/ascii/kst_atof.cpp #1321912:1321913
@@ -14,19 +14,22 @@
 // http://www.bsdlover.cn/study/UnixTree/V7/usr/src/libc/gen/atof.c.html
 
 #include "kst_atof.h"
+#include "math_kst.h"
 
 #include <math.h>
 #include <ctype.h>
 
 #include <QLocale>
+#include <QTime>
+#include <QDateTime>
+#include <QDebug>
 
-
 #define LOGHUGE 39
 
 
 //-------------------------------------------------------------------------------------------
 #ifdef KST_USE_KST_ATOF
-double LexicalCast::toDouble(const char* signedp) const
+double LexicalCast::fromDouble(const char* signedp) const
 {
 	unsigned char* p = (unsigned char*)signedp;
 	unsigned char c;
@@ -119,6 +122,20 @@
 
 
 //-------------------------------------------------------------------------------------------
+LexicalCast::AutoReset::AutoReset(bool useDot)
+{
+  instance().setDecimalSeparator(useDot);
+}
+
+//-------------------------------------------------------------------------------------------
+LexicalCast::AutoReset::~AutoReset()
+{
+  instance().resetLocal();
+  instance()._isTime = false;
+  instance()._timeFormat.clear();
+}
+
+//-------------------------------------------------------------------------------------------
 LexicalCast& LexicalCast::instance()
 {
   static LexicalCast lexcInstance;
@@ -126,7 +143,7 @@
 }
 
 //-------------------------------------------------------------------------------------------
-LexicalCast::LexicalCast()
+LexicalCast::LexicalCast() : _isTime(false), _timeWithDate(false)
 {
 }
 
@@ -168,5 +185,38 @@
   return *setlocale(LC_NUMERIC, 0);
 }
 
+//-------------------------------------------------------------------------------------------
+void LexicalCast::setTimeFormat(const QString& format)
+{
+  _timeFormat = format;
+  _isTime = !format.isEmpty();
+  _timeWithDate = format.contains("d") || format.contains("M") || format.contains("y");
+}
 
+//-------------------------------------------------------------------------------------------
+double LexicalCast::fromTime(const char* p) const
+{
+  int maxScan = 100;
+  int end = 0;
+  for (; *(p + end) != ' ' && *(p + end) != '\t'; end++) {
+    if (end > maxScan)
+      return Kst::NOPOINT;
+  }
+
+  const QString time = QString::fromLatin1(p, end);
+  double sec = Kst::NOPOINT;
+  if (_timeWithDate) {
+    const QDateTime t = QDateTime::fromString(time, _timeFormat);
+    if (t.isValid())
+      sec = QDateTime::fromString(time, _timeFormat).toMSecsSinceEpoch() / 1000;
+  } else {
+    const QTime t = QTime::fromString(time, _timeFormat);
+    if (t.isValid())
+      sec = QTime(0, 0, 0).msecsTo(t) / 1000;
+  }
+  return sec;
+}
+
+
+
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/datasources/ascii/kst_atof.h #1321912:1321913
@@ -14,6 +14,7 @@
 #define KST_ATOF_H
 
 #include <QByteArray>
+#include <QString>
 #include <stdlib.h>
 
 
@@ -22,9 +23,9 @@
 public:
   static LexicalCast& instance();
   
-  struct AutoDot {
-    inline AutoDot(bool useDot) { instance().setDecimalSeparator(useDot); }
-    inline ~AutoDot() { instance().resetLocal(); }
+  struct AutoReset {
+    AutoReset(bool useDot);
+    ~AutoReset();
   };
   
   // use second parameter when useDot is false
@@ -33,17 +34,24 @@
   char localSeparator() const;
 
 #ifdef KST_USE_KST_ATOF
-  double toDouble(const char* p) const;
+  double fromDouble(const char* p) const;
 #else
-  inline double toDouble(const char* p) const { return atof(p); }
+  inline double fromDouble(const char* p) const { return atof(p); }
 #endif
+  double fromTime(const char*) const;
+  inline double toDouble(const char* p) const { return _isTime ? fromTime(p) : fromDouble(p); }
 
+  void setTimeFormat(const QString& format);
+  
 private:
   LexicalCast();
   ~LexicalCast();
 
   char _separator;
   QByteArray _originalLocal;
+  QString _timeFormat;
+  bool _isTime;
+  bool _timeWithDate;
 
   void resetLocal();
 


More information about the Kst mailing list