worldfile.hh
Go to the documentation of this file.
1 /*
2  * Stage : a multi-robot simulator.
3  * Copyright (C) 2001, 2002 Richard Vaughan, Andrew Howard and Brian Gerkey.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20 /*
21  * Desc: A class for reading in the world file.
22  * Author: Andrew Howard, Richard Vaughan
23  * Date: 15 Nov 2001
24  * CVS info: $Id$
25  */
26 
27 #ifndef WORLDFILE_HH
28 #define WORLDFILE_HH
29 
30 
31 #include <stdint.h> // for portable int types eg. uint32_t
32 #include <stdio.h> // for FILE ops
33 
34 namespace Stg {
35 
37  class CProperty
38  {
39  public:
41  int entity;
42 
44  std::string name;
45 
47  std::vector<int> values;
48 
50  int line;
51 
53  bool used;
54 
55  CProperty( int entity, const char* name, int line ) :
56  entity(entity),
57  name(name),
58  values(),
59  line(line),
60  used(false) {}
61  };
62 
63 
64  // Class for loading/saving world file. This class hides the syntax
65  // of the world file and provides an 'entity.property = value' style
66  // interface. Global settings go in entity 0; every other entity
67  // refers to a specific entity. Parent/child relationships are
68  // encoded in the form of entity/subentity relationships.
69  class Worldfile
70  {
71  // Standard constructors/destructors
72  public: Worldfile();
73  public: ~Worldfile();
74 
75  // replacement for fopen() that checks STAGEPATH dirs for the named file
76  // (thanks to Douglas S. Blank <dblank@brynmawr.edu>)
77  protected: FILE* FileOpen(const std::string& filename, const char* method);
78 
79  // Load world from file
80  public: bool Load(const std::string& filename );
81 
82  // Save world into named file
83  public: bool Save(const std::string& filename);
84 
85  // Check for unused properties and print warnings
86  public: bool WarnUnused();
87 
88  // Read a string
89  public: const std::string ReadString(int entity, const char* name, const std::string& value);
90 
91  // Write a string
92  public: void WriteString(int entity, const char *name, const std::string& value );
93 
94  // Read an integer
95  public: int ReadInt(int entity, const char *name, int value);
96 
97  // Write an integer
98  public: void WriteInt(int entity, const char *name, int value);
99 
100  // Read a float
101  public: double ReadFloat(int entity, const char *name, double value);
102 
103  // Write a float
104  public: void WriteFloat(int entity, const char *name, double value);
105 
106  // Read a length (includes unit conversion)
107  public: double ReadLength(int entity, const char *name, double value)
108  {
109  return (ReadFloat( entity, name, value/unit_length ) * unit_length);
110  }
111 
112  // Write a length (includes units conversion)
113  public: void WriteLength(int entity, const char *name, double value);
114 
115  // Read an angle (includes unit conversion)
116  public: double ReadAngle(int entity, const char *name, double value)
117  {
118  return (ReadFloat( entity, name, value/unit_angle ) * unit_angle);
119  }
120 
121  // Read a boolean
122  // REMOVE? public: bool ReadBool(int entity, const char *name, bool value);
123 
124  // Read a color (includes text to RGB conversion)
125  public: uint32_t ReadColor(int entity, const char *name, uint32_t value);
126 
127  // Read a file name. Always returns an absolute path. If the
128  // filename is entered as a relative path, we prepend the world
129  // files path to it.
130  public: const char *ReadFilename(int entity, const char *name, const char *value);
131 
132  // Read a series of values from a tuplee
133  public: int ReadTuple( const int entity, const char* name,
134  const unsigned int first, const unsigned int num, const char* format, ... );
135 
136  // Write a series of values to a tuple
137  public: void WriteTuple( const int entity, const char *name,
138  const unsigned int first, const unsigned int count, const char* format, ... );
139 
140 
142  // Private methods used to load stuff from the world file
143 
144  // Load tokens from a file.
145  private: bool LoadTokens(FILE *file, int include);
146 
147  // Read in a comment token
148  private: bool LoadTokenComment(FILE *file, int *line, int include);
149 
150  // Read in a word token
151  private: bool LoadTokenWord(FILE *file, int *line, int include);
152 
153  // Load an include token; this will load the include file.
154  private: bool LoadTokenInclude(FILE *file, int *line, int include);
155 
156  // Read in a number token
157  private: bool LoadTokenNum(FILE *file, int *line, int include);
158 
159  // Read in a string token
160  private: bool LoadTokenString(FILE *file, int *line, int include);
161 
162  // Read in a whitespace token
163  private: bool LoadTokenSpace(FILE *file, int *line, int include);
164 
165  // Save tokens to a file.
166  private: bool SaveTokens(FILE *file);
167 
168  // Clear the token list
169  private: void ClearTokens();
170 
171  // Add a token to the token list
172  private: bool AddToken(int type, const char *value, int include);
173 
174  // Set a token in the token list
175  private: bool SetTokenValue(int index, const char *value);
176 
177  // Get the value of a token
178  private: const char *GetTokenValue(int index);
179 
180  // Dump the token list (for debugging).
181  private: void DumpTokens();
182 
183  // Parse a line
184  private: bool ParseTokens();
185 
186  // Parse an include statement
187  private: bool ParseTokenInclude(int *index, int *line);
188 
189  // Parse a macro definition
190  private: bool ParseTokenDefine(int *index, int *line);
191 
192  // Parse an word (could be a entity or an property) from the token list.
193  private: bool ParseTokenWord(int entity, int *index, int *line);
194 
195  // Parse a entity from the token list.
196  private: bool ParseTokenEntity(int entity, int *index, int *line);
197 
198  // Parse an property from the token list.
199  private: bool ParseTokenProperty(int entity, int *index, int *line);
200 
201  // Parse a tuple.
202  private: bool ParseTokenTuple( CProperty* property, int *index, int *line);
203 
204  // Clear the macro list
205  private: void ClearMacros();
206 
207  // Add a macro
208  private: void AddMacro(const char *macroname, const char *entityname,
209  int line, int starttoken, int endtoken);
210 
211  // Lookup a macro by name
212 
213  // Returns a pointer to a macro with this name, or NULL if there is none..
214  class CMacro;
215  private: CMacro* LookupMacro(const char *macroname);
216 
217  // Dump the macro list for debugging
218  private: void DumpMacros();
219 
220  // Clear the entity list
221  private: void ClearEntities();
222 
223  // Add a entity
224  private: int AddEntity(int parent, const char *type);
225 
226  // Get the number of entities.
227  public: int GetEntityCount();
228 
229  // Get a entity (returns the entity type value)
230  public: const char *GetEntityType(int entity);
231 
232  // Lookup a entity number by type name
233  // Returns -1 if there is entity with this type
234  public: int LookupEntity(const char *type);
235 
236  // Get a entity's parent entity.
237  // Returns -1 if there is no parent.
238  public: int GetEntityParent(int entity);
239 
240  // Dump the entity list for debugging
241  private: void DumpEntities();
242 
243  // Clear the property list
244  private: void ClearProperties();
245 
246  // Add an property
247  private: CProperty* AddProperty(int entity, const char *name, int line);
248  // Add an property value.
249  private: void AddPropertyValue( CProperty* property, int index, int value_token);
250 
251  // Get an property
252  public: CProperty* GetProperty(int entity, const char *name);
253 
254  // returns true iff the property exists in the file, so that you can
255  // be sure that GetProperty() will work
256  bool PropertyExists( int section, const char* token );
257 
258  // Set the value of an property.
259  private: void SetPropertyValue( CProperty* property, int index, const char *value);
260 
261  // Get the value of an property.
262  public: const char *GetPropertyValue( CProperty* property, int index);
263 
264  // Dump the property list for debugging
265  private: void DumpProperties();
266 
267  // Token types.
268  private: enum
269  {
271  TokenWord, TokenNum, TokenString,
272  TokenOpenEntity, TokenCloseEntity,
273  TokenOpenTuple, TokenCloseTuple,
274  TokenSpace, TokenEOL
275  };
276 
277  // Token structure.
278  private:
279  class CToken
280  {
281  public:
282  // Non-zero if token is from an include file.
283  int include;
284 
285  // Token type (enumerated value).
286  int type;
287 
288  // Token value
289  std::string value;
290 
291  CToken( int include, int type, const char* value ) :
292  include(include), type(type), value(value) {}
293  };
294 
295  // A list of tokens loaded from the file.
296  // Modified values are written back into the token list.
297  //private: int token_size, token_count;
298  private: std::vector<CToken> tokens;
299 
300  // Private macro class
301  private:
302  class CMacro
303  {
304  public:
305  // Name of macro
306  std::string macroname;
307 
308  // Name of entity
309  std::string entityname;
310 
311  // Line the macro definition starts on.
312  int line;
313 
314  // Range of tokens in the body of the macro definition.
315  int starttoken, endtoken;
316 
317  CMacro(const char *macroname, const char *entityname,
318  int line, int starttoken, int endtoken) :
319  macroname(macroname),
320  entityname(entityname),
321  line(line),
322  starttoken(starttoken),
323  endtoken(endtoken) {}
324  };
325 
326  // Macro table
327  private: std::map<std::string,CMacro> macros;
328 
329  // Private entity class
330  private:
331  class CEntity
332  {
333  public:
334  // Parent entity
335  int parent;
336 
337  // Type of entity (i.e. position, laser, etc).
338  std::string type;
339 
340  CEntity( int parent, const char* type ) : parent(parent), type(type) {}
341  };
342 
343  // Entity list
344  private: std::vector<CEntity> entities;
345 
346  // Property list
347  private: std::map<std::string,CProperty*> properties;
348 
349  // Name of the file we loaded
350  public: std::string filename;
351 
352  // Conversion units
353  public: double unit_length;
354  public: double unit_angle;
355 
356  };
357 
358 };
359 
360 #endif
std::string name
Name of property.
Definition: worldfile.hh:44
std::map< std::string, CMacro > macros
Definition: worldfile.hh:327
bool used
Flag set if property has been used.
Definition: worldfile.hh:53
double unit_angle
Definition: worldfile.hh:354
The Stage library uses its own namespace.
Definition: canvas.hh:8
int line
Line this property came from.
Definition: worldfile.hh:50
CProperty(int entity, const char *name, int line)
Definition: worldfile.hh:55
std::string entityname
Definition: worldfile.hh:309
std::string filename
Definition: worldfile.hh:350
CMacro(const char *macroname, const char *entityname, int line, int starttoken, int endtoken)
Definition: worldfile.hh:317
int entity
Index of entity this property belongs to.
Definition: worldfile.hh:41
CToken(int include, int type, const char *value)
Definition: worldfile.hh:291
double ReadLength(int entity, const char *name, double value)
Definition: worldfile.hh:107
CEntity(int parent, const char *type)
Definition: worldfile.hh:340
std::string macroname
Definition: worldfile.hh:306
double unit_length
Definition: worldfile.hh:353
std::vector< CToken > tokens
Definition: worldfile.hh:298
std::vector< CEntity > entities
Definition: worldfile.hh:344
double ReadAngle(int entity, const char *name, double value)
Definition: worldfile.hh:116
Property class.
Definition: worldfile.hh:37
std::vector< int > values
A list of token indexes.
Definition: worldfile.hh:47
std::map< std::string, CProperty * > properties
Definition: worldfile.hh:347


stage
Author(s): Richard Vaughan , Brian Gerkey , Reed Hedges , Andrew Howard , Toby Collett , Pooya Karimian , Jeremy Asher , Alex Couture-Beil , Geoff Biggs , Rich Mattes , Abbas Sadat
autogenerated on Mon Jun 10 2019 15:06:10