Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cpp_utils/include/cpp_utils/user_interface/CommandReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ class CommandReader
void read_command_callback_(
std::string command_read);

std::vector<std::string> join_quoted_strings(
const std::vector<std::string>& input);

//! Builder to transform string into a command enum value.
EnumBuilder<CommandEnum> builder_;

Expand Down
36 changes: 34 additions & 2 deletions cpp_utils/include/cpp_utils/user_interface/impl/CommandReader.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,45 @@ bool CommandReader<CommandEnum>::read_next_command(
std::string full_command = commands_read_.consume();

// Divide command
command.arguments = utils::split_string(full_command, " ");

command.arguments = join_quoted_strings(utils::split_string(full_command, " "));
// Check if command exists
// The args are already set in command, and the enum value will be set string_to_enumeration
return builder_.string_to_enumeration(command.arguments[0], command.command);
}

template <typename CommandEnum>
std::vector<std::string> CommandReader<CommandEnum>::join_quoted_strings(
const std::vector<std::string>& input)
{
std::vector<std::string> result;

for (size_t i = 0; i < input.size(); ++i)
{
// Check if string starts with a quote
if (!input[i].empty() && input[i].front() == '"')
{
std::string joined = input[i];

// Keep joining until we find a string ending with a quote
while (i + 1 < input.size() &&
(joined.empty() || joined.back() != '"'))
{
joined += " " + input[++i];
}

result.push_back(joined.substr(1, joined.size() - 2));
}
else
{
result.push_back(input[i]);
}
}

return result;
}



template <typename CommandEnum>
void CommandReader<CommandEnum>::read_command_callback_(
std::string command_read)
Expand Down
Loading