configfile.h

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.8.2.1 2006/06/09 01:17:52 gerkey Exp $
00025  */
00026 #ifndef CONFFILE_H
00027 #define CONFFILE_H
00028 
00029 #include <stdio.h>
00030 
00031 #include <libplayercore/player.h>
00032 
00167 class ConfigFile
00168 {
00170   public: ConfigFile(uint32_t _default_host, uint32_t _default_robot);
00171 
00173   public: ConfigFile(const char* _default_host, uint32_t _default_robot);
00174 
00176   public: ~ConfigFile();
00177 
00179   private: void InitFields();
00180 
00184   public: bool Load(const char *filename);
00185 
00186   // Save config back into file
00187   // Set filename to NULL to save back into the original file
00188   private: bool Save(const char *filename);
00189 
00192   public: bool WarnUnused();
00193 
00199   public: bool ReadBool(int section, const char *name, bool value);
00200 
00201   // Write a bool as "yes" or "no"
00202   private: void WriteBool(int section, const char* name, bool value);
00203 
00204   // Write a bool as "1" or "0" (for backward compatability)
00205   private: void WriteBool_Compat(int section, const char* name, bool value);
00206 
00207 
00213   public: const char *ReadString(int section, 
00214                                  const char *name, 
00215                                  const char *value);
00216 
00217   // Write a string
00218   private: void WriteString(int section, 
00219                             const char *name, 
00220                             const char *value);
00221 
00227   public: int ReadInt(int section, 
00228                       const char *name, 
00229                       int value);
00230 
00231   // Write an integer
00232   private: void WriteInt(int section, 
00233                          const char *name, 
00234                          int value);
00235 
00241   public: double ReadFloat(int section, 
00242                            const char *name, 
00243                            double value);
00244 
00245   // Write a float
00246   private: void WriteFloat(int section, 
00247                            const char *name, 
00248                            double value);
00249 
00255   public: double ReadLength(int section, 
00256                             const char *name, 
00257                             double value);
00258 
00259   // Write a length (includes units conversion)
00260   private: void WriteLength(int section, 
00261                             const char *name, 
00262                             double value);
00263   
00273   public: double ReadAngle(int section, const char *name, double value);
00274 
00285   public: uint32_t ReadColor(int section, 
00286                              const char *name, 
00287                              uint32_t value);
00288 
00298   public: const char *ReadFilename(int section, 
00299                                    const char *name, 
00300                                    const char *value);
00301 
00305   public: int GetTupleCount(int section, const char *name);
00306 
00313   public: const char *ReadTupleString(int section, 
00314                                       const char *name,
00315                                       int index, 
00316                                       const char *value);
00317   
00318   // Write a string to a tuple
00319   private: void WriteTupleString(int section, 
00320                                 const char *name,
00321                                 int index, 
00322                                 const char *value);
00323   
00330   public: int ReadTupleInt(int section, 
00331                            const char *name,
00332                            int index, 
00333                            int value);
00334 
00335   // Write a int to a tuple
00336   private: void WriteTupleInt(int section, 
00337                              const char *name,
00338                              int index, 
00339                              int value);
00340   
00341 
00348   public: double ReadTupleFloat(int section, 
00349                                 const char *name,
00350                                 int index, 
00351                                 double value);
00352 
00353   // Write a float to a tuple
00354   private: void WriteTupleFloat(int section, 
00355                                const char *name,
00356                                int index, 
00357                                double value);
00358 
00365   public: double ReadTupleLength(int section, 
00366                                  const char *name,
00367                                  int index, 
00368                                  double value);
00369 
00370   // Write a to a tuple length (includes units conversion)
00371   private: void WriteTupleLength(int section, 
00372                                 const char *name,
00373                                 int index, 
00374                                 double value);
00375 
00386   public: double ReadTupleAngle(int section, 
00387                                 const char *name,
00388                                 int index, 
00389                                 double value);
00390 
00391   // Write an angle to a tuple (includes units conversion)
00392   private: void WriteTupleAngle(int section, 
00393                                const char *name,
00394                                int index, 
00395                                double value);
00396 
00408   public: uint32_t ReadTupleColor(int section, 
00409                                   const char *name,
00410                                   int index, 
00411                                   uint32_t value); 
00412 
00417   //
00425   public: int ReadDeviceAddr(player_devaddr_t* addr, int section, 
00426                              const char *name, int code, int index, 
00427                              const char *key);
00428 
00429   // Parse a driver block, and update the deviceTable accordingly
00430   public: bool ParseDriver(int section);
00431 
00432   // Parse all driver blocks
00433   public: bool ParseAllDrivers();
00434 
00436   public: int GetSectionCount();
00437 
00439   public: const char *GetSectionType(int section);
00440 
00443   public: int LookupSection(const char *type);
00444   
00447   public: int GetSectionParent(int section);
00448 
00449 
00451   // Private methods used to load stuff from the config file
00452   
00453   // Load tokens from a file.
00454   private: bool LoadTokens(FILE *file, int include);
00455 
00456   // Read in a comment token
00457   private: bool LoadTokenComment(FILE *file, int *line, int include);
00458 
00459   // Read in a word token
00460   private: bool LoadTokenWord(FILE *file, int *line, int include);
00461 
00462   // Load an include token; this will load the include file.
00463   private: bool LoadTokenInclude(FILE *file, int *line, int include);
00464 
00465   // Read in a number token
00466   private: bool LoadTokenNum(FILE *file, int *line, int include);
00467 
00468   // Read in a string token
00469   private: bool LoadTokenString(FILE *file, int *line, int include);
00470 
00471   // Read in a whitespace token
00472   private: bool LoadTokenSpace(FILE *file, int *line, int include);
00473 
00474   // Save tokens to a file.
00475   private: bool SaveTokens(FILE *file);
00476 
00477   // Clear the token list
00478   private: void ClearTokens();
00479 
00480   // Add a token to the token list
00481   private: bool AddToken(int type, const char *value, int include);
00482 
00483   // Set a token in the token list
00484   private: bool SetTokenValue(int index, const char *value);
00485 
00486   // Get the value of a token
00487   private: const char *GetTokenValue(int index);
00488 
00490   public: void DumpTokens();
00491 
00492   // Parse a line
00493   private: bool ParseTokens();
00494 
00495   // Parse an include statement
00496   private: bool ParseTokenInclude(int *index, int *line);
00497 
00498   // Parse a macro definition
00499   private: bool ParseTokenDefine(int *index, int *line);
00500 
00501   // Parse an word (could be a section or an field) from the token list.
00502   private: bool ParseTokenWord(int section, int *index, int *line);
00503 
00504   // Parse a section from the token list.
00505   private: bool ParseTokenSection(int section, int *index, int *line);
00506 
00507   // Parse an field from the token list.
00508   private: bool ParseTokenField(int section, int *index, int *line);
00509 
00510   // Parse a tuple.
00511   private: bool ParseTokenTuple(int section, int field, 
00512                                 int *index, int *line);
00513 
00514   // Clear the macro list
00515   private: void ClearMacros();
00516 
00517   // Add a macro
00518   private: int AddMacro(const char *macroname, const char *sectionname,
00519                         int line, int starttoken, int endtoken);
00520 
00521   // Lookup a macro by name
00522   // Returns -1 if there is no macro with this name.
00523   private: int LookupMacro(const char *macroname);
00524 
00525   // Dump the macro list for debugging
00526   private: void DumpMacros();
00527 
00528   // Clear the section list
00529   private: void ClearSections();
00530 
00531   // Add a section
00532   private: int AddSection(int parent, const char *type);
00533 
00535   public: void DumpSections();
00536 
00537   // Clear the field list
00538   private: void ClearFields();
00539 
00540   // Add a field
00541   private: int AddField(int section, const char *name, int line);
00542 
00543   // Add a field value.
00544   private: void AddFieldValue(int field, int index, int value_token);
00545   
00546   // Get a field
00547   private: int GetField(int section, const char *name);
00548 
00549   // Get the number of elements for this field
00550   private: int GetFieldValueCount(int field);
00551 
00552   // Get the value of an field element
00553   // Set flag_used to true mark the field element as read.
00554   private: const char *GetFieldValue(int field, int index, bool flag_used = true);
00555 
00556   // Set the value of an field.
00557   private: void SetFieldValue(int field, int index, const char *value);
00558 
00560   public: void DumpFields();
00561 
00562   // Look up the color in a data based (transform color name -> color value).
00563   private: uint32_t LookupColor(const char *name);
00564 
00566   public: char *filename;
00567 
00568   // Token types.
00569   private: enum
00570     {
00571       TokenComment,
00572       TokenWord, TokenNum, TokenString, TokenBool,
00573       TokenOpenSection, TokenCloseSection,
00574       TokenOpenTuple, TokenCloseTuple,
00575       TokenSpace, TokenEOL
00576     };
00577 
00578   // Token structure.
00579   private: struct Token
00580   {
00581     // Non-zero if token is from an include file.
00582     int include;
00583     
00584     // Token type (enumerated value).
00585     int type;
00586 
00587     // Token value
00588     char *value;
00589   };
00590 
00591   // A list of tokens loaded from the file.
00592   // Modified values are written back into the token list.
00593   private: int token_size, token_count;
00594   private: Token *tokens;
00595 
00596   // Private macro class
00597   private: struct CMacro
00598   {
00599     // Name of macro
00600     const char *macroname;
00601 
00602     // Name of section
00603     const char *sectionname;
00604 
00605     // Line the macro definition starts on.
00606     int line;
00607     
00608     // Range of tokens in the body of the macro definition.
00609     int starttoken, endtoken;
00610   };
00611 
00612   // Macro list
00613   private: int macro_size;
00614   private: int macro_count;
00615   private: CMacro *macros;
00616   
00617   // Private section class
00618   private: struct Section
00619   {
00620     // Parent section
00621     int parent;
00622 
00623     // Type of section (i.e. position, laser, etc).
00624     const char *type;
00625   };
00626 
00627   // Section list
00628   private: int section_size;
00629   private: int section_count;
00630   private: Section *sections;
00631 
00632   // Private field class
00633   private: struct Field
00634   {
00635     // Index of section this field belongs to
00636     int section;
00637 
00638     // Name of field
00639     const char *name;
00640     
00641     // A list of token indexes
00642     int value_count;
00643     int *values;
00644 
00645     // Flag set if field value has been used
00646     bool *useds;
00647 
00648     // Line this field came from
00649     int line;
00650   };
00651   
00652   // Field list
00653   private: int field_size;
00654   private: int field_count;
00655   private: Field *fields;
00656   private: uint32_t default_host;
00657   private: uint32_t default_robot;
00658 
00659   // Conversion units
00660   private: double unit_length;
00661   private: double unit_angle;
00662 };
00663 
00664 #endif

Last updated 12 September 2005 21:38:45