Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for comments in ERS files #4835

Merged
merged 4 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions autotest/gdrivers/data/ers/srtm.ers
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# This is an ERS file with a comment at the start
DatasetHeader Begin
Version = "5.2"
DataSetType = ERStorage
DataType = Raster
ByteOrder = MSBFirst
Foo = { "x\\\"
y" }
Bar = "# not a comment"
Baz = { "x\\\"
#also not a comment" }

CoordinateSpace Begin
Datum = "WGS84"
Expand All @@ -13,8 +17,9 @@ y" }
Rotation = 0:0:0.0
CoordinateSpace End

# Comment in the middle
RasterInfo Begin
CellType = Signed16BitInteger
CellType = Signed16BitInteger # Comment at the end of a line
NullCellValue = +9999
CellInfo Begin
Xdimension = 0.00833333
Expand All @@ -37,7 +42,8 @@ y" }
RegionInfo Begin
RegionName = "All"
Stats Begin
MinimumValue = { -4315 }
MinimumValue = { -4315 # comment in brackets
}
MaximumValue = { -3744 }
MeanValue = { -4020.25 }
MedianValue = { -4000 }
Expand Down
25 changes: 8 additions & 17 deletions frmts/ers/ersdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -816,23 +816,20 @@ int ERSDataset::Identify( GDALOpenInfo * poOpenInfo )
/* -------------------------------------------------------------------- */
/* We assume the user selects the .ers file. */
/* -------------------------------------------------------------------- */
if( poOpenInfo->nHeaderBytes > 15
&& STARTS_WITH_CI((const char *) poOpenInfo->pabyHeader, "Algorithm Begin") )
CPLString osHeader((const char *)poOpenInfo->pabyHeader, poOpenInfo->nHeaderBytes);

if( osHeader.ifind( "Algorithm Begin" ) != std::string::npos )
{
CPLError( CE_Failure, CPLE_OpenFailed,
"%s appears to be an algorithm ERS file, which is not currently supported.",
poOpenInfo->pszFilename );
return FALSE;
}

/* -------------------------------------------------------------------- */
/* We assume the user selects the .ers file. */
/* -------------------------------------------------------------------- */
if( poOpenInfo->nHeaderBytes < 15
|| !STARTS_WITH_CI((const char *) poOpenInfo->pabyHeader, "DatasetHeader ") )
return FALSE;
if( osHeader.ifind( "DatasetHeader " ) != std::string::npos )
return TRUE;

return TRUE;
return FALSE;
}

/************************************************************************/
Expand Down Expand Up @@ -896,17 +893,11 @@ GDALDataset *ERSDataset::Open( GDALOpenInfo * poOpenInfo )
return nullptr;

/* -------------------------------------------------------------------- */
/* Read the first line. */
/* -------------------------------------------------------------------- */

CPLReadLineL( poOpenInfo->fpL );

/* -------------------------------------------------------------------- */
/* Now ingest the rest of the file as a tree of header nodes. */
/* Ingest the file as a tree of header nodes. */
/* -------------------------------------------------------------------- */
ERSHdrNode *poHeader = new ERSHdrNode();

if( !poHeader->ParseChildren( poOpenInfo->fpL ) )
if( !poHeader->ParseHeader( poOpenInfo->fpL ) )
{
delete poHeader;
VSIFCloseL( poOpenInfo->fpL );
Expand Down
45 changes: 44 additions & 1 deletion frmts/ers/ershdrnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,55 @@ int ERSHdrNode::ReadLine( VSILFILE * fp, CPLString &osLine )
{
bLastCharWasSlashInQuote = true;
}
// A comment is a '#' up to the end of the line.
else if( ch == '#' && !bInQuote )
{
osLine = osLine.substr(0, i) + "\n";
}
}
} while( nBracketLevel > 0 );

return TRUE;
}

/************************************************************************/
/* ParseHeader() */
/* */
/* We receive the FILE * positioned at the start of the file */
/* and read all children. This allows reading comment lines */
/* at the start of the file. */
/************************************************************************/

int ERSHdrNode::ParseHeader( VSILFILE * fp )

{
while( true )
{
/* -------------------------------------------------------------------- */
/* Read the next line */
/* -------------------------------------------------------------------- */
CPLString osLine;
size_t iOff;

if( !ReadLine( fp, osLine ) )
return FALSE;

/* -------------------------------------------------------------------- */
/* Got a DatasetHeader Begin */
/* -------------------------------------------------------------------- */
else if( (iOff = osLine.ifind( " Begin" )) != std::string::npos )
{
CPLString osName = osLine.substr(0,iOff);
osName.Trim();

if ( osName.tolower() == CPLString("DatasetHeader").tolower() )
{
return ParseChildren( fp );
}
}
}
}

/************************************************************************/
/* ParseChildren() */
/* */
Expand All @@ -155,7 +198,7 @@ int ERSHdrNode::ParseChildren( VSILFILE * fp, int nRecLevel )
{
CPLError(CE_Failure, CPLE_AppDefined,
"Too many recursion level while parsing .ers header");
return false;
return FALSE;
}

while( true )
Expand Down
1 change: 1 addition & 0 deletions frmts/ers/ershdrnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ERSHdrNode
ERSHdrNode();
~ERSHdrNode();

int ParseHeader( VSILFILE *fp );
int ParseChildren( VSILFILE *fp, int nRecLevel = 0 );
int WriteSelf( VSILFILE *fp, int nIndent );

Expand Down