From: Patrik Gornicz Date: Fri, 22 Aug 2008 19:29:54 +0000 (-0400) Subject: wip: config reader, reading input X-Git-Tag: physics-premerge~127 X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?a=commitdiff_plain;h=07df2aea152374f1d2be55bfa0a05ba55fc1a112;p=libbear.git wip: config reader, reading input --- diff --git a/configs/test.cfg b/configs/test.cfg index e965047..c4d7b7f 100644 --- a/configs/test.cfg +++ b/configs/test.cfg @@ -1 +1,35 @@ Hello + +roger=flag0000 +roger=flag0001 +roger= flag0010 +roger= flag0011 +roger =flag0100 +roger =flag0101 +roger = flag0110 +roger = flag0111 + roger=flag1000 + roger=flag1001 + roger= flag1010 + roger= flag1011 + roger =flag1100 + roger =flag1101 + roger = flag1110 + roger = flag1111 + +roger=flag0000 +roger=flag0002 +roger= flag0020 +roger= flag0022 +roger =flag0200 +roger =flag0202 +roger = flag0220 +roger = flag0222 + roger=flag2000 + roger=flag2002 + roger= flag2020 + roger= flag2022 + roger =flag2200 + roger =flag2202 + roger = flag2220 + roger = flag2222 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; }