Skip to content

Commit

Permalink
Merge pull request #44211 from nirvn/qt6_9
Browse files Browse the repository at this point in the history
[qt6] Insure delimited text provider compiles against qt6
  • Loading branch information
nirvn committed Jul 18, 2021
2 parents d10695c + df5f799 commit 8510650
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
32 changes: 13 additions & 19 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 All @@ -33,6 +32,7 @@
QgsDelimitedTextFile::QgsDelimitedTextFile( const QString &url )
: mFileName( QString() )
, mEncoding( QStringLiteral( "UTF-8" ) )
, mFirstEOLChar( QChar( 0 ) )
, mDefaultFieldName( QStringLiteral( "field_%1" ) )
, mDefaultFieldRegexp( QStringLiteral( "^(?:field_)(\\d+)$" ) )
{
Expand All @@ -54,10 +54,9 @@ QgsDelimitedTextFile::~QgsDelimitedTextFile()

void QgsDelimitedTextFile::close()
{
if ( mStream )
if ( mCodec )
{
delete mStream;
mStream = nullptr;
mCodec = nullptr;
}
if ( mFile )
{
Expand Down Expand Up @@ -90,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 @@ -544,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 @@ -571,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 @@ -628,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 @@ -659,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 @@ -675,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 Expand Up @@ -778,7 +772,7 @@ QgsDelimitedTextFile::Status QgsDelimitedTextFile::parseQuoted( QString &buffer,
QString field; // String in which to accumulate next field
bool escaped = false; // Next char is escaped
bool quoted = false; // In quotes
QChar quoteChar = 0; // Actual quote character used to open quotes
QChar quoteChar( 0 ); // Actual quote character used to open quotes
bool started = false; // Non-blank chars in field or quotes started
bool ended = false; // Quoted field ended
int cp = 0; // Pointer to the next character in the buffer
Expand Down
12 changes: 6 additions & 6 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 Expand Up @@ -432,7 +432,7 @@ class QgsDelimitedTextFile : public QObject
QString mBuffer;
int mPosInBuffer = 0;
int mMaxBufferSize = 0;
QChar mFirstEOLChar = 0; // '\r' if EOL is "\r" or "\r\n", or `\n' if EOL is "\n"
QChar mFirstEOLChar; // '\r' if EOL is "\r" or "\r\n", or `\n' if EOL is "\n"
QStringList mCurrentRecord;
bool mHoldCurrentRecord = false;
// Maximum number of record (ie maximum record number visited)
Expand Down

0 comments on commit 8510650

Please sign in to comment.