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

Peter Kümmel syntheticpp at gmx.net
Sat Feb 5 13:56:56 CET 2011


SVN commit 1218983 by kuemmel:

- fix crash for files with size < 2
- fix reading data with same column width for all columns

 M  +15 -8     asciisource.cpp  


--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.cpp #1218982:1218983
@@ -294,12 +294,19 @@
 //-------------------------------------------------------------------------------------------
 AsciiSource::LineEndingType AsciiSource::detectLineEndingType(QFile& file) const
 {
+  QByteArray line;
+  int line_size = 0;
+  while (line_size < 2 && !file.atEnd()) {
+     line = file.readLine();
+     line_size = line.size();
+  }
+  file.seek(0);
+  if (line_size < 2) {
+    return LineEndingType();
+  }
   LineEndingType end;
-  QByteArray line = file.readLine();
-  file.seek(0);
-  int lsize = line.size();
-  end.is_crlf = line[lsize - 2] == '\r' && line[lsize - 1] == '\n' ;
-  end.character =  end.is_crlf ? line[lsize - 2] : line[lsize - 1];
+  end.is_crlf = line[line_size - 2] == '\r' && line[line_size - 1] == '\n' ;
+  end.character =  end.is_crlf ? line[line_size - 2] : line[line_size - 1];
   return end;
 }
 
@@ -503,10 +510,10 @@
     MeasureTime t("AsciiSource::readField: same width for all columns");
     LexicalCast lexc;
     lexc.setDecimalSeparator(_config._useDot);
-    const char* col_start = &buffer[0] + _config._columnWidth * (col - 1);
+    // &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 = &buffer[0] - _rowIndex[0] + _config._columnWidth * (col - 1);
     for (int i = 0; i < n; ++i) {
-      /* Read appropriate column and convert to double
-      v[i] = lexc.toDouble(&buffer[0] + _rowIndex[i] + _config._columnWidth * (col - 1));*/
       v[i] = lexc.toDouble(col_start + _rowIndex[i]);
     }
     return n;


More information about the Kst mailing list