13 return (letter ==
' ')
21 enum state_t { start, unquoted, quote_next_letter, singly_quoted,
25 state_t state = start;
26 std::vector<std::string> cmd_tokens;
27 std::stringstream token_strm;
30 for (
auto it = raw_command.cbegin(); it != raw_command.cend(); it++)
45 if (it == raw_command.cend())
goto endloop;
52 state = quote_next_letter;
56 else if (letter ==
'\'')
58 state = singly_quoted;
62 else if (letter ==
'\"')
64 state = doubly_quoted;
78 case quote_next_letter:
93 while (letter !=
'\'')
97 if (it == raw_command.cend())
goto endloop;
111 while (letter !=
'\"')
113 token_strm << letter;
115 if (it == raw_command.cend())
goto endloop;
134 token_strm << letter;
136 if (it == raw_command.cend())
goto endloop;
144 cmd_tokens.push_back(token_strm.str());
151 else if (letter ==
'\\')
152 state = quote_next_letter;
156 else if (letter ==
'\'')
157 state = singly_quoted;
161 else if (letter ==
'\"')
162 state = doubly_quoted;
171 if ( (state == singly_quoted) || (state == doubly_quoted) ||
172 (state == quote_next_letter))
174 std::string error_msg;
175 error_msg +=
"Encountered unfinished quotations " 176 "while parsing command: ";
177 error_msg += raw_command;
178 throw std::runtime_error(error_msg);
182 if (state == unquoted)
184 cmd_tokens.push_back(token_strm.str());
191 const std::string& delimiters)
193 std::vector<std::string> str_vector;
195 char *c_str = strdup(str.c_str());
196 char *pch = strtok(c_str, delimiters.c_str());
198 while (pch !=
nullptr)
200 str_vector.push_back(pch);
201 pch = strtok(
nullptr, delimiters.c_str());
213 quote_next_value_letter };
217 std::map<std::string, std::string> dict;
218 std::stringstream key_strm, value_strm;
221 for (
auto it = str.cbegin(); it != str.cend(); it++)
235 state = quote_next_key_letter;
239 else if (letter ==
'=')
259 state = quote_next_value_letter;
264 else if (letter ==
';')
266 dict[key_strm.str()] = value_strm.str();
280 value_strm << letter;
286 case quote_next_key_letter:
297 case quote_next_value_letter:
300 value_strm << letter;
311 if (state == quote_next_value_letter)
313 std::string error_msg;
314 error_msg +=
"Encountered unfinished quotations " 315 "while parsing key-value pairs: ";
317 throw std::runtime_error(error_msg);
321 if ( (state ==
read_key) || (state == quote_next_key_letter) )
323 std::string error_msg;
324 error_msg +=
"Encountered incomplete key-value pairs " 325 "while parsing key-value pairs: ";
327 throw std::runtime_error(error_msg);
334 dict[key_strm.str()] = value_strm.str();
std::map< std::string, std::string > parse_key_value_pairs(const std::string &str)
void read_key(const LineString &key, std::istream &in)
bool is_whitespace(const char letter)
std::vector< std::string > parse_tokens(const std::string &str, const std::string &delimiters=" ")
std::vector< std::string > parse_command_tokens(const std::string &raw_command)
std::string read_value(std::istream &in)