This commit is contained in:
Steve Checkoway
2006-02-19 11:24:37 +00:00
parent 2c99a2b194
commit 4c593087a3
2 changed files with 27 additions and 22 deletions
+23 -18
View File
@@ -24,12 +24,12 @@
void MsdFile::AddParam( char *buf, int len ) void MsdFile::AddParam( char *buf, int len )
{ {
values.back().params.push_back(RString(buf, len)); values.back().params.push_back( RString(buf, len) );
} }
void MsdFile::AddValue() /* (no extra charge) */ void MsdFile::AddValue() /* (no extra charge) */
{ {
values.push_back(value_t()); values.push_back( value_t() );
values.back().params.reserve( 32 ); values.back().params.reserve( 32 );
} }
@@ -41,27 +41,30 @@ void MsdFile::ReadBuf( char *buf, int len )
bool ReadingValue=false; bool ReadingValue=false;
int i = 0; int i = 0;
while(i < len) while( i < len )
{ {
if( i+1 < len && buf[i] == '/' && buf[i+1] == '/' ) if( i+1 < len && buf[i] == '/' && buf[i+1] == '/' )
{ {
/* //; erase with spaces until newline */ /* //; erase with spaces until newline */
do { do
{
buf[i] = ' '; buf[i] = ' ';
i++; i++;
} while(i < len && buf[i] != '\n'); }
while( i < len && buf[i] != '\n' );
continue; continue;
} }
if(ReadingValue && buf[i] == '#') { if( ReadingValue && buf[i] == '#' )
{
/* Unfortunately, many of these files are missing ;'s. /* Unfortunately, many of these files are missing ;'s.
* If we get a # when we thought we were inside a value, assume we * If we get a # when we thought we were inside a value, assume we
* missed the ;. Back up and end the value. */ * missed the ;. Back up and end the value. */
/* Make sure this # is the first non-whitespace character on the line. */ /* Make sure this # is the first non-whitespace character on the line. */
bool FirstChar = true; bool FirstChar = true;
int j; int j;
for(j = i-1; j >= 0 && !strchr("\r\n", buf[j]); --j) for( j = i-1; j >= 0 && !strchr("\r\n", buf[j]); --j )
{ {
if(buf[j] == ' ' || buf[j] == '\t') if(buf[j] == ' ' || buf[j] == '\t')
continue; continue;
@@ -70,7 +73,8 @@ void MsdFile::ReadBuf( char *buf, int len )
break; break;
} }
if(!FirstChar) { if( !FirstChar )
{
/* Oops, we're not; handle this like a regular character. */ /* Oops, we're not; handle this like a regular character. */
i++; i++;
continue; continue;
@@ -80,28 +84,29 @@ void MsdFile::ReadBuf( char *buf, int len )
while( j >= 1 && strchr("\r\n", buf[j-1]) ) while( j >= 1 && strchr("\r\n", buf[j-1]) )
--j; --j;
AddParam(buf+value_start, j - value_start); AddParam( buf+value_start, j - value_start );
ReadingValue=false; ReadingValue=false;
} }
/* # starts a new value. */ /* # starts a new value. */
if(!ReadingValue && buf[i] == '#') { if( !ReadingValue && buf[i] == '#' )
{
AddValue(); AddValue();
ReadingValue=true; ReadingValue=true;
} }
if(!ReadingValue) if( !ReadingValue )
{ {
i++; i++;
continue; /* nothing else is meaningful outside of a value */ continue; /* nothing else is meaningful outside of a value */
} }
/* : and ; end the current param, if any. */ /* : and ; end the current param, if any. */
if(value_start != -1 && (buf[i] == ':' || buf[i] == ';')) if( value_start != -1 && (buf[i] == ':' || buf[i] == ';') )
AddParam(buf+value_start, i - value_start); AddParam(buf+value_start, i - value_start);
/* # and : begin new params. */ /* # and : begin new params. */
if(buf[i] == '#' || buf[i] == ':') if( buf[i] == '#' || buf[i] == ':' )
{ {
i++; /* skip */ i++; /* skip */
value_start = i; value_start = i;
@@ -109,15 +114,15 @@ void MsdFile::ReadBuf( char *buf, int len )
} }
/* ; ends the current value. */ /* ; ends the current value. */
if(buf[i] == ';') if( buf[i] == ';' )
ReadingValue=false; ReadingValue=false;
i++; i++;
} }
/* Add any unterminated value at the very end. */ /* Add any unterminated value at the very end. */
if(ReadingValue) if( ReadingValue )
AddParam(buf+value_start, i - value_start); AddParam( buf+value_start, i - value_start );
} }
// returns true if successful, false otherwise // returns true if successful, false otherwise
@@ -160,8 +165,8 @@ void MsdFile::ReadFromString( const RString &sString )
RString MsdFile::GetParam(unsigned val, unsigned par) const RString MsdFile::GetParam(unsigned val, unsigned par) const
{ {
if(val >= GetNumValues()) return RString(); if( val >= GetNumValues() || par >= GetNumParams(val) )
if(par >= GetNumParams(val)) return RString(); return RString();
return values[val].params[par]; return values[val].params[par];
} }
+4 -4
View File
@@ -11,7 +11,7 @@ public:
{ {
vector<RString> params; vector<RString> params;
RString operator[](unsigned i) const { if(i >= params.size()) return RString(); return params[i]; } RString operator[]( unsigned i ) const { if( i >= params.size() ) return RString(); return params[i]; }
}; };
virtual ~MsdFile() { } virtual ~MsdFile() { }
@@ -23,9 +23,9 @@ public:
RString GetError() const { return error; } RString GetError() const { return error; }
unsigned GetNumValues() const { return values.size(); } unsigned GetNumValues() const { return values.size(); }
unsigned GetNumParams(unsigned val) const { if(val >= GetNumValues()) return 0; return values[val].params.size(); } unsigned GetNumParams( unsigned val ) const { if( val >= GetNumValues() ) return 0; return values[val].params.size(); }
const value_t &GetValue(unsigned val) const { ASSERT(val < GetNumValues()); return values[val]; } const value_t &GetValue( unsigned val ) const { ASSERT(val < GetNumValues()); return values[val]; }
RString GetParam(unsigned val, unsigned par) const; RString GetParam( unsigned val, unsigned par ) const;
private: private: