[Kst] branches/work/kst/portto4/kst/src/datasources/ascii
Peter Kümmel
syntheticpp at gmx.net
Sun Oct 14 12:03:00 UTC 2012
SVN commit 1320471 by kuemmel:
add comment about template chain
and more small cleanups and coding style changes
M +47 -59 asciidatareader.cpp
M +2 -1 asciidatareader.h
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciidatareader.cpp #1320470:1320471
@@ -11,7 +11,6 @@
***************************************************************************/
#include "asciidatareader.h"
-
#include "asciisourceconfig.h"
#include "math_kst.h"
@@ -21,14 +20,13 @@
#include "measuretime.h"
#include <QFile>
-#include <QMessageBox>
-
-#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
-#include <iostream>
+using namespace AsciiCharacterTraits;
+
+
// Enable QASSERT in QVarLengthArray when using [] on data
#if 0
#define constData constArray
@@ -37,23 +35,16 @@
#endif
-using namespace AsciiCharacterTraits;
-
-
-AsciiDataReader::AsciiDataReader(AsciiSourceConfig& c) : _config(c)
+//-------------------------------------------------------------------------------------------
+AsciiDataReader::AsciiDataReader(AsciiSourceConfig& config) : _config(config)
{
}
-
+//-------------------------------------------------------------------------------------------
AsciiDataReader::~AsciiDataReader()
{
}
-
-
-
-
-
//-------------------------------------------------------------------------------------------
void AsciiDataReader::detectLineEndingType(QFile& file)
{
@@ -72,8 +63,6 @@
}
}
-
-
//-------------------------------------------------------------------------------------------
void AsciiDataReader::toDouble(const LexicalCast& lexc, const char* buffer, int bufread, int ch, double* v, int)
{
@@ -99,10 +88,9 @@
}
}
#endif
-
}
-
+//-------------------------------------------------------------------------------------------
int AsciiDataReader::readFromFile(QFile& file, AsciiFileBuffer& buffer, int start, int bytesToRead, int maximalBytes)
{
if (maximalBytes == -1) {
@@ -125,48 +113,12 @@
return bytesRead;
}
-
-int AsciiDataReader::readField(AsciiFileBuffer* _fileBuffer, int col, int bufstart, int bufread,
- double *v, const QString& field, int s, int n)
- {
-
- if (_config._columnType == AsciiSourceConfig::Fixed) {
- MeasureTime t("AsciiSource::readField: same width for all columns");
- LexicalCast lexc;
- lexc.setDecimalSeparator(_config._useDot);
- // &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 = &_fileBuffer->constData()[0] - _rowIndex[0] + _config._columnWidth * (col - 1);
- for (int i = 0; i < n; ++i) {
- v[i] = lexc.toDouble(_rowIndex[i] + col_start);
- }
- return n;
- } else if (_config._columnType == AsciiSourceConfig::Custom) {
- if (_config._columnDelimiter.value().size() == 1) {
- MeasureTime t("AsciiSource::readField: 1 custom column delimiter");
- const IsCharacter column_del(_config._columnDelimiter.value()[0].toLatin1());
- return readColumns(v, _fileBuffer->constData(), bufstart, bufread, col, s, n, _lineending, column_del);
- } if (_config._columnDelimiter.value().size() > 1) {
- MeasureTime t(QString("AsciiSource::readField: %1 custom column delimiters").arg(_config._columnDelimiter.value().size()));
- const IsInString column_del(_config._columnDelimiter.value());
- return readColumns(v, _fileBuffer->constData(), bufstart, bufread, col, s, n, _lineending, column_del);
- }
- } else if (_config._columnType == AsciiSourceConfig::Whitespace) {
- MeasureTime t("AsciiSource::readField: whitespace separated columns");
- const IsWhiteSpace column_del;
- return readColumns(v, _fileBuffer->constData(), bufstart, bufread, col, s, n, _lineending, column_del);
- }
-
- return 0;
-}
-
-
+//-------------------------------------------------------------------------------------------
bool AsciiDataReader::findDataRows(int& numFrames, bool read_completely, QFile& file, int _byteLength)
{
detectLineEndingType(file);
bool new_data = false;
-
AsciiFileBuffer buf;
do {
// Read the tmpbuffer, starting at row_index[_numFrames]
@@ -206,11 +158,11 @@
return new_data;
}
+//-------------------------------------------------------------------------------------------
template<class Buffer, typename IsLineBreak, typename CommentDelimiter>
bool AsciiDataReader::findDataRows(int& _numFrames, const Buffer& buffer, int bufstart, int bufread, const IsLineBreak& isLineBreak, const CommentDelimiter& comment_del)
{
const IsWhiteSpace isWhiteSpace;
-
bool new_data = false;
bool row_has_data = false;
bool is_comment = false;
@@ -242,7 +194,43 @@
return new_data;
}
+//-------------------------------------------------------------------------------------------
+int AsciiDataReader::readField(AsciiFileBuffer* _fileBuffer, int col, int bufstart, int bufread,
+ double *v, const QString& field, int s, int n)
+{
+ if (_config._columnType == AsciiSourceConfig::Fixed) {
+ MeasureTime t("AsciiSource::readField: same width for all columns");
+ LexicalCast lexc;
+ lexc.setDecimalSeparator(_config._useDot);
+ // &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 = &_fileBuffer->constData()[0] - _rowIndex[0] + _config._columnWidth * (col - 1);
+ for (int i = 0; i < n; ++i) {
+ v[i] = lexc.toDouble(_rowIndex[i] + col_start);
+ }
+ return n;
+ } else if (_config._columnType == AsciiSourceConfig::Custom) {
+ if (_config._columnDelimiter.value().size() == 1) {
+ MeasureTime t("AsciiSource::readField: 1 custom column delimiter");
+ const IsCharacter column_del(_config._columnDelimiter.value()[0].toLatin1());
+ return readColumns(v, _fileBuffer->constData(), bufstart, bufread, col, s, n, _lineending, column_del);
+ } if (_config._columnDelimiter.value().size() > 1) {
+ MeasureTime t(QString("AsciiSource::readField: %1 custom column delimiters").arg(_config._columnDelimiter.value().size()));
+ const IsInString column_del(_config._columnDelimiter.value());
+ return readColumns(v, _fileBuffer->constData(), bufstart, bufread, col, s, n, _lineending, column_del);
+ }
+ } else if (_config._columnType == AsciiSourceConfig::Whitespace) {
+ MeasureTime t("AsciiSource::readField: whitespace separated columns");
+ const IsWhiteSpace column_del;
+ return readColumns(v, _fileBuffer->constData(), bufstart, bufread, col, s, n, _lineending, column_del);
+ }
+ return 0;
+}
+//
+// template instantiation chain to generate optimal code for all possible data configurations
+//
+
//-------------------------------------------------------------------------------------------
template<class Buffer, typename ColumnDelimiter>
int AsciiDataReader::readColumns(double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
@@ -258,10 +246,10 @@
const IsInString comment_del(_config._delimiters.value());
return readColumns(v, buffer, bufstart, bufread, col, s, n, lineending, column_del, comment_del);
}
-
return 0;
}
+//-------------------------------------------------------------------------------------------
template<class Buffer, typename ColumnDelimiter, typename CommentDelimiter>
int AsciiDataReader::readColumns(double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
const LineEndingType& lineending, const ColumnDelimiter& column_del, const CommentDelimiter& comment_del)
@@ -283,7 +271,7 @@
}
}
-
+//-------------------------------------------------------------------------------------------
template<class Buffer, typename IsLineBreak, typename ColumnDelimiter, typename CommentDelimiter, typename ColumnWidthsAreConst>
int AsciiDataReader::readColumns(double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
const IsLineBreak& isLineBreak,
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciidatareader.h #1320470:1320471
@@ -20,10 +20,11 @@
struct LexicalCast;
class AsciiSourceConfig;
+
class AsciiDataReader
{
public:
- AsciiDataReader(AsciiSourceConfig& c);
+ AsciiDataReader(AsciiSourceConfig& config);
~AsciiDataReader();
typedef QVarLengthArray<int, AsciiFileBuffer::Prealloc> RowIndex;
More information about the Kst
mailing list