|
17 | 17 | #include <cpp_utils/utils.hpp> |
18 | 18 |
|
19 | 19 | namespace eprosima { |
20 | | -namespace utils { |
21 | | - |
22 | | -template <typename CommandEnum> |
23 | | -CommandReader<CommandEnum>::CommandReader( |
24 | | - const EnumBuilder<CommandEnum>& builder, |
25 | | - std::istream& source /* = std::cin */) |
26 | | - : builder_(builder) |
27 | | - , stdin_handler_( |
28 | | - [this](std::string st) |
| 20 | + namespace utils { |
| 21 | + |
| 22 | + template < typename CommandEnum > |
| 23 | + CommandReader < CommandEnum > ::CommandReader( |
| 24 | + const EnumBuilder < CommandEnum > &builder, |
| 25 | + std::istream& source /* = std::cin */) |
| 26 | + : builder_(builder) |
| 27 | + , stdin_handler_( |
| 28 | + [this](std::string st) |
29 | 29 | { |
30 | 30 | this->read_command_callback_(st); |
31 | 31 | }, |
32 | | - true, |
33 | | - 0, |
34 | | - source) |
35 | | - , commands_read_(0, true) |
36 | | -{ |
37 | | - // Do nothing |
38 | | -} |
39 | | - |
40 | | -template <typename CommandEnum> |
41 | | -bool CommandReader<CommandEnum>::read_next_command( |
42 | | - Command<CommandEnum>& command) |
43 | | -{ |
44 | | - stdin_handler_.read_one_more_line(); |
45 | | - std::string full_command = commands_read_.consume(); |
46 | | - |
47 | | - // Divide command |
48 | | - command.arguments = join_quoted_strings(utils::split_string(full_command, " ")); |
49 | | - // Check if command exists |
50 | | - // The args are already set in command, and the enum value will be set string_to_enumeration |
51 | | - return builder_.string_to_enumeration(command.arguments[0], command.command); |
52 | | -} |
53 | | - |
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() == '"') |
| 32 | + true, |
| 33 | + 0, |
| 34 | + source) |
| 35 | + , commands_read_(0, true) |
64 | 36 | { |
65 | | - std::string joined = input[i]; |
| 37 | + // Do nothing |
| 38 | + } |
| 39 | + |
| 40 | + template < typename CommandEnum > |
| 41 | + bool CommandReader < CommandEnum > ::read_next_command( |
| 42 | + Command < CommandEnum > &command) |
| 43 | + { |
| 44 | + stdin_handler_.read_one_more_line(); |
| 45 | + std::string full_command = commands_read_.consume(); |
| 46 | + |
| 47 | + // Divide command |
| 48 | + command.arguments = join_quoted_strings(utils::split_string(full_command, " ")); |
| 49 | + // Check if command exists |
| 50 | + // The args are already set in command, and the enum value will be set string_to_enumeration |
| 51 | + return builder_.string_to_enumeration(command.arguments[0], command.command); |
| 52 | + } |
| 53 | + |
| 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; |
66 | 59 |
|
67 | | - // Keep joining until we find a string ending with a quote |
68 | | - while (i + 1 < input.size() && |
69 | | - (joined.empty() || joined.back() != '"')) |
| 60 | + for (size_t i = 0; i < input.size(); ++i) |
70 | 61 | { |
71 | | - joined += " " + input[++i]; |
| 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 | + result.push_back(joined.substr(1, joined.size() - 2)); |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + result.push_back(input[i]); |
| 79 | + } |
72 | 80 | } |
73 | 81 |
|
74 | | - result.push_back(joined.substr(1, joined.size() - 2)); |
| 82 | + return result; |
75 | 83 | } |
76 | | - else |
77 | | - { |
78 | | - result.push_back(input[i]); |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - return result; |
83 | | -} |
84 | 84 |
|
85 | 85 |
|
86 | 86 |
|
87 | | -template <typename CommandEnum> |
88 | | -void CommandReader<CommandEnum>::read_command_callback_( |
89 | | - std::string command_read) |
90 | | -{ |
91 | | - commands_read_.produce(command_read); |
92 | | -} |
| 87 | + template < typename CommandEnum > |
| 88 | + void CommandReader < CommandEnum > ::read_command_callback_( |
| 89 | + std::string command_read) |
| 90 | + { |
| 91 | + commands_read_.produce(command_read); |
| 92 | + } |
93 | 93 |
|
94 | | -} /* namespace utils */ |
| 94 | + } /* namespace utils */ |
95 | 95 | } /* namespace eprosima */ |
96 | 96 |
|
97 | 97 | // Include implementation template file |
|
0 commit comments