Skip to content

Commit

Permalink
nasakeywordhandler: fixes to be able to read some labels with metadat…
Browse files Browse the repository at this point in the history
…a items whose value is a list on several lines
  • Loading branch information
rouault committed Nov 2, 2019
1 parent f86a5db commit e154f9e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
57 changes: 57 additions & 0 deletions autotest/gdrivers/pds.py
Expand Up @@ -315,4 +315,61 @@ def test_pds_band_storage_type_line_interleaved():
return tst.testOpen()


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

def test_pds_sharp_on_continuing_line():

gdal.FileFromMemBuffer('/vsimem/test',
"""PDS_VERSION_ID = "PDS3"
NOTE = (#9933FF,
#FFFF33)
^IMAGE = 1 <BYTES>
OBJECT = IMAGE
BANDS = 1
BAND_STORAGE_TYPE = "BAND SEQUENTIAL"
LINES = 1
LINE_SAMPLES = 1
SAMPLE_BITS = 8
END_OBJECT = IMAGE
END
""")

ds = gdal.Open('/vsimem/test')

assert ds.GetMetadataItem('NOTE') == '(#9933FF,#FFFF33)'

gdal.Unlink('/vsimem/test')


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

def test_pds_sharp_comma_continuing_line():

gdal.FileFromMemBuffer('/vsimem/test',
"""PDS_VERSION_ID = "PDS3"
NOTE = ("a"
,"b")
^IMAGE = 1 <BYTES>
OBJECT = IMAGE
BANDS = 1
BAND_STORAGE_TYPE = "BAND SEQUENTIAL"
LINES = 1
LINE_SAMPLES = 1
SAMPLE_BITS = 8
END_OBJECT = IMAGE
END
""")

ds = gdal.Open('/vsimem/test')

assert ds.GetMetadataItem('NOTE') == '("a","b")'

gdal.Unlink('/vsimem/test')
29 changes: 27 additions & 2 deletions gdal/frmts/pds/nasakeywordhandler.cpp
Expand Up @@ -197,6 +197,18 @@ int NASAKeywordHandler::ReadGroup( const char *pszPathPrefix, CPLJSONObject &oCu
}
}

/************************************************************************/
/* StripQuotesIfNeeded() */
/************************************************************************/

static CPLString StripQuotesIfNeeded(const CPLString& osWord,
bool bQuotesAlreadyRemoved)
{
if( bQuotesAlreadyRemoved || osWord.size() < 2 || osWord[0] != '"' )
return osWord;
return osWord.substr(1, osWord.size() - 2);
}

/************************************************************************/
/* ReadPair() */
/* */
Expand Down Expand Up @@ -266,7 +278,7 @@ int NASAKeywordHandler::ReadPair( CPLString &osName, CPLString &osValue,
*pszHeaderNext == '{' || *pszHeaderNext == ')' ||
*pszHeaderNext == '}')) )
{
oArray.Add(osWord);
oArray.Add(StripQuotesIfNeeded(osWord, m_bStripSurroundingQuotes));
}
}
else if( CPLGetValueType(osWord) == CPL_VALUE_INTEGER )
Expand All @@ -279,6 +291,10 @@ int NASAKeywordHandler::ReadPair( CPLString &osName, CPLString &osValue,
}

osValue += osWord;
while ( isspace( static_cast<unsigned char>( *pszHeaderNext ) ) )
{
pszHeaderNext++;
}

if( *pszHeaderNext == ')' )
{
Expand Down Expand Up @@ -312,6 +328,15 @@ int NASAKeywordHandler::ReadPair( CPLString &osName, CPLString &osValue,
{
osValue += *pszHeaderNext;
pszHeaderNext ++;
// Do not use SkipWhite() here to avoid being confuse by
// constructs like
// FOO = (#123456,
// #123456)
// where we could confuse the second line with a comment.
while ( isspace( static_cast<unsigned char>( *pszHeaderNext ) ) )
{
pszHeaderNext++;
}
}
SkipWhite();

Expand Down Expand Up @@ -339,7 +364,7 @@ int NASAKeywordHandler::ReadPair( CPLString &osName, CPLString &osValue,
{
if( bIsString )
{
oCur.Add( osName, osValue );
oCur.Add( osName, StripQuotesIfNeeded(osValue, m_bStripSurroundingQuotes) );
}
else if( CPLGetValueType(osValue) == CPL_VALUE_INTEGER )
{
Expand Down

0 comments on commit e154f9e

Please sign in to comment.