DSL: Strip comments "{{...}}" when reading file

This commit is contained in:
Abs62 2013-07-07 14:48:37 +04:00
parent 7e7adaf7c0
commit 4af2d43dd2
3 changed files with 73 additions and 6 deletions

16
dsl.cc
View file

@ -73,7 +73,7 @@ DEF_EX_STR( exCantReadFile, "Can't read file", Dictionary::Ex )
enum
{
Signature = 0x584c5344, // DSLX on little-endian, XLSD on big-endian
CurrentFormatVersion = 18 + BtreeIndexing::FormatVersion + Folding::Version,
CurrentFormatVersion = 19 + BtreeIndexing::FormatVersion + Folding::Version,
CurrentZipSupportVersion = 2
};
@ -519,6 +519,10 @@ void DslDictionary::loadArticle( uint32_t address,
DslIconv::getEncodingNameFor( DslEncoding( idxHeader.dslEncoding ) ),
articleBody, articleSize );
free( articleBody );
// Strip DSL comments
bool b = false;
stripComments( articleData, b );
}
catch( ... )
{
@ -1637,7 +1641,7 @@ vector< sptr< Dictionary::Class > > makeDictionaries(
for( ; ; )
{
// Skip any whitespace
if ( !abrvScanner.readNextLine( curString, curOffset ) )
if ( !abrvScanner.readNextLineWithoutComments( curString, curOffset ) )
break;
if ( curString.empty() || isDslWs( curString[ 0 ] ) )
continue;
@ -1656,7 +1660,7 @@ vector< sptr< Dictionary::Class > > makeDictionaries(
expandOptionalParts( curString, &keys );
if ( !abrvScanner.readNextLine( curString, curOffset ) || curString.empty() )
if ( !abrvScanner.readNextLineWithoutComments( curString, curOffset ) || curString.empty() )
{
FDPRINTF( stderr, "Warning: premature end of file %s\n", abrvFileName.c_str() );
eof = true;
@ -1725,7 +1729,7 @@ vector< sptr< Dictionary::Class > > makeDictionaries(
{
// Find the main headword
if ( !hasString && !scanner.readNextLine( curString, curOffset ) )
if ( !hasString && !scanner.readNextLineWithoutComments( curString, curOffset ) )
break; // Clean end of file
hasString = false;
@ -1766,7 +1770,7 @@ vector< sptr< Dictionary::Class > > makeDictionaries(
for( ; ; )
{
if ( ! ( hasString = scanner.readNextLine( curString, curOffset ) ) )
if ( ! ( hasString = scanner.readNextLineWithoutComments( curString, curOffset ) ) )
{
FDPRINTF( stderr, "Warning: premature end of file %s\n", i->c_str() );
break;
@ -1821,7 +1825,7 @@ vector< sptr< Dictionary::Class > > makeDictionaries(
for( ; ; )
{
if ( ! ( hasString = scanner.readNextLine( curString, curOffset ) )
if ( ! ( hasString = scanner.readNextLineWithoutComments( curString, curOffset ) )
|| ( curString.size() && !isDslWs( curString[ 0 ] ) ) )
{
if( insideInsided )

View file

@ -861,6 +861,34 @@ bool DslScanner::readNextLine( wstring & out, size_t & offset ) throw( Ex,
}
}
bool DslScanner::readNextLineWithoutComments( wstring & out, size_t & offset )
throw( Ex, Iconv::Ex )
{
wstring str;
bool commentToNextLine = false;
size_t currentOffset;
out.erase();
offset = 0;
do
{
bool b = readNextLine( str, currentOffset );
if( offset == 0 )
offset = currentOffset;
if( !b )
return false;
stripComments( str, commentToNextLine);
out += str;
}
while( commentToNextLine );
return true;
}
/////////////// DslScanner
@ -1068,6 +1096,35 @@ void expandOptionalParts( wstring & str, list< wstring > * result,
result->merge( expanded );
}
static const wstring openBraces( GD_NATIVE_TO_WS( L"{{" ) );
static const wstring closeBraces( GD_NATIVE_TO_WS( L"}}" ) );
void stripComments( wstring & str, bool & nextLine )
{
string::size_type n = 0, n2 = 0;
for( ; ; )
{
if( nextLine )
{
n = str.find( closeBraces, n2 );
if( n == string::npos )
{
str.erase( n2, n );
return;
}
str.erase( n2, n - n2 + 2 );
nextLine = false;
}
n = str.find( openBraces, n2 );
if( n == string::npos )
return;
nextLine = true;
n2 = n;
}
}
void expandTildes( wstring & str, wstring const & tildeReplacement )
{
for( size_t x = 0; x < str.size(); )

View file

@ -145,6 +145,9 @@ public:
/// with #).
bool readNextLine( wstring &, size_t & offset ) throw( Ex, Iconv::Ex );
/// Similar readNextLine but strip all DSL comments {{...}}
bool readNextLineWithoutComments( wstring &, size_t & offset ) throw( Ex, Iconv::Ex );
/// Returns the number of lines read so far from the file.
unsigned getLinesRead() const
{ return linesRead; }
@ -176,6 +179,9 @@ void unescapeDsl( wstring & str );
// into a single space.
void normalizeHeadword( wstring & );
/// Strip DSL {{...}} comments
void stripComments( wstring &, bool & );
inline size_t DslScanner::distanceToBytes( size_t x ) const
{
switch( encoding )