/// ***** Private Method Headers *****
void processLine(const string& str, keyMap* map);
+bool extractLine(const string& str, string* name, string* value);
/// ***** Private Variables *****
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;
}