ArgsParser.cpp

Go to the documentation of this file.
00001 #include "ArgsParser.h"
00002 #include <stdlib.h>
00003 #include <stdio.h>
00004 #include <string.h>
00005 
00006 #define DEFAULT_MAX_NAMELENGTH 256
00007 #define DEFAULT_MAX_VALUELENGTH 1024
00008 
00009 void ArgsParser::test() {
00010    ArgsParser parser;
00011 
00012    bool boolValue1, boolValue2;
00013    float floatValue1, floatValue2;
00014    std::string string1, string2;
00015 
00016    int i;
00017    char c;
00018 
00019    parser.addCharArg("char", c);
00020    parser.addIntArg("int", i);
00021 
00022    parser.addBoolArg("bool3", boolValue1, true);
00023 
00024    parser.addBoolArg("bool1", boolValue1);
00025    parser.addBoolArg("bool2", boolValue2);
00026 
00027    parser.addFloatArg("float1", floatValue1);
00028    parser.addFloatArg("float2", floatValue2);
00029 
00030    parser.addStringArg("string1", string1);
00031    parser.addStringArg("string2", string2);
00032 
00033    int ret = parser.parse("c:\\data\\parserTestFile.txt");
00034    printf("%d\n", boolValue1);
00035    printf("%d\n", boolValue2);
00036    printf("%f\n", floatValue1);
00037    printf("%f\n", floatValue2);
00038    printf("%s\n", string1.c_str());
00039    printf("%s\n", string2.c_str());
00040    printf("%c\n", c);
00041    printf("%d\n", i);
00042 
00043    printf("Ret: %d\n", ret);
00044 }
00045 
00046 ArgsParser::ArgsParser(void) {
00047    m_maxNameLength = DEFAULT_MAX_NAMELENGTH;
00048    m_maxValueLength = DEFAULT_MAX_VALUELENGTH;
00049    m_ignoreUnspecified = false;
00050 }
00051 
00052 ArgsParser::~ArgsParser(void) {
00053 }
00054 
00055 int ArgsParser::parse(const char* filename) {
00056    m_file = NULL;
00057    char* buffer = NULL;
00058 
00059    if (NULL == (m_file = fopen(filename, "r"))) {
00060       return ARGS_PARSER_ERROR_NOFILE;
00061    }
00062 
00063    buffer = new char[m_maxNameLength];
00064    if (buffer == NULL) {
00065       return ARGS_PARSER_ERROR_NOMEMORY;
00066    }
00067 
00068    m_stringBuffer = new char[m_maxValueLength];
00069    if (m_stringBuffer == NULL) {
00070       delete[] buffer;
00071       return ARGS_PARSER_ERROR_NOMEMORY;
00072    }
00073 
00074    int error = 0;
00075 
00076    while (!feof(m_file)) {
00077       bool found = false;
00078 
00079       /* Extract the name of the parameter */
00080       if (fscanf(m_file, "%s:", buffer) == -1)
00081          break;
00082       char *buf = stripWhiteSpace(buffer);
00083 
00084       /* Strip the last : */
00085       if (buf[strlen(buf) - 1] == ':')
00086          buf[strlen(buf) - 1] = '\0';
00087 
00088       /* Try to match with all of the types */
00089       if (!found)
00090          found = assignBool(buf);
00091       if (!found)
00092          found = assignChar(buf);
00093       if (!found)
00094          found = assignInt(buf);
00095       if (!found)
00096          found = assignFloat(buf);
00097       if (!found)
00098          found = assignString(buf);
00099 
00100       /* If not found - check the error type and do some clean up */
00101       if (!found) {
00102          /* This is relatively low priority - only assign if no error was there before */
00103          if (!m_ignoreUnspecified && !error)
00104             error = ARGS_PARSER_ERROR_UNSPECIFIEDPARAMETER;
00105 
00106          /* Clean up trailing newlines */
00107          fgets(buf, DEFAULT_MAX_NAMELENGTH, m_file);
00108       }
00109       /* Take it out of the required buffer */
00110       else {
00111          std::map<std::string, bool>::iterator iter;
00112          iter = requiredParams.find(buf);
00113          if (iter != requiredParams.end()) {
00114             requiredParams[buf] = true;
00115          }
00116       }
00117    }
00118 
00119    /* Check for required */
00120    std::map<std::string, bool>::iterator iter = requiredParams.begin();
00121    for (; iter != requiredParams.end(); iter++) {
00122       if (iter->second == false) {
00123          error = ARGS_PARSER_ERROR_REQUIREDPARAMETER;
00124          break;
00125       }
00126    }
00127 
00128    fclose(m_file);
00129    delete[] buffer;
00130    delete[] m_stringBuffer;
00131    return error;
00132 }
00133 
00134 bool ArgsParser::assignBool(const char* name) {
00135    /* See if the map contains the name */
00136    std::map<std::string, bool*>::iterator iter;
00137    iter = boolArgs.find(name);
00138    if (iter == boolArgs.end())
00139       return false;
00140 
00141    /* Parse the file for the value */
00142    char buffer[DEFAULT_MAX_NAMELENGTH];
00143    char* buf;
00144 
00145    fgets(buffer, DEFAULT_MAX_NAMELENGTH, m_file);
00146    //  fscanf(m_file, "%s", buffer);
00147    buf = stripWhiteSpace(buffer);
00148 
00149    if (!strcmp(buf, "true")) {
00150       *(iter->second) = true;
00151    }
00152    else if (!strcmp(buf, "false")) {
00153       *(iter->second) = false;
00154    }
00155    else {
00156       error(name, buf);
00157    }
00158 
00159    return true;
00160 }
00161 
00162 bool ArgsParser::assignChar(const char* name) {
00163    /* See if the map contains the name */
00164    std::map<std::string, char*>::iterator iter;
00165    iter = charArgs.find(name);
00166    if (iter == charArgs.end())
00167       return false;
00168 
00169    /* Parse the file for the value */
00170    fgets(m_stringBuffer, DEFAULT_MAX_NAMELENGTH, m_file);
00171    char* buf = stripWhiteSpace(m_stringBuffer);
00172    *(iter->second) = buf[0];
00173 
00174    return true;
00175 }
00176 
00177 bool ArgsParser::assignInt(const char* name) {
00178    /* See if the map contains the name */
00179    std::map<std::string, int*>::iterator iter;
00180    iter = intArgs.find(name);
00181    if (iter == intArgs.end())
00182       return false;
00183 
00184    /* Parse the file for the value */
00185    int value;
00186    fscanf(m_file, "%d", &value);
00187    *(iter->second) = value;
00188 
00189    return true;
00190 }
00191 
00192 bool ArgsParser::assignFloat(const char* name) {
00193    /* See if the map contains the name */
00194    std::map<std::string, float*>::iterator iter;
00195    iter = floatArgs.find(name);
00196    if (iter == floatArgs.end())
00197       return false;
00198 
00199    /* Parse the file for the value */
00200    float value;
00201    fscanf(m_file, "%f", &value);
00202    *(iter->second) = value;
00203 
00204    return true;
00205 }
00206 
00207 bool ArgsParser::assignString(const char* name) {
00208    /* See if the map contains the name */
00209    std::map<std::string, std::string*>::iterator iter;
00210    iter = stringArgs.find(name);
00211    if (iter == stringArgs.end())
00212       return false;
00213 
00214    /* Parse the file for the value */
00215    fgets(m_stringBuffer, DEFAULT_MAX_NAMELENGTH, m_file);
00216    char* buf = stripWhiteSpace(m_stringBuffer);
00217 
00218    *(iter->second) = buf;
00219 
00220    return true;
00221 }
00222 
00223 char* ArgsParser::stripWhiteSpace(char* buffer) {
00224    while (*buffer == ' ' || *buffer == '\n' || 
00225           *buffer == '\t' || *buffer == '\r') {
00226       buffer++;
00227    }
00228 
00229    size_t len = strlen(buffer);
00230 
00231    while ( buffer[len - 1] == ' ' || buffer[len - 1] == '\n' || 
00232            buffer[len - 1] == '\t' || buffer[len - 1] == '\r') {
00233       buffer[len - 1] = '\0';
00234       len--;
00235    }
00236    return buffer;
00237 }
00238 
00239 void ArgsParser::error(const char*, const char*) {
00240 }
00241 
00242 void ArgsParser::addBoolArg(const char* argName, bool &value, bool required) {
00243    boolArgs[argName] = &value;
00244    if (required) {
00245       requiredParams[argName] = false;
00246    }
00247 }
00248 void ArgsParser::addCharArg(const char* argName, char &value, bool required) {
00249    charArgs[argName] = &value;
00250    if (required) {
00251       requiredParams[argName] = false;
00252    }
00253 }
00254 void ArgsParser::addIntArg(const char* argName, int &value, bool required) {
00255    intArgs[argName] = &value;
00256    if (required) {
00257       requiredParams[argName] = false;
00258    }
00259 }
00260 void ArgsParser::addFloatArg(const char *argName, float &value, bool required) {
00261    floatArgs[argName] = &value;
00262    if (required) {
00263       requiredParams[argName] = false;
00264    }
00265 }
00266 void ArgsParser::addStringArg(const char *argName, std::string &value, bool required) {
00267    stringArgs[argName] = &value;
00268    if (required) {
00269       requiredParams[argName] = false;
00270    }
00271 }
00272 
00273 void ArgsParser::setMaxParameterNameLength(unsigned int length) {
00274    m_maxNameLength = length;
00275 }
00276 void ArgsParser::setMaxParameterValueLength(unsigned int length) {
00277    m_maxValueLength = length;
00278 }
00279 void ArgsParser::ignoreUnspecifiedParams(bool ignore) {
00280    m_ignoreUnspecified = ignore;
00281 }
00282 

Generated on 28 Feb 2009 for Milton by doxygen 1.5.6