A fast low level parser for ASCII contents. More...
#include <AsciiLineParser.h>
Public Member Functions | |
AsciiLineParser (QByteArray *buffer, int nColumns=1) | |
bool | atEnd () const |
int | count () const |
void | countColumns () |
bool | readColumn (int index) |
bool | readColumns (int count, int *columns) |
void | readLine () |
void | reset () |
double | toDouble (int index=0) |
double | toDouble (int begin, int count) |
int | toInt (int index=0) |
int | toInt (int begin, int count) |
QString | toString (int index=0) |
QString | toString (int begin, int count) |
~AsciiLineParser () |
A fast low level parser for ASCII contents.
The parser can be used in several ways:
QGpCoreTools::AsciiLineParser::AsciiLineParser | ( | QByteArray * | buffer, |
int | nColumns = 1 |
||
) |
Sets buffer to be parsed. Maximum number of columns nColumns has to set only when using readLine(). For other usages, the default value is fine.
The buffer must be terminated by '\0', ensured by QByteArray. No copy of buffer is made internally, hence the buffer must exist all along the life of this object.
{ ASSERT(nColumns>0); _buffer=buffer->data(); _bufferSize=buffer->count(); _index=0; _maximumValueCount=nColumns; _valueCount=_maximumValueCount; _values=new char *[_maximumValueCount]; }
Description of destructor still missing
{
delete [] _values;
}
bool QGpCoreTools::AsciiLineParser::atEnd | ( | ) | const [inline] |
{return _index>=_bufferSize;}
int QGpCoreTools::AsciiLineParser::count | ( | ) | const [inline] |
Referenced by toDouble(), and toInt().
{return _valueCount;}
Counts the number of columns and moves the pointer to the next line. count() returns the number of columns. Maximum number of columns set in constructor is ignored here.
{ register int iValue=0; register int iBuffer=_index; register int iBeginValue=_index; while(iBuffer<_bufferSize) { switch(_buffer[iBuffer]) { case '\0': case '\n': if(iBuffer>iBeginValue) { _valueCount=iValue+1; } else { _valueCount=iValue; } _index=iBuffer+1; return; case '\r': case ' ': case '\t': if(iBuffer>iBeginValue) { iValue++; } iBeginValue=iBuffer+1; break; default: break; } iBuffer++; } if(iBuffer>iBeginValue) { _valueCount=iValue+1; } else { _valueCount=iValue; } _index=iBuffer+1; }
bool QGpCoreTools::AsciiLineParser::readColumn | ( | int | index | ) |
Reads one column in current line. toInt() and toDouble() are then ready to be used with index 0 only. count() will return 1 or 0.
{ _valueCount=1; bool endOfLine=false; if(index==0) { return readValue(0, true, endOfLine); } register int iValue=0; register int iBuffer=_index; register int iBeginValue=iBuffer; // Read all columns before index while(iBuffer<_bufferSize) { switch(_buffer[iBuffer]) { case '\0': case '\n': _index=iBuffer+1; return false; case '\r': case ' ': case '\t': if(iBuffer>iBeginValue) { iValue++; if(iValue==index) { // We are in the good column _index=iBuffer+1; return readValue(0, true, endOfLine); } } iBeginValue=iBuffer+1; break; default: break; } iBuffer++; } _index=iBuffer+1; return false; }
bool QGpCoreTools::AsciiLineParser::readColumns | ( | int | count, |
int * | columns | ||
) |
Reads count columns in current line. toInt() and toDouble() are then ready to be used with index 0 to count-1. count() will return count or 0. Constructor must be initialized with at least count columns. Index of columns must be sort ascendingly .
{ _valueCount=0; if(count<=0) { return false; } bool endOfLine=false; int lastColIndex=count-1; int colIndex=0; register int iValue=0; while(colIndex<count) { if(columns[colIndex]==iValue) { if(colIndex==lastColIndex) { if(!readValue(colIndex, true, endOfLine)) { return false; } _valueCount++; return true; } else { if(!readValue(colIndex, false, endOfLine)) { return false; } _valueCount++; colIndex++; if(endOfLine) { return false; } } } else { skipValue(endOfLine); if(endOfLine) { return false; } } iValue++; } return false; }
Reads one line. toInt() and toDouble() are then ready to be used.
{ register int iValue=0; register int iBuffer=_index; register int iBeginValue=_index; while(iBuffer<_bufferSize) { switch(_buffer[iBuffer]) { case '\n': _buffer[iBuffer]='\0'; case '\0': if(iBuffer>iBeginValue) { _values[iValue]=_buffer+iBeginValue; _valueCount=iValue+1; } else { _valueCount=iValue; } _index=iBuffer+1; return; case '\r': case ' ': case '\t': if(iBuffer>iBeginValue) { _buffer[iBuffer]='\0'; _values[iValue]=_buffer+iBeginValue; iValue++; if(iValue==_maximumValueCount) { _valueCount=iValue; // seek to end of line iBuffer++; while(iBuffer<_bufferSize && _buffer[iBuffer]!='\n') { iBuffer++; } _index=iBuffer+1; return; } } iBeginValue=iBuffer+1; break; default: break; } iBuffer++; } if(iBuffer>iBeginValue) { _values[iValue]=_buffer+iBeginValue; _valueCount=iValue+1; } else { _valueCount=iValue; } _index=iBuffer+1; }
void QGpCoreTools::AsciiLineParser::reset | ( | ) |
Resets the pointer to the beginning of the buffer
{ _index=0; }
double QGpCoreTools::AsciiLineParser::toDouble | ( | int | index = 0 | ) | [inline] |
{
return atof(_values[index]);
}
double QGpCoreTools::AsciiLineParser::toDouble | ( | int | begin, |
int | count | ||
) |
int QGpCoreTools::AsciiLineParser::toInt | ( | int | index = 0 | ) | [inline] |
{
return atoi(_values[index]);
}
int QGpCoreTools::AsciiLineParser::toInt | ( | int | begin, |
int | count | ||
) |
QString QGpCoreTools::AsciiLineParser::toString | ( | int | index = 0 | ) | [inline] |
{
return QString::fromAscii(_values[index]);
}
QString QGpCoreTools::AsciiLineParser::toString | ( | int | begin, |
int | count | ||
) | [inline] |
{ return QString::fromAscii(_buffer+begin, count); }