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

ArFileParser.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 ARFILEPARSER_H
00028 #define ARFILEPARSER_H
00029 
00030 #include "ariaTypedefs.h"
00031 #include "ArArgumentParser.h"
00032 #include "ArFunctor.h"
00033 #include "ariaUtil.h"
00034 
00036 
00056 class ArFileParser
00057 {
00058 public:
00060   ArFileParser(const char *baseDirectory = "./");
00062   ~ArFileParser(void);
00064   bool addHandler(const char *keyword, 
00065                            ArRetFunctor1<bool, ArArgumentBuilder *> *functor);
00067   bool addHandlerWithError(const char *keyword, 
00068                            ArRetFunctor3<bool, ArArgumentBuilder *, 
00069                                     char *, size_t> *functor);
00071   bool remHandler(const char *keyword, bool logIfCannotFind = true);
00073   bool remHandler(ArRetFunctor1<bool, ArArgumentBuilder *> *functor);
00075   bool remHandler(
00076           ArRetFunctor3<bool, ArArgumentBuilder *, char *, size_t> *functor);
00077   /* this shouldn't be needed and would be inelegant with the new scheme, 
00078      if someone needs it let us know and I'll update it somehow
00080   ArRetFunctor1<bool, ArArgumentBuilder *> *getHandler(const char *keyword);
00081   */
00082   
00084   bool parseFile(const char *fileName, bool continueOnErrors = true,
00085                           bool noFileNotFoundMessage = false,
00086                           char *errorBuffer = NULL, size_t errorBufferLen = 0);
00087 
00089 
00096   bool parseFile(FILE *file, char *buffer, int bufferLength, 
00097                           bool continueOnErrors = true, 
00098                           char *errorBuffer = NULL, size_t errorBufferLen = 0);
00099 
00101   const char *getBaseDirectory(void) const;
00103   void setBaseDirectory(const char *baseDirectory);
00105   bool parseLine(char *line, char *errorBuffer = NULL, 
00106                           size_t errorBufferLen = 0);
00108   void resetCounters(void);
00109 protected:
00110   class HandlerCBType
00111   {
00112     public:
00113     HandlerCBType(
00114             ArRetFunctor3<bool, ArArgumentBuilder *, char *, size_t> *functor)
00115     {
00116       myCallbackWithError = functor;
00117       myCallback = NULL;
00118     }
00119     HandlerCBType(ArRetFunctor1<bool, ArArgumentBuilder *> *functor)
00120     {
00121       myCallbackWithError = NULL;
00122       myCallback = functor;
00123     }
00124     ~HandlerCBType() {}
00125     bool call(ArArgumentBuilder *arg, char *errorBuffer, 
00126               size_t errorBufferLen) 
00127     { 
00128       if (myCallbackWithError != NULL) 
00129         return myCallbackWithError->invokeR(arg, errorBuffer, errorBufferLen);
00130       else if (myCallback != NULL) 
00131         return myCallback->invokeR(arg); 
00132       // if we get here there's a problem
00133       ArLog::log(ArLog::Terse, "ArFileParser: Horrible problem with process callbacks");
00134       return false;
00135     }
00136     bool haveFunctor(
00137             ArRetFunctor3<bool, ArArgumentBuilder *, char *, size_t> *functor)
00138     { 
00139       if (myCallbackWithError == functor) 
00140         return true; 
00141       else 
00142         return false; 
00143     }
00144     bool haveFunctor(ArRetFunctor1<bool, ArArgumentBuilder *> *functor)
00145     { 
00146       if (myCallback == functor) 
00147         return true; 
00148       else 
00149         return false; 
00150     }
00151     const char *getName(void) 
00152     { 
00153       if (myCallbackWithError != NULL)
00154         return myCallbackWithError->getName();
00155       else if (myCallback != NULL)
00156         return myCallback->getName();
00157       // if we get here there's a problem
00158       ArLog::log(ArLog::Terse, "ArFileParser: Horrible problem with process callback names");
00159       return NULL;
00160     }
00161     protected:
00162     ArRetFunctor3<bool, ArArgumentBuilder *, char *, size_t> *myCallbackWithError;
00163     ArRetFunctor1<bool, ArArgumentBuilder *> *myCallback;
00164   };
00165   int myLineNumber;
00166   std::string myBaseDir;
00167   std::map<std::string, HandlerCBType *, ArStrCaseCmpOp> myMap;
00168   // handles that NULL case
00169   HandlerCBType *myRemainderHandler;
00170 };
00171 
00172 #endif // ARFILEPARSER_H
00173 
00174 

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