1 | // |
---|
2 | // You received this file as part of RRLib |
---|
3 | // Robotics Research Library |
---|
4 | // |
---|
5 | // Copyright (C) Finroc GbR (finroc.org) |
---|
6 | // |
---|
7 | // This program is free software; you can redistribute it and/or |
---|
8 | // modify it under the terms of the GNU General Public License |
---|
9 | // as published by the Free Software Foundation; either version 2 |
---|
10 | // of the License, or (at your option) any later version. |
---|
11 | // |
---|
12 | // This program is distributed in the hope that it will be useful, |
---|
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | // GNU General Public License for more details. |
---|
16 | // |
---|
17 | // You should have received a copy of the GNU General Public License |
---|
18 | // along with this program; if not, write to the Free Software |
---|
19 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | // |
---|
21 | //---------------------------------------------------------------------- |
---|
22 | /*!\file sStringUtils.h |
---|
23 | * |
---|
24 | * \author Bernd Helge Schaefer |
---|
25 | * \author Jens Wettach |
---|
26 | * \date 15.11.05 |
---|
27 | * |
---|
28 | * |
---|
29 | * \brief Contains static class sStringUtils |
---|
30 | * |
---|
31 | */ |
---|
32 | //---------------------------------------------------------------------- |
---|
33 | // Includes |
---|
34 | //---------------------------------------------------------------------- |
---|
35 | #ifndef _rrlib_util_sStringUtils_h_ |
---|
36 | #define _rrlib_util_sStringUtils_h_ |
---|
37 | |
---|
38 | //C++ stl includes |
---|
39 | #include <vector> |
---|
40 | #include <cassert> |
---|
41 | #include <cstring> |
---|
42 | #include <sstream> |
---|
43 | #include <cstdio> |
---|
44 | #include <algorithm> |
---|
45 | #include <iterator> |
---|
46 | |
---|
47 | |
---|
48 | //---------------------------------------------------------------------- |
---|
49 | // Namespace declaration |
---|
50 | //---------------------------------------------------------------------- |
---|
51 | namespace rrlib |
---|
52 | { |
---|
53 | namespace util |
---|
54 | { |
---|
55 | /*! |
---|
56 | Stream manipulator definition for reading a string from actual position within the stream up to the end of line. |
---|
57 | */ |
---|
58 | struct imanip |
---|
59 | { |
---|
60 | std::istream& (*f)(std::istream&, std::string&); |
---|
61 | std::string& str; |
---|
62 | |
---|
63 | imanip(std::istream & (*ff)(std::istream&, std::string&), std::string& sstr) : f(ff), str(sstr) |
---|
64 | {} |
---|
65 | } |
---|
66 | ; |
---|
67 | |
---|
68 | /*! |
---|
69 | Overloading of stream operator >> to be used with new stream manipulator \em imanip. |
---|
70 | */ |
---|
71 | template <class Ch, class Tr> |
---|
72 | std::basic_istream<Ch, Tr>& operator>>(std::basic_istream<Ch, Tr>& is, imanip m) |
---|
73 | { |
---|
74 | return m.f(is, m.str); |
---|
75 | }; |
---|
76 | |
---|
77 | |
---|
78 | /*! |
---|
79 | Stream manipulator implementation for reading a string from actual position within the stream up to the end of line. |
---|
80 | */ |
---|
81 | inline std::istream& get_line(std::istream& fin, std::string& str) |
---|
82 | { |
---|
83 | std::ostringstream ss; |
---|
84 | char c; |
---|
85 | fin.get(c); |
---|
86 | while ((c == ' ' || c == '\t') && (c != '\n')) // skip leading whitespace(s) |
---|
87 | { |
---|
88 | fin.get(c); |
---|
89 | } |
---|
90 | while (c != '\n' && !fin.eof()) |
---|
91 | { |
---|
92 | //DEBUGMSG(DD_DEFAULT,DL_DEFAULT, "reading %c\n", c ); |
---|
93 | ss << c; |
---|
94 | fin.get(c); |
---|
95 | } |
---|
96 | // fin.ignore(0, '\n'); // skip end of line |
---|
97 | str = ss.str(); |
---|
98 | if (fin.eof()) |
---|
99 | { |
---|
100 | //DEBUGMSG(DD_DEFAULT,DL_DEFAULT, "---------------------------------------------------------------------------------->end of stream\n" ); |
---|
101 | } |
---|
102 | return fin; |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | class sStringUtils |
---|
109 | { |
---|
110 | |
---|
111 | private: |
---|
112 | |
---|
113 | static bool StringCompare(const std::string &left, const std::string &right) |
---|
114 | { |
---|
115 | for (std::string::const_iterator lit = left.begin(), rit = right.begin(); lit != left.end() && rit != right.end(); ++lit, ++rit) |
---|
116 | if (std::tolower(*lit) < std::tolower(*rit)) |
---|
117 | return true; |
---|
118 | else if (std::tolower(*lit) > std::tolower(*rit)) |
---|
119 | return false; |
---|
120 | if (left.size() < right.size()) |
---|
121 | return true; |
---|
122 | return false; |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | public: |
---|
127 | |
---|
128 | static const char* Description() |
---|
129 | { |
---|
130 | return "sStringUtils"; |
---|
131 | } |
---|
132 | |
---|
133 | static const size_t max_chars = 255; |
---|
134 | |
---|
135 | /*! |
---|
136 | * \brief Converts the input string to an upper case string. |
---|
137 | * |
---|
138 | * Note that if input and output string pointer are not the same, the |
---|
139 | * caller needs to assure that for the output string at least as |
---|
140 | * much bytes are allocated as the length of the input string. |
---|
141 | * Otherwise a memory violation will occur. |
---|
142 | * |
---|
143 | * \param input_str The string to be converted to upper case. |
---|
144 | * \param output_str Handle to the upper case string (May be the same as the input string for destructive mode of operation). |
---|
145 | */ |
---|
146 | static void ToUpper(const char* input_str, char* output_str) |
---|
147 | { |
---|
148 | unsigned int num_chars = strlen(input_str); |
---|
149 | |
---|
150 | if (num_chars > max_chars) |
---|
151 | { |
---|
152 | fprintf(stderr, "%s::ToUpper>> string length (%d) exceeds max chars (%zu)\n", |
---|
153 | Description(), num_chars, max_chars); |
---|
154 | fprintf(stderr, "Due to security reasons this is not permitted.\nOnly %zu chars will be converted to upper case.\n", |
---|
155 | max_chars); |
---|
156 | num_chars = max_chars; |
---|
157 | } |
---|
158 | |
---|
159 | for (unsigned int i = 0; i < num_chars; i++) |
---|
160 | { |
---|
161 | output_str [i] = toupper(input_str [i]); |
---|
162 | } |
---|
163 | output_str [num_chars] = '\0'; |
---|
164 | } |
---|
165 | |
---|
166 | /*! |
---|
167 | * \brief Converts the input string to an lower case string. |
---|
168 | * |
---|
169 | * For details see documentation of routine ToUpper(). |
---|
170 | */ |
---|
171 | static void ToLower(const char* input_str, char* output_str) |
---|
172 | { |
---|
173 | unsigned int num_chars = (strlen(input_str) < strlen(output_str) ? strlen(input_str) : strlen(output_str)); |
---|
174 | |
---|
175 | if (num_chars > max_chars) |
---|
176 | { |
---|
177 | fprintf(stderr, "%s::ToLower>> string length (%d) exceeds max chars (%zu)\n", |
---|
178 | Description(), num_chars, max_chars); |
---|
179 | fprintf(stderr, "Due to security reasons this is not permitted.\nOnly %zu chars will be converted to lower case.\n", |
---|
180 | max_chars); |
---|
181 | num_chars = max_chars; |
---|
182 | } |
---|
183 | |
---|
184 | for (unsigned int i = 0; i < num_chars; i++) |
---|
185 | { |
---|
186 | output_str [i] = tolower(input_str [i]); |
---|
187 | } |
---|
188 | output_str [num_chars] = '\0'; |
---|
189 | } |
---|
190 | |
---|
191 | /*! |
---|
192 | * \brief Removes brackets and spaces |
---|
193 | */ |
---|
194 | static void RemoveBracketsAndSpaces(const char* input_str, char* output_str, const char replace_token = '_'); |
---|
195 | |
---|
196 | /*! |
---|
197 | * \brief Splits a string into a list of tokes using the provided delimiter. |
---|
198 | * |
---|
199 | * \param str The string to be split up |
---|
200 | * \param tokens The list of tokens |
---|
201 | * \param The delimiter |
---|
202 | */ |
---|
203 | static void Tokenize(const std::string& str, |
---|
204 | std::vector<std::string>& tokens, |
---|
205 | const std::string& delimiters); |
---|
206 | |
---|
207 | |
---|
208 | /*! |
---|
209 | * \brief Replaces a section of the current string by some other content. |
---|
210 | * |
---|
211 | * \param str The string whose content should be replaced |
---|
212 | * \param target_token The part of the string that should be replaced |
---|
213 | * \param replace_token The token that should be placed in the string instead of the target_token |
---|
214 | */ |
---|
215 | static void Replace(std::string &input_str, const char* target_token, const char* replace_token); |
---|
216 | |
---|
217 | /*! |
---|
218 | * \brief Replaces one character of the current string by an other char. |
---|
219 | * |
---|
220 | * \param str The string whose content should be replaced |
---|
221 | * \param target_token The character of the string that should be replaced |
---|
222 | * \param replace_token The character that should be placed in the string instead of the target_token |
---|
223 | */ |
---|
224 | |
---|
225 | static void Replace(char *input_str, char target_token, char replace_token); |
---|
226 | |
---|
227 | static std::string ConstReplace(const std::string &input_str, const char* target_token, const char* replace_token); |
---|
228 | |
---|
229 | /*! |
---|
230 | * Stream manipulator implementation for reading a string from actual position within the stream up to the end of line. |
---|
231 | * Uses function <get_line>, but provides normal stream manipulator interface. |
---|
232 | * |
---|
233 | * Example: input_stream >> sStringUtils::getline( word ); |
---|
234 | * |
---|
235 | */ |
---|
236 | static imanip getline(std::string& str) |
---|
237 | { |
---|
238 | return imanip(get_line, str); |
---|
239 | } |
---|
240 | |
---|
241 | |
---|
242 | /*! |
---|
243 | * \brief Replaces commata by points in the provided c string. |
---|
244 | * |
---|
245 | * \param str The c string in which all commata shall be replaced by points. |
---|
246 | */ |
---|
247 | static void ReplaceCommasByPoints(char* str) |
---|
248 | { |
---|
249 | char comma [2]; |
---|
250 | memset(comma, 0, 2); |
---|
251 | comma [0] = ','; |
---|
252 | // fprintf (stderr, "test str: %s\n", test); |
---|
253 | char* temp_str = str; |
---|
254 | for (unsigned int i = 0; i < strlen(str); i++) |
---|
255 | { |
---|
256 | if (strncmp(comma, temp_str, 1) == 0) |
---|
257 | { |
---|
258 | temp_str [0] = '.'; |
---|
259 | // fprintf (stderr, "replacing: %s\n", str); |
---|
260 | } |
---|
261 | temp_str++; |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | |
---|
266 | /*! |
---|
267 | * \brief Trims whitespace at the front and the back of given string. |
---|
268 | * To be used as function object in for_each statements. |
---|
269 | * |
---|
270 | */ |
---|
271 | template<class T> |
---|
272 | struct Trim : public std::unary_function<T, void> |
---|
273 | { |
---|
274 | void operator()(T& x) |
---|
275 | { |
---|
276 | TrimWhitespace(x); |
---|
277 | } |
---|
278 | }; |
---|
279 | |
---|
280 | /*! |
---|
281 | * \brief Trims whitespace at the front and the back of given string |
---|
282 | * |
---|
283 | */ |
---|
284 | static void TrimWhitespace(std::string &text); |
---|
285 | |
---|
286 | /*! |
---|
287 | * \brief Removes multiple whitespaces in the given string |
---|
288 | * |
---|
289 | */ |
---|
290 | static void RemoveMultipleWhitespaces(std::string &text); |
---|
291 | |
---|
292 | /*! |
---|
293 | * \brief returns true if given element is first part of given text |
---|
294 | */ |
---|
295 | static bool BeginsWith(const std::string& text, const std::string& element); |
---|
296 | |
---|
297 | /*! |
---|
298 | * \brief returns true if given element is last part of given text |
---|
299 | */ |
---|
300 | static bool EndsWith(const std::string& text, const std::string& element); |
---|
301 | |
---|
302 | /*! |
---|
303 | * \brief Builds a list of strings from a list of character arrays. |
---|
304 | */ |
---|
305 | static void BuildStringListFromCharArrays(std::vector <std::string>& name_list, const char* const* names, size_t number_of_names) |
---|
306 | { |
---|
307 | assert(names != NULL); |
---|
308 | name_list.clear(); |
---|
309 | for (size_t i = 0; i < number_of_names; i++) |
---|
310 | { |
---|
311 | assert(names [i] != NULL); |
---|
312 | name_list.push_back(names [i]); |
---|
313 | } |
---|
314 | } |
---|
315 | |
---|
316 | static void BuildStringListFromCharArrays(std::vector <std::string>& name_list, const std::vector <const char*>& names) |
---|
317 | { |
---|
318 | name_list.clear(); |
---|
319 | for (size_t i = 0; i < names.size(); i++) |
---|
320 | { |
---|
321 | assert(names [i] != NULL); |
---|
322 | name_list.push_back(names [i]); |
---|
323 | } |
---|
324 | } |
---|
325 | |
---|
326 | /*! |
---|
327 | * \brief Returns true if string equals "true" or "1". |
---|
328 | * \param s string to be checked |
---|
329 | */ |
---|
330 | static bool StringToBool(std::string s); |
---|
331 | |
---|
332 | |
---|
333 | /*! |
---|
334 | * \brief Converting object to std::string and returning the string |
---|
335 | * \param object to be converted |
---|
336 | */ |
---|
337 | template <class T> |
---|
338 | static std::string StringOf(const T& object) |
---|
339 | { |
---|
340 | std::ostringstream os; |
---|
341 | os << object; |
---|
342 | return(os.str()); |
---|
343 | } |
---|
344 | |
---|
345 | /*! |
---|
346 | * \brief Converting object to std::string |
---|
347 | * \param object to be converted, string is stored in s |
---|
348 | */ |
---|
349 | template <class T> |
---|
350 | static void StringOf(const T& object, std::string & s) |
---|
351 | { |
---|
352 | std::ostringstream os; |
---|
353 | os << object; |
---|
354 | s = os.str(); |
---|
355 | } |
---|
356 | /*! |
---|
357 | * \brief Converting a std::string representing a hexadecimal number into a decimal number |
---|
358 | * \param string representing hexadecimal number |
---|
359 | */ |
---|
360 | |
---|
361 | template<class T> |
---|
362 | static T HexStringToDecNumber(std::string str) |
---|
363 | { |
---|
364 | std::istringstream iss(str); |
---|
365 | T result = 0; |
---|
366 | iss >> std::hex >> result; |
---|
367 | return result; |
---|
368 | } |
---|
369 | |
---|
370 | |
---|
371 | /*! |
---|
372 | * \brief Enclosing string with the given prefix and suffix |
---|
373 | * \param input string, prefix, and suffix |
---|
374 | * \return the enclosed string |
---|
375 | */ |
---|
376 | static std::string EncloseString(const char* string, const char* prefix, const char* suffix) |
---|
377 | { |
---|
378 | return std::string(std::string(prefix) + std::string(string) + std::string(suffix)); |
---|
379 | } |
---|
380 | |
---|
381 | /*! |
---|
382 | * \brief Enclosing string with the given prefix and suffix |
---|
383 | * \param output string, where the enclosed string will be stored, input string, prefix, and suffix |
---|
384 | */ |
---|
385 | static bool EncloseString(std::string &output_string, const char* input_string, const char* prefix, const char* suffix) |
---|
386 | { |
---|
387 | output_string = std::string(prefix) + std::string(input_string) + std::string(suffix); |
---|
388 | return true; |
---|
389 | } |
---|
390 | |
---|
391 | |
---|
392 | static bool SortStringVector(std::vector<std::string> &vec) |
---|
393 | { |
---|
394 | std::sort(vec.begin(), vec.end(), StringCompare); |
---|
395 | return true; |
---|
396 | } |
---|
397 | |
---|
398 | static int FindStringInArray(const char* str, char*const* str_array, size_t num_str_in_array) |
---|
399 | { |
---|
400 | char*const* descr_begin(&str_array[0]); |
---|
401 | char*const* descr_end(&str_array[0] + num_str_in_array); |
---|
402 | char*const* pos = std::find(descr_begin, descr_end, str); |
---|
403 | if (pos != descr_end) |
---|
404 | return std::distance(descr_begin, pos); |
---|
405 | else |
---|
406 | return -1; |
---|
407 | } |
---|
408 | |
---|
409 | }; |
---|
410 | |
---|
411 | //---------------------------------------------------------------------- |
---|
412 | // End of namespace declaration |
---|
413 | //---------------------------------------------------------------------- |
---|
414 | }; // namespace util |
---|
415 | }; // namespace rrlib |
---|
416 | #endif |
---|
417 | |
---|