00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef ARCONFIG_H
00027 #define ARCONFIG_H
00028
00029 #include "ArConfigArg.h"
00030 #include "ArFileParser.h"
00031 #include <set>
00032
00033 class ArArgumentBuilder;
00034 class ArConfigSection;
00035
00037
00055 class ArConfig
00056 {
00057 public:
00059 AREXPORT ArConfig(const char *baseDirectory = NULL,
00060 bool noBlanksBetweenParams = false,
00061 bool ignoreBounds = false,
00062 bool failOnBadSection = false,
00063 bool saveUnknown = true);
00064
00066 AREXPORT ArConfig(const ArConfig &config);
00067
00068 AREXPORT ArConfig &operator=(const ArConfig &config);
00069
00071 AREXPORT virtual ~ArConfig();
00072
00074 AREXPORT virtual void setConfigName(const char *configName,
00075 const char *robotName = NULL);
00076
00078 AREXPORT bool parseFile(const char *fileName,
00079 bool continueOnError = false,
00080 bool noFileNotFoundMessage = false,
00081 char *errorBuffer = NULL,
00082 size_t errorBufferLen = 0,
00083 std::list<std::string> *sectionsToParse = NULL);
00084
00086 AREXPORT bool writeFile(const char *fileName,
00087 bool append = false,
00088 std::set<std::string> *alreadyWritten = NULL,
00089 bool writePriorities = false,
00090 std::list<std::string> *sectionsToWrite = NULL);
00091
00093 AREXPORT bool addParam(const ArConfigArg &arg,
00094 const char *sectionName = "",
00095 ArPriority::Priority priority = ArPriority::NORMAL,
00096 const char *displayHint = NULL);
00097
00099 AREXPORT bool addComment(const char *comment, const char *sectionName = "",
00100 ArPriority::Priority priority = ArPriority::NORMAL);
00102 AREXPORT void setSectionComment(const char *sectionName,
00103 const char *comment);
00105 AREXPORT void useArgumentParser(ArArgumentParser *parser);
00107
00118 AREXPORT virtual bool processFile(void) { return true; }
00121 AREXPORT void addProcessFileCB(ArRetFunctor<bool> *functor,
00122 int priority = 0);
00125 AREXPORT void addProcessFileWithErrorCB(
00126 ArRetFunctor2<bool, char *, size_t> *functor,
00127 int priority = 0);
00129 AREXPORT void remProcessFileCB(ArRetFunctor<bool> *functor);
00131 AREXPORT void remProcessFileCB(
00132 ArRetFunctor2<bool, char *, size_t> *functor);
00134 AREXPORT bool callProcessFileCallBacks(bool continueOnError,
00135 char *errorBuffer = NULL,
00136 size_t errorBufferLen = 0);
00138 AREXPORT bool parseArgument(ArArgumentBuilder *arg,
00139 char *errorBuffer = NULL,
00140 size_t errorBufferLen = 0);
00142 AREXPORT bool parseSection(ArArgumentBuilder *arg,
00143 char *errorBuffer = NULL,
00144 size_t errorBufferLen = 0);
00146 AREXPORT bool parseUnknown(ArArgumentBuilder *arg,
00147 char *errorBuffer = NULL,
00148 size_t errorBufferLen = 0);
00150 AREXPORT const char *getBaseDirectory(void) const;
00152 AREXPORT void setBaseDirectory(const char *baseDirectory);
00154 AREXPORT const char *getFileName(void) const;
00156 AREXPORT void setNoBlanksBetweenParams(bool noBlanksBetweenParams);
00157
00159 AREXPORT bool getNoBlanksBetweenParams(void);
00160
00162 AREXPORT bool parseArgumentParser(ArArgumentParser *parser,
00163 bool continueOnError = false,
00164 char *errorBuffer = NULL,
00165 size_t errorBufferLen = 0);
00166
00168 AREXPORT std::list<ArConfigSection *> *getSections(void);
00169
00172 AREXPORT ArConfigSection *findSection(const char *sectionName) const;
00174 AREXPORT void setProcessFileCallbacksLogLevel(ArLog::LogLevel level)
00175 { myProcessFileCallbacksLogLevel = level; }
00177 AREXPORT ArLog::LogLevel getProcessFileCallbacksLogLevel(void)
00178 { return myProcessFileCallbacksLogLevel; }
00180 AREXPORT void setSaveUnknown(bool saveUnknown)
00181 { mySaveUnknown = saveUnknown; }
00183 AREXPORT bool getSaveUnknown(void) { return mySaveUnknown; }
00185 AREXPORT void clearSections(void);
00187 AREXPORT void clearAll(void);
00189 AREXPORT bool addSectionFlags(const char *sectionName,
00190 const char *flags);
00192 AREXPORT bool remSectionFlag(const char *sectionName,
00193 const char *flag);
00194
00196 AREXPORT void clearAllValueSet(void);
00198 AREXPORT void removeAllUnsetValues(void);
00199
00200 AREXPORT void log(bool isSummary = true);
00201
00202 protected:
00204 AREXPORT void writeSection(ArConfigSection *section, FILE *file,
00205 std::set<std::string> *alreadyWritten,
00206 bool writePriorities);
00207
00208 void copySectionsToParse(std::list<std::string> *from);
00209
00219 class ProcessFileCBType
00220 {
00221 public:
00222 ProcessFileCBType(
00223 ArRetFunctor2<bool, char *, size_t> *functor)
00224 {
00225 myCallbackWithError = functor;
00226 myCallback = NULL;
00227 }
00228 ProcessFileCBType(ArRetFunctor<bool> *functor)
00229 {
00230 myCallbackWithError = NULL;
00231 myCallback = functor;
00232 }
00233 ~ProcessFileCBType() {}
00234 bool call(char *errorBuffer, size_t errorBufferLen)
00235 {
00236 if (myCallbackWithError != NULL)
00237 return myCallbackWithError->invokeR(errorBuffer, errorBufferLen);
00238 else if (myCallback != NULL)
00239 return myCallback->invokeR();
00240
00241 ArLog::log(ArLog::Terse, "ArConfig: Horrible problem with process callbacks");
00242 return false;
00243 }
00244 bool haveFunctor(ArRetFunctor2<bool, char *, size_t> *functor)
00245 {
00246 if (myCallbackWithError == functor)
00247 return true;
00248 else
00249 return false;
00250 }
00251 bool haveFunctor(ArRetFunctor<bool> *functor)
00252 {
00253 if (myCallback == functor)
00254 return true;
00255 else
00256 return false;
00257 }
00258 const char *getName(void)
00259 {
00260 if (myCallbackWithError != NULL)
00261 return myCallbackWithError->getName();
00262 else if (myCallback != NULL)
00263 return myCallback->getName();
00264
00265 ArLog::log(ArLog::Terse, "ArConfig: Horrible problem with process callback names");
00266 return NULL;
00267 }
00268 protected:
00269 ArRetFunctor2<bool, char *, size_t> *myCallbackWithError;
00270 ArRetFunctor<bool> *myCallback;
00271 };
00272 void addParserHandlers(void);
00273
00275 std::string myRobotName;
00277 std::string myConfigName;
00279 std::string myLogPrefix;
00280
00281 ArArgumentParser *myArgumentParser;
00282 std::multimap<int, ProcessFileCBType *> myProcessFileCBList;
00283 bool myNoBlanksBetweenParams;
00284 std::string mySection;
00285 std::list<std::string> *mySectionsToParse;
00286 bool mySectionBroken;
00287 bool mySectionIgnored;
00288 bool myUsingSections;
00289 std::string myFileName;
00290 std::string myBaseDirectory;
00291 ArFileParser myParser;
00292 bool myIgnoreBounds;
00293 bool myFailOnBadSection;
00294 bool myDuplicateParams;
00295 bool mySaveUnknown;
00296 ArLog::LogLevel myProcessFileCallbacksLogLevel;
00297
00298 std::list<ArConfigSection *> mySections;
00299
00300 ArRetFunctor3C<bool, ArConfig, ArArgumentBuilder *, char *, size_t> myParserCB;
00301
00302 ArRetFunctor3C<bool, ArConfig, ArArgumentBuilder *, char *, size_t> mySectionCB;
00303
00304 ArRetFunctor3C<bool, ArConfig, ArArgumentBuilder *, char *, size_t> myUnknownCB;
00305 };
00306
00307
00311 class ArConfigSection
00312 {
00313 public:
00314 AREXPORT ArConfigSection(const char *name = NULL,
00315 const char *comment = NULL);
00316 AREXPORT virtual ~ArConfigSection();
00317 AREXPORT ArConfigSection(const ArConfigSection §ion);
00318 AREXPORT ArConfigSection &operator=(const ArConfigSection §ion);
00319
00321 const char *getName(void) const { return myName.c_str(); }
00322
00324 const char *getComment(void) const { return myComment.c_str(); }
00325 const char *getFlags(void) const { return myFlags->getFullString(); }
00326 AREXPORT bool hasFlag(const char *flag) const;
00327 std::list<ArConfigArg> *getParams(void) { return &myParams; }
00328 void setName(const char *name) { myName = name; }
00329 void setComment(const char *comment) { myComment = comment; }
00330 bool addFlags(const char *flags) { myFlags->add(flags); return true; }
00331 AREXPORT bool remFlag(const char *dataFlag);
00333 AREXPORT ArConfigArg *findParam(const char *paramName);
00335 AREXPORT bool remStringHolder(const char *paramName);
00336
00337 protected:
00338 std::string myName;
00339 std::string myComment;
00340 ArArgumentBuilder *myFlags;
00341 std::list<ArConfigArg> myParams;
00342 };
00343
00344 #endif // ARCONFIG