ArgsParser.h

Go to the documentation of this file.
00001 #pragma once
00002 
00003 /* A simple generic file input parser.
00004  *
00005  * It assumes that the format of the file will be:
00006  *    PARAM_NAME1: value
00007  *    PARAM_NAME2: value
00008  *
00009  * All arguments should be on a seperate line. All parameter names must be unique.
00010  *
00011  * The implementation is simple and naive - it has running time NxM where N
00012  * is the number of requested arguments and M is the number of lines in the file.
00013  * This should not be used for anything time consuming.
00014  *
00015  * Boolean arguments should have the value of either "true" or "false".
00016  *
00017  * Original Author: Nong Li (2007)
00018  */
00019 
00020 #define ARGS_PARSER_ERROR_NOFILE -1
00021 #define ARGS_PARSER_ERROR_INVALIDFILE -2
00022 #define ARGS_PARSER_ERROR_NOMEMORY -3
00023 #define ARGS_PARSER_ERROR_REQUIREDPARAMETER -4
00024 #define ARGS_PARSER_ERROR_UNSPECIFIEDPARAMETER -5
00025 
00026 #include <string>
00027 #include <map>
00028 
00029 class ArgsParser {
00030    public:
00031 
00032       static void test();
00033 
00034       ArgsParser(void);
00035       ~ArgsParser(void);
00036 
00037       /* Returns 0 on success and an error if it failed.
00038        * There are a few #define errors at the top of this file.
00039        */
00040       int parse(const char* file);
00041 
00042       /* Sets the maximum length of the parameter name. Uses default if not specified.
00043       */
00044       void setMaxParameterNameLength(unsigned int length);
00045 
00046       /* Sets the maximum length of the parameter value. Uses default if not specified.
00047       */
00048       void setMaxParameterValueLength(unsigned int length);
00049 
00050       /* Sets whether the parser should ignore fields that have not been specified.
00051        * If ignore is set to true - parser will not report an error on parse.
00052        * If ignore is set to false - parser will continue to parse values it can but
00053        * error on parse.
00054        * Default value is false.
00055        */
00056       void ignoreUnspecifiedParams(bool ignore);
00057 
00058       /* Functions to configure the parser.  For all the functions below,
00059        * the first parameter is the exact (case-sensitive) name for the argument,
00060        * the second parameter is the value that will be assigned upon parsing and
00061        * the optional third parameter specifies whether that value is required
00062        * in the file.
00063        *
00064        */
00065       void addBoolArg(const char* argName, bool& value, bool required=false); 
00066       void addCharArg(const char* argName, char& value, bool required=false);
00067       void addIntArg(const char* argName, int& value, bool required=false);
00068       void addFloatArg(const char* argName, float& value, bool required=false);
00069       void addStringArg(const char* argName, std::string& value, bool required=false); 
00070 
00071    private:
00072       std::map<std::string, bool*> boolArgs;
00073       std::map<std::string, char*> charArgs;
00074       std::map<std::string, int*> intArgs;
00075       std::map<std::string, float*> floatArgs;
00076       std::map<std::string, std::string*> stringArgs;
00077 
00078       std::map<std::string, bool> requiredParams;
00079 
00080       unsigned int m_maxNameLength;
00081       unsigned int m_maxValueLength;
00082       char* m_stringBuffer;
00083       bool m_ignoreUnspecified;
00084       FILE* m_file;
00085 
00086       bool assignBool(const char* name);
00087       bool assignChar(const char* name);
00088       bool assignInt(const char* name);
00089       bool assignFloat(const char* name);
00090       bool assignString(const char* name);
00091 
00092       char* stripWhiteSpace(char* buffer);
00093 
00094       void error(const char* name, const char* value);
00095 };
00096 

Generated on 28 Feb 2009 for Milton by doxygen 1.5.6