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
00084 public: bool Save(const std::string& filename);
00085
00086
00087 public: bool WarnUnused();
00088
00089
00090 public: const std::string ReadString(int entity, const char* name, const std::string& value);
00091
00092
00093 public: void WriteString(int entity, const char *name, const std::string& value );
00094
00095
00096 public: int ReadInt(int entity, const char *name, int value);
00097
00098
00099 public: void WriteInt(int entity, const char *name, int value);
00100
00101
00102 public: double ReadFloat(int entity, const char *name, double value);
00103
00104
00105 public: void WriteFloat(int entity, const char *name, double value);
00106
00107
00108 public: double ReadLength(int entity, const char *name, double value);
00109
00110
00111 public: void WriteLength(int entity, const char *name, double value);
00112
00113
00114 public: double ReadAngle(int entity, const char *name, double value);
00115
00116
00117
00118
00119
00120 public: uint32_t ReadColor(int entity, const char *name, uint32_t value);
00121
00122
00123
00124
00125 public: const char *ReadFilename(int entity, const char *name, const char *value);
00126
00127
00128 public: const char *ReadTupleString(int entity, const char *name,
00129 int index, const char *value);
00130
00131
00132 public: void WriteTupleString(int entity, const char *name,
00133 int index, const char *value);
00134
00135
00136 public: double ReadTupleFloat(int entity, const char *name,
00137 int index, double value);
00138
00139
00140 public: void WriteTupleFloat(int entity, const char *name,
00141 int index, double value);
00142
00143
00144 public: double ReadTupleLength(int entity, const char *name,
00145 int index, double value);
00146
00147
00148 public: void WriteTupleLength(int entity, const char *name,
00149 int index, double value);
00150
00151
00152 public: double ReadTupleAngle(int entity, const char *name,
00153 int index, double value);
00154
00155
00156 public: void WriteTupleAngle(int entity, const char *name,
00157 int index, double value);
00158
00159
00161
00162
00163
00164 private: bool LoadTokens(FILE *file, int include);
00165
00166
00167 private: bool LoadTokenComment(FILE *file, int *line, int include);
00168
00169
00170 private: bool LoadTokenWord(FILE *file, int *line, int include);
00171
00172
00173 private: bool LoadTokenInclude(FILE *file, int *line, int include);
00174
00175
00176 private: bool LoadTokenNum(FILE *file, int *line, int include);
00177
00178
00179 private: bool LoadTokenString(FILE *file, int *line, int include);
00180
00181
00182 private: bool LoadTokenSpace(FILE *file, int *line, int include);
00183
00184
00185 private: bool SaveTokens(FILE *file);
00186
00187
00188 private: void ClearTokens();
00189
00190
00191 private: bool AddToken(int type, const char *value, int include);
00192
00193
00194 private: bool SetTokenValue(int index, const char *value);
00195
00196
00197 private: const char *GetTokenValue(int index);
00198
00199
00200 private: void DumpTokens();
00201
00202
00203 private: bool ParseTokens();
00204
00205
00206 private: bool ParseTokenInclude(int *index, int *line);
00207
00208
00209 private: bool ParseTokenDefine(int *index, int *line);
00210
00211
00212 private: bool ParseTokenWord(int entity, int *index, int *line);
00213
00214
00215 private: bool ParseTokenEntity(int entity, int *index, int *line);
00216
00217
00218 private: bool ParseTokenProperty(int entity, int *index, int *line);
00219
00220
00221 private: bool ParseTokenTuple( CProperty* property, int *index, int *line);
00222
00223
00224 private: void ClearMacros();
00225
00226
00227 private: void AddMacro(const char *macroname, const char *entityname,
00228 int line, int starttoken, int endtoken);
00229
00230
00231
00232
00233 class CMacro;
00234 private: CMacro* LookupMacro(const char *macroname);
00235
00236
00237 private: void DumpMacros();
00238
00239
00240 private: void ClearEntities();
00241
00242
00243 private: int AddEntity(int parent, const char *type);
00244
00245
00246 public: int GetEntityCount();
00247
00248
00249 public: const char *GetEntityType(int entity);
00250
00251
00252
00253 public: int LookupEntity(const char *type);
00254
00255
00256
00257 public: int GetEntityParent(int entity);
00258
00259
00260 private: void DumpEntities();
00261
00262
00263 private: void ClearProperties();
00264
00265
00266 private: CProperty* AddProperty(int entity, const char *name, int line);
00267
00268 private: void AddPropertyValue( CProperty* property, int index, int value_token);
00269
00270
00271 public: CProperty* GetProperty(int entity, const char *name);
00272
00273
00274
00275 bool PropertyExists( int section, const char* token );
00276
00277
00278 private: void SetPropertyValue( CProperty* property, int index, const char *value);
00279
00280
00281 public: const char *GetPropertyValue( CProperty* property, int index);
00282
00283
00284 private: void DumpProperties();
00285
00286
00287 private: enum
00288 {
00289 TokenComment,
00290 TokenWord, TokenNum, TokenString,
00291 TokenOpenEntity, TokenCloseEntity,
00292 TokenOpenTuple, TokenCloseTuple,
00293 TokenSpace, TokenEOL
00294 };
00295
00296
00297 private:
00298 class CToken
00299 {
00300 public:
00301
00302 int include;
00303
00304
00305 int type;
00306
00307
00308 std::string value;
00309
00310 CToken( int include, int type, const char* value ) :
00311 include(include), type(type), value(value) {}
00312 };
00313
00314
00315
00316
00317 private: std::vector<CToken> tokens;
00318
00319
00320 private:
00321 class CMacro
00322 {
00323 public:
00324
00325 std::string macroname;
00326
00327
00328 std::string entityname;
00329
00330
00331 int line;
00332
00333
00334 int starttoken, endtoken;
00335
00336 CMacro(const char *macroname, const char *entityname,
00337 int line, int starttoken, int endtoken) :
00338 macroname(macroname),
00339 entityname(entityname),
00340 line(line),
00341 starttoken(starttoken),
00342 endtoken(endtoken) {}
00343 };
00344
00345
00346 private: std::map<std::string,CMacro> macros;
00347
00348
00349 private:
00350 class CEntity
00351 {
00352 public:
00353
00354 int parent;
00355
00356
00357 std::string type;
00358
00359 CEntity( int parent, const char* type ) : parent(parent), type(type) {}
00360 };
00361
00362
00363 private: std::vector<CEntity> entities;
00364
00365
00366 private: std::map<std::string,CProperty*> properties;
00367
00368
00369 public: std::string filename;
00370
00371
00372 public: double unit_length;
00373 public: double unit_angle;
00374
00375 };
00376
00377 };
00378
00379 #endif