[Kst] branches/work/kst/portto4/kst/src/datasources/ascii
Peter Kümmel
syntheticpp at gmx.net
Tue Jul 27 00:45:14 CEST 2010
SVN commit 1155105 by kuemmel:
AsciiPlugin: use QFile::readLine. simplify file code
M +2 -1 asciiplugin.cpp
M +44 -69 asciisource.cpp
M +5 -1 asciisource.h
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciiplugin.cpp #1155104:1155105
@@ -331,7 +331,8 @@
int skip = config._dataLine;
while (!done) {
- rc = AsciiSource::readFullLine(f, s);
+ QByteArray s = f.readLine();
+ rc = f.size();
if (skip > 0) {
--skip;
if (rc <= 0) {
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.cpp #1155104:1155105
@@ -113,22 +113,15 @@
{
setInterface(iv);
- // enable all pre-allocated memory
- _tmpBuffer.resize(_tmpBuffer.capacity());
- _rowIndex.resize(_rowIndex.capacity());
+ reset();
-
- //TIME_IN_SCOPE(Ctor_AsciiSource);
-
setUpdateType(File);
- _valid = false;
- _haveHeader = false;
- _fieldListComplete = false;
_source = asciiTypeString;
if (!type.isEmpty() && type != asciiTypeString) {
return;
}
+
_config.readGroup(*cfg, filename);
if (!e.isNull()) {
_config.load(e);
@@ -149,7 +142,12 @@
{
_tmpBuffer.clear();
_rowIndex.clear();
+ // enable all pre-allocated memory
+ _tmpBuffer.resize(_tmpBuffer.capacity());
+ _rowIndex.resize(_rowIndex.capacity());
+
+ _valid = false;
_numFrames = 0;
_haveHeader = false;
_fieldListComplete = false;
@@ -161,22 +159,16 @@
}
-int AsciiSource::readFullLine(QFile &file, QByteArray &str) {
- str = file.readLine(1000);
- if (str.isEmpty())
- return str.size();
-
- QByteArray strExtra;
- while (str[str.size()-1] != '\n') {
- strExtra = file.readLine(1000);
- if (!strExtra.isEmpty()) {
- str += strExtra;
- } else {
- break;
+bool AsciiSource::openFile(QFile &file)
+{
+ return file.open(QIODevice::ReadOnly | QIODevice::Text);
}
- }
- return str.size();
+
+bool AsciiSource::openValidFile(QFile &file)
+{
+ _valid = openFile(file);
+ return _valid;
}
@@ -188,18 +180,17 @@
if (_config._dataLine > 0) {
QFile file(_filename);
- if (!file.open(QIODevice::ReadOnly)) {
+ if (!openValidFile(file)) {
return false;
}
int left = _config._dataLine;
int didRead = 0;
- QByteArray ignore;
while (left > 0) {
- int thisRead = AsciiSource::readFullLine(file, ignore);
- if (thisRead <= 0 || file.atEnd()) {
+ QByteArray line = file.readLine();
+ if (line.isEmpty() || file.atEnd()) {
return false;
}
- didRead += thisRead;
+ didRead += line.size();
--left;
}
_rowIndex[0] = didRead;
@@ -210,7 +201,8 @@
#define MAXBUFREADLEN 32768
-Kst::Object::UpdateType AsciiSource::internalDataSourceUpdate() {
+Kst::Object::UpdateType AsciiSource::internalDataSourceUpdate()
+{
if (!_haveHeader) {
_haveHeader = initRowIndex();
if (!_haveHeader) {
@@ -227,24 +219,17 @@
bool forceUpdate = false;
QFile file(_filename);
- if (file.exists()) {
- if (uint(_byteLength) != file.size() || !_valid) {
+ if (openValidFile(file)) {
+ // Qt: If the device is closed, the size returned will not reflect the actual size of the device.
+ if (_byteLength != file.size()) {
forceUpdate = true;
}
_byteLength = file.size();
} else {
- _valid = false;
return NoChange;
}
- if (!file.open(QIODevice::ReadOnly)) {
- // quietly fail - no data to be had here
- _valid = false;
- return NoChange;
- }
- _valid = true;
-
int bufstart, bufread;
bool new_data = false;
char tmpbuf[MAXBUFREADLEN+1];
@@ -277,7 +262,7 @@
_rowIndex.resize(_rowIndex.size() + 32768);
if (_numFrames >= _rowIndex.size()) {
// TODO where could we report an error;
- return;
+ return NoChange;
}
}
new_data = true;
@@ -294,7 +279,6 @@
}
} while ((bufread == MAXBUFREADLEN) && (!first_read));
- file.close();
return (forceUpdate ? Updated : (new_data ? Updated : NoChange));
}
@@ -360,7 +344,7 @@
}
QFile file(_filename);
- if (!file.open(QIODevice::ReadOnly)) {
+ if (!openValidFile(file)) {
_valid = false;
return 0;
}
@@ -441,8 +425,6 @@
}
}
- file.close();
-
return n;
}
@@ -457,43 +439,35 @@
return _numFrames < 1;
}
-QStringList AsciiSource::scalarListFor(const QString& filename, AsciiSourceConfig *cfg) {
- Q_UNUSED(cfg)
+QStringList AsciiSource::scalarListFor(const QString& filename, AsciiSourceConfig*)
+{
QStringList rc;
QFile file(filename);
-
- if (!file.open(QIODevice::ReadOnly)) {
+ if (!openFile(file)) {
return rc;
}
-
- file.close();
-
rc += "FRAMES";
return rc;
-
}
-QStringList AsciiSource::stringListFor(const QString& filename, AsciiSourceConfig *cfg) {
- Q_UNUSED(cfg)
+
+QStringList AsciiSource::stringListFor(const QString& filename, AsciiSourceConfig*)
+{
QStringList rc;
QFile file(filename);
-
- if (!file.open(QIODevice::ReadOnly)) {
+ if (!openFile(file)) {
return rc;
}
-
- file.close();
-
rc += "FILE";
return rc;
-
}
-QStringList AsciiSource::fieldListFor(const QString& filename, AsciiSourceConfig *cfg) {
+
+QStringList AsciiSource::fieldListFor(const QString& filename, AsciiSourceConfig* cfg)
+{
QStringList rc;
QFile file(filename);
-
- if (!file.open(QIODevice::ReadOnly)) {
+ if (!openFile(file)) {
return rc;
}
@@ -501,9 +475,9 @@
if (cfg->_readFields) {
int l = cfg->_fieldsLine;
- QByteArray line;
while (!file.atEnd()) {
- int r = readFullLine(file, line);
+ const QByteArray line = file.readLine();
+ int r = line.size();
if (l-- == 0) {
if (r >= 0) {
if (cfg->_columnType == AsciiSourceConfig::Custom && !cfg->_columnDelimiter.value().isEmpty()) {
@@ -546,9 +520,9 @@
int cnt;
int nextscan = 0;
int curscan = 0;
- QByteArray line;
while (!file.atEnd() && !done && (nextscan < 200)) {
- int r = readFullLine(file, line);
+ QByteArray line = file.readLine();
+ int r = line.size();
if (skip > 0) { //keep skipping until desired line
--skip;
if (r < 0) {
@@ -593,7 +567,6 @@
}
}
- file.close();
for (int i = 1; i <= maxcnt; ++i) {
rc += i18n("Column %1").arg(i);
}
@@ -601,7 +574,9 @@
return rc;
}
-void AsciiSource::save(QXmlStreamWriter &s) {
+
+void AsciiSource::save(QXmlStreamWriter &s)
+{
Kst::DataSource::save(s);
_config.save(s);
}
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.h #1155104:1155105
@@ -37,7 +37,7 @@
~AsciiSource();
- static int readFullLine(QFile &file, QByteArray &str);
+
bool initRowIndex();
virtual UpdateType internalDataSourceUpdate();
@@ -90,6 +90,10 @@
DataInterfaceAsciiVector* iv;
+ bool openValidFile(QFile &file);
+
+ static bool openFile(QFile &file);
+
// TODO remove
friend class DataInterfaceAsciiVector;
};
More information about the Kst
mailing list