Image Component Library (ICL)
StringUtils.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLUtils/src/ICLUtils/StringUtils.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLUtils/BasicTypes.h>
35 #include <ICLUtils/Exception.h>
36 #include <ICLUtils/Time.h>
37 
38 #include <string>
39 #include <vector>
40 #include <algorithm>
41 #include <iterator>
42 #include <sstream>
43 #include <iostream>
44 
45 namespace icl{
46  namespace utils{
47 
49 
50  template<class T>
51  inline std::ostream &icl_to_stream(std::ostream &s, T t){
52  return s << t;
53  }
54 
56 
57  template<class T>
58  inline std::istream &icl_from_stream(std::istream &s, T &t){
59  return s >> t;
60  }
61 
62 
64  template<> inline std::ostream &icl_to_stream(std::ostream &s, icl8u t){
65  return s << (int)t;
66  }
67 
68  template<> inline std::istream &icl_from_stream(std::istream &s, icl8u &t){
69  int tmp;
70  s >> tmp;
71  t = (icl8u)tmp;
72  return s;
73  }
74 
75  template<> inline std::ostream &icl_to_stream(std::ostream &s, bool b){
76  return s << (int)b;
77  }
78 
79  template<> inline std::istream &icl_from_stream(std::istream &s, bool &b){
80  int tmp;
81  s >> tmp;
82  b = (bool)tmp;
83  return s;
84  }
88  ICLUtils_API std::string &toLowerI(std::string &s);
90 
92  ICLUtils_API std::string &toUpperI(std::string &s);
93 
95  ICLUtils_API std::string toLower(const std::string &s);
96 
98  ICLUtils_API std::string toUpper(const std::string &s);
99 
101  ICLUtils_API std::vector<std::string> tok(const std::string &s, const std::string &delims = " ",
102  bool singleCharDelims=true, char escapeChar='\0');
103 
105  ICLUtils_API std::vector<std::string> &tok(const std::string &s, const std::string &delim, std::vector<std::string> &dst,
106  bool singleCharDelims=true, char escapeChar='\0');
107 
109  ICLUtils_API std::string cat(const std::vector<std::string> &v);
110 
112 
116  ICLUtils_API std::string toStr(int i, const char* format, char *buf = 0);
117 
119 
123  ICLUtils_API std::string toStr(double d, const char* format, char *buf = 0);
124 
126 
127  ICLUtils_API std::string toStr(int i, char *buf = 0);
128 
130 
131  ICLUtils_API std::string toStr(double d, char *buf = 0);
132 
133 
135  template<class T>
136  inline std::string str(const T &t){
137  std::ostringstream s;
138  s << t;
139  return s.str();
140  }
141 
143  template<>
144  inline std::string str(const icl8u &t){
145  std::ostringstream s;
146  s << (int)t;
147  return s.str();
148  }
149 
151  template<>
152  inline std::string str(const bool &b){
153  return b ? "true" : "false";
154  }
155 
157  template<> inline std::string str(const std::string &s) { return s; }
158 
160  template<> inline std::string str(char* const &pc) { return pc; }
161 
163  template<> inline std::string str(const char* const &pc) { return pc; }
164 
165 
167 
170  template<class T>
171  std::string cat(const std::vector<T> &v, const std::string &delim = ","){
172  if(!v.size()) return "";
173  std::ostringstream s; s << v[0];
174  for(unsigned int i=1;i<v.size();++i){ s << delim << v[i]; }
175  return s.str();
176  }
177 
178 
179 
181 
182  template<class T>
183  inline T parse(const std::string &s){
184  std::istringstream str(s);
185  T t;
186  str >> t;
187  return t;
188  }
189 
190  template<>
191  inline const char* parse(const std::string &s){
192  std::istringstream str(s);
193  char* t = NULL;
194  str >> t;
195  return t;
196  }
197 
199  // we use this support functions here to avoid massive header code blow!
200  ICLUtils_API icl8u parse_icl8u(const std::string &s);
201  ICLUtils_API icl32f parse_icl32f(const std::string &s);
202  ICLUtils_API icl64f parse_icl64f(const std::string &s);
203  ICLUtils_API bool parse_bool(const std::string &s);
204 
205  template<>
206  inline icl8u parse<icl8u>(const std::string &s){
207  return parse_icl8u(s);
208  }
209  template<>
210  inline icl32f parse<icl32f>(const std::string &s){
211  return parse_icl32f(s);
212  }
213  template<>
214  inline icl64f parse<icl64f>(const std::string &s){
215  return parse_icl64f(s);
216  }
217  template<>
218  inline bool parse<bool>(const std::string &s){
219  return parse_bool(s);
220  }
221  template<>
222  inline std::string parse<std::string>(const std::string &s){
223  return s;
224  }
225 
229  ICLUtils_API icl8u to8u(const std::string &s);
231 
233  ICLUtils_API icl16s to16s(const std::string &s);
234 
236  ICLUtils_API icl32s to32s(const std::string &s);
237 
239  ICLUtils_API icl32f to32f(const std::string &s);
240 
242  ICLUtils_API icl64f to64f(const std::string &s);
243 
245  template<class T>
246  inline std::vector<T> parseVec(const std::vector<std::string> &v){
247  std::vector<T> r(v.size());
248  std::transform(v.begin(),v.end(),r.begin(),parse<T>);
249  return r;
250  }
251 
253  template<class T>
254  inline std::vector<T> parseVecStr(const std::string &vecStr, const std::string &delims = ","){
255  return parseVec<T>(tok(vecStr,delims));
256  }
257 
259  template<class T>
260  inline std::vector<std::string> strVec(const std::vector<T> &v){
261  std::vector<std::string> r(v.size());
262  std::transform(v.begin(),v.end(),r.begin(),str<T>);
263  return r;
264  }
265 
266 
268 
269  struct MatchResult{
270  bool matched;
271 
272  struct Match{
273  int begin;
274  int end;
275  };
276  std::vector<std::string> submatches;
277 
279 
280  operator bool()const{ return matched; }
281  };
282 
284 
292  ICLUtils_API MatchResult match(const std::string &text, const std::string &regex, int numSubMatches = 0)
293  ;
294 
295 
297  ICLUtils_API std::string time2str(Time::value_type x);
298 
300  ICLUtils_API std::string skipWhitespaces(const std::string &s);
301 
302 
304  ICLUtils_API bool endsWith(const std::string &s, const std::string &suffix);
305 
307  ICLUtils_API bool startsWith(const std::string &s, const std::string &prefix);
308 
310 
316  ICLUtils_API void analyseHashes(const std::string &sFileName, unsigned int& nHashes, std::string::size_type& iPostfixPos);
317 
318  } // namespace utils
319 }
320 
int begin
Definition: StringUtils.h:273
ICLUtils_API MatchResult match(const std::string &text, const std::string &regex, int numSubMatches=0)
Applies a regular expression match on given text and regex pattern (internally using regex....
ICLUtils_API icl32f to32f(const std::string &s)
cast a string to an icl32f (parse)
undocument this line if you encounter any issues!
Definition: Any.h:37
ICLUtils_API std::string toStr(int i, const char *format, char *buf=0)
creates a string from a given integer
Ipp8u icl8u
8Bit unsigned integer type for the ICL
Definition: BasicTypes.h:64
ICLUtils_API std::string & toUpperI(std::string &s)
inplace upper case conversion
Utility structure for matching results.
Definition: StringUtils.h:269
ICLUtils_API std::string & toLowerI(std::string &s)
inplace lower case conversion
#define ICLUtils_API
this macros are important for creating dll's
Definition: CompatMacros.h:171
ICLQt_API void text(ImgQ &image, int x, int y, const string &text)
renders a text into an image (only available with Qt-Support)
std::vector< std::string > submatches
Definition: StringUtils.h:276
ICLUtils_API std::string cat(const std::vector< std::string > &v)
concatinates at string-vector to a single string
ICLUtils_API bool startsWith(const std::string &s, const std::string &prefix)
returns whether a given string begins with a given prefix
std::vector< T > parseVec(const std::vector< std::string > &v)
parse a vector of strings into a vector of T's
Definition: StringUtils.h:246
format
determines the color-format, that is associated with the images channels
Definition: Types.h:70
int64_t value_type
internal data type (64Bit integer)
Definition: Time.h:56
Ipp32s icl32s
32bit signed integer type for the ICL
Definition: BasicTypes.h:58
ICLUtils_API icl8u to8u(const std::string &s)
cast a string to an icl8u (parse)
Ipp32f icl32f
32Bit floating point type for the ICL
Definition: BasicTypes.h:55
ICLUtils_API bool endsWith(const std::string &s, const std::string &suffix)
returns whether a given string ends with a given suffix
ICLUtils_API std::string skipWhitespaces(const std::string &s)
crops trailing whitespaces of a string
Ipp64f icl64f
64Bit floating point type for the ICL
Definition: BasicTypes.h:52
std::ostream & icl_to_stream(std::ostream &s, T t)
compatibility function that writes a datatype instance into a stream
Definition: StringUtils.h:51
ICLUtils_API std::string toLower(const std::string &s)
lower case conversion
ICLUtils_API icl16s to16s(const std::string &s)
cast a string to an icl16s (parse)
std::string str(const T &t)
convert a data type into a string using an std::ostringstream instance
Definition: StringUtils.h:136
bool matched
was the match successful
Definition: StringUtils.h:270
ICLUtils_API void analyseHashes(const std::string &sFileName, unsigned int &nHashes, std::string::size_type &iPostfixPos)
analyses a file pattern with hash-characters
std::vector< std::string > strVec(const std::vector< T > &v)
convert a vector of T's into a vector of strings
Definition: StringUtils.h:260
int end
Definition: StringUtils.h:274
T parse(const std::string &s)
parses a string into template parameter (defined for iclXX and std::string)
Definition: StringUtils.h:183
ICLUtils_API icl32s to32s(const std::string &s)
cast a string to an icl32ss (parse)
std::istream & icl_from_stream(std::istream &s, T &t)
compability function that reads a datatype instance from a stream
Definition: StringUtils.h:58
Ipp16s icl16s
16bit signed integer type for the ICL (range [-32767, 32768 ])
Definition: BasicTypes.h:61
ICLUtils_API std::string time2str(Time::value_type x)
converts a Time::value_type (long int) into a string
std::vector< T > parseVecStr(const std::string &vecStr, const std::string &delims=",")
parse a delims seperated string into a vector of T's
Definition: StringUtils.h:254
ICLUtils_API std::vector< std::string > tok(const std::string &s, const std::string &delims=" ", bool singleCharDelims=true, char escapeChar='\0')
tokenizes a string with given delimiters (internally using a temporary StrTok instance)
ICLUtils_API std::string toUpper(const std::string &s)
upper case conversion
ICLUtils_API icl64f to64f(const std::string &s)
cast a string to an icl64f (parse)
Definition: StringUtils.h:272