Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ArConfig.h

00001 /*
00002 ActivMedia Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004,2005 ActivMedia Robotics, LLC
00004 
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 ActivMedia Robotics for information about a commercial version of ARIA at 
00022 robots@activmedia.com or 
00023 ActivMedia Robotics, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 
00025 */
00026 
00027 #ifndef ARCONFIG_H
00028 #define ARCONFIG_H
00029 
00030 #include "ArConfigArg.h"
00031 #include "ArFileParser.h"
00032 #include <set>
00033 
00034 class ArArgumentBuilder;
00035 class ArConfigSection;
00036 
00038 
00041 class ArConfig
00042 {
00043 public:
00045   ArConfig(const char *baseDirectory = NULL, 
00046                     bool noBlanksBetweenParams = false,
00047                     bool ignoreBounds = false,
00048                     bool failOnBadSection = false);
00050   ArConfig(const ArConfig &config);
00051 
00052   ArConfig &operator=(const ArConfig &config);
00053 
00055   virtual ~ArConfig();
00057   bool parseFile(const char *fileName, bool continueOnError = false,
00058                           bool noFileNotFoundMessage = false, 
00059                           char *errorBuffer = NULL,
00060                           size_t errorBufferLen = 0);
00062   bool writeFile(const char *fileName, bool append = false,
00063                           std::set<std::string> *alreadyWritten = NULL,
00064                           bool writePriorities = false);
00066   bool addParam(const ArConfigArg &arg, const char *sectionName = "", 
00067                          ArPriority::Priority priority = ArPriority::NORMAL);
00069   bool addComment(const char *comment, const char *sectionName = "", 
00070                            ArPriority::Priority priority = ArPriority::NORMAL);
00072   void setSectionComment(const char *sectionName, 
00073                                   const char *comment);
00075   void useArgumentParser(ArArgumentParser *parser);
00077 
00088   virtual bool processFile(void) { return true; }
00090   void addProcessFileCB(ArRetFunctor<bool> *functor, 
00091                                  int priority = 0);
00093   void addProcessFileWithErrorCB(
00094           ArRetFunctor2<bool, char *, size_t> *functor, 
00095           int priority = 0);
00097   void remProcessFileCB(ArRetFunctor<bool> *functor);
00099   void remProcessFileCB(
00100           ArRetFunctor2<bool, char *, size_t> *functor);
00102   bool callProcessFileCallBacks(bool continueOnError,
00103                                          char *errorBuffer,
00104                                          size_t errorBufferLen);
00106   bool parseArgument(ArArgumentBuilder *arg, 
00107                               char *errorBuffer = NULL,
00108                               size_t errorBufferLen = 0);
00110   bool parseSection(ArArgumentBuilder *arg, 
00111                               char *errorBuffer = NULL,
00112                               size_t errorBufferLen = 0);
00114   const char *getBaseDirectory(void) const;
00116   void setBaseDirectory(const char *baseDirectory);
00118   const char *getFileName(void) const;
00120   void setNoBlanksBetweenParams(bool noBlanksBetweenParams);
00121 
00123   bool getNoBlanksBetweenParams(void);
00124 
00126   bool parseArgumentParser(ArArgumentParser *parser,
00127                                     bool continueOnError = false,
00128                                     char *errorBuffer = NULL,
00129                                     size_t errorBufferLen = 0);
00130 
00132   std::list<ArConfigSection *> *getSections(void);
00133 
00135   ArConfigSection *findSection(const char *sectionName) const;
00137   void setProcessFileCallbacksLogLevel(ArLog::LogLevel level) 
00138     {  myProcessFileCallbacksLogLevel = level; }
00140   ArLog::LogLevel getProcessFileCallbacksLogLevel(void)
00141     {  return myProcessFileCallbacksLogLevel; }
00143   void clearSections(void);
00145   void clearAll(void);
00146 protected:
00156   class ProcessFileCBType
00157   {
00158     public:
00159     ProcessFileCBType(
00160             ArRetFunctor2<bool, char *, size_t> *functor)
00161     {
00162       myCallbackWithError = functor;
00163       myCallback = NULL;
00164     }
00165     ProcessFileCBType(ArRetFunctor<bool> *functor)
00166     {
00167       myCallbackWithError = NULL;
00168       myCallback = functor;
00169     }
00170     ~ProcessFileCBType() {}
00171     bool call(char *errorBuffer, size_t errorBufferLen) 
00172     { 
00173       if (myCallbackWithError != NULL) 
00174         return myCallbackWithError->invokeR(errorBuffer, errorBufferLen);
00175       else if (myCallback != NULL) 
00176         return myCallback->invokeR(); 
00177       // if we get here there's a problem
00178       ArLog::log(ArLog::Terse, "ArConfig: Horrible problem with process callbacks");
00179       return false;
00180     }
00181     bool haveFunctor(ArRetFunctor2<bool, char *, size_t> *functor)
00182     { 
00183       if (myCallbackWithError == functor) 
00184         return true; 
00185       else 
00186         return false; 
00187     }
00188     bool haveFunctor(ArRetFunctor<bool> *functor)
00189     { 
00190       if (myCallback == functor) 
00191         return true; 
00192       else 
00193         return false; 
00194     }
00195     const char *getName(void) 
00196     { 
00197       if (myCallbackWithError != NULL)
00198         return myCallbackWithError->getName();
00199       else if (myCallback != NULL)
00200         return myCallback->getName();
00201       // if we get here there's a problem
00202       ArLog::log(ArLog::Terse, "ArConfig: Horrible problem with process callback names");
00203       return NULL;
00204     }
00205     protected:
00206     ArRetFunctor2<bool, char *, size_t> *myCallbackWithError;
00207     ArRetFunctor<bool> *myCallback;
00208   };
00209   void addParserHandlers(void);
00210   ArArgumentParser *myArgumentParser;
00211   std::multimap<int, ProcessFileCBType *> myProcessFileCBList;
00212   bool myNoBlanksBetweenParams;
00213   std::string mySection;
00214   bool mySectionBroken;
00215   bool myUsingSections;
00216   std::string myFileName;
00217   std::string myBaseDirectory;
00218   ArFileParser myParser;
00219   bool myIgnoreBounds;
00220   bool myFailOnBadSection;
00221   bool myDuplicateParams;
00222   ArLog::LogLevel myProcessFileCallbacksLogLevel;
00223   // our list of sections which has in it the argument list for each
00224   std::list<ArConfigSection *> mySections;
00225   // callback for the file parser
00226   ArRetFunctor3C<bool, ArConfig, ArArgumentBuilder *, char *, size_t> myParserCB;
00227   // callback for the section in the file parser
00228   ArRetFunctor3C<bool, ArConfig, ArArgumentBuilder *, char *, size_t> mySectionCB;
00229 };
00230 
00231 
00235 class ArConfigSection
00236 {
00237 public:
00238   ArConfigSection(const char *name = NULL, 
00239                                                    const char *comment = NULL);
00240   virtual ~ArConfigSection();
00241   ArConfigSection(const ArConfigSection &section);
00242   ArConfigSection &operator=(const ArConfigSection &section);
00243 
00245   const char *getName(void) const { return myName.c_str(); }
00246 
00248   const char *getComment(void) const { return myComment.c_str(); }
00249   std::list<ArConfigArg> *getParams(void) { return &myParams; }
00250   void setName(const char *name) { myName = name; }
00251   void setComment(const char *comment) { myComment = comment; }
00252 
00254   ArConfigArg *findParam(const char *paramName); 
00255 
00256 protected:
00257   std::string myName;
00258   std::string myComment;
00259   std::list<ArConfigArg> myParams;
00260 };
00261 
00262 #endif // ARCONFIG

Generated on Wed Oct 19 12:56:34 2005 for Aria by  doxygen 1.4.0