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
36namespace Stg {
37
39class CProperty {
40public:
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.
67class Worldfile {
68 // Standard constructors/destructors
69public:
70 Worldfile();
71
72public:
73 ~Worldfile();
74
75 // replacement for fopen() that checks STAGEPATH dirs for the named file
76 // (thanks to Douglas S. Blank <dblank@brynmawr.edu>)
77protected:
78 FILE *FileOpen(const std::string &filename, const char *method);
79
80 // Load world from file
81public:
82 bool Load(const std::string &filename);
83
84protected:
85 bool LoadCommon();
86
87public:
88 bool Load(std::istream &world_content, const std::string &filename = std::string());
89
90 // Save world into named file
91public:
92 bool Save(const std::string &filename);
93
94 // Check for unused properties and print warnings
95public:
96 bool WarnUnused();
97
98 // Read a string
99public:
100 const std::string ReadString(int entity, const char *name, const std::string &value);
101
102 // Write a string
103public:
104 void WriteString(int entity, const char *name, const std::string &value);
105
106 // Read an integer
107public:
108 int ReadInt(int entity, const char *name, int value);
109
110 // Write an integer
111public:
112 void WriteInt(int entity, const char *name, int value);
113
114 // Read a float
115public:
116 double ReadFloat(int entity, const char *name, double value);
117
118 // Write a float
119public:
120 void WriteFloat(int entity, const char *name, double value);
121
122 // Read a length (includes unit conversion)
123public:
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)
130public:
131 void WriteLength(int entity, const char *name, double value);
132
133 // Read an angle (includes unit conversion)
134public:
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)
144public:
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.
150public:
151 const char *ReadFilename(int entity, const char *name, const char *value);
152
153 // Read a series of values from a tuplee
154public:
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
159public:
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.
167private:
168 bool LoadTokens(FILE *file, int include);
169 // Load tokens from a stream.
170private:
171 bool LoadTokens(std::istream &content, int include);
172
173 // Read in a comment token
174private:
175 bool LoadTokenComment(FILE *file, int *line, int include);
176 // Read in a comment token (stream version)
177private:
178 bool LoadTokenComment(std::istream &content, int *line, int include);
179
180 // Read in a word token
181private:
182 bool LoadTokenWord(FILE *file, int *line, int include);
183 // Read in a word token (stream version)
184private:
185 bool LoadTokenWord(std::istream &content, int *line, int include);
186
187 // Load an include token; this will load the include file.
188private:
189 bool LoadTokenInclude(FILE *file, int *line, int include);
190 // Load an include token; this will load the include file (stream version)
191private:
192 bool LoadTokenInclude(std::istream &content, int *line, int include);
193
194 // Read in a number token
195private:
196 bool LoadTokenNum(FILE *file, int *line, int include);
197 // Read in a number token (stream version)
198private:
199 bool LoadTokenNum(std::istream &content, int *line, int include);
200
201 // Read in a string token
202private:
203 bool LoadTokenString(FILE *file, int *line, int include);
204 // Read in a string token (stream version)
205private:
206 bool LoadTokenString(std::istream &content, int *line, int include);
207
208 // Read in a whitespace token
209private:
210 bool LoadTokenSpace(FILE *file, int *line, int include);
211 // Read in a whitespace token (stream version)
212private:
213 bool LoadTokenSpace(std::istream &content, int *line, int include);
214
215 // Save tokens to a file.
216private:
217 bool SaveTokens(FILE *file);
218
219 // Clear the token list
220private:
221 void ClearTokens();
222
223 // Add a token to the token list
224private:
225 bool AddToken(int type, const char *value, int include);
226
227 // Set a token in the token list
228private:
229 bool SetTokenValue(int index, const char *value);
230
231 // Get the value of a token
232private:
233 const char *GetTokenValue(int index);
234
235 // Dump the token list (for debugging).
236private:
237 void DumpTokens();
238
239 // Parse a line
240private:
241 bool ParseTokens();
242
243 // Parse an include statement
244private:
245 bool ParseTokenInclude(int *index, int *line);
246
247 // Parse a macro definition
248private:
249 bool ParseTokenDefine(int *index, int *line);
250
251 // Parse an word (could be a entity or an property) from the token list.
252private:
253 bool ParseTokenWord(int entity, int *index, int *line);
254
255 // Parse a entity from the token list.
256private:
257 bool ParseTokenEntity(int entity, int *index, int *line);
258
259 // Parse an property from the token list.
260private:
261 bool ParseTokenProperty(int entity, int *index, int *line);
262
263 // Parse a tuple.
264private:
265 bool ParseTokenTuple(CProperty *property, int *index, int *line);
266
267 // Clear the macro list
268private:
269 void ClearMacros();
270
271 // Add a macro
272private:
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
281private:
282 CMacro *LookupMacro(const char *macroname);
283
284 // Dump the macro list for debugging
285private:
286 void DumpMacros();
287
288 // Clear the entity list
289private:
290 void ClearEntities();
291
292 // Add a entity
293private:
294 int AddEntity(int parent, const char *type);
295
296 // Get the number of entities.
297public:
298 int GetEntityCount();
299
300 // Get a entity (returns the entity type value)
301public:
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
306public:
307 int LookupEntity(const char *type);
308
309 // Get a entity's parent entity.
310 // Returns -1 if there is no parent.
311public:
312 int GetEntityParent(int entity);
313
314 // Dump the entity list for debugging
315private:
316 void DumpEntities();
317
318 // Clear the property list
319private:
320 void ClearProperties();
321
322 // Add an property
323private:
324 CProperty *AddProperty(int entity, const char *name, int line);
325 // Add an property value.
326private:
327 void AddPropertyValue(CProperty *property, int index, int value_token);
328
329 // Get an property
330public:
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.
338private:
339 void SetPropertyValue(CProperty *property, int index, const char *value);
340
341 // Get the value of an property.
342public:
343 const char *GetPropertyValue(CProperty *property, int index);
344
345 // Dump the property list for debugging
346private:
347 void DumpProperties();
348
349 // Token types.
350private:
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.
365private:
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;
383private:
384 std::vector<CToken> tokens;
385
386 // Private macro class
387private:
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
410private:
411 std::map<std::string, CMacro> macros;
412
413 // Private entity class
414private:
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
427private:
428 std::vector<CEntity> entities;
429
430 // Property list
431private:
432 std::map<std::string, CProperty *> properties;
433
434 // Name of the file we loaded
435public:
436 std::string filename;
437
438 // Conversion units
439public:
441
442public:
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