doom3-bfg/neo/framework/TokenParser.h

181 lines
4.6 KiB
C
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#ifndef __TOKENPARSER_H__
#define __TOKENPARSER_H__
class idBinaryToken
{
2012-11-26 18:58:24 +00:00
public:
idBinaryToken()
{
2012-11-26 18:58:24 +00:00
tokenType = 0;
tokenSubType = 0;
}
idBinaryToken( const idToken& tok )
{
2012-11-26 18:58:24 +00:00
token = tok.c_str();
tokenType = tok.type;
tokenSubType = tok.subtype;
}
bool operator==( const idBinaryToken& b ) const
{
2012-11-26 18:58:24 +00:00
return ( tokenType == b.tokenType && tokenSubType == b.tokenSubType && token.Cmp( b.token ) == 0 );
}
void Read( idFile* inFile )
{
2012-11-26 18:58:24 +00:00
inFile->ReadString( token );
inFile->ReadBig( tokenType );
inFile->ReadBig( tokenSubType );
}
void Write( idFile* inFile )
{
2012-11-26 18:58:24 +00:00
inFile->WriteString( token );
inFile->WriteBig( tokenType );
inFile->WriteBig( tokenSubType );
}
idStr token;
int8 tokenType;
short tokenSubType;
};
class idTokenIndexes
{
2012-11-26 18:58:24 +00:00
public:
idTokenIndexes() {}
void Clear()
{
2012-11-26 18:58:24 +00:00
tokenIndexes.Clear();
}
int Append( short sdx )
{
2012-11-26 18:58:24 +00:00
return tokenIndexes.Append( sdx );
}
int Num()
{
2012-11-26 18:58:24 +00:00
return tokenIndexes.Num();
}
void SetNum( int num )
{
2012-11-26 18:58:24 +00:00
tokenIndexes.SetNum( num );
}
short& operator[]( const int index )
{
2012-11-26 18:58:24 +00:00
return tokenIndexes[ index ];
}
void SetName( const char* name )
{
2012-11-26 18:58:24 +00:00
fileName = name;
}
const char* GetName()
{
2012-11-26 18:58:24 +00:00
return fileName.c_str();
}
void Write( idFile* outFile )
{
2012-11-26 18:58:24 +00:00
outFile->WriteString( fileName );
outFile->WriteBig( ( int )tokenIndexes.Num() );
outFile->WriteBigArray( tokenIndexes.Ptr(), tokenIndexes.Num() );
}
void Read( idFile* inFile )
{
2012-11-26 18:58:24 +00:00
inFile->ReadString( fileName );
int num;
inFile->ReadBig( num );
tokenIndexes.SetNum( num );
inFile->ReadBigArray( tokenIndexes.Ptr(), num );
}
private:
idList< short > tokenIndexes;
idStr fileName;
};
class idTokenParser
{
2012-11-26 18:58:24 +00:00
public:
idTokenParser()
{
2012-11-26 18:58:24 +00:00
timeStamp = FILE_NOT_FOUND_TIMESTAMP;
preloaded = false;
currentToken = 0;
currentTokenList = 0;
}
~idTokenParser()
{
2012-11-26 18:58:24 +00:00
Clear();
}
void Clear()
{
2012-11-26 18:58:24 +00:00
tokens.Clear();
guiTokenIndexes.Clear();
currentToken = 0;
currentTokenList = -1;
preloaded = false;
}
void LoadFromFile( const char* filename );
void WriteToFile( const char* filename );
void LoadFromParser( idParser& parser, const char* guiName );
bool StartParsing( const char* fileName );
void DoneParsing()
{
currentTokenList = -1;
}
bool IsLoaded()
{
return tokens.Num() > 0;
}
bool ReadToken( idToken* tok );
int ExpectTokenString( const char* string );
int ExpectTokenType( int type, int subtype, idToken* token );
int ExpectAnyToken( idToken* token );
2012-11-26 18:58:24 +00:00
void SetMarker() {}
void UnreadToken( const idToken* token );
void Error( VERIFY_FORMAT_STRING const char* str, ... ) ID_INSTANCE_ATTRIBUTE_PRINTF( 1, 2 );
void Warning( VERIFY_FORMAT_STRING const char* str, ... ) ID_INSTANCE_ATTRIBUTE_PRINTF( 1, 2 );
2012-11-26 18:58:24 +00:00
int ParseInt();
bool ParseBool();
float ParseFloat( bool* errorFlag = NULL );
void UpdateTimeStamp( ID_TIME_T& t )
{
if( t > timeStamp )
{
2012-11-26 18:58:24 +00:00
timeStamp = t;
}
}
private:
idList< idBinaryToken > tokens;
idList< idTokenIndexes > guiTokenIndexes;
int currentToken;
int currentTokenList;
ID_TIME_T timeStamp;
bool preloaded;
};
#endif /* !__TOKENPARSER_H__ */