Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StringUtils.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * StringUtils..h
17  * Random String functions.
18  * Copyright (C) 2005-2008 Simon Newton
19  */
20 
26 #ifndef INCLUDE_OLA_STRINGUTILS_H_
27 #define INCLUDE_OLA_STRINGUTILS_H_
28 
29 #include <stdint.h>
30 #include <sstream>
31 #include <string>
32 #include <vector>
33 
34 namespace ola {
35 
36 using std::string;
37 using std::vector;
38 
48 void StringSplit(const string &input,
49  vector<string> &tokens,
50  const string &delimiters=" ");
51 
56 void StringTrim(string *input);
57 
62 void ShortenString(string *input);
63 
70 bool StringEndsWith(const string &s, const string &suffix);
71 
77 string IntToString(int i);
78 
84 string IntToString(unsigned int i);
85 
101 void Escape(string *original);
102 
109 string EscapeString(const string &original);
110 
118 bool StringToBool(const string &value, bool *output);
119 
128 bool StringToInt(const string &value,
129  unsigned int *output,
130  bool strict = false);
131 
141 bool StringToInt(const string &value, uint16_t *output, bool strict = false);
142 
152 bool StringToInt(const string &value, uint8_t *output, bool strict = false);
153 
163 bool StringToInt(const string &value, int *output, bool strict = false);
164 
174 bool StringToInt(const string &value, int16_t *output, bool strict = false);
175 
185 bool StringToInt(const string &value, int8_t *output, bool strict = false);
186 
196 bool HexStringToInt(const string &value, uint8_t *output);
197 
207 bool HexStringToInt(const string &value, uint16_t *output);
208 
218 bool HexStringToInt(const string &value, uint32_t *output);
219 
229 bool HexStringToInt(const string &value, int8_t *output);
230 
240 bool HexStringToInt(const string &value, int16_t *output);
241 
251 bool HexStringToInt(const string &value, int32_t *output);
252 
257 void ToLower(string *s);
258 
263 void ToUpper(string *s);
264 
273 void CapitalizeLabel(string *s);
274 
284 void CustomCapitalizeLabel(string *s);
285 
298 void FormatData(std::ostream *out,
299  const uint8_t *data,
300  unsigned int length,
301  unsigned int indent = 0,
302  unsigned int byte_per_line = 8);
303 
307 template <typename int_type>
308 bool PrefixedHexStringToInt(const string &input, int_type *output) {
309  if (input.find("0x") != 0)
310  return false;
311  string modified_input = input.substr(2);
312  return HexStringToInt(modified_input, output);
313 }
314 
319 template<typename T>
320 string StringJoin(const string &delim, const T &input) {
321  std::ostringstream str;
322  typename T::const_iterator iter = input.begin();
323  while (iter != input.end()) {
324  str << *iter++;
325  if (iter != input.end())
326  str << delim;
327  }
328  return str.str();
329 }
330 } // namespace ola
331 #endif // INCLUDE_OLA_STRINGUTILS_H_