Skip to content

Commit 4afb729

Browse files
author
jef
committed
fix #1022
git-svn-id: http://svn.osgeo.org/qgis/trunk@14448 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 5291238 commit 4afb729

File tree

3 files changed

+47
-45
lines changed

3 files changed

+47
-45
lines changed

src/plugins/delimited_text/qgsdelimitedtextplugingui.cpp

+16-40
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void QgsDelimitedTextPluginGui::updateFieldLists()
123123
if ( QFile::exists( txtFilePath->text() ) )
124124
{
125125
QFile *file = new QFile( txtFilePath->text() );
126-
if ( file->open( QIODevice::ReadOnly | QIODevice::Text ) )
126+
if ( file->open( QIODevice::ReadOnly ) )
127127
{
128128
// clear the field lists
129129
cmbXField->clear();
@@ -211,12 +211,12 @@ void QgsDelimitedTextPluginGui::updateFieldLists()
211211
txtSample->insertPlainText( line + "\n" );
212212
// put a few more lines into the sample box
213213
int counter = 0;
214-
line = QgsDelimitedTextPluginGui::readLine( stream );
214+
line = readLine( stream );
215215
while ( !line.isEmpty() && ( counter < 20 ) )
216216
{
217217
txtSample->insertPlainText( line + "\n" );
218218
counter++;
219-
line = QgsDelimitedTextPluginGui::readLine( stream );
219+
line = readLine( stream );
220220
}
221221
// close the file
222222
file->close();
@@ -259,51 +259,27 @@ void QgsDelimitedTextPluginGui::on_txtDelimiter_textChanged( const QString & tex
259259
}
260260
}
261261

262-
QString QgsDelimitedTextPluginGui::readLine( QTextStream & stream )
262+
QString QgsDelimitedTextPluginGui::readLine( QTextStream &stream )
263263
{
264-
QString buffer( "" );
265-
QString c;
264+
QString buffer;
266265

267-
// Strip leading newlines
268-
269-
c = stream.read( 1 );
270-
if ( c == NULL || c.size() == 0 )
271-
{
272-
// Reach end of file
273-
return buffer;
274-
}
275-
while ( c == ( char * )"\r" || c == ( char * )"\n" )
266+
while ( !stream.atEnd() )
276267
{
277-
c = stream.read( 1 );
278-
if ( c == NULL || c.size() == 0 )
279-
{
280-
// Reach end of file
281-
return buffer;
282-
}
283-
}
284-
285-
// First non-newline character
286-
buffer.append( c );
268+
QChar c = stream.read( 1 ).at( 0 );
287269

288-
c = stream.read( 1 );
289-
if ( c == NULL || c.size() == 0 )
290-
{
291-
// Reach end of file
292-
return buffer;
293-
}
270+
if ( c == '\r' || c == '\n' )
271+
{
272+
if ( buffer.isEmpty() )
273+
{
274+
// skip leading CR / LF
275+
continue;
276+
}
294277

295-
while ( !( c == ( char * )"\r" || c == ( char * )"\n" ) )
296-
{
278+
break;
279+
}
297280

298281
buffer.append( c );
299-
c = stream.read( 1 );
300-
if ( c == NULL || c.size() == 0 )
301-
{
302-
// Reach end of file
303-
return buffer;
304-
}
305282
}
306283

307284
return buffer;
308-
309285
}

src/providers/delimitedtext/qgsdelimitedtextprovider.cpp

+30-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,31 @@ static const QString TEXT_PROVIDER_KEY = "delimitedtext";
4444
static const QString TEXT_PROVIDER_DESCRIPTION = "Delimited text data provider";
4545

4646

47+
QString QgsDelimitedTextProvider::readLine( QTextStream *stream )
48+
{
49+
QString buffer;
50+
51+
while ( !stream->atEnd() )
52+
{
53+
QChar c = stream->read( 1 ).at( 0 );
54+
55+
if ( c == '\r' || c == '\n' )
56+
{
57+
if ( buffer.isEmpty() )
58+
{
59+
// skip leading CR / LF
60+
continue;
61+
}
62+
63+
break;
64+
}
65+
66+
buffer.append( c );
67+
}
68+
69+
return buffer;
70+
}
71+
4772
QStringList QgsDelimitedTextProvider::splitLine( QString line )
4873
{
4974
QgsDebugMsg( "Attempting to split the input line: " + line + " using delimiter " + mDelimiter );
@@ -192,7 +217,7 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
192217
while ( !mStream->atEnd() )
193218
{
194219
lineNumber++;
195-
line = mStream->readLine(); // line of text excluding '\n', default local 8 bit encoding.
220+
line = readLine( mStream ); // line of text excluding '\n', default local 8 bit encoding.
196221
if ( !hasFields )
197222
{
198223
// Get the fields from the header row and store them in the
@@ -267,7 +292,8 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
267292
mExtent.combineExtentWith( x, y );
268293
}
269294
else
270-
{ // Extent for the first point is just the first point
295+
{
296+
// Extent for the first point is just the first point
271297
mExtent.set( x, y, x, y );
272298
firstPoint = false;
273299
}
@@ -335,7 +361,7 @@ bool QgsDelimitedTextProvider::nextFeature( QgsFeature& feature )
335361
{
336362
double x = 0.0;
337363
double y = 0.0;
338-
QString line = mStream->readLine(); // Default local 8 bit encoding
364+
QString line = readLine( mStream ); // Default local 8 bit encoding
339365

340366
// lex the tokens from the current data line
341367
QStringList tokens = splitLine( line );
@@ -531,7 +557,7 @@ void QgsDelimitedTextProvider::rewind()
531557
// Skip ahead one line since first record is always assumed to be
532558
// the header record
533559
mStream->seek( 0 );
534-
mStream->readLine();
560+
readLine( mStream );
535561
}
536562

537563
bool QgsDelimitedTextProvider::isValid()

src/providers/delimitedtext/qgsdelimitedtextprovider.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,6 @@ class QgsDelimitedTextProvider : public QgsVectorDataProvider
221221

222222
QGis::WkbType mWkbType; //can be WKBPoint or NoGeometry
223223

224+
QString readLine( QTextStream *stream );
224225
QStringList splitLine( QString line );
225-
226226
};

0 commit comments

Comments
 (0)