X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?p=physics.git;a=blobdiff_plain;f=src%2Fconfig%2Freader.cpp;h=de6da20584224d70268c62461507644e69c75c39;hp=ce35ab89f50f270f7428af438e4021bf0054d807;hb=07df2aea152374f1d2be55bfa0a05ba55fc1a112;hpb=87c9b12ffbcb8458c7e031045d34d2a1de5ea78a diff --git a/src/config/reader.cpp b/src/config/reader.cpp index ce35ab8..de6da20 100644 --- a/src/config/reader.cpp +++ b/src/config/reader.cpp @@ -30,6 +30,7 @@ char* configDir = "../configs/"; /// ***** Private Method Headers ***** void processLine(const string& str, keyMap* map); +bool extractLine(const string& str, string* name, string* value); /// ***** Private Variables ***** @@ -68,5 +69,108 @@ void readConfigs(keyMap* map) void processLine(const string& str, keyMap* map) { - cout << str << endl; + string name; + string value; + + bool extracted; + + extracted = extractLine(str, &name, &value); + + if(extracted) + { + cout << name << endl; + cout << value << endl; + } +} + +/* + * Return true iff the string str, had a name and value extracted and saved in + * name and value. Otherwise return false and leave name and value alone. + */ +bool extractLine(const string& str, string* name, string* value) +{ + /* + * state := 0 if eating leading name whitespace + * := 1 if saving name + * := 2 if eating trailing name whitespace + * := 3 if eating leading value whitespace + * := 4 if saving value + * := 5 if all is done and well + */ + int state = 0; + + int char_pos = 0; + + int name_start; + int name_end; + + int value_start; + int value_end; + + const char* c_str = str.c_str(); + + while(true) + { + if(c_str[char_pos] == 0 && state != 4) + return false; + + switch (state) + { + case 0: + if(c_str[char_pos] != ' ' + && c_str[char_pos] != '\t') + { + state++; + name_start = char_pos; + } + break; + case 1: + if(c_str[char_pos] == ' ' + || c_str[char_pos] == '\t' + || c_str[char_pos] == '=') + { + state++; + name_end = char_pos; + } + if(c_str[char_pos] == '=') + char_pos--; // decrement to stay on this char + break; + case 2: + if(c_str[char_pos] == '=') + { + state++; + } + break; + case 3: + if(c_str[char_pos] != ' ' + && c_str[char_pos] != '\t') + { + state++; + value_start = char_pos; + } + break; + case 4: + if(c_str[char_pos] == ' ' + || c_str[char_pos] == '\t' + || c_str[char_pos] == 0) + { + state++; + value_end = char_pos; + } + break; + } + + if(state == 5) + break; + + char_pos++; + } + + name->clear(); + value->clear(); + + name->replace(0, 0, c_str, name_start, name_end-name_start); + value->replace(0, 0, c_str, value_start, value_end-value_start); + + return true; }