Skip to content

Instantly share code, notes, and snippets.

@hoxnox
Created November 3, 2018 06:43
Show Gist options
  • Save hoxnox/a864c7f7aa120d57711076c623659073 to your computer and use it in GitHub Desktop.
Save hoxnox/a864c7f7aa120d57711076c623659073 to your computer and use it in GitHub Desktop.
string to argv/agrc for testing purposes (doesn't handle one quote correctly)
std::vector<char*>
str2argv(std::string& str)
{
std::vector<char*> rs;
bool was_space = true;
bool in_quotes = false;
for (size_t i = 0; i < str.length(); ++i)
{
if (std::isspace(str[i]) && !in_quotes)
{
if (!was_space)
was_space = true;
str[i] = 0;
continue;
}
if (str[i] == '\'' || str[i] == '"')
{
str[i] = 0;
if (in_quotes)
{
was_space = true;
in_quotes = false;
continue;
}
in_quotes = true;
rs.emplace_back(const_cast<char*>(str.c_str()) + i + 1);
was_space = false;
continue;
}
if (!was_space)
continue;
rs.emplace_back(const_cast<char*>(str.c_str()) + i);
was_space = false;
}
return rs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment