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,v 1.2 2008-01-15 01:16:49 rtv Exp $
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 #include <glib.h>
00034 
00035   // Private property class
00036 struct CProperty
00037   {
00038     // Index of entity this property belongs to
00039     int entity;
00040 
00041     // Name of property
00042     const char *name;
00043 
00044     char* key; // this property's hash table key
00045     
00046     // A list of token indexes
00047     int value_count;
00048     int *values;
00049 
00050     // Line this property came from
00051     int line;
00052 
00053     // Flag set if property has been used
00054     bool used;
00055   };
00056 
00057 
00058 // Class for loading/saving world file.  This class hides the syntax
00059 // of the world file and provides an 'entity.property = value' style
00060 // interface.  Global settings go in entity 0; every other entity
00061 // refers to a specific entity.  Parent/child relationships are
00062 // encoded in the form of entity/subentity relationships.
00063 class Stg::Worldfile
00064 {
00065   // Standard constructors/destructors
00066 public: Worldfile();
00067 public: ~Worldfile();
00068 
00069   // replacement for fopen() that checks STAGEPATH dirs for the named file
00070   // (thanks to  Douglas S. Blank <dblank@brynmawr.edu>)
00071 protected: FILE* FileOpen(const char *filename, const char* method);
00072 
00073   // Load world from file
00074   public: bool Load(const char *filename);
00075 
00076   // Save world back into file
00077   // Set filename to NULL to save back into the original file
00078   public: bool Save(const char *filename);
00079 
00080   // Check for unused properties and print warnings
00081   public: bool WarnUnused();
00082 
00083   // Read a string
00084   public: const char *ReadString(int entity, const char *name, const char *value);
00085 
00086   // Write a string
00087   public: void WriteString(int entity, const char *name, const char *value);
00088 
00089   // Read an integer 
00090   public: int ReadInt(int entity, const char *name, int value);
00091 
00092   // Write an integer
00093   public: void WriteInt(int entity, const char *name, int value);
00094 
00095   // Read a float 
00096   public: double ReadFloat(int entity, const char *name, double value);
00097 
00098   // Write a float
00099   public: void WriteFloat(int entity, const char *name, double value);
00100 
00101   // Read a length (includes unit conversion)
00102   public: double ReadLength(int entity, const char *name, double value);
00103 
00104   // Write a length (includes units conversion)
00105   public: void WriteLength(int entity, const char *name, double value);
00106   
00107   // Read an angle (includes unit conversion)
00108   public: double ReadAngle(int entity, const char *name, double value);
00109 
00110   // Read a boolean
00111   // REMOVE? public: bool ReadBool(int entity, const char *name, bool value);
00112 
00113   // Read a color (includes text to RGB conversion)
00114   public: uint32_t ReadColor(int entity, const char *name, uint32_t value);
00115 
00116   // Read a file name.  Always returns an absolute path.  If the
00117   // filename is entered as a relative path, we prepend the world
00118   // files path to it.
00119   public: const char *ReadFilename(int entity, const char *name, const char *value);
00120   
00121   // Read a string from a tuple
00122   public: const char *ReadTupleString(int entity, const char *name,
00123                                       int index, const char *value);
00124   
00125   // Write a string to a tuple
00126   public: void WriteTupleString(int entity, const char *name,
00127                                 int index, const char *value);
00128   
00129   // Read a float from a tuple
00130   public: double ReadTupleFloat(int entity, const char *name,
00131                                 int index, double value);
00132 
00133   // Write a float to a tuple
00134   public: void WriteTupleFloat(int entity, const char *name,
00135                                int index, double value);
00136 
00137   // Read a length from a tuple (includes units conversion)
00138   public: double ReadTupleLength(int entity, const char *name,
00139                                  int index, double value);
00140 
00141   // Write a to a tuple length (includes units conversion)
00142   public: void WriteTupleLength(int entity, const char *name,
00143                                 int index, double value);
00144 
00145   // Read an angle form a tuple (includes units conversion)
00146   public: double ReadTupleAngle(int entity, const char *name,
00147                                 int index, double value);
00148 
00149   // Write an angle to a tuple (includes units conversion)
00150   public: void WriteTupleAngle(int entity, const char *name,
00151                                int index, double value);
00152 
00153 
00155   // Private methods used to load stuff from the world file
00156   
00157   // Load tokens from a file.
00158   private: bool LoadTokens(FILE *file, int include);
00159 
00160   // Read in a comment token
00161   private: bool LoadTokenComment(FILE *file, int *line, int include);
00162 
00163   // Read in a word token
00164   private: bool LoadTokenWord(FILE *file, int *line, int include);
00165 
00166   // Load an include token; this will load the include file.
00167   private: bool LoadTokenInclude(FILE *file, int *line, int include);
00168 
00169   // Read in a number token
00170   private: bool LoadTokenNum(FILE *file, int *line, int include);
00171 
00172   // Read in a string token
00173   private: bool LoadTokenString(FILE *file, int *line, int include);
00174 
00175   // Read in a whitespace token
00176   private: bool LoadTokenSpace(FILE *file, int *line, int include);
00177 
00178   // Save tokens to a file.
00179   private: bool SaveTokens(FILE *file);
00180 
00181   // Clear the token list
00182   private: void ClearTokens();
00183 
00184   // Add a token to the token list
00185   private: bool AddToken(int type, const char *value, int include);
00186 
00187   // Set a token in the token list
00188   private: bool SetTokenValue(int index, const char *value);
00189 
00190   // Get the value of a token
00191   private: const char *GetTokenValue(int index);
00192 
00193   // Dump the token list (for debugging).
00194   private: void DumpTokens();
00195 
00196   // Parse a line
00197   private: bool ParseTokens();
00198 
00199   // Parse an include statement
00200   private: bool ParseTokenInclude(int *index, int *line);
00201 
00202   // Parse a macro definition
00203   private: bool ParseTokenDefine(int *index, int *line);
00204 
00205   // Parse an word (could be a entity or an property) from the token list.
00206   private: bool ParseTokenWord(int entity, int *index, int *line);
00207 
00208   // Parse a entity from the token list.
00209   private: bool ParseTokenEntity(int entity, int *index, int *line);
00210 
00211   // Parse an property from the token list.
00212   private: bool ParseTokenProperty(int entity, int *index, int *line);
00213 
00214   // Parse a tuple.
00215   private: bool ParseTokenTuple( CProperty*  property, int *index, int *line);
00216 
00217   // Clear the macro list
00218   private: void ClearMacros();
00219 
00220   // Add a macro
00221   private: int AddMacro(const char *macroname, const char *entityname,
00222                         int line, int starttoken, int endtoken);
00223 
00224   // Lookup a macro by name
00225   // Returns -1 if there is no macro with this name.
00226   private: int LookupMacro(const char *macroname);
00227 
00228   // Dump the macro list for debugging
00229   private: void DumpMacros();
00230 
00231   // Clear the entity list
00232   private: void ClearEntities();
00233 
00234   // Add a entity
00235   private: int AddEntity(int parent, const char *type);
00236 
00237     // Get the number of entities.
00238   public: int GetEntityCount();
00239 
00240   // Get a entity (returns the entity type value)
00241   public: const char *GetEntityType(int entity);
00242 
00243   // Lookup a entity number by type name
00244   // Returns -1 if there is entity with this type
00245   public: int LookupEntity(const char *type);
00246   
00247   // Get a entity's parent entity.
00248   // Returns -1 if there is no parent.
00249   public: int GetEntityParent(int entity);
00250 
00251   // Dump the entity list for debugging
00252   private: void DumpEntities();
00253 
00254   // Clear the property list
00255   private: void ClearProperties();
00256 
00257   // Add an property
00258   private: CProperty* AddProperty(int entity, const char *name, int line);
00259   // Add an property value.
00260   private: void AddPropertyValue( CProperty* property, int index, int value_token);
00261   
00262   // Get an property
00263   public: CProperty* GetProperty(int entity, const char *name);
00264 
00265   // returns true iff the property exists in the file, so that you can
00266   // be sure that GetProperty() will work
00267   bool PropertyExists( int section, char* token );
00268 
00269   // Set the value of an property.
00270   private: void SetPropertyValue( CProperty* property, int index, const char *value);
00271 
00272   // Get the value of an property.
00273   private: const char *GetPropertyValue( CProperty* property, int index);
00274 
00275   // Dump the property list for debugging
00276   private: void DumpProperties();
00277 
00278   // Token types.
00279   private: enum
00280   {
00281     TokenComment,
00282     TokenWord, TokenNum, TokenString,
00283     TokenOpenEntity, TokenCloseEntity,
00284     TokenOpenTuple, TokenCloseTuple,
00285     TokenSpace, TokenEOL
00286   };
00287 
00288   // Token structure.
00289   private: struct CToken
00290   {
00291     // Non-zero if token is from an include file.
00292     int include;
00293     
00294     // Token type (enumerated value).
00295     int type;
00296 
00297     // Token value
00298     char *value;
00299   };
00300 
00301   // A list of tokens loaded from the file.
00302   // Modified values are written back into the token list.
00303   private: int token_size, token_count;
00304   private: CToken *tokens;
00305 
00306   // Private macro class
00307   private: struct CMacro
00308   {
00309     // Name of macro
00310     const char *macroname;
00311 
00312     // Name of entity
00313     const char *entityname;
00314 
00315     // Line the macro definition starts on.
00316     int line;
00317     
00318     // Range of tokens in the body of the macro definition.
00319     int starttoken, endtoken;
00320   };
00321 
00322   // Macro list
00323   private: int macro_size;
00324   private: int macro_count;
00325   private: CMacro *macros;
00326   
00327   // Private entity class
00328   private: struct CEntity
00329   {
00330     // Parent entity
00331     int parent;
00332 
00333     // Type of entity (i.e. position, laser, etc).
00334     const char *type;
00335   };
00336 
00337   // Entity list
00338   private: int entity_size;
00339   private: int entity_count;
00340   private: CEntity *entities;
00341 
00342   
00343   // Property list
00344   //private: int property_size;
00345   private: int property_count;
00346   //private: CProperty *properties;
00347   
00348   private: GHashTable* nametable;
00349   
00350   // Name of the file we loaded
00351   public: char *filename;
00352 
00353   // Conversion units
00354   private: double unit_length;
00355   private: double unit_angle;
00356 };
00357 
00358 #endif

Generated on Thu Jan 7 17:42:16 2010 for Stage by  doxygen 1.5.5