diff --git a/cpp_utils/include/cpp_utils/user_interface/CommandReader.hpp b/cpp_utils/include/cpp_utils/user_interface/CommandReader.hpp index f73a7aed..1544c94f 100644 --- a/cpp_utils/include/cpp_utils/user_interface/CommandReader.hpp +++ b/cpp_utils/include/cpp_utils/user_interface/CommandReader.hpp @@ -26,7 +26,7 @@ namespace utils { * * @tparam CommandEnum enumeration that represent the different commands available. */ -template +template struct Command { //! Command in the way of a enumeration class. @@ -48,7 +48,7 @@ struct Command * * @note This class relies on \c StdinEventHandler to read from stdin and in \c EnumBuilder to interpret command. */ -template +template class CommandReader { public: @@ -94,6 +94,9 @@ class CommandReader void read_command_callback_( std::string command_read); + std::vector join_quoted_strings( + const std::vector& input); + //! Builder to transform string into a command enum value. EnumBuilder builder_; @@ -118,7 +121,7 @@ class CommandReader }; -} /* namespace utils */ +} /* namespace utils */ } /* namespace eprosima */ // Include implementation template file diff --git a/cpp_utils/include/cpp_utils/user_interface/impl/CommandReader.ipp b/cpp_utils/include/cpp_utils/user_interface/impl/CommandReader.ipp index 443ac811..b2e42343 100644 --- a/cpp_utils/include/cpp_utils/user_interface/impl/CommandReader.ipp +++ b/cpp_utils/include/cpp_utils/user_interface/impl/CommandReader.ipp @@ -19,7 +19,7 @@ namespace eprosima { namespace utils { -template +template CommandReader::CommandReader( const EnumBuilder& builder, std::istream& source /* = std::cin */) @@ -37,7 +37,7 @@ CommandReader::CommandReader( // Do nothing } -template +template bool CommandReader::read_next_command( Command& command) { @@ -45,21 +45,51 @@ bool CommandReader::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 +template +std::vector CommandReader::join_quoted_strings( + const std::vector& input) +{ + std::vector 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 void CommandReader::read_command_callback_( std::string command_read) { commands_read_.produce(command_read); } -} /* namespace utils */ +} /* namespace utils */ } /* namespace eprosima */ // Include implementation template file