Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Patch for ticket #3320 to fix delimited text plugin crash
git-svn-id: http://svn.osgeo.org/qgis/trunk@14890 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
gsherman committed Dec 11, 2010
1 parent 9f0c60f commit b52d074
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/providers/delimitedtext/qgsdelimitedtextprovider.cpp
Expand Up @@ -134,6 +134,7 @@ QStringList QgsDelimitedTextProvider::splitLine( QString line )
QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
: QgsVectorDataProvider( uri )
, mHasWktField( false )
, mFirstDataLine(0)
, mFieldCount( 0 )
, mXFieldIndex( -1 ), mYFieldIndex( -1 )
, mWktFieldIndex( -1 )
Expand Down Expand Up @@ -199,7 +200,7 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
if ( mFileName.isEmpty() || mDelimiter.isEmpty() )
{
// uri is invalid so the layer must be too...
QString( "Data source is invalid" );
QgsDebugMsg( "Data source is invalid" );
return;
}

Expand Down Expand Up @@ -238,12 +239,13 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
{
lineNumber++;
line = readLine( mStream ); // line of text excluding '\n', default local 8 bit encoding.
if ( line.isEmpty() )
continue;

if ( lineNumber < mSkipLines + 1 )
continue;

if ( line.isEmpty() )
continue;

if ( !hasFields )
{
// Get the fields from the header row and store them in the
Expand Down Expand Up @@ -297,15 +299,14 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
}
else // hasFields == true - field names already read
{
if( mFirstDataLine == 0 ) mFirstDataLine = lineNumber;

// split the line on the delimiter
QStringList parts = splitLine( line );

// Skip malformed lines silently. Report line number with nextFeature()
if ( parts.size() != mFieldCount )
{
continue;
}
// Ensure that the input has at least the required number of fields (mainly to tolerate
// missed blank strings at end of row)
while( parts.size() < mFieldCount ) parts.append("");

if ( mHasWktField && mWktFieldIndex >= 0 )
{
Expand Down Expand Up @@ -443,13 +444,17 @@ bool QgsDelimitedTextProvider::nextFeature( QgsFeature& feature )
// lex the tokens from the current data line
QStringList tokens = splitLine( line );

while( tokens.size() < mFieldCount ) tokens.append("");

QgsGeometry *geom = 0;

if ( mHasWktField && mWktFieldIndex >= 0 )
{
try
{
QString &sWkt = tokens[mWktFieldIndex];
// Remove Z and M coordinates if present, as currently fromWkt doesn't
// support these.
if ( mWktHasZM )
{
sWkt.remove( mWktZMRegexp ).replace( mWktCrdRegexp, "\\1" );
Expand Down Expand Up @@ -505,6 +510,7 @@ bool QgsDelimitedTextProvider::nextFeature( QgsFeature& feature )
i != mAttributesToFetch.end();
++i )
{

QString &value = tokens[attributeColumns[*i]];
QVariant val;
switch ( attributeFields[*i].type() )
Expand Down Expand Up @@ -624,11 +630,10 @@ void QgsDelimitedTextProvider::rewind()
{
// Reset feature id to 0
mFid = 0;
// Skip ahead one line since first record is always assumed to be
// the header record
// Skip to first data record
mStream->seek( 0 );
int n = mSkipLines + 1;
while ( n-- )
int n = mFirstDataLine-1;
while ( n-- > 0 )
readLine( mStream );
}

Expand Down
1 change: 1 addition & 0 deletions src/providers/delimitedtext/qgsdelimitedtextprovider.h
Expand Up @@ -222,6 +222,7 @@ class QgsDelimitedTextProvider : public QgsVectorDataProvider

long mNumberFeatures;
int mSkipLines;
int mFirstDataLine; // Actual first line of data (accounting for blank lines)

//! Storage for any lines in the file that couldn't be loaded
QStringList mInvalidLines;
Expand Down

0 comments on commit b52d074

Please sign in to comment.