git-svn-id: http://svn.osgeo.org/qgis/trunk@14448 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
jef 2010-10-29 20:59:18 +00:00
parent 5291238ded
commit 4afb7293ba
3 changed files with 47 additions and 45 deletions

View File

@ -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();
@ -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();
@ -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
while ( !stream.atEnd() )
{
QChar c = stream.read( 1 ).at( 0 );
c = stream.read( 1 );
if ( c == NULL || c.size() == 0 )
{
// Reach end of file
return buffer;
}
while ( c == ( char * )"\r" || c == ( char * )"\n" )
{
c = stream.read( 1 );
if ( c == NULL || c.size() == 0 )
if ( c == '\r' || c == '\n' )
{
// Reach end of file
return buffer;
if ( buffer.isEmpty() )
{
// skip leading CR / LF
continue;
}
break;
}
}
// First non-newline character
buffer.append( c );
c = stream.read( 1 );
if ( c == NULL || c.size() == 0 )
{
// Reach end of file
return buffer;
}
while ( !( c == ( char * )"\r" || c == ( char * )"\n" ) )
{
buffer.append( c );
c = stream.read( 1 );
if ( c == NULL || c.size() == 0 )
{
// Reach end of file
return buffer;
}
}
return buffer;
}

View File

@ -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 );
@ -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
@ -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;
}
@ -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 );
@ -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()

View File

@ -221,6 +221,6 @@ class QgsDelimitedTextProvider : public QgsVectorDataProvider
QGis::WkbType mWkbType; //can be WKBPoint or NoGeometry
QString readLine( QTextStream *stream );
QStringList splitLine( QString line );
};