Skip to content

Commit 8a826bb

Browse files
authored
Update read_next_command() for the new FastDDS-Spy filter (#148)
* [#24061] Updated read_next_command() for the new FastDDS-Spy filter Signed-off-by: danipiza <dpizarrogallego@gmail.com> * [#24061] Uncrustify Signed-off-by: danipiza <dpizarrogallego@gmail.com> * [#24061] New uncrustify Signed-off-by: danipiza <dpizarrogallego@gmail.com> * [#24061] Changed eProsima-CI tag for test Signed-off-by: danipiza <dpizarrogallego@gmail.com> * [#24061] Reverted eProsima-CI tags to realease Signed-off-by: danipiza <dpizarrogallego@gmail.com> * [#24061] Covered case when the filter does not contain a closing quote Signed-off-by: danipiza <dpizarrogallego@gmail.com> * [#24061] Applied revision Signed-off-by: danipiza <dpizarrogallego@gmail.com> --------- Signed-off-by: danipiza <dpizarrogallego@gmail.com>
1 parent 306edca commit 8a826bb

2 files changed

Lines changed: 45 additions & 7 deletions

File tree

cpp_utils/include/cpp_utils/user_interface/CommandReader.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace utils {
2626
*
2727
* @tparam CommandEnum enumeration that represent the different commands available.
2828
*/
29-
template <typename CommandEnum>
29+
template<typename CommandEnum>
3030
struct Command
3131
{
3232
//! Command in the way of a enumeration class.
@@ -48,7 +48,7 @@ struct Command
4848
*
4949
* @note This class relies on \c StdinEventHandler to read from stdin and in \c EnumBuilder to interpret command.
5050
*/
51-
template <typename CommandEnum>
51+
template<typename CommandEnum>
5252
class CommandReader
5353
{
5454
public:
@@ -94,6 +94,9 @@ class CommandReader
9494
void read_command_callback_(
9595
std::string command_read);
9696

97+
std::vector<std::string> join_quoted_strings(
98+
const std::vector<std::string>& input);
99+
97100
//! Builder to transform string into a command enum value.
98101
EnumBuilder<CommandEnum> builder_;
99102

cpp_utils/include/cpp_utils/user_interface/impl/CommandReader.ipp

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
namespace eprosima {
2020
namespace utils {
2121

22-
template <typename CommandEnum>
22+
template<typename CommandEnum>
2323
CommandReader<CommandEnum>::CommandReader(
2424
const EnumBuilder<CommandEnum>& builder,
2525
std::istream& source /* = std::cin */)
@@ -37,22 +37,57 @@ CommandReader<CommandEnum>::CommandReader(
3737
// Do nothing
3838
}
3939

40-
template <typename CommandEnum>
40+
template<typename CommandEnum>
4141
bool CommandReader<CommandEnum>::read_next_command(
4242
Command<CommandEnum>& command)
4343
{
4444
stdin_handler_.read_one_more_line();
4545
std::string full_command = commands_read_.consume();
4646

4747
// Divide command
48-
command.arguments = utils::split_string(full_command, " ");
49-
48+
command.arguments = join_quoted_strings(utils::split_string(full_command, " "));
5049
// Check if command exists
5150
// The args are already set in command, and the enum value will be set string_to_enumeration
5251
return builder_.string_to_enumeration(command.arguments[0], command.command);
5352
}
5453

55-
template <typename CommandEnum>
54+
template<typename CommandEnum>
55+
std::vector<std::string> CommandReader<CommandEnum>::join_quoted_strings(
56+
const std::vector<std::string>& input)
57+
{
58+
std::vector<std::string> result;
59+
60+
for (size_t i = 0; i < input.size(); ++i)
61+
{
62+
// Check if string starts with a quote
63+
if (!input[i].empty() && input[i].front() == '"')
64+
{
65+
std::string joined = input[i];
66+
67+
// Keep joining until we find a string ending with a quote
68+
while (i + 1 < input.size() &&
69+
(joined.empty() || joined.back() != '"'))
70+
{
71+
joined += " " + input[++i];
72+
}
73+
74+
// Check if the last character in the joined string is
75+
// a closing quote (") or not. If it is a closing quote
76+
// remove it from the string (value = 2)
77+
// otherwise keep all the string (value = 1)
78+
int tmp = joined[joined.size() - 1] == '"' ? 2 : 1;
79+
result.push_back(joined.substr(1, joined.size() - tmp));
80+
}
81+
else
82+
{
83+
result.push_back(input[i]);
84+
}
85+
}
86+
87+
return result;
88+
}
89+
90+
template<typename CommandEnum>
5691
void CommandReader<CommandEnum>::read_command_callback_(
5792
std::string command_read)
5893
{

0 commit comments

Comments
 (0)