worldfile.hh
Go to the documentation of this file.00001 /* 00002 * Stage : a multi-robot simulator. 00003 * Copyright (C) 2001, 2002 Richard Vaughan, Andrew Howard and Brian Gerkey. 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 * 00019 */ 00020 /* 00021 * Desc: A class for reading in the world file. 00022 * Author: Andrew Howard 00023 * Date: 15 Nov 2001 00024 * CVS info: $Id: worldfile.hh 8070 2009-07-22 06:41:10Z rtv $ 00025 */ 00026 00027 #ifndef WORLDFILE_HH 00028 #define WORLDFILE_HH 00029 00030 00031 #include <stdint.h> // for portable int types eg. uint32_t 00032 #include <stdio.h> // for FILE ops 00033 00034 namespace Stg { 00035 00036 // Private property class 00037 struct CProperty 00038 { 00039 // Index of entity this property belongs to 00040 int entity; 00041 00042 // Name of property 00043 const char *name; 00044 00045 char* key; // this property's hash table key 00046 00047 // A list of token indexes 00048 int value_count; 00049 int *values; 00050 00051 // Line this property came from 00052 int line; 00053 00054 // Flag set if property has been used 00055 bool used; 00056 }; 00057 00058 00059 // Class for loading/saving world file. This class hides the syntax 00060 // of the world file and provides an 'entity.property = value' style 00061 // interface. Global settings go in entity 0; every other entity 00062 // refers to a specific entity. Parent/child relationships are 00063 // encoded in the form of entity/subentity relationships. 00064 class Worldfile 00065 { 00066 // Standard constructors/destructors 00067 public: Worldfile(); 00068 public: ~Worldfile(); 00069 00070 // replacement for fopen() that checks STAGEPATH dirs for the named file 00071 // (thanks to Douglas S. Blank <dblank@brynmawr.edu>) 00072 protected: FILE* FileOpen(const char *filename, const char* method); 00073 00074 // Load world from file 00075 public: bool Load(const char *filename); 00076 00077 // Save world back into file 00078 // Set filename to NULL to save back into the original file 00079 public: bool Save(const char *filename); 00080 00081 // Check for unused properties and print warnings 00082 public: bool WarnUnused(); 00083 00084 // Read a string 00085 public: const char *ReadString(int entity, const char *name, const char *value); 00086 00087 // Write a string 00088 public: void WriteString(int entity, const char *name, const char *value); 00089 00090 // Read an integer 00091 public: int ReadInt(int entity, const char *name, int value); 00092 00093 // Write an integer 00094 public: void WriteInt(int entity, const char *name, int value); 00095 00096 // Read a float 00097 public: double ReadFloat(int entity, const char *name, double value); 00098 00099 // Write a float 00100 public: void WriteFloat(int entity, const char *name, double value); 00101 00102 // Read a length (includes unit conversion) 00103 public: double ReadLength(int entity, const char *name, double value); 00104 00105 // Write a length (includes units conversion) 00106 public: void WriteLength(int entity, const char *name, double value); 00107 00108 // Read an angle (includes unit conversion) 00109 public: double ReadAngle(int entity, const char *name, double value); 00110 00111 // Read a boolean 00112 // REMOVE? public: bool ReadBool(int entity, const char *name, bool value); 00113 00114 // Read a color (includes text to RGB conversion) 00115 public: uint32_t ReadColor(int entity, const char *name, uint32_t value); 00116 00117 // Read a file name. Always returns an absolute path. If the 00118 // filename is entered as a relative path, we prepend the world 00119 // files path to it. 00120 public: const char *ReadFilename(int entity, const char *name, const char *value); 00121 00122 // Read a string from a tuple 00123 public: const char *ReadTupleString(int entity, const char *name, 00124 int index, const char *value); 00125 00126 // Write a string to a tuple 00127 public: void WriteTupleString(int entity, const char *name, 00128 int index, const char *value); 00129 00130 // Read a float from a tuple 00131 public: double ReadTupleFloat(int entity, const char *name, 00132 int index, double value); 00133 00134 // Write a float to a tuple 00135 public: void WriteTupleFloat(int entity, const char *name, 00136 int index, double value); 00137 00138 // Read a length from a tuple (includes units conversion) 00139 public: double ReadTupleLength(int entity, const char *name, 00140 int index, double value); 00141 00142 // Write a to a tuple length (includes units conversion) 00143 public: void WriteTupleLength(int entity, const char *name, 00144 int index, double value); 00145 00146 // Read an angle form a tuple (includes units conversion) 00147 public: double ReadTupleAngle(int entity, const char *name, 00148 int index, double value); 00149 00150 // Write an angle to a tuple (includes units conversion) 00151 public: void WriteTupleAngle(int entity, const char *name, 00152 int index, double value); 00153 00154 00156 // Private methods used to load stuff from the world file 00157 00158 // Load tokens from a file. 00159 private: bool LoadTokens(FILE *file, int include); 00160 00161 // Read in a comment token 00162 private: bool LoadTokenComment(FILE *file, int *line, int include); 00163 00164 // Read in a word token 00165 private: bool LoadTokenWord(FILE *file, int *line, int include); 00166 00167 // Load an include token; this will load the include file. 00168 private: bool LoadTokenInclude(FILE *file, int *line, int include); 00169 00170 // Read in a number token 00171 private: bool LoadTokenNum(FILE *file, int *line, int include); 00172 00173 // Read in a string token 00174 private: bool LoadTokenString(FILE *file, int *line, int include); 00175 00176 // Read in a whitespace token 00177 private: bool LoadTokenSpace(FILE *file, int *line, int include); 00178 00179 // Save tokens to a file. 00180 private: bool SaveTokens(FILE *file); 00181 00182 // Clear the token list 00183 private: void ClearTokens(); 00184 00185 // Add a token to the token list 00186 private: bool AddToken(int type, const char *value, int include); 00187 00188 // Set a token in the token list 00189 private: bool SetTokenValue(int index, const char *value); 00190 00191 // Get the value of a token 00192 private: const char *GetTokenValue(int index); 00193 00194 // Dump the token list (for debugging). 00195 private: void DumpTokens(); 00196 00197 // Parse a line 00198 private: bool ParseTokens(); 00199 00200 // Parse an include statement 00201 private: bool ParseTokenInclude(int *index, int *line); 00202 00203 // Parse a macro definition 00204 private: bool ParseTokenDefine(int *index, int *line); 00205 00206 // Parse an word (could be a entity or an property) from the token list. 00207 private: bool ParseTokenWord(int entity, int *index, int *line); 00208 00209 // Parse a entity from the token list. 00210 private: bool ParseTokenEntity(int entity, int *index, int *line); 00211 00212 // Parse an property from the token list. 00213 private: bool ParseTokenProperty(int entity, int *index, int *line); 00214 00215 // Parse a tuple. 00216 private: bool ParseTokenTuple( CProperty* property, int *index, int *line); 00217 00218 // Clear the macro list 00219 private: void ClearMacros(); 00220 00221 // Add a macro 00222 private: int AddMacro(const char *macroname, const char *entityname, 00223 int line, int starttoken, int endtoken); 00224 00225 // Lookup a macro by name 00226 // Returns -1 if there is no macro with this name. 00227 private: int LookupMacro(const char *macroname); 00228 00229 // Dump the macro list for debugging 00230 private: void DumpMacros(); 00231 00232 // Clear the entity list 00233 private: void ClearEntities(); 00234 00235 // Add a entity 00236 private: int AddEntity(int parent, const char *type); 00237 00238 // Get the number of entities. 00239 public: int GetEntityCount(); 00240 00241 // Get a entity (returns the entity type value) 00242 public: const char *GetEntityType(int entity); 00243 00244 // Lookup a entity number by type name 00245 // Returns -1 if there is entity with this type 00246 public: int LookupEntity(const char *type); 00247 00248 // Get a entity's parent entity. 00249 // Returns -1 if there is no parent. 00250 public: int GetEntityParent(int entity); 00251 00252 // Dump the entity list for debugging 00253 private: void DumpEntities(); 00254 00255 // Clear the property list 00256 private: void ClearProperties(); 00257 00258 // Add an property 00259 private: CProperty* AddProperty(int entity, const char *name, int line); 00260 // Add an property value. 00261 private: void AddPropertyValue( CProperty* property, int index, int value_token); 00262 00263 // Get an property 00264 public: CProperty* GetProperty(int entity, const char *name); 00265 00266 // returns true iff the property exists in the file, so that you can 00267 // be sure that GetProperty() will work 00268 bool PropertyExists( int section, const char* token ); 00269 00270 // Set the value of an property. 00271 private: void SetPropertyValue( CProperty* property, int index, const char *value); 00272 00273 // Get the value of an property. 00274 private: const char *GetPropertyValue( CProperty* property, int index); 00275 00276 // Dump the property list for debugging 00277 private: void DumpProperties(); 00278 00279 // Token types. 00280 private: enum 00281 { 00282 TokenComment, 00283 TokenWord, TokenNum, TokenString, 00284 TokenOpenEntity, TokenCloseEntity, 00285 TokenOpenTuple, TokenCloseTuple, 00286 TokenSpace, TokenEOL 00287 }; 00288 00289 // Token structure. 00290 private: struct CToken 00291 { 00292 // Non-zero if token is from an include file. 00293 int include; 00294 00295 // Token type (enumerated value). 00296 int type; 00297 00298 // Token value 00299 char *value; 00300 }; 00301 00302 // A list of tokens loaded from the file. 00303 // Modified values are written back into the token list. 00304 private: int token_size, token_count; 00305 private: CToken *tokens; 00306 00307 // Private macro class 00308 private: struct CMacro 00309 { 00310 // Name of macro 00311 const char *macroname; 00312 00313 // Name of entity 00314 const char *entityname; 00315 00316 // Line the macro definition starts on. 00317 int line; 00318 00319 // Range of tokens in the body of the macro definition. 00320 int starttoken, endtoken; 00321 }; 00322 00323 // Macro list 00324 private: int macro_size; 00325 private: int macro_count; 00326 private: CMacro *macros; 00327 00328 // Private entity class 00329 private: struct CEntity 00330 { 00331 // Parent entity 00332 int parent; 00333 00334 // Type of entity (i.e. position, laser, etc). 00335 const char *type; 00336 }; 00337 00338 // Entity list 00339 private: int entity_size; 00340 private: int entity_count; 00341 private: CEntity *entities; 00342 00343 // Property list 00344 private: int property_count; 00345 00346 private: CProperty *properties; 00347 00348 // Name of the file we loaded 00349 public: char *filename; 00350 00351 // Conversion units 00352 private: double unit_length; 00353 private: double unit_angle; 00354 00355 private: std::map<std::string,CProperty*> nametable; 00356 }; 00357 00358 }; 00359 00360 #endif
Generated on Wed Jul 22 11:51:03 2009 for Stage by
