[Kst] kdeextragear-2/kst/kst/datasources/ascii

George Staikos staikos at kde.org
Mon Apr 11 03:36:27 CEST 2005


CVS commit by staikos: 

- implement much of the code for the data formatting options
- optimize: reduce malloc calls


  M +61 -21    ascii.cpp   1.41
  M +2 -0      ascii.h   1.15


--- kdeextragear-2/kst/kst/datasources/ascii/ascii.cpp  #1.40:1.41
@@ -68,5 +68,5 @@ class AsciiSource::Config {
 
 AsciiSource::AsciiSource(KConfig *cfg, const QString& filename, const QString& type)
-: KstDataSource(cfg, filename, type), _rowIndex(0L), _config(0L) {
+: KstDataSource(cfg, filename, type), _rowIndex(0L), _config(0L), _tmpBuf(0L), _tmpBufSize(0) {
   if (!type.isEmpty() && type != "ASCII") {
     return;
@@ -83,4 +83,10 @@ AsciiSource::AsciiSource(KConfig *cfg, c
 
 AsciiSource::~AsciiSource() {
+  if (_tmpBuf) {
+    free(_tmpBuf);
+    _tmpBuf = 0L;
+    _tmpBufSize = 0;
+  }
+
   if (_rowIndex) {
     free(_rowIndex);
@@ -225,9 +231,20 @@ int AsciiSource::readField(double *v, co
   }
 
-  char *tmpbuf = new char[bufread];
+  if (_tmpBufSize < unsigned(bufread)) {
+    _tmpBuf = static_cast<char*>(realloc(_tmpBuf, _tmpBufSize = bufread));
+    if (!_tmpBuf) {
+      return -1;
+    }
+  }
 
   file.at(bufstart);
-  file.readBlock(tmpbuf, bufread);
+  file.readBlock(_tmpBuf, bufread);
 
+  if (_config->_columnType == AsciiSource::Config::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));
+    }
+  } else if (_config->_columnType == AsciiSource::Config::Custom) {
   for (int i = 0; i < n; i++, s++) {
     bool incol = false;
@@ -235,11 +252,35 @@ int AsciiSource::readField(double *v, co
     v[i] = 0.0;
     for (int ch = _rowIndex[s] - bufstart; ch < bufread; ++ch) {
-      if (isspace(tmpbuf[ch])) {
-        if (tmpbuf[ch] == '\n' || tmpbuf[ch] == '\r') {
+        if (_config->_columnDelimiter.contains(_tmpBuf[ch])) {
+          incol = false;
+        } else if (_tmpBuf[ch] == '\n' || _tmpBuf[ch] == '\r') {
+          break;
+        } else if (_config->_delimiters.contains(_tmpBuf[ch])) {
+          break;
+        } else {
+          if (!incol) {
+            incol = true;
+            i_col++;
+            if (i_col == col) {
+              v[i] = atof(_tmpBuf + ch);
+              break;
+            }
+          }
+        }
+      }
+    }
+  } else {
+    for (int i = 0; i < n; i++, s++) {
+      bool incol = false;
+      int i_col = 0;
+      v[i] = 0.0;
+      for (int ch = _rowIndex[s] - bufstart; ch < bufread; ++ch) {
+        if (isspace(_tmpBuf[ch])) {
+          if (_tmpBuf[ch] == '\n' || _tmpBuf[ch] == '\r') {
           break;
         } else {
           incol = false;
         }
-      } else if (_config->_delimiters.contains(tmpbuf[ch])) {
+        } else if (_config->_delimiters.contains(_tmpBuf[ch])) {
         break;
       } else {
@@ -248,5 +289,5 @@ int AsciiSource::readField(double *v, co
           i_col++;
           if (i_col == col) {
-            v[i] = atof(tmpbuf + ch);
+              v[i] = atof(_tmpBuf + ch);
             break;
           }
@@ -255,6 +296,5 @@ int AsciiSource::readField(double *v, co
     }
   }
-
-  delete[] tmpbuf;
+  }
 
   file.close();

--- kdeextragear-2/kst/kst/datasources/ascii/ascii.h  #1.14:1.15
@@ -63,4 +63,6 @@ class AsciiSource : public KstDataSource
     class Config;
     mutable Config *_config;
+    char *_tmpBuf;
+    uint _tmpBufSize;
 };
 




More information about the Kst mailing list