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 8004 2009-07-13 14:03:44Z thjc $
00042  */
00043 #ifndef CONFFILE_H
00044 #define CONFFILE_H
00045 
00046 #if defined (WIN32)
00047   #if defined (PLAYER_STATIC)
00048     #define PLAYERCORE_EXPORT
00049   #elif defined (playercore_EXPORTS)
00050     #define PLAYERCORE_EXPORT    __declspec (dllexport)
00051   #else
00052     #define PLAYERCORE_EXPORT    __declspec (dllimport)
00053   #endif
00054 #else
00055   #define PLAYERCORE_EXPORT
00056 #endif
00057 
00058 #include <stdio.h>
00059 
00060 #include <libplayerinterface/player.h>
00061 
00196 class PLAYERCORE_EXPORT ConfigFile
00197 {
00199   public: ConfigFile(uint32_t _default_host, uint32_t _default_robot);
00200 
00202   public: ConfigFile(const char* _default_host, uint32_t _default_robot);
00203 
00205   public: ConfigFile();
00206 
00208   public: ~ConfigFile();
00209 
00211   private: void InitFields();
00212 
00216   public: bool Load(const char *filename);
00217 
00226   public: void InsertFieldValue(int index,
00227                                 const char* name, 
00228                                 const char* value);
00229 
00230   // Save config back into file
00231   // Set filename to NULL to save back into the original file
00232   private: bool Save(const char *filename);
00233 
00236   public: bool WarnUnused();
00237 
00243   public: bool ReadBool(int section, const char *name, bool value);
00244 
00245   // Write a bool as "yes" or "no"
00246   private: void WriteBool(int section, const char* name, bool value);
00247 
00248   // Write a bool as "1" or "0" (for backward compatability)
00249   private: void WriteBool_Compat(int section, const char* name, bool value);
00250 
00251 
00257   public: const char *ReadString(int section, 
00258                                  const char *name, 
00259                                  const char *value);
00260 
00261   // Write a string
00262   private: void WriteString(int section, 
00263                             const char *name, 
00264                             const char *value);
00265 
00271   public: int ReadInt(int section, 
00272                       const char *name, 
00273                       int value);
00274 
00275   // Write an integer
00276   private: void WriteInt(int section, 
00277                          const char *name, 
00278                          int value);
00279 
00285   public: double ReadFloat(int section, 
00286                            const char *name, 
00287                            double value);
00288 
00289   // Write a float
00290   private: void WriteFloat(int section, 
00291                            const char *name, 
00292                            double value);
00293 
00299   public: double ReadLength(int section, 
00300                             const char *name, 
00301                             double value);
00302 
00303   // Write a length (includes units conversion)
00304   private: void WriteLength(int section, 
00305                             const char *name, 
00306                             double value);
00307   
00317   public: double ReadAngle(int section, const char *name, double value);
00318 
00329   public: uint32_t ReadColor(int section, 
00330                              const char *name, 
00331                              uint32_t value);
00332 
00342   public: const char *ReadFilename(int section, 
00343                                    const char *name, 
00344                                    const char *value);
00345 
00349   public: int GetTupleCount(int section, const char *name);
00350 
00357   public: const char *ReadTupleString(int section, 
00358                                       const char *name,
00359                                       int index, 
00360                                       const char *value);
00361   
00362   // Write a string to a tuple
00363   private: void WriteTupleString(int section, 
00364                                 const char *name,
00365                                 int index, 
00366                                 const char *value);
00367   
00374   public: int ReadTupleInt(int section, 
00375                            const char *name,
00376                            int index, 
00377                            int value);
00378 
00379   // Write a int to a tuple
00380   private: void WriteTupleInt(int section, 
00381                              const char *name,
00382                              int index, 
00383                              int value);
00384   
00385 
00392   public: double ReadTupleFloat(int section, 
00393                                 const char *name,
00394                                 int index, 
00395                                 double value);
00396 
00397   // Write a float to a tuple
00398   private: void WriteTupleFloat(int section, 
00399                                const char *name,
00400                                int index, 
00401                                double value);
00402 
00409   public: double ReadTupleLength(int section, 
00410                                  const char *name,
00411                                  int index, 
00412                                  double value);
00413 
00414   // Write a to a tuple length (includes units conversion)
00415   private: void WriteTupleLength(int section, 
00416                                 const char *name,
00417                                 int index, 
00418                                 double value);
00419 
00430   public: double ReadTupleAngle(int section, 
00431                                 const char *name,
00432                                 int index, 
00433                                 double value);
00434 
00435   // Write an angle to a tuple (includes units conversion)
00436   private: void WriteTupleAngle(int section, 
00437                                const char *name,
00438                                int index, 
00439                                double value);
00440 
00452   public: uint32_t ReadTupleColor(int section, 
00453                                   const char *name,
00454                                   int index, 
00455                                   uint32_t value); 
00456 
00461   //
00469   public: int ReadDeviceAddr(player_devaddr_t* addr, int section, 
00470                              const char *name, int code, int index, 
00471                              const char *key);
00472 
00473   // Parse a driver block, and update the deviceTable accordingly
00474   public: bool ParseDriver(int section);
00475 
00476   // Parse an interface block, and update the interface systems accordingly
00477   public: bool ParseInterface(int section);
00478 
00479   // Parse all driver blocks
00480   public: bool ParseAllDrivers();
00481 
00482   // Parse all interface blocks
00483   public: bool ParseAllInterfaces();
00484 
00486   public: int GetSectionCount();
00487 
00489   public: const char *GetSectionType(int section);
00490 
00493   public: int LookupSection(const char *type);
00494   
00497   public: int GetSectionParent(int section);
00498 
00499 
00501   // Private methods used to load stuff from the config file
00502   
00503   // Load tokens from a file.
00504   private: bool LoadTokens(FILE *file, int include);
00505 
00506   // Read in a comment token
00507   private: bool LoadTokenComment(FILE *file, int *line, int include);
00508 
00509   // Read in a word token
00510   private: bool LoadTokenWord(FILE *file, int *line, int include);
00511 
00512   // Load an include token; this will load the include file.
00513   private: bool LoadTokenInclude(FILE *file, int *line, int include);
00514 
00515   // Read in a number token
00516   private: bool LoadTokenNum(FILE *file, int *line, int include);
00517 
00518   // Read in a string token
00519   private: bool LoadTokenString(FILE *file, int *line, int include);
00520 
00521   // Read in a whitespace token
00522   private: bool LoadTokenSpace(FILE *file, int *line, int include);
00523 
00524   // Save tokens to a file.
00525   private: bool SaveTokens(FILE *file);
00526 
00527   // Clear the token list
00528   private: void ClearTokens();
00529 
00530   // Add a token to the token list
00531   private: bool AddToken(int type, const char *value, int include);
00532 
00533   // Set a token in the token list
00534   private: bool SetTokenValue(int index, const char *value);
00535 
00536   // Get the value of a token
00537   private: const char *GetTokenValue(int index);
00538 
00540   public: void DumpTokens();
00541 
00542   // Parse a line
00543   private: bool ParseTokens();
00544 
00545   // Parse an include statement
00546   private: bool ParseTokenInclude(int *index, int *line);
00547 
00548   // Parse a macro definition
00549   private: bool ParseTokenDefine(int *index, int *line);
00550 
00551   // Parse a macro definition
00552   private: bool ParseTokenPlugin(int *index, int *line);
00553 
00554   // Parse an word (could be a section or an field) from the token list.
00555   private: bool ParseTokenWord(int section, int *index, int *line);
00556 
00557   // Parse a section from the token list.
00558   private: bool ParseTokenSection(int section, int *index, int *line);
00559 
00560   // Parse an field from the token list.
00561   private: bool ParseTokenField(int section, int *index, int *line);
00562 
00563   // Parse a tuple.
00564   private: bool ParseTokenTuple(int section, int field, 
00565                                 int *index, int *line);
00566 
00567   // Clear the macro list
00568   private: void ClearMacros();
00569 
00570   // Add a macro
00571   private: int AddMacro(const char *macroname, const char *sectionname,
00572                         int line, int starttoken, int endtoken);
00573 
00574   // Lookup a macro by name
00575   // Returns -1 if there is no macro with this name.
00576   private: int LookupMacro(const char *macroname);
00577 
00578   // Dump the macro list for debugging
00579   private: void DumpMacros();
00580 
00581   // Clear the section list
00582   private: void ClearSections();
00583 
00584   // Add a section
00585   private: int AddSection(int parent, const char *type);
00586 
00588   public: void DumpSections();
00589 
00590   // Clear the field list
00591   private: void ClearFields();
00592 
00593   // Add a field
00594   private: int AddField(int section, const char *name, int line);
00595 
00596   // Add a field value.
00597   private: void AddFieldValue(int field, int index, int value_token);
00598   
00599   // Get a field
00600   private: int GetField(int section, const char *name);
00601 
00602   // Get the number of elements for this field
00603   private: int GetFieldValueCount(int field);
00604 
00605   // Get the value of an field element
00606   // Set flag_used to true mark the field element as read.
00607   private: const char *GetFieldValue(int field, int index, bool flag_used = true);
00608 
00609   // Set the value of an field.
00610   private: void SetFieldValue(int field, int index, const char *value);
00611 
00613   public: void DumpFields();
00614 
00615   // Look up the color in a data based (transform color name -> color value).
00616   private: uint32_t LookupColor(const char *name);
00617 
00619   public: char *filename;
00620 
00621   // Token types.
00622   private: enum
00623     {
00624       TokenComment,
00625       TokenWord, TokenNum, TokenString, TokenBool,
00626       TokenOpenSection, TokenCloseSection,
00627       TokenOpenTuple, TokenCloseTuple,
00628       TokenSpace, TokenEOL
00629     };
00630 
00631   // Token structure.
00632   private: struct Token
00633   {
00634     // Non-zero if token is from an include file.
00635     int include;
00636     
00637     // Token type (enumerated value).
00638     int type;
00639 
00640     // Token value
00641     char *value;
00642   };
00643 
00644   // A list of tokens loaded from the file.
00645   // Modified values are written back into the token list.
00646   private: int token_size, token_count;
00647   private: Token *tokens;
00648 
00649   // Private macro class
00650   private: struct CMacro
00651   {
00652     // Name of macro
00653     const char *macroname;
00654 
00655     // Name of section
00656     const char *sectionname;
00657 
00658     // Line the macro definition starts on.
00659     int line;
00660     
00661     // Range of tokens in the body of the macro definition.
00662     int starttoken, endtoken;
00663   };
00664 
00665   // Macro list
00666   private: int macro_size;
00667   private: int macro_count;
00668   private: CMacro *macros;
00669   
00670   // Private section class
00671   private: struct Section
00672   {
00673     // Parent section
00674     int parent;
00675 
00676     // Type of section (i.e. position, laser, etc).
00677     const char *type;
00678   };
00679 
00680   // Section list
00681   private: int section_size;
00682   private: int section_count;
00683   private: Section *sections;
00684 
00685   // Private field class
00686   private: struct Field
00687   {
00688     // Index of section this field belongs to
00689     int section;
00690 
00691     // Name of field
00692     const char *name;
00693     
00694     // A list of token indexes
00695     int value_count;
00696     int *values;
00697 
00698     // Flag set if field value has been used
00699     bool *useds;
00700 
00701     // Line this field came from
00702     int line;
00703   };
00704   
00705   // Field list
00706   private: int field_size;
00707   private: int field_count;
00708   private: Field *fields;
00709   private: uint32_t default_host;
00710   private: uint32_t default_robot;
00711 
00712   // Conversion units
00713   private: double unit_length;
00714   private: double unit_angle;
00715 };
00716 
00717 #endif

Last updated 25 May 2011 21:17:00