Skip to content

Commit e87623d

Browse files
committed
Fix to potential unterminated loop if a delimited text file is deleted
Another try at fixing test cases with file watcher, don't work on some platforms.
1 parent a39b78b commit e87623d

File tree

5 files changed

+173
-169
lines changed

5 files changed

+173
-169
lines changed

src/providers/delimitedtext/qgsdelimitedtextfile.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ void QgsDelimitedTextFile::close()
8282
delete mWatcher;
8383
mWatcher = 0;
8484
}
85+
mLineNumber = -1;
86+
mRecordLineNumber = -1;
87+
mRecordNumber = -1;
88+
mMaxRecordNumber = -1;
89+
mHoldCurrentRecord = false;
8590
}
8691

8792
bool QgsDelimitedTextFile::open()
@@ -95,25 +100,24 @@ bool QgsDelimitedTextFile::open()
95100
QgsDebugMsg( "Data file " + mFileName + " could not be opened" );
96101
delete mFile;
97102
mFile = 0;
98-
return false;
99-
}
100-
mStream = new QTextStream( mFile );
101-
if ( ! mEncoding.isEmpty() )
102-
{
103-
QTextCodec *codec = QTextCodec::codecForName( mEncoding.toAscii() );
104-
mStream->setCodec( codec );
105103
}
106-
mMaxRecordNumber = -1;
107-
mHoldCurrentRecord = false;
108-
if ( mWatcher ) delete mWatcher;
109-
if ( mUseWatcher )
104+
if( mFile )
110105
{
111-
mWatcher = new QFileSystemWatcher( this );
112-
mWatcher->addPath( mFileName );
113-
connect( mWatcher, SIGNAL( fileChanged( QString ) ), this, SLOT( updateFile() ) );
106+
mStream = new QTextStream( mFile );
107+
if ( ! mEncoding.isEmpty() )
108+
{
109+
QTextCodec *codec = QTextCodec::codecForName( mEncoding.toAscii() );
110+
mStream->setCodec( codec );
111+
}
112+
if ( mUseWatcher )
113+
{
114+
mWatcher = new QFileSystemWatcher( this );
115+
mWatcher->addPath( mFileName );
116+
connect( mWatcher, SIGNAL( fileChanged( QString ) ), this, SLOT( updateFile() ) );
117+
}
114118
}
115119
}
116-
return true;
120+
return mFile != 0;
117121
}
118122

119123
void QgsDelimitedTextFile::updateFile()
@@ -501,6 +505,7 @@ int QgsDelimitedTextFile::fieldIndex( QString name )
501505

502506
bool QgsDelimitedTextFile::setNextRecordId( long nextRecordId )
503507
{
508+
if( ! mFile ) return false;
504509
mHoldCurrentRecord = nextRecordId == mRecordLineNumber;
505510
if ( mHoldCurrentRecord ) return true;
506511
return setNextLineNumber( nextRecordId );
@@ -524,7 +529,7 @@ QgsDelimitedTextFile::Status QgsDelimitedTextFile::nextRecord( QStringList &reco
524529
// Find the first non-blank line to read
525530
QString buffer;
526531
status = nextLine( buffer, true );
527-
if ( status != RecordOk ) return status;
532+
if ( status != RecordOk ) return RecordEOF;
528533

529534
mCurrentRecord.clear();
530535
mRecordLineNumber = mLineNumber;

0 commit comments

Comments
 (0)