Home
FAQ
Player
Stage
Gazebo
Contrib
Documentation
Publications
Contributors
Users

Project
Download
Bugs/Feedback
Mailing lists

Radish

Old news
Old stuff

configfile.h

Go to the documentation of this file.
00001 /*
00002  *  Player - One Hell of a Robot Server
00003  *  Copyright (C) 2000  
00004  *     Brian Gerkey, Kasper Stoy, Richard Vaughan, & Andrew Howard
00005  *                      
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  */
00022 
00023 /*
00024  * $Id: configfile.h,v 1.17 2004/11/11 07:13:09 inspectorg Exp $
00025  */
00026 #ifndef CONFFILE_H
00027 #define CONFFILE_H
00028 
00029 #include <stdio.h>
00030 #include "playercommon.h"
00031 #include "player.h"
00032 
00033 
00049 class ConfigFile
00050 {
00052   public: ConfigFile();
00053 
00055   public: ~ConfigFile();
00056 
00060   public: bool Load(const char *filename);
00061 
00062   // Save config back into file
00063   // Set filename to NULL to save back into the original file
00064   private: bool Save(const char *filename);
00065 
00068   public: bool WarnUnused();
00069 
00075   public: const char *ReadString(int section, 
00076                                  const char *name, 
00077                                  const char *value);
00078 
00079   // Write a string
00080   private: void WriteString(int section, 
00081                             const char *name, 
00082                             const char *value);
00083 
00089   public: int ReadInt(int section, 
00090                       const char *name, 
00091                       int value);
00092 
00093   // Write an integer
00094   private: void WriteInt(int section, 
00095                          const char *name, 
00096                          int value);
00097 
00103   public: double ReadFloat(int section, 
00104                            const char *name, 
00105                            double value);
00106 
00107   // Write a float
00108   private: void WriteFloat(int section, 
00109                            const char *name, 
00110                            double value);
00111 
00117   public: double ReadLength(int section, 
00118                             const char *name, 
00119                             double value);
00120 
00121   // Write a length (includes units conversion)
00122   private: void WriteLength(int section, 
00123                             const char *name, 
00124                             double value);
00125   
00135   public: double ReadAngle(int section, const char *name, double value);
00136 
00147   public: uint32_t ReadColor(int section, 
00148                              const char *name, 
00149                              uint32_t value);
00150 
00160   public: const char *ReadFilename(int section, 
00161                                    const char *name, 
00162                                    const char *value);
00163 
00167   public: int GetTupleCount(int section, const char *name);
00168 
00175   public: const char *ReadTupleString(int section, 
00176                                       const char *name,
00177                                       int index, 
00178                                       const char *value);
00179   
00180   // Write a string to a tuple
00181   private: void WriteTupleString(int section, 
00182                                 const char *name,
00183                                 int index, 
00184                                 const char *value);
00185   
00192   public: int ReadTupleInt(int section, 
00193                            const char *name,
00194                            int index, 
00195                            int value);
00196 
00197   // Write a int to a tuple
00198   private: void WriteTupleInt(int section, 
00199                              const char *name,
00200                              int index, 
00201                              int value);
00202   
00203 
00210   public: double ReadTupleFloat(int section, 
00211                                 const char *name,
00212                                 int index, 
00213                                 double value);
00214 
00215   // Write a float to a tuple
00216   private: void WriteTupleFloat(int section, 
00217                                const char *name,
00218                                int index, 
00219                                double value);
00220 
00227   public: double ReadTupleLength(int section, 
00228                                  const char *name,
00229                                  int index, 
00230                                  double value);
00231 
00232   // Write a to a tuple length (includes units conversion)
00233   private: void WriteTupleLength(int section, 
00234                                 const char *name,
00235                                 int index, 
00236                                 double value);
00237 
00248   public: double ReadTupleAngle(int section, 
00249                                 const char *name,
00250                                 int index, 
00251                                 double value);
00252 
00253   // Write an angle to a tuple (includes units conversion)
00254   private: void WriteTupleAngle(int section, 
00255                                const char *name,
00256                                int index, 
00257                                double value);
00258 
00270   public: uint32_t ReadTupleColor(int section, 
00271                                   const char *name,
00272                                   int index, 
00273                                   uint32_t value); 
00274 
00279   //
00287   public: int ReadDeviceId(player_device_id_t *id, int section, const char *name,
00288                            int code, int index, const char *key);
00289 
00291   public: int GetSectionCount();
00292 
00294   public: const char *GetSectionType(int section);
00295 
00298   public: int LookupSection(const char *type);
00299   
00302   public: int GetSectionParent(int section);
00303 
00304 
00306   // Private methods used to load stuff from the config file
00307   
00308   // Load tokens from a file.
00309   private: bool LoadTokens(FILE *file, int include);
00310 
00311   // Read in a comment token
00312   private: bool LoadTokenComment(FILE *file, int *line, int include);
00313 
00314   // Read in a word token
00315   private: bool LoadTokenWord(FILE *file, int *line, int include);
00316 
00317   // Load an include token; this will load the include file.
00318   private: bool LoadTokenInclude(FILE *file, int *line, int include);
00319 
00320   // Read in a number token
00321   private: bool LoadTokenNum(FILE *file, int *line, int include);
00322 
00323   // Read in a string token
00324   private: bool LoadTokenString(FILE *file, int *line, int include);
00325 
00326   // Read in a whitespace token
00327   private: bool LoadTokenSpace(FILE *file, int *line, int include);
00328 
00329   // Save tokens to a file.
00330   private: bool SaveTokens(FILE *file);
00331 
00332   // Clear the token list
00333   private: void ClearTokens();
00334 
00335   // Add a token to the token list
00336   private: bool AddToken(int type, const char *value, int include);
00337 
00338   // Set a token in the token list
00339   private: bool SetTokenValue(int index, const char *value);
00340 
00341   // Get the value of a token
00342   private: const char *GetTokenValue(int index);
00343 
00345   public: void DumpTokens();
00346 
00347   // Parse a line
00348   private: bool ParseTokens();
00349 
00350   // Parse an include statement
00351   private: bool ParseTokenInclude(int *index, int *line);
00352 
00353   // Parse a macro definition
00354   private: bool ParseTokenDefine(int *index, int *line);
00355 
00356   // Parse an word (could be a section or an field) from the token list.
00357   private: bool ParseTokenWord(int section, int *index, int *line);
00358 
00359   // Parse a section from the token list.
00360   private: bool ParseTokenSection(int section, int *index, int *line);
00361 
00362   // Parse an field from the token list.
00363   private: bool ParseTokenField(int section, int *index, int *line);
00364 
00365   // Parse a tuple.
00366   private: bool ParseTokenTuple(int section, int field, 
00367                                 int *index, int *line);
00368 
00369   // Clear the macro list
00370   private: void ClearMacros();
00371 
00372   // Add a macro
00373   private: int AddMacro(const char *macroname, const char *sectionname,
00374                         int line, int starttoken, int endtoken);
00375 
00376   // Lookup a macro by name
00377   // Returns -1 if there is no macro with this name.
00378   private: int LookupMacro(const char *macroname);
00379 
00380   // Dump the macro list for debugging
00381   private: void DumpMacros();
00382 
00383   // Clear the section list
00384   private: void ClearSections();
00385 
00386   // Add a section
00387   private: int AddSection(int parent, const char *type);
00388 
00390   public: void DumpSections();
00391 
00392   // Clear the field list
00393   private: void ClearFields();
00394 
00395   // Add a field
00396   private: int AddField(int section, const char *name, int line);
00397 
00398   // Add a field value.
00399   private: void AddFieldValue(int field, int index, int value_token);
00400   
00401   // Get a field
00402   private: int GetField(int section, const char *name);
00403 
00404   // Get the number of elements for this field
00405   private: int GetFieldValueCount(int field);
00406 
00407   // Get the value of an field element
00408   // Set flag_used to true mark the field element as read.
00409   private: const char *GetFieldValue(int field, int index, bool flag_used = true);
00410 
00411   // Set the value of an field.
00412   private: void SetFieldValue(int field, int index, const char *value);
00413 
00415   public: void DumpFields();
00416 
00417   // Look up the color in a data based (transform color name -> color value).
00418   private: uint32_t LookupColor(const char *name);
00419 
00421   public: char *filename;
00422 
00423   // Token types.
00424   private: enum
00425     {
00426       TokenComment,
00427       TokenWord, TokenNum, TokenString,
00428       TokenOpenSection, TokenCloseSection,
00429       TokenOpenTuple, TokenCloseTuple,
00430       TokenSpace, TokenEOL
00431     };
00432 
00433   // Token structure.
00434   private: struct Token
00435   {
00436     // Non-zero if token is from an include file.
00437     int include;
00438     
00439     // Token type (enumerated value).
00440     int type;
00441 
00442     // Token value
00443     char *value;
00444   };
00445 
00446   // A list of tokens loaded from the file.
00447   // Modified values are written back into the token list.
00448   private: int token_size, token_count;
00449   private: Token *tokens;
00450 
00451   // Private macro class
00452   private: struct CMacro
00453   {
00454     // Name of macro
00455     const char *macroname;
00456 
00457     // Name of section
00458     const char *sectionname;
00459 
00460     // Line the macro definition starts on.
00461     int line;
00462     
00463     // Range of tokens in the body of the macro definition.
00464     int starttoken, endtoken;
00465   };
00466 
00467   // Macro list
00468   private: int macro_size;
00469   private: int macro_count;
00470   private: CMacro *macros;
00471   
00472   // Private section class
00473   private: struct Section
00474   {
00475     // Parent section
00476     int parent;
00477 
00478     // Type of section (i.e. position, laser, etc).
00479     const char *type;
00480   };
00481 
00482   // Section list
00483   private: int section_size;
00484   private: int section_count;
00485   private: Section *sections;
00486 
00487   // Private field class
00488   private: struct Field
00489   {
00490     // Index of section this field belongs to
00491     int section;
00492 
00493     // Name of field
00494     const char *name;
00495     
00496     // A list of token indexes
00497     int value_count;
00498     int *values;
00499 
00500     // Flag set if field value has been used
00501     bool *useds;
00502 
00503     // Line this field came from
00504     int line;
00505   };
00506   
00507   // Field list
00508   private: int field_size;
00509   private: int field_count;
00510   private: Field *fields;
00511 
00512   // Conversion units
00513   private: double unit_length;
00514   private: double unit_angle;
00515 };
00516 
00517 #endif

Generated on Tue May 3 14:15:33 2005 for Player by doxygen 1.3.6