Skip to content
Permalink
Browse files

nasakeywordhandler: fixes to be able to read some labels with metadat…

…a items whose value is a list on several lines
  • Loading branch information...
rouault committed Nov 2, 2019
1 parent f86a5db commit e154f9ed078e6b9d750c383b3d4fefb3cdfb109f
Showing with 84 additions and 2 deletions.
  1. +57 −0 autotest/gdrivers/pds.py
  2. +27 −2 gdal/frmts/pds/nasakeywordhandler.cpp
@@ -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')
@@ -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() */
/* */
@@ -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 )
@@ -279,6 +291,10 @@ int NASAKeywordHandler::ReadPair( CPLString &osName, CPLString &osValue,
}

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

if( *pszHeaderNext == ')' )
{
@@ -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();

@@ -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 )
{

0 comments on commit e154f9e

Please sign in to comment.
You can’t perform that action at this time.