Chitika

Wednesday, October 26, 2011

C++: Load configurations


Header:
#include <cstdio>
#include <cstring> 
#include <map>
#include <fstream>
#include <algorithm>

using namespace std;

void LoadConfig(const char *fileName);
string GetConf(string key);
int GetIntConf(string key);

Definition:

char *strtrim(char *&str) {

        int len = strlen(str);
        char *ptr = str+len-1;
        while((*ptr == ' ' ||
                        *ptr == '\t' ||
                        *ptr == '\r' ||
                        *ptr == '\n') &&
                        str <= ptr) {
                ptr--;
        }
        *(ptr+1) = 0;

        ptr=str;
        while((*ptr == ' ' ||
                        *ptr == '\t' ||
                        *ptr == '\r' ||
                        *ptr == '\n') &&
                        ptr < str+len-1 &&
                        *ptr != 0) {
                ptr++;
        }

        str = ptr;
        return ptr;
}

char *strlower(char *str) {
        char *p=str;

        while(*str != 0) {

                if(*str >= 'A' &&
                                *str <= 'Z') {
                        *str -= 'A';
                        *str += 'a';
                }

                str++;
        }

        return p;
}

map<string, string> configuration;
void LoadConfig(const char* fileName) {

        char ltmp[1024];

        sprintf(ltmp, "Loading config file (%s)", fileName);
        log(ltmp);

        ifstream infile(fileName);

                        while(!infile.eof()) {

                                char tmp[2048];
                                infile.getline(tmp, sizeof(tmp));

                                if(tmp[0] == '#') {
                                        sprintf(ltmp, "skipping commnet (%s)", tmp);
                                        log(ltmp);
                                        continue;
                                }

                                char *ptmp = tmp;
                                strtrim(ptmp);
                                if(strlen(ptmp) <= 0) continue;
#define SEP "="

                                char key[1024];
                                char value[1024];
                                char *pstr=strtok(tmp, SEP);
                                strcpy(key,strlower(pstr));
                                string strKey = key;
                                pstr=strtok(NULL, SEP);
                                strcpy(value, strtrim(pstr));
                                string strValue = value;

                                sprintf(ltmp, "(%s) = (%s)", key, value);
                                log(ltmp);

                                configuration[strKey]=strValue;

                        }

                infile.close();
                configLoad = true;

                if(GetConf("DebugEnabled") == "true") {
                        debugEnabled = true;
                }
}

string GetConf(string key) {

        char tmp[1024];

        std::transform(key.begin(), key.end(), key.begin(), ::tolower);
        if(configuration.find(key) == configuration.end())
        {
                sprintf(tmp, "Key (%s) not found in configuration", key.c_str());
                log(tmp);

                return "";
        }
        return configuration[key];
}

int GetIntConf(string key) {

        char tmp[1024];

        std::transform(key.begin(), key.end(), key.begin(), ::tolower);
        if(configuration.find(key) == configuration.end())
        {
                sprintf(tmp, "Key (%s) not found in configuration", key.c_str());
                log(tmp);

                return 0;
        }
        return atoi(configuration[key].c_str());
}

for log, please see previous post minimal logging.

No comments:

Post a Comment