Skip to content

Commit

Permalink
CSV: allow creating a new .csv file in a directory where there are in…
Browse files Browse the repository at this point in the history
…valid .csv files; use a trick so that the CSV driver creates valid single column files (https://trac.osgeo.org/gdal/ticket/4824)

git-svn-id: https://svn.osgeo.org/gdal/trunk@24965 f0d54148-0727-0410-94bb-9a71ac55c965
  • Loading branch information
rouault committed Sep 24, 2012
1 parent 117cf5a commit 92dcffc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
44 changes: 44 additions & 0 deletions autotest/ogr/ogr_csv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
# $Id$
#
Expand Down Expand Up @@ -925,6 +926,48 @@ def ogr_csv_22():

return 'success'

###############################################################################
# Test single column CSV files

def ogr_csv_23():

# Create an invalid CSV file
f = gdal.VSIFOpenL('/vsimem/invalid.csv', 'wb')
gdal.VSIFCloseL(f)

# and check that it doesn't prevent from creating a new CSV file (#4824)
ds = ogr.GetDriverByName('CSV').CreateDataSource( '/vsimem/single.csv' )
lyr = ds.CreateLayer('single')
lyr.CreateField(ogr.FieldDefn('foo', ogr.OFTString))
feat = ogr.Feature(lyr.GetLayerDefn())
lyr.CreateFeature(feat)
feat = ogr.Feature(lyr.GetLayerDefn())
feat.SetField(0, 'bar')
lyr.CreateFeature(feat)
feat = None
lyr = None
ds = None

ds = ogr.Open( '/vsimem/single.csv' )
lyr = ds.GetLayer(0)
if lyr.GetLayerDefn().GetFieldCount() != 1:
gdaltest.post_reason('fail')
return 'fail'
feat = lyr.GetNextFeature()
if feat.GetField(0) != '':
gdaltest.post_reason('fail')
return 'fail'
feat = lyr.GetNextFeature()
if feat.GetField(0) != 'bar':
gdaltest.post_reason('fail')
return 'fail'
ds = None

gdal.Unlink('/vsimem/single.csv')
gdal.Unlink('/vsimem/invalid.csv')

return 'success'

###############################################################################
#

Expand Down Expand Up @@ -977,6 +1020,7 @@ def ogr_csv_cleanup():
ogr_csv_20,
ogr_csv_21,
ogr_csv_22,
ogr_csv_23,
ogr_csv_cleanup ]

if __name__ == '__main__':
Expand Down
12 changes: 6 additions & 6 deletions gdal/ogr/ogrsf_frmts/csv/ogrcsvdatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ int OGRCSVDataSource::Open( const char * pszFilename, int bUpdateIn,
{
if( !OpenTable( oSubFilename ) )
{
CSLDestroy( papszNames );
CPLDebug("CSV", "Cannot open %s", oSubFilename.c_str());
nNotCSVCount++;
return FALSE;
continue;
}
}

Expand All @@ -294,9 +294,9 @@ int OGRCSVDataSource::Open( const char * pszFilename, int bUpdateIn,
bRet |= OpenTable( oSubFilename, NULL, "SOURCE");
if ( !bRet )
{
CSLDestroy( papszNames );
CPLDebug("CSV", "Cannot open %s", oSubFilename.c_str());
nNotCSVCount++;
return FALSE;
continue;
}
}
/* GNIS specific */
Expand All @@ -306,9 +306,9 @@ int OGRCSVDataSource::Open( const char * pszFilename, int bUpdateIn,
{
if ( !OpenTable( oSubFilename, NULL, "PRIMARY") )
{
CSLDestroy( papszNames );
CPLDebug("CSV", "Cannot open %s", oSubFilename.c_str());
nNotCSVCount++;
return FALSE;
continue;
}
}
else
Expand Down
25 changes: 23 additions & 2 deletions gdal/ogr/ogrsf_frmts/csv/ogrcsvlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,13 @@ OGRCSVLayer::OGRCSVLayer( const char *pszLayerNameIn,

if (pszFieldName == NULL)
{
/* Re-read single column CSV files that have a trailing comma */
/* in the header line */
if( iField == 1 && nFieldCount == 2 && papszTokens[1][0] == '\0' )
{
nFieldCount = 1;
break;
}
pszFieldName = szFieldNameBuffer;
sprintf( szFieldNameBuffer, "field_%d", iField+1 );
}
Expand Down Expand Up @@ -950,6 +957,14 @@ OGRErr OGRCSVLayer::CreateFeature( OGRFeature *poNewFeature )
}
}
}

/* The CSV driver will not recognize single column tables, so add */
/* a fake second blank field */
if( poFeatureDefn->GetFieldCount() == 1 )
{
if (fpCSV) VSIFPrintfL( fpCSV, "%c", chDelimiter );
}

if( bUseCRLF )
{
if (fpCSV) VSIFPutcL( 13, fpCSV );
Expand Down Expand Up @@ -1047,6 +1062,7 @@ OGRErr OGRCSVLayer::CreateFeature( OGRFeature *poNewFeature )
/* -------------------------------------------------------------------- */
/* Write out all the field values. */
/* -------------------------------------------------------------------- */
int bNonEmptyLine = FALSE;
for( iField = 0; iField < poFeatureDefn->GetFieldCount(); iField++ )
{
char *pszEscaped;
Expand All @@ -1069,10 +1085,15 @@ OGRErr OGRCSVLayer::CreateFeature( OGRFeature *poNewFeature )
-1, CPLES_CSV );
}

VSIFWriteL( pszEscaped, 1, strlen(pszEscaped), fpCSV );
int nLen = (int)strlen(pszEscaped);
bNonEmptyLine |= (nLen != 0);
VSIFWriteL( pszEscaped, 1, nLen, fpCSV );
CPLFree( pszEscaped );
}


if( poFeatureDefn->GetFieldCount() == 1 && !bNonEmptyLine )
VSIFPrintfL( fpCSV, "%c", chDelimiter );

if( bUseCRLF )
VSIFPutcL( 13, fpCSV );
VSIFPutcL( '\n', fpCSV );
Expand Down

0 comments on commit 92dcffc

Please sign in to comment.