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  *  This library is free software; you can redistribute it and/or
00025  *  modify it under the terms of the GNU Lesser General Public
00026  *  License as published by the Free Software Foundation; either
00027  *  version 2.1 of the License, or (at your option) any later version.
00028  *
00029  *  This library is distributed in the hope that it will be useful,
00030  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00031  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00032  *  Lesser General Public License for more details.
00033  *
00034  *  You should have received a copy of the GNU Lesser General Public
00035  *  License along with this library; if not, write to the Free Software
00036  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00037  *
00038  ********************************************************************/
00039 
00040 /*
00041  * $Id: configfile.h 4419 2008-03-16 08:41:58Z thjc $
00042  */
00043 #ifndef CONFFILE_H
00044 #define CONFFILE_H
00045 
00046 #include <stdio.h>
00047 
00048 #include <libplayercore/player.h>
00049 
00184 class ConfigFile
00185 {
00187   public: ConfigFile(uint32_t _default_host, uint32_t _default_robot);
00188 
00190   public: ConfigFile(const char* _default_host, uint32_t _default_robot);
00191 
00193   public: ConfigFile();
00194 
00196   public: ~ConfigFile();
00197 
00199   private: void InitFields();
00200 
00204   public: bool Load(const char *filename);
00205 
00214   public: void InsertFieldValue(int index,
00215                                 const char* name, 
00216                                 const char* value);
00217 
00218   // Save config back into file
00219   // Set filename to NULL to save back into the original file
00220   private: bool Save(const char *filename);
00221 
00224   public: bool WarnUnused();
00225 
00231   public: bool ReadBool(int section, const char *name, bool value);
00232 
00233   // Write a bool as "yes" or "no"
00234   private: void WriteBool(int section, const char* name, bool value);
00235 
00236   // Write a bool as "1" or "0" (for backward compatability)
00237   private: void WriteBool_Compat(int section, const char* name, bool value);
00238 
00239 
00245   public: const char *ReadString(int section, 
00246                                  const char *name, 
00247                                  const char *value);
00248 
00249   // Write a string
00250   private: void WriteString(int section, 
00251                             const char *name, 
00252                             const char *value);
00253 
00259   public: int ReadInt(int section, 
00260                       const char *name, 
00261                       int value);
00262 
00263   // Write an integer
00264   private: void WriteInt(int section, 
00265                          const char *name, 
00266                          int value);
00267 
00273   public: double ReadFloat(int section, 
00274                            const char *name, 
00275                            double value);
00276 
00277   // Write a float
00278   private: void WriteFloat(int section, 
00279                            const char *name, 
00280                            double value);
00281 
00287   public: double ReadLength(int section, 
00288                             const char *name, 
00289                             double value);
00290 
00291   // Write a length (includes units conversion)
00292   private: void WriteLength(int section, 
00293                             const char *name, 
00294                             double value);
00295   
00305   public: double ReadAngle(int section, const char *name, double value);
00306 
00317   public: uint32_t ReadColor(int section, 
00318                              const char *name, 
00319                              uint32_t value);
00320 
00330   public: const char *ReadFilename(int section, 
00331                                    const char *name, 
00332                                    const char *value);
00333 
00337   public: int GetTupleCount(int section, const char *name);
00338 
00345   public: const char *ReadTupleString(int section, 
00346                                       const char *name,
00347                                       int index, 
00348                                       const char *value);
00349   
00350   // Write a string to a tuple
00351   private: void WriteTupleString(int section, 
00352                                 const char *name,
00353                                 int index, 
00354                                 const char *value);
00355   
00362   public: int ReadTupleInt(int section, 
00363                            const char *name,
00364                            int index, 
00365                            int value);
00366 
00367   // Write a int to a tuple
00368   private: void WriteTupleInt(int section, 
00369                              const char *name,
00370                              int index, 
00371                              int value);
00372   
00373 
00380   public: double ReadTupleFloat(int section, 
00381                                 const char *name,
00382                                 int index, 
00383                                 double value);
00384 
00385   // Write a float to a tuple
00386   private: void WriteTupleFloat(int section, 
00387                                const char *name,
00388                                int index, 
00389                                double value);
00390 
00397   public: double ReadTupleLength(int section, 
00398                                  const char *name,
00399                                  int index, 
00400                                  double value);
00401 
00402   // Write a to a tuple length (includes units conversion)
00403   private: void WriteTupleLength(int section, 
00404                                 const char *name,
00405                                 int index, 
00406                                 double value);
00407 
00418   public: double ReadTupleAngle(int section, 
00419                                 const char *name,
00420                                 int index, 
00421                                 double value);
00422 
00423   // Write an angle to a tuple (includes units conversion)
00424   private: void WriteTupleAngle(int section, 
00425                                const char *name,
00426                                int index, 
00427                                double value);
00428 
00440   public: uint32_t ReadTupleColor(int section, 
00441                                   const char *name,
00442                                   int index, 
00443                                   uint32_t value); 
00444 
00449   //
00457   public: int ReadDeviceAddr(player_devaddr_t* addr, int section, 
00458                              const char *name, int code, int index, 
00459                              const char *key);
00460 
00461   // Parse a driver block, and update the deviceTable accordingly
00462   public: bool ParseDriver(int section);
00463 
00464   // Parse an interface block, and update the interface systems accordingly
00465   public: bool ParseInterface(int section);
00466 
00467   // Parse all driver blocks
00468   public: bool ParseAllDrivers();
00469 
00470   // Parse all interface blocks
00471   public: bool ParseAllInterfaces();
00472 
00474   public: int GetSectionCount();
00475 
00477   public: const char *GetSectionType(int section);
00478 
00481   public: int LookupSection(const char *type);
00482   
00485   public: int GetSectionParent(int section);
00486 
00487 
00489   // Private methods used to load stuff from the config file
00490   
00491   // Load tokens from a file.
00492   private: bool LoadTokens(FILE *file, int include);
00493 
00494   // Read in a comment token
00495   private: bool LoadTokenComment(FILE *file, int *line, int include);
00496 
00497   // Read in a word token
00498   private: bool LoadTokenWord(FILE *file, int *line, int include);
00499 
00500   // Load an include token; this will load the include file.
00501   private: bool LoadTokenInclude(FILE *file, int *line, int include);
00502 
00503   // Read in a number token
00504   private: bool LoadTokenNum(FILE *file, int *line, int include);
00505 
00506   // Read in a string token
00507   private: bool LoadTokenString(FILE *file, int *line, int include);
00508 
00509   // Read in a whitespace token
00510   private: bool LoadTokenSpace(FILE *file, int *line, int include);
00511 
00512   // Save tokens to a file.
00513   private: bool SaveTokens(FILE *file);
00514 
00515   // Clear the token list
00516   private: void ClearTokens();
00517 
00518   // Add a token to the token list
00519   private: bool AddToken(int type, const char *value, int include);
00520 
00521   // Set a token in the token list
00522   private: bool SetTokenValue(int index, const char *value);
00523 
00524   // Get the value of a token
00525   private: const char *GetTokenValue(int index);
00526 
00528   public: void DumpTokens();
00529 
00530   // Parse a line
00531   private: bool ParseTokens();
00532 
00533   // Parse an include statement
00534   private: bool ParseTokenInclude(int *index, int *line);
00535 
00536   // Parse a macro definition
00537   private: bool ParseTokenDefine(int *index, int *line);
00538 
00539   // Parse a macro definition
00540   private: bool ParseTokenPlugin(int *index, int *line);
00541 
00542   // Parse an word (could be a section or an field) from the token list.
00543   private: bool ParseTokenWord(int section, int *index, int *line);
00544 
00545   // Parse a section from the token list.
00546   private: bool ParseTokenSection(int section, int *index, int *line);
00547 
00548   // Parse an field from the token list.
00549   private: bool ParseTokenField(int section, int *index, int *line);
00550 
00551   // Parse a tuple.
00552   private: bool ParseTokenTuple(int section, int field, 
00553                                 int *index, int *line);
00554 
00555   // Clear the macro list
00556   private: void ClearMacros();
00557 
00558   // Add a macro
00559   private: int AddMacro(const char *macroname, const char *sectionname,
00560                         int line, int starttoken, int endtoken);
00561 
00562   // Lookup a macro by name
00563   // Returns -1 if there is no macro with this name.
00564   private: int LookupMacro(const char *macroname);
00565 
00566   // Dump the macro list for debugging
00567   private: void DumpMacros();
00568 
00569   // Clear the section list
00570   private: void ClearSections();
00571 
00572   // Add a section
00573   private: int AddSection(int parent, const char *type);
00574 
00576   public: void DumpSections();
00577 
00578   // Clear the field list
00579   private: void ClearFields();
00580 
00581   // Add a field
00582   private: int AddField(int section, const char *name, int line);
00583 
00584   // Add a field value.
00585   private: void AddFieldValue(int field, int index, int value_token);
00586   
00587   // Get a field
00588   private: int GetField(int section, const char *name);
00589 
00590   // Get the number of elements for this field
00591   private: int GetFieldValueCount(int field);
00592 
00593   // Get the value of an field element
00594   // Set flag_used to true mark the field element as read.
00595   private: const char *GetFieldValue(int field, int index, bool flag_used = true);
00596 
00597   // Set the value of an field.
00598   private: void SetFieldValue(int field, int index, const char *value);
00599 
00601   public: void DumpFields();
00602 
00603   // Look up the color in a data based (transform color name -> color value).
00604   private: uint32_t LookupColor(const char *name);
00605 
00607   public: char *filename;
00608 
00609   // Token types.
00610   private: enum
00611     {
00612       TokenComment,
00613       TokenWord, TokenNum, TokenString, TokenBool,
00614       TokenOpenSection, TokenCloseSection,
00615       TokenOpenTuple, TokenCloseTuple,
00616       TokenSpace, TokenEOL
00617     };
00618 
00619   // Token structure.
00620   private: struct Token
00621   {
00622     // Non-zero if token is from an include file.
00623     int include;
00624     
00625     // Token type (enumerated value).
00626     int type;
00627 
00628     // Token value
00629     char *value;
00630   };
00631 
00632   // A list of tokens loaded from the file.
00633   // Modified values are written back into the token list.
00634   private: int token_size, token_count;
00635   private: Token *tokens;
00636 
00637   // Private macro class
00638   private: struct CMacro
00639   {
00640     // Name of macro
00641     const char *macroname;
00642 
00643     // Name of section
00644     const char *sectionname;
00645 
00646     // Line the macro definition starts on.
00647     int line;
00648     
00649     // Range of tokens in the body of the macro definition.
00650     int starttoken, endtoken;
00651   };
00652 
00653   // Macro list
00654   private: int macro_size;
00655   private: int macro_count;
00656   private: CMacro *macros;
00657   
00658   // Private section class
00659   private: struct Section
00660   {
00661     // Parent section
00662     int parent;
00663 
00664     // Type of section (i.e. position, laser, etc).
00665     const char *type;
00666   };
00667 
00668   // Section list
00669   private: int section_size;
00670   private: int section_count;
00671   private: Section *sections;
00672 
00673   // Private field class
00674   private: struct Field
00675   {
00676     // Index of section this field belongs to
00677     int section;
00678 
00679     // Name of field
00680     const char *name;
00681     
00682     // A list of token indexes
00683     int value_count;
00684     int *values;
00685 
00686     // Flag set if field value has been used
00687     bool *useds;
00688 
00689     // Line this field came from
00690     int line;
00691   };
00692   
00693   // Field list
00694   private: int field_size;
00695   private: int field_count;
00696   private: Field *fields;
00697   private: uint32_t default_host;
00698   private: uint32_t default_robot;
00699 
00700   // Conversion units
00701   private: double unit_length;
00702   private: double unit_angle;
00703 };
00704 
00705 #endif

Last updated 12 September 2005 21:38:45