ARGoS 3
A parallel, multi-engine simulator for swarm robotics
string_utilities.h
Go to the documentation of this file.
1
9#ifndef STRING_UTILITIES_H
10#define STRING_UTILITIES_H
11
12#include <argos3/core/utility/configuration/argos_exception.h>
13#include <argos3/core/utility/datatypes/datatypes.h>
14#include <string>
15#include <vector>
16#include <sstream>
17
18namespace argos {
19
20 /****************************************/
21 /****************************************/
22
36 template<typename T> std::string ToString(const T& t_value) {
37 std::ostringstream ss;
38 ss.setf(std::ios::boolalpha);
39 ss << t_value;
40 return ss.str();
41 }
42
43 /****************************************/
44 /****************************************/
45
59 template<typename T> T FromString(const std::string& str_value) {
60 T tReturnValue;
61 std::istringstream ss(str_value);
62 ss.setf(std::ios::boolalpha);
63 ss >> tReturnValue;
64 return tReturnValue;
65 }
66
67 /****************************************/
68 /****************************************/
69
70 template<typename T> void ParseValues(std::istream& str_input,
71 UInt32 un_num_fields,
72 T* pt_field_buffer,
73 const char ch_delimiter = '\n') {
74 std::vector<std::string> s(un_num_fields);
75 UInt32 i = 0;
76 while(i < un_num_fields && std::getline(str_input, s[i], ch_delimiter)) {
77 i++;
78 }
79 if (i == un_num_fields) {
80 str_input.clear(); // the istream was read completely and this is fine, so set the flag to 'good'
81 for(i = 0; i < un_num_fields; i++) {
82 std::istringstream iss(s[i]);
83 iss >> pt_field_buffer[i];
84 }
85 }
86 else {
87 THROW_ARGOSEXCEPTION("Parse error: expected " << un_num_fields
88 << " values, but " << i << " have been found in \""
89 << str_input.rdbuf() << "\"");
90 }
91 }
92
93 /****************************************/
94 /****************************************/
95
96 template<typename T> void ParseValues(const std::string& str_input,
97 const UInt32 un_num_fields,
98 T* pt_field_buffer,
99 const char ch_delimiter = '\n') {
100 std::istringstream issInput(str_input);
101 ParseValues(issInput, un_num_fields, pt_field_buffer, ch_delimiter);
102 }
103
104 /****************************************/
105 /****************************************/
106
113 void Tokenize(const std::string& str_string,
114 std::vector<std::string>& vec_tokens,
115 const std::string& str_delimiters = " ");
116
122 std::string StringToUpperCase(const std::string& str_string);
123
129 std::string StringToLowerCase(const std::string& str_string);
130
131 /****************************************/
132 /****************************************/
133
141 void Replace(std::string& str_buffer,
142 const std::string& str_original,
143 const std::string& str_new);
144
145 /****************************************/
146 /****************************************/
147
155 bool MatchPattern(const std::string& str_input,
156 const std::string& str_pattern);
157
158 /****************************************/
159 /****************************************/
160
167 std::string& ExpandEnvVariables(std::string& str_buffer);
168
169 /****************************************/
170 /****************************************/
171
172}
173
174#endif
unsigned int UInt32
32-bit unsigned integer.
Definition datatypes.h:97
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
The namespace containing all the ARGoS related code.
Definition ci_actuator.h:12
void Replace(std::string &str_buffer, const std::string &str_original, const std::string &str_new)
Searches into str_buffer for occurrences of str_original and substitutes them with str_new.
std::string & ExpandEnvVariables(std::string &str_buffer)
Searches into str_buffer for occurrences of an environment variable of the form $VAR and substitutes ...
bool MatchPattern(const std::string &str_input, const std::string &str_pattern)
Returns true if str_pattern is matched by str_input.
void ParseValues(std::istream &str_input, UInt32 un_num_fields, T *pt_field_buffer, const char ch_delimiter='\n')
T FromString(const std::string &str_value)
Converts the given std::string parameter to the wanted type.
std::string StringToLowerCase(const std::string &str_string)
Converts a string to lower case.
void Tokenize(const std::string &str_string, std::vector< std::string > &vec_tokens, const std::string &str_delimiters)
Tokenizes the given string according to the wanted delimiters (by default just a " ").
std::string StringToUpperCase(const std::string &str_string)
Converts a string to upper case.
std::string ToString(const T &t_value)
Converts the given parameter to a std::string.