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.7 2006/02/15 06:48:38 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 ConfigFile::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: const char *ReadString(int section, 
00200                                  const char *name, 
00201                                  const char *value);
00202 
00203   // Write a string
00204   private: void WriteString(int section, 
00205                             const char *name, 
00206                             const char *value);
00207 
00213   public: int ReadInt(int section, 
00214                       const char *name, 
00215                       int value);
00216 
00217   // Write an integer
00218   private: void WriteInt(int section, 
00219                          const char *name, 
00220                          int value);
00221 
00227   public: double ReadFloat(int section, 
00228                            const char *name, 
00229                            double value);
00230 
00231   // Write a float
00232   private: void WriteFloat(int section, 
00233                            const char *name, 
00234                            double value);
00235 
00241   public: double ReadLength(int section, 
00242                             const char *name, 
00243                             double value);
00244 
00245   // Write a length (includes units conversion)
00246   private: void WriteLength(int section, 
00247                             const char *name, 
00248                             double value);
00249   
00259   public: double ReadAngle(int section, const char *name, double value);
00260 
00271   public: uint32_t ReadColor(int section, 
00272                              const char *name, 
00273                              uint32_t value);
00274 
00284   public: const char *ReadFilename(int section, 
00285                                    const char *name, 
00286                                    const char *value);
00287 
00291   public: int GetTupleCount(int section, const char *name);
00292 
00299   public: const char *ReadTupleString(int section, 
00300                                       const char *name,
00301                                       int index, 
00302                                       const char *value);
00303   
00304   // Write a string to a tuple
00305   private: void WriteTupleString(int section, 
00306                                 const char *name,
00307                                 int index, 
00308                                 const char *value);
00309   
00316   public: int ReadTupleInt(int section, 
00317                            const char *name,
00318                            int index, 
00319                            int value);
00320 
00321   // Write a int to a tuple
00322   private: void WriteTupleInt(int section, 
00323                              const char *name,
00324                              int index, 
00325                              int value);
00326   
00327 
00334   public: double ReadTupleFloat(int section, 
00335                                 const char *name,
00336                                 int index, 
00337                                 double value);
00338 
00339   // Write a float to a tuple
00340   private: void WriteTupleFloat(int section, 
00341                                const char *name,
00342                                int index, 
00343                                double value);
00344 
00351   public: double ReadTupleLength(int section, 
00352                                  const char *name,
00353                                  int index, 
00354                                  double value);
00355 
00356   // Write a to a tuple length (includes units conversion)
00357   private: void WriteTupleLength(int section, 
00358                                 const char *name,
00359                                 int index, 
00360                                 double value);
00361 
00372   public: double ReadTupleAngle(int section, 
00373                                 const char *name,
00374                                 int index, 
00375                                 double value);
00376 
00377   // Write an angle to a tuple (includes units conversion)
00378   private: void WriteTupleAngle(int section, 
00379                                const char *name,
00380                                int index, 
00381                                double value);
00382 
00394   public: uint32_t ReadTupleColor(int section, 
00395                                   const char *name,
00396                                   int index, 
00397                                   uint32_t value); 
00398 
00403   //
00411   public: int ReadDeviceAddr(player_devaddr_t* addr, int section, 
00412                              const char *name, int code, int index, 
00413                              const char *key);
00414 
00415   // Parse a driver block, and update the deviceTable accordingly
00416   public: bool ParseDriver(int section);
00417 
00418   // Parse all driver blocks
00419   public: bool ParseAllDrivers();
00420 
00422   public: int GetSectionCount();
00423 
00425   public: const char *GetSectionType(int section);
00426 
00429   public: int LookupSection(const char *type);
00430   
00433   public: int GetSectionParent(int section);
00434 
00435 
00437   // Private methods used to load stuff from the config file
00438   
00439   // Load tokens from a file.
00440   private: bool LoadTokens(FILE *file, int include);
00441 
00442   // Read in a comment token
00443   private: bool LoadTokenComment(FILE *file, int *line, int include);
00444 
00445   // Read in a word token
00446   private: bool LoadTokenWord(FILE *file, int *line, int include);
00447 
00448   // Load an include token; this will load the include file.
00449   private: bool LoadTokenInclude(FILE *file, int *line, int include);
00450 
00451   // Read in a number token
00452   private: bool LoadTokenNum(FILE *file, int *line, int include);
00453 
00454   // Read in a string token
00455   private: bool LoadTokenString(FILE *file, int *line, int include);
00456 
00457   // Read in a whitespace token
00458   private: bool LoadTokenSpace(FILE *file, int *line, int include);
00459 
00460   // Save tokens to a file.
00461   private: bool SaveTokens(FILE *file);
00462 
00463   // Clear the token list
00464   private: void ClearTokens();
00465 
00466   // Add a token to the token list
00467   private: bool AddToken(int type, const char *value, int include);
00468 
00469   // Set a token in the token list
00470   private: bool SetTokenValue(int index, const char *value);
00471 
00472   // Get the value of a token
00473   private: const char *GetTokenValue(int index);
00474 
00476   public: void DumpTokens();
00477 
00478   // Parse a line
00479   private: bool ParseTokens();
00480 
00481   // Parse an include statement
00482   private: bool ParseTokenInclude(int *index, int *line);
00483 
00484   // Parse a macro definition
00485   private: bool ParseTokenDefine(int *index, int *line);
00486 
00487   // Parse an word (could be a section or an field) from the token list.
00488   private: bool ParseTokenWord(int section, int *index, int *line);
00489 
00490   // Parse a section from the token list.
00491   private: bool ParseTokenSection(int section, int *index, int *line);
00492 
00493   // Parse an field from the token list.
00494   private: bool ParseTokenField(int section, int *index, int *line);
00495 
00496   // Parse a tuple.
00497   private: bool ParseTokenTuple(int section, int field, 
00498                                 int *index, int *line);
00499 
00500   // Clear the macro list
00501   private: void ClearMacros();
00502 
00503   // Add a macro
00504   private: int AddMacro(const char *macroname, const char *sectionname,
00505                         int line, int starttoken, int endtoken);
00506 
00507   // Lookup a macro by name
00508   // Returns -1 if there is no macro with this name.
00509   private: int LookupMacro(const char *macroname);
00510 
00511   // Dump the macro list for debugging
00512   private: void DumpMacros();
00513 
00514   // Clear the section list
00515   private: void ClearSections();
00516 
00517   // Add a section
00518   private: int AddSection(int parent, const char *type);
00519 
00521   public: void DumpSections();
00522 
00523   // Clear the field list
00524   private: void ClearFields();
00525 
00526   // Add a field
00527   private: int AddField(int section, const char *name, int line);
00528 
00529   // Add a field value.
00530   private: void AddFieldValue(int field, int index, int value_token);
00531   
00532   // Get a field
00533   private: int GetField(int section, const char *name);
00534 
00535   // Get the number of elements for this field
00536   private: int GetFieldValueCount(int field);
00537 
00538   // Get the value of an field element
00539   // Set flag_used to true mark the field element as read.
00540   private: const char *GetFieldValue(int field, int index, bool flag_used = true);
00541 
00542   // Set the value of an field.
00543   private: void SetFieldValue(int field, int index, const char *value);
00544 
00546   public: void DumpFields();
00547 
00548   // Look up the color in a data based (transform color name -> color value).
00549   private: uint32_t LookupColor(const char *name);
00550 
00552   public: char *filename;
00553 
00554   // Token types.
00555   private: enum
00556     {
00557       TokenComment,
00558       TokenWord, TokenNum, TokenString,
00559       TokenOpenSection, TokenCloseSection,
00560       TokenOpenTuple, TokenCloseTuple,
00561       TokenSpace, TokenEOL
00562     };
00563 
00564   // Token structure.
00565   private: struct Token
00566   {
00567     // Non-zero if token is from an include file.
00568     int include;
00569     
00570     // Token type (enumerated value).
00571     int type;
00572 
00573     // Token value
00574     char *value;
00575   };
00576 
00577   // A list of tokens loaded from the file.
00578   // Modified values are written back into the token list.
00579   private: int token_size, token_count;
00580   private: Token *tokens;
00581 
00582   // Private macro class
00583   private: struct CMacro
00584   {
00585     // Name of macro
00586     const char *macroname;
00587 
00588     // Name of section
00589     const char *sectionname;
00590 
00591     // Line the macro definition starts on.
00592     int line;
00593     
00594     // Range of tokens in the body of the macro definition.
00595     int starttoken, endtoken;
00596   };
00597 
00598   // Macro list
00599   private: int macro_size;
00600   private: int macro_count;
00601   private: CMacro *macros;
00602   
00603   // Private section class
00604   private: struct Section
00605   {
00606     // Parent section
00607     int parent;
00608 
00609     // Type of section (i.e. position, laser, etc).
00610     const char *type;
00611   };
00612 
00613   // Section list
00614   private: int section_size;
00615   private: int section_count;
00616   private: Section *sections;
00617 
00618   // Private field class
00619   private: struct Field
00620   {
00621     // Index of section this field belongs to
00622     int section;
00623 
00624     // Name of field
00625     const char *name;
00626     
00627     // A list of token indexes
00628     int value_count;
00629     int *values;
00630 
00631     // Flag set if field value has been used
00632     bool *useds;
00633 
00634     // Line this field came from
00635     int line;
00636   };
00637   
00638   // Field list
00639   private: int field_size;
00640   private: int field_count;
00641   private: Field *fields;
00642   private: uint32_t default_host;
00643   private: uint32_t default_robot;
00644 
00645   // Conversion units
00646   private: double unit_length;
00647   private: double unit_angle;
00648 };
00649 
00650 #endif

Last updated 12 September 2005 21:38:45