00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef WORLDFILE_HH
00028 #define WORLDFILE_HH
00029
00030
00031 #include <stdint.h>
00032 #include <stdio.h>
00033
00034 namespace Stg {
00035
00037 class CProperty
00038 {
00039 public:
00041 int entity;
00042
00044 std::string name;
00045
00047 std::vector<int> values;
00048
00050 int line;
00051
00053 bool used;
00054
00055 CProperty( int entity, const char* name, int line ) :
00056 entity(entity),
00057 name(name),
00058 values(),
00059 line(line),
00060 used(false) {}
00061 };
00062
00063
00064
00065
00066
00067
00068
00069 class Worldfile
00070 {
00071
00072 public: Worldfile();
00073 public: ~Worldfile();
00074
00075
00076
00077 protected: FILE* FileOpen(const std::string& filename, const char* method);
00078
00079
00080 public: bool Load(const std::string& filename );
00081
00082
00083 public: bool Save(const std::string& filename);
00084
00085
00086 public: bool WarnUnused();
00087
00088
00089 public: const std::string ReadString(int entity, const char* name, const std::string& value);
00090
00091
00092 public: void WriteString(int entity, const char *name, const std::string& value );
00093
00094
00095 public: int ReadInt(int entity, const char *name, int value);
00096
00097
00098 public: void WriteInt(int entity, const char *name, int value);
00099
00100
00101 public: double ReadFloat(int entity, const char *name, double value);
00102
00103
00104 public: void WriteFloat(int entity, const char *name, double value);
00105
00106
00107 public: double ReadLength(int entity, const char *name, double value)
00108 {
00109 return (ReadFloat( entity, name, value/unit_length ) * unit_length);
00110 }
00111
00112
00113 public: void WriteLength(int entity, const char *name, double value);
00114
00115
00116 public: double ReadAngle(int entity, const char *name, double value)
00117 {
00118 return (ReadFloat( entity, name, value/unit_angle ) * unit_angle);
00119 }
00120
00121
00122
00123
00124
00125 public: uint32_t ReadColor(int entity, const char *name, uint32_t value);
00126
00127
00128
00129
00130 public: const char *ReadFilename(int entity, const char *name, const char *value);
00131
00132
00133 public: int ReadTuple( const int entity, const char* name,
00134 const unsigned int first, const unsigned int num, const char* format, ... );
00135
00136
00137 public: void WriteTuple( const int entity, const char *name,
00138 const unsigned int first, const unsigned int count, const char* format, ... );
00139
00140
00142
00143
00144
00145 private: bool LoadTokens(FILE *file, int include);
00146
00147
00148 private: bool LoadTokenComment(FILE *file, int *line, int include);
00149
00150
00151 private: bool LoadTokenWord(FILE *file, int *line, int include);
00152
00153
00154 private: bool LoadTokenInclude(FILE *file, int *line, int include);
00155
00156
00157 private: bool LoadTokenNum(FILE *file, int *line, int include);
00158
00159
00160 private: bool LoadTokenString(FILE *file, int *line, int include);
00161
00162
00163 private: bool LoadTokenSpace(FILE *file, int *line, int include);
00164
00165
00166 private: bool SaveTokens(FILE *file);
00167
00168
00169 private: void ClearTokens();
00170
00171
00172 private: bool AddToken(int type, const char *value, int include);
00173
00174
00175 private: bool SetTokenValue(int index, const char *value);
00176
00177
00178 private: const char *GetTokenValue(int index);
00179
00180
00181 private: void DumpTokens();
00182
00183
00184 private: bool ParseTokens();
00185
00186
00187 private: bool ParseTokenInclude(int *index, int *line);
00188
00189
00190 private: bool ParseTokenDefine(int *index, int *line);
00191
00192
00193 private: bool ParseTokenWord(int entity, int *index, int *line);
00194
00195
00196 private: bool ParseTokenEntity(int entity, int *index, int *line);
00197
00198
00199 private: bool ParseTokenProperty(int entity, int *index, int *line);
00200
00201
00202 private: bool ParseTokenTuple( CProperty* property, int *index, int *line);
00203
00204
00205 private: void ClearMacros();
00206
00207
00208 private: void AddMacro(const char *macroname, const char *entityname,
00209 int line, int starttoken, int endtoken);
00210
00211
00212
00213
00214 class CMacro;
00215 private: CMacro* LookupMacro(const char *macroname);
00216
00217
00218 private: void DumpMacros();
00219
00220
00221 private: void ClearEntities();
00222
00223
00224 private: int AddEntity(int parent, const char *type);
00225
00226
00227 public: int GetEntityCount();
00228
00229
00230 public: const char *GetEntityType(int entity);
00231
00232
00233
00234 public: int LookupEntity(const char *type);
00235
00236
00237
00238 public: int GetEntityParent(int entity);
00239
00240
00241 private: void DumpEntities();
00242
00243
00244 private: void ClearProperties();
00245
00246
00247 private: CProperty* AddProperty(int entity, const char *name, int line);
00248
00249 private: void AddPropertyValue( CProperty* property, int index, int value_token);
00250
00251
00252 public: CProperty* GetProperty(int entity, const char *name);
00253
00254
00255
00256 bool PropertyExists( int section, const char* token );
00257
00258
00259 private: void SetPropertyValue( CProperty* property, int index, const char *value);
00260
00261
00262 public: const char *GetPropertyValue( CProperty* property, int index);
00263
00264
00265 private: void DumpProperties();
00266
00267
00268 private: enum
00269 {
00270 TokenComment,
00271 TokenWord, TokenNum, TokenString,
00272 TokenOpenEntity, TokenCloseEntity,
00273 TokenOpenTuple, TokenCloseTuple,
00274 TokenSpace, TokenEOL
00275 };
00276
00277
00278 private:
00279 class CToken
00280 {
00281 public:
00282
00283 int include;
00284
00285
00286 int type;
00287
00288
00289 std::string value;
00290
00291 CToken( int include, int type, const char* value ) :
00292 include(include), type(type), value(value) {}
00293 };
00294
00295
00296
00297
00298 private: std::vector<CToken> tokens;
00299
00300
00301 private:
00302 class CMacro
00303 {
00304 public:
00305
00306 std::string macroname;
00307
00308
00309 std::string entityname;
00310
00311
00312 int line;
00313
00314
00315 int starttoken, endtoken;
00316
00317 CMacro(const char *macroname, const char *entityname,
00318 int line, int starttoken, int endtoken) :
00319 macroname(macroname),
00320 entityname(entityname),
00321 line(line),
00322 starttoken(starttoken),
00323 endtoken(endtoken) {}
00324 };
00325
00326
00327 private: std::map<std::string,CMacro> macros;
00328
00329
00330 private:
00331 class CEntity
00332 {
00333 public:
00334
00335 int parent;
00336
00337
00338 std::string type;
00339
00340 CEntity( int parent, const char* type ) : parent(parent), type(type) {}
00341 };
00342
00343
00344 private: std::vector<CEntity> entities;
00345
00346
00347 private: std::map<std::string,CProperty*> properties;
00348
00349
00350 public: std::string filename;
00351
00352
00353 public: double unit_length;
00354 public: double unit_angle;
00355
00356 };
00357
00358 };
00359
00360 #endif