DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

TokenParser.h (4571B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
     10 it under the terms of the GNU General Public License as published by
     11 the Free Software Foundation, either version 3 of the License, or
     12 (at your option) any later version.
     13 
     14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
     15 but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 GNU General Public License for more details.
     18 
     19 You should have received a copy of the GNU General Public License
     20 along with Doom 3 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 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.
     23 
     24 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.
     25 
     26 ===========================================================================
     27 */
     28 
     29 #ifndef __TOKENPARSER_H__
     30 #define __TOKENPARSER_H__
     31 class idBinaryToken {
     32 public:
     33 	idBinaryToken() {
     34 		tokenType = 0;
     35 		tokenSubType = 0;
     36 	}
     37 	idBinaryToken( const idToken &tok ) {
     38 		token = tok.c_str();
     39 		tokenType = tok.type;
     40 		tokenSubType = tok.subtype;
     41 	}
     42 	bool operator==( const idBinaryToken &b ) const { 
     43 		return ( tokenType == b.tokenType && tokenSubType == b.tokenSubType && token.Cmp( b.token ) == 0 );
     44 	}
     45 	void Read( idFile *inFile ) {
     46 		inFile->ReadString( token );
     47 		inFile->ReadBig( tokenType );
     48 		inFile->ReadBig( tokenSubType );
     49 	}
     50 	void Write( idFile *inFile ) {
     51 		inFile->WriteString( token );
     52 		inFile->WriteBig( tokenType );
     53 		inFile->WriteBig( tokenSubType );
     54 	}
     55 	idStr token;
     56 	int8  tokenType;
     57 	short tokenSubType;
     58 };
     59 
     60 class idTokenIndexes {
     61 public:
     62 	idTokenIndexes() {}
     63 	void Clear() {
     64 		tokenIndexes.Clear();
     65 	}
     66 	int Append( short sdx ) {
     67 		return tokenIndexes.Append( sdx );
     68 	}
     69 	int Num() {
     70 		return tokenIndexes.Num();
     71 	}
     72 	void SetNum( int num ) {
     73 		tokenIndexes.SetNum( num );
     74 	}
     75 	short &	operator[]( const int index ) {
     76 		return tokenIndexes[ index ];
     77 	}
     78 	void SetName( const char *name ) {
     79 		fileName = name;
     80 	}
     81 	const char *GetName() {
     82 		return fileName.c_str();
     83 	}
     84 	void Write( idFile *outFile ) {
     85 		outFile->WriteString( fileName );
     86 		outFile->WriteBig( ( int )tokenIndexes.Num() );
     87 		outFile->WriteBigArray( tokenIndexes.Ptr(), tokenIndexes.Num() );
     88 	}
     89 	void Read( idFile *inFile ) {
     90 		inFile->ReadString( fileName );
     91 		int num;
     92 		inFile->ReadBig( num );
     93 		tokenIndexes.SetNum( num );
     94 		inFile->ReadBigArray( tokenIndexes.Ptr(), num );
     95 	}
     96 private:
     97 	idList< short > tokenIndexes;
     98 	idStr fileName;
     99 };
    100 
    101 class idTokenParser {
    102 public:
    103 	idTokenParser() {
    104 		timeStamp = FILE_NOT_FOUND_TIMESTAMP;
    105 		preloaded = false;
    106 		currentToken = 0;
    107 		currentTokenList = 0;
    108 	}
    109 	~idTokenParser() {
    110 		Clear();
    111 	}
    112 	void Clear() {
    113 		tokens.Clear();
    114 		guiTokenIndexes.Clear();
    115 		currentToken = 0;
    116 		currentTokenList = -1;
    117 		preloaded = false;
    118 	}
    119 	void LoadFromFile( const char *filename );
    120 	void WriteToFile (const char *filename );
    121 	void LoadFromParser( idParser &parser, const char *guiName );
    122 
    123 	bool StartParsing( const char *fileName );
    124 	void DoneParsing() { currentTokenList = -1; }
    125 
    126 	bool IsLoaded() { return tokens.Num() > 0; }
    127 	bool ReadToken( idToken * tok );
    128 	int	ExpectTokenString( const char *string );
    129 	int	ExpectTokenType( int type, int subtype, idToken *token );
    130 	int ExpectAnyToken( idToken *token );
    131 	void SetMarker() {}
    132 	void UnreadToken( const idToken *token );
    133 	void Error( VERIFY_FORMAT_STRING const char *str, ... );
    134 	void Warning( VERIFY_FORMAT_STRING const char *str, ... );
    135 	int ParseInt();
    136 	bool ParseBool();
    137 	float ParseFloat( bool *errorFlag = NULL );
    138 	void UpdateTimeStamp( ID_TIME_T &t ) {
    139 		if ( t > timeStamp ) {
    140 			timeStamp = t;
    141 		}
    142 	}
    143 private:
    144 	idList< idBinaryToken > tokens;
    145 	idList< idTokenIndexes > guiTokenIndexes;
    146 	int currentToken;
    147 	int currentTokenList;
    148 	ID_TIME_T timeStamp;
    149 	bool preloaded;
    150 };
    151 
    152 #endif /* !__TOKENPARSER_H__ */