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 #include <cstdio> // for FILE ops
31 #include <istream>
32 #include <map>
33 #include <stdint.h> // for portable int types eg. uint32_t
34 #include <vector>
35 
36 namespace Stg {
37 
39 class CProperty {
40 public:
42  int entity;
43 
45  std::string name;
46 
48  std::vector<int> values;
49 
51  int line;
52 
54  bool used;
55 
56  CProperty(int entity, const char *name, int line)
57  : entity(entity), name(name), values(), line(line), used(false)
58  {
59  }
60 };
61 
62 // Class for loading/saving world file. This class hides the syntax
63 // of the world file and provides an 'entity.property = value' style
64 // interface. Global settings go in entity 0; every other entity
65 // refers to a specific entity. Parent/child relationships are
66 // encoded in the form of entity/subentity relationships.
67 class Worldfile {
68  // Standard constructors/destructors
69 public:
70  Worldfile();
71 
72 public:
73  ~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:
78  FILE *FileOpen(const std::string &filename, const char *method);
79 
80  // Load world from file
81 public:
82  bool Load(const std::string &filename);
83 
84 protected:
85  bool LoadCommon();
86 
87 public:
88  bool Load(std::istream &world_content, const std::string &filename = std::string());
89 
90  // Save world into named file
91 public:
92  bool Save(const std::string &filename);
93 
94  // Check for unused properties and print warnings
95 public:
96  bool WarnUnused();
97 
98  // Read a string
99 public:
100  const std::string ReadString(int entity, const char *name, const std::string &value);
101 
102  // Write a string
103 public:
104  void WriteString(int entity, const char *name, const std::string &value);
105 
106  // Read an integer
107 public:
108  int ReadInt(int entity, const char *name, int value);
109 
110  // Write an integer
111 public:
112  void WriteInt(int entity, const char *name, int value);
113 
114  // Read a float
115 public:
116  double ReadFloat(int entity, const char *name, double value);
117 
118  // Write a float
119 public:
120  void WriteFloat(int entity, const char *name, double value);
121 
122  // Read a length (includes unit conversion)
123 public:
124  double ReadLength(int entity, const char *name, double value)
125  {
126  return (ReadFloat(entity, name, value / unit_length) * unit_length);
127  }
128 
129  // Write a length (includes units conversion)
130 public:
131  void WriteLength(int entity, const char *name, double value);
132 
133  // Read an angle (includes unit conversion)
134 public:
135  double ReadAngle(int entity, const char *name, double value)
136  {
137  return (ReadFloat(entity, name, value / unit_angle) * unit_angle);
138  }
139 
140  // Read a boolean
141  // REMOVE? public: bool ReadBool(int entity, const char *name, bool value);
142 
143  // Read a color (includes text to RGB conversion)
144 public:
145  uint32_t ReadColor(int entity, const char *name, uint32_t value);
146 
147  // Read a file name. Always returns an absolute path. If the
148  // filename is entered as a relative path, we prepend the world
149  // files path to it.
150 public:
151  const char *ReadFilename(int entity, const char *name, const char *value);
152 
153  // Read a series of values from a tuplee
154 public:
155  int ReadTuple(const int entity, const char *name, const unsigned int first,
156  const unsigned int num, const char *format, ...);
157 
158  // Write a series of values to a tuple
159 public:
160  void WriteTuple(const int entity, const char *name, const unsigned int first,
161  const unsigned int count, const char *format, ...);
162 
164  // Private methods used to load stuff from the world file
165 
166  // Load tokens from a file.
167 private:
168  bool LoadTokens(FILE *file, int include);
169  // Load tokens from a stream.
170 private:
171  bool LoadTokens(std::istream &content, int include);
172 
173  // Read in a comment token
174 private:
175  bool LoadTokenComment(FILE *file, int *line, int include);
176  // Read in a comment token (stream version)
177 private:
178  bool LoadTokenComment(std::istream &content, int *line, int include);
179 
180  // Read in a word token
181 private:
182  bool LoadTokenWord(FILE *file, int *line, int include);
183  // Read in a word token (stream version)
184 private:
185  bool LoadTokenWord(std::istream &content, int *line, int include);
186 
187  // Load an include token; this will load the include file.
188 private:
189  bool LoadTokenInclude(FILE *file, int *line, int include);
190  // Load an include token; this will load the include file (stream version)
191 private:
192  bool LoadTokenInclude(std::istream &content, int *line, int include);
193 
194  // Read in a number token
195 private:
196  bool LoadTokenNum(FILE *file, int *line, int include);
197  // Read in a number token (stream version)
198 private:
199  bool LoadTokenNum(std::istream &content, int *line, int include);
200 
201  // Read in a string token
202 private:
203  bool LoadTokenString(FILE *file, int *line, int include);
204  // Read in a string token (stream version)
205 private:
206  bool LoadTokenString(std::istream &content, int *line, int include);
207 
208  // Read in a whitespace token
209 private:
210  bool LoadTokenSpace(FILE *file, int *line, int include);
211  // Read in a whitespace token (stream version)
212 private:
213  bool LoadTokenSpace(std::istream &content, int *line, int include);
214 
215  // Save tokens to a file.
216 private:
217  bool SaveTokens(FILE *file);
218 
219  // Clear the token list
220 private:
221  void ClearTokens();
222 
223  // Add a token to the token list
224 private:
225  bool AddToken(int type, const char *value, int include);
226 
227  // Set a token in the token list
228 private:
229  bool SetTokenValue(int index, const char *value);
230 
231  // Get the value of a token
232 private:
233  const char *GetTokenValue(int index);
234 
235  // Dump the token list (for debugging).
236 private:
237  void DumpTokens();
238 
239  // Parse a line
240 private:
241  bool ParseTokens();
242 
243  // Parse an include statement
244 private:
245  bool ParseTokenInclude(int *index, int *line);
246 
247  // Parse a macro definition
248 private:
249  bool ParseTokenDefine(int *index, int *line);
250 
251  // Parse an word (could be a entity or an property) from the token list.
252 private:
253  bool ParseTokenWord(int entity, int *index, int *line);
254 
255  // Parse a entity from the token list.
256 private:
257  bool ParseTokenEntity(int entity, int *index, int *line);
258 
259  // Parse an property from the token list.
260 private:
261  bool ParseTokenProperty(int entity, int *index, int *line);
262 
263  // Parse a tuple.
264 private:
265  bool ParseTokenTuple(CProperty *property, int *index, int *line);
266 
267  // Clear the macro list
268 private:
269  void ClearMacros();
270 
271  // Add a macro
272 private:
273  void AddMacro(const char *macroname, const char *entityname, int line, int starttoken,
274  int endtoken);
275 
276  // Lookup a macro by name
277 
278  // Returns a pointer to a macro with this name, or NULL if there is none..
279  class CMacro;
280 
281 private:
282  CMacro *LookupMacro(const char *macroname);
283 
284  // Dump the macro list for debugging
285 private:
286  void DumpMacros();
287 
288  // Clear the entity list
289 private:
290  void ClearEntities();
291 
292  // Add a entity
293 private:
294  int AddEntity(int parent, const char *type);
295 
296  // Get the number of entities.
297 public:
298  int GetEntityCount();
299 
300  // Get a entity (returns the entity type value)
301 public:
302  const char *GetEntityType(int entity);
303 
304  // Lookup a entity number by type name
305  // Returns -1 if there is entity with this type
306 public:
307  int LookupEntity(const char *type);
308 
309  // Get a entity's parent entity.
310  // Returns -1 if there is no parent.
311 public:
312  int GetEntityParent(int entity);
313 
314  // Dump the entity list for debugging
315 private:
316  void DumpEntities();
317 
318  // Clear the property list
319 private:
320  void ClearProperties();
321 
322  // Add an property
323 private:
324  CProperty *AddProperty(int entity, const char *name, int line);
325  // Add an property value.
326 private:
327  void AddPropertyValue(CProperty *property, int index, int value_token);
328 
329  // Get an property
330 public:
331  CProperty *GetProperty(int entity, const char *name);
332 
333  // returns true iff the property exists in the file, so that you can
334  // be sure that GetProperty() will work
335  bool PropertyExists(int section, const char *token);
336 
337  // Set the value of an property.
338 private:
339  void SetPropertyValue(CProperty *property, int index, const char *value);
340 
341  // Get the value of an property.
342 public:
343  const char *GetPropertyValue(CProperty *property, int index);
344 
345  // Dump the property list for debugging
346 private:
347  void DumpProperties();
348 
349  // Token types.
350 private:
351  enum {
352  TokenComment,
353  TokenWord,
354  TokenNum,
355  TokenString,
356  TokenOpenEntity,
357  TokenCloseEntity,
358  TokenOpenTuple,
359  TokenCloseTuple,
360  TokenSpace,
361  TokenEOL
362  };
363 
364  // Token structure.
365 private:
366  class CToken {
367  public:
368  // Non-zero if token is from an include file.
369  int include;
370 
371  // Token type (enumerated value).
372  int type;
373 
374  // Token value
375  std::string value;
376 
377  CToken(int include, int type, const char *value) : include(include), type(type), value(value) {}
378  };
379 
380  // A list of tokens loaded from the file.
381  // Modified values are written back into the token list.
382  // private: int token_size, token_count;
383 private:
384  std::vector<CToken> tokens;
385 
386  // Private macro class
387 private:
388  class CMacro {
389  public:
390  // Name of macro
391  std::string macroname;
392 
393  // Name of entity
394  std::string entityname;
395 
396  // Line the macro definition starts on.
397  int line;
398 
399  // Range of tokens in the body of the macro definition.
400  int starttoken, endtoken;
401 
402  CMacro(const char *macroname, const char *entityname, int line, int starttoken, int endtoken)
403  : macroname(macroname), entityname(entityname), line(line), starttoken(starttoken),
404  endtoken(endtoken)
405  {
406  }
407  };
408 
409  // Macro table
410 private:
411  std::map<std::string, CMacro> macros;
412 
413  // Private entity class
414 private:
415  class CEntity {
416  public:
417  // Parent entity
418  int parent;
419 
420  // Type of entity (i.e. position, laser, etc).
421  std::string type;
422 
423  CEntity(int parent, const char *type) : parent(parent), type(type) {}
424  };
425 
426  // Entity list
427 private:
428  std::vector<CEntity> entities;
429 
430  // Property list
431 private:
432  std::map<std::string, CProperty *> properties;
433 
434  // Name of the file we loaded
435 public:
436  std::string filename;
437 
438  // Conversion units
439 public:
440  double unit_length;
441 
442 public:
443  double unit_angle;
444 };
445 }
446 
447 #endif
Property class.
Definition: worldfile.hh:39
std::string name
Name of property.
Definition: worldfile.hh:45
int entity
Index of entity this property belongs to.
Definition: worldfile.hh:42
bool used
Flag set if property has been used.
Definition: worldfile.hh:54
std::vector< int > values
A list of token indexes.
Definition: worldfile.hh:48
int line
Line this property came from.
Definition: worldfile.hh:51
CProperty(int entity, const char *name, int line)
Definition: worldfile.hh:56
Definition: worldfile.hh:67
const char * GetEntityType(int entity)
Definition: worldfile.cc:1299
std::string filename
Definition: worldfile.hh:436
CProperty * GetProperty(int entity, const char *name)
Definition: worldfile.cc:1375
int GetEntityCount()
Definition: worldfile.cc:1283
int ReadTuple(const int entity, const char *name, const unsigned int first, const unsigned int num, const char *format,...)
Definition: worldfile.cc:1572
bool WarnUnused()
Definition: worldfile.cc:223
double ReadLength(int entity, const char *name, double value)
Definition: worldfile.hh:124
bool Load(const std::string &filename)
Definition: worldfile.cc:131
const char * GetPropertyValue(CProperty *property, int index)
Definition: worldfile.cc:1421
bool LoadCommon()
Definition: worldfile.cc:155
void WriteString(int entity, const char *name, const std::string &value)
Definition: worldfile.cc:1460
void WriteTuple(const int entity, const char *name, const unsigned int first, const unsigned int count, const char *format,...)
Definition: worldfile.cc:1635
const std::string ReadString(int entity, const char *name, const std::string &value)
Definition: worldfile.cc:1450
void WriteInt(int entity, const char *name, int value)
Definition: worldfile.cc:1480
Worldfile()
Definition: worldfile.cc:61
int ReadInt(int entity, const char *name, int value)
Definition: worldfile.cc:1470
int GetEntityParent(int entity)
Definition: worldfile.cc:1290
void WriteLength(int entity, const char *name, double value)
int LookupEntity(const char *type)
Definition: worldfile.cc:1309
const char * ReadFilename(int entity, const char *name, const char *value)
Definition: worldfile.cc:1518
double unit_length
Definition: worldfile.hh:440
double unit_angle
Definition: worldfile.hh:443
uint32_t ReadColor(int entity, const char *name, uint32_t value)
~Worldfile()
Definition: worldfile.cc:69
double ReadFloat(int entity, const char *name, double value)
Definition: worldfile.cc:1503
FILE * FileOpen(const std::string &filename, const char *method)
Definition: worldfile.cc:77
bool PropertyExists(int section, const char *token)
Definition: worldfile.cc:1401
bool Save(const std::string &filename)
Definition: worldfile.cc:196
void WriteFloat(int entity, const char *name, double value)
Definition: worldfile.cc:1489
double ReadAngle(int entity, const char *name, double value)
Definition: worldfile.hh:135
The Stage library uses its own namespace.
Definition: canvas.hh:8