Skip to content

Commit

Permalink
fix #1022
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14448 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Oct 29, 2010
1 parent 7e9b110 commit cdfff70
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 45 deletions.
56 changes: 16 additions & 40 deletions src/plugins/delimited_text/qgsdelimitedtextplugingui.cpp
Expand Up @@ -123,7 +123,7 @@ void QgsDelimitedTextPluginGui::updateFieldLists()
if ( QFile::exists( txtFilePath->text() ) )
{
QFile *file = new QFile( txtFilePath->text() );
if ( file->open( QIODevice::ReadOnly | QIODevice::Text ) )
if ( file->open( QIODevice::ReadOnly ) )
{
// clear the field lists
cmbXField->clear();
Expand Down Expand Up @@ -211,12 +211,12 @@ void QgsDelimitedTextPluginGui::updateFieldLists()
txtSample->insertPlainText( line + "\n" );
// put a few more lines into the sample box
int counter = 0;
line = QgsDelimitedTextPluginGui::readLine( stream );
line = readLine( stream );
while ( !line.isEmpty() && ( counter < 20 ) )
{
txtSample->insertPlainText( line + "\n" );
counter++;
line = QgsDelimitedTextPluginGui::readLine( stream );
line = readLine( stream );
}
// close the file
file->close();
Expand Down Expand Up @@ -259,51 +259,27 @@ void QgsDelimitedTextPluginGui::on_txtDelimiter_textChanged( const QString & tex
}
}

QString QgsDelimitedTextPluginGui::readLine( QTextStream & stream )
QString QgsDelimitedTextPluginGui::readLine( QTextStream &stream )
{
QString buffer( "" );
QString c;
QString buffer;

// Strip leading newlines

c = stream.read( 1 );
if ( c == NULL || c.size() == 0 )
{
// Reach end of file
return buffer;
}
while ( c == ( char * )"\r" || c == ( char * )"\n" )
while ( !stream.atEnd() )
{
c = stream.read( 1 );
if ( c == NULL || c.size() == 0 )
{
// Reach end of file
return buffer;
}
}

// First non-newline character
buffer.append( c );
QChar c = stream.read( 1 ).at( 0 );

c = stream.read( 1 );
if ( c == NULL || c.size() == 0 )
{
// Reach end of file
return buffer;
}
if ( c == '\r' || c == '\n' )
{
if ( buffer.isEmpty() )
{
// skip leading CR / LF
continue;
}

while ( !( c == ( char * )"\r" || c == ( char * )"\n" ) )
{
break;
}

buffer.append( c );
c = stream.read( 1 );
if ( c == NULL || c.size() == 0 )
{
// Reach end of file
return buffer;
}
}

return buffer;

}
34 changes: 30 additions & 4 deletions src/providers/delimitedtext/qgsdelimitedtextprovider.cpp
Expand Up @@ -44,6 +44,31 @@ static const QString TEXT_PROVIDER_KEY = "delimitedtext";
static const QString TEXT_PROVIDER_DESCRIPTION = "Delimited text data provider";


QString QgsDelimitedTextProvider::readLine( QTextStream *stream )
{
QString buffer;

while ( !stream->atEnd() )
{
QChar c = stream->read( 1 ).at( 0 );

if ( c == '\r' || c == '\n' )
{
if ( buffer.isEmpty() )
{
// skip leading CR / LF
continue;
}

break;
}

buffer.append( c );
}

return buffer;
}

QStringList QgsDelimitedTextProvider::splitLine( QString line )
{
QgsDebugMsg( "Attempting to split the input line: " + line + " using delimiter " + mDelimiter );
Expand Down Expand Up @@ -192,7 +217,7 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
while ( !mStream->atEnd() )
{
lineNumber++;
line = mStream->readLine(); // line of text excluding '\n', default local 8 bit encoding.
line = readLine( mStream ); // line of text excluding '\n', default local 8 bit encoding.
if ( !hasFields )
{
// Get the fields from the header row and store them in the
Expand Down Expand Up @@ -267,7 +292,8 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
mExtent.combineExtentWith( x, y );
}
else
{ // Extent for the first point is just the first point
{
// Extent for the first point is just the first point
mExtent.set( x, y, x, y );
firstPoint = false;
}
Expand Down Expand Up @@ -335,7 +361,7 @@ bool QgsDelimitedTextProvider::nextFeature( QgsFeature& feature )
{
double x = 0.0;
double y = 0.0;
QString line = mStream->readLine(); // Default local 8 bit encoding
QString line = readLine( mStream ); // Default local 8 bit encoding

// lex the tokens from the current data line
QStringList tokens = splitLine( line );
Expand Down Expand Up @@ -531,7 +557,7 @@ void QgsDelimitedTextProvider::rewind()
// Skip ahead one line since first record is always assumed to be
// the header record
mStream->seek( 0 );
mStream->readLine();
readLine( mStream );
}

bool QgsDelimitedTextProvider::isValid()
Expand Down
2 changes: 1 addition & 1 deletion src/providers/delimitedtext/qgsdelimitedtextprovider.h
Expand Up @@ -221,6 +221,6 @@ class QgsDelimitedTextProvider : public QgsVectorDataProvider

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

QString readLine( QTextStream *stream );
QStringList splitLine( QString line );

};

0 comments on commit cdfff70

Please sign in to comment.