Skip to content

Commit

Permalink
[delimitedtext] Remove use of QTextStream in favor of QTextCodec for …
Browse files Browse the repository at this point in the history
…decoding

(needed to compile against qt6)
  • Loading branch information
nirvn committed Jul 17, 2021
1 parent a2e8fbd commit df5f799
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
29 changes: 11 additions & 18 deletions src/providers/delimitedtext/qgsdelimitedtextfile.cpp
Expand Up @@ -22,7 +22,6 @@
#include <QFile>
#include <QFileInfo>
#include <QDataStream>
#include <QTextStream>
#include <QFileSystemWatcher>
#include <QTextCodec>
#include <QStringList>
Expand Down Expand Up @@ -55,10 +54,9 @@ QgsDelimitedTextFile::~QgsDelimitedTextFile()

void QgsDelimitedTextFile::close()
{
if ( mStream )
if ( mCodec )
{
delete mStream;
mStream = nullptr;
mCodec = nullptr;
}
if ( mFile )
{
Expand Down Expand Up @@ -91,12 +89,7 @@ bool QgsDelimitedTextFile::open()
}
if ( mFile )
{
mStream = new QTextStream( mFile );
if ( ! mEncoding.isEmpty() )
{
QTextCodec *codec = QTextCodec::codecForName( mEncoding.toLatin1() );
mStream->setCodec( codec );
}
mCodec = QTextCodec::codecForName( !mEncoding.isEmpty() ? mEncoding.toLatin1() : "UTF-8" );
if ( mUseWatcher )
{
mWatcher = new QFileSystemWatcher();
Expand Down Expand Up @@ -545,7 +538,7 @@ QgsDelimitedTextFile::Status QgsDelimitedTextFile::reset()
if ( ! isValid() || ! open() ) return InvalidDefinition;

// Reset the file pointer
mStream->seek( 0 );
mFile->seek( 0 );
mLineNumber = 0;
mRecordNumber = -1;
mRecordLineNumber = -1;
Expand All @@ -572,21 +565,21 @@ QgsDelimitedTextFile::Status QgsDelimitedTextFile::reset()

QgsDelimitedTextFile::Status QgsDelimitedTextFile::nextLine( QString &buffer, bool skipBlank )
{
if ( ! mStream )
if ( ! mFile )
{
Status status = reset();
if ( status != RecordOk ) return status;
}
if ( mLineNumber == 0 )
{
mPosInBuffer = 0;
mBuffer = mStream->read( mMaxBufferSize );
mBuffer = mCodec->toUnicode( mFile->read( mMaxBufferSize ) );
}

while ( !mBuffer.isEmpty() )
{
// Identify position of \r , \n or \r\n
// We should rather use mStream->readLine(), but it fails to detect \r
// We should rather use mFile->readLine(), but it fails to detect \r
// line endings.
int eolPos = -1;
{
Expand Down Expand Up @@ -629,7 +622,7 @@ QgsDelimitedTextFile::Status QgsDelimitedTextFile::nextLine( QString &buffer, bo
{
// If we are just at the end of the buffer, read an extra character
// from the stream
QString newChar = mStream->read( 1 );
QString newChar = mCodec->toUnicode( mFile->read( 1 ) );
mBuffer += newChar;
if ( newChar == '\n' )
{
Expand Down Expand Up @@ -660,7 +653,7 @@ QgsDelimitedTextFile::Status QgsDelimitedTextFile::nextLine( QString &buffer, bo
// Read more bytes from file to have up to mMaxBufferSize characters
// in our buffer (after having subset it from mPosInBuffer)
mBuffer = mBuffer.mid( mPosInBuffer );
mBuffer += mStream->read( mMaxBufferSize - mBuffer.size() );
mBuffer += mCodec->toUnicode( mFile->read( mMaxBufferSize - mBuffer.size() ) );
mPosInBuffer = 0;
continue;
}
Expand All @@ -676,11 +669,11 @@ QgsDelimitedTextFile::Status QgsDelimitedTextFile::nextLine( QString &buffer, bo

bool QgsDelimitedTextFile::setNextLineNumber( long nextLineNumber )
{
if ( ! mStream ) return false;
if ( ! mFile ) return false;
if ( mLineNumber > nextLineNumber - 1 )
{
mRecordNumber = -1;
mStream->seek( 0 );
mFile->seek( 0 );
mLineNumber = 0;
}
QString buffer;
Expand Down
10 changes: 5 additions & 5 deletions src/providers/delimitedtext/qgsdelimitedtextfile.h
Expand Up @@ -27,15 +27,15 @@ class QgsFeature;
class QgsField;
class QFile;
class QFileSystemWatcher;
class QTextStream;
class QTextCodec;


/**
* \class QgsDelimitedTextFile
* \brief Delimited text file parser extracts records from a QTextStream as a QStringList.
* \brief Delimited text file parser extracts records from a text file as a QStringList.
*
* The delimited text parser is used by the QgsDelimitedTextProvider to parse
* a QTextStream into records of QStringList. It provides a number of variants
* a text file into records of QStringList. It provides a number of variants
* for parsing each record. The following options are supported:
*
* - Basic whitespace parsing. Each line in the file is treated as a record.
Expand Down Expand Up @@ -267,7 +267,7 @@ class QgsDelimitedTextFile : public QObject
int fieldIndex( const QString &name );

/**
* Reads the next record from the stream splits into string fields.
* Reads the next record from the file splits into string fields.
* \param fields The string list to populate with the fields
* \returns status The result of trying to parse a record. RecordOk
* if read successfully, RecordEOF if reached the end of the
Expand Down Expand Up @@ -403,7 +403,7 @@ class QgsDelimitedTextFile : public QObject
QString mFileName;
QString mEncoding;
QFile *mFile = nullptr;
QTextStream *mStream = nullptr;
QTextCodec *mCodec = nullptr;
bool mUseWatcher = false;
QFileSystemWatcher *mWatcher = nullptr;

Expand Down

0 comments on commit df5f799

Please sign in to comment.