Image Component Library (ICL)
|
00001 /******************************************************************** 00002 ** Image Component Library (ICL) ** 00003 ** ** 00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld ** 00005 ** Neuroinformatics Group ** 00006 ** Website: www.iclcv.org and ** 00007 ** http://opensource.cit-ec.de/projects/icl ** 00008 ** ** 00009 ** File : ICLUtils/src/ICLUtils/StringUtils.h ** 00010 ** Module : ICLUtils ** 00011 ** Authors: Christof Elbrechter ** 00012 ** ** 00013 ** ** 00014 ** GNU LESSER GENERAL PUBLIC LICENSE ** 00015 ** This file may be used under the terms of the GNU Lesser General ** 00016 ** Public License version 3.0 as published by the ** 00017 ** ** 00018 ** Free Software Foundation and appearing in the file LICENSE.LGPL ** 00019 ** included in the packaging of this file. Please review the ** 00020 ** following information to ensure the license requirements will ** 00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt ** 00022 ** ** 00023 ** The development of this software was supported by the ** 00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** 00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** 00026 ** Forschungsgemeinschaft (DFG) in the context of the German ** 00027 ** Excellence Initiative. ** 00028 ** ** 00029 ********************************************************************/ 00030 00031 #pragma once 00032 00033 #include <ICLUtils/CompatMacros.h> 00034 #include <ICLUtils/BasicTypes.h> 00035 #include <ICLUtils/Exception.h> 00036 #include <ICLUtils/Time.h> 00037 00038 #include <string> 00039 #include <vector> 00040 #include <algorithm> 00041 #include <iterator> 00042 #include <sstream> 00043 #include <iostream> 00044 00045 namespace icl{ 00046 namespace utils{ 00047 00049 00050 template<class T> 00051 inline std::ostream &icl_to_stream(std::ostream &s, T t){ 00052 return s << t; 00053 } 00054 00056 00057 template<class T> 00058 inline std::istream &icl_from_stream(std::istream &s, T &t){ 00059 return s >> t; 00060 } 00061 00062 00064 template<> inline std::ostream &icl_to_stream(std::ostream &s, icl8u t){ 00065 return s << (int)t; 00066 } 00067 00068 template<> inline std::istream &icl_from_stream(std::istream &s, icl8u &t){ 00069 int tmp; 00070 s >> tmp; 00071 t = (icl8u)tmp; 00072 return s; 00073 } 00074 00075 template<> inline std::ostream &icl_to_stream(std::ostream &s, bool b){ 00076 return s << (int)b; 00077 } 00078 00079 template<> inline std::istream &icl_from_stream(std::istream &s, bool &b){ 00080 int tmp; 00081 s >> tmp; 00082 b = (bool)tmp; 00083 return s; 00084 } 00088 00089 ICLUtils_API std::string &toLowerI(std::string &s); 00090 00092 ICLUtils_API std::string &toUpperI(std::string &s); 00093 00095 ICLUtils_API std::string toLower(const std::string &s); 00096 00098 ICLUtils_API std::string toUpper(const std::string &s); 00099 00101 ICLUtils_API std::vector<std::string> tok(const std::string &s, const std::string &delims = " ", 00102 bool singleCharDelims=true, char escapeChar='\0'); 00103 00105 ICLUtils_API std::vector<std::string> &tok(const std::string &s, const std::string &delim, std::vector<std::string> &dst, 00106 bool singleCharDelims=true, char escapeChar='\0'); 00107 00109 ICLUtils_API std::string cat(const std::vector<std::string> &v); 00110 00112 00116 ICLUtils_API std::string toStr(int i, const char* format, char *buf = 0); 00117 00119 00123 ICLUtils_API std::string toStr(double d, const char* format, char *buf = 0); 00124 00126 00127 ICLUtils_API std::string toStr(int i, char *buf = 0); 00128 00130 00131 ICLUtils_API std::string toStr(double d, char *buf = 0); 00132 00133 00135 template<class T> 00136 inline std::string str(const T &t){ 00137 std::ostringstream s; 00138 s << t; 00139 return s.str(); 00140 } 00141 00143 template<> 00144 inline std::string str(const icl8u &t){ 00145 std::ostringstream s; 00146 s << (int)t; 00147 return s.str(); 00148 } 00149 00151 template<> 00152 inline std::string str(const bool &b){ 00153 return b ? "true" : "false"; 00154 } 00155 00157 template<> inline std::string str(const std::string &s) { return s; } 00158 00160 template<> inline std::string str(char* const &pc) { return pc; } 00161 00163 template<> inline std::string str(const char* const &pc) { return pc; } 00164 00165 00167 00170 template<class T> 00171 std::string cat(const std::vector<T> &v, const std::string &delim = ","){ 00172 if(!v.size()) return ""; 00173 std::ostringstream s; s << v[0]; 00174 for(unsigned int i=1;i<v.size();++i){ s << delim << v[i]; } 00175 return s.str(); 00176 } 00177 00178 00179 00181 00182 template<class T> 00183 inline T parse(const std::string &s){ 00184 std::istringstream str(s); 00185 T t; 00186 str >> t; 00187 return t; 00188 } 00189 00190 template<> 00191 inline const char* parse(const std::string &s){ 00192 std::istringstream str(s); 00193 char* t; 00194 str >> t; 00195 return t; 00196 } 00197 00199 // we use this support functions here to avoid massive header code blow! 00200 ICLUtils_API icl8u parse_icl8u(const std::string &s); 00201 ICLUtils_API icl32f parse_icl32f(const std::string &s); 00202 ICLUtils_API icl64f parse_icl64f(const std::string &s); 00203 ICLUtils_API bool parse_bool(const std::string &s); 00204 00205 template<> 00206 inline icl8u parse<icl8u>(const std::string &s){ 00207 return parse_icl8u(s); 00208 } 00209 template<> 00210 inline icl32f parse<icl32f>(const std::string &s){ 00211 return parse_icl32f(s); 00212 } 00213 template<> 00214 inline icl64f parse<icl64f>(const std::string &s){ 00215 return parse_icl64f(s); 00216 } 00217 template<> 00218 inline bool parse<bool>(const std::string &s){ 00219 return parse_bool(s); 00220 } 00221 template<> 00222 inline std::string parse<std::string>(const std::string &s){ 00223 return s; 00224 } 00225 00229 00230 ICLUtils_API icl8u to8u(const std::string &s); 00231 00233 ICLUtils_API icl16s to16s(const std::string &s); 00234 00236 ICLUtils_API icl32s to32s(const std::string &s); 00237 00239 ICLUtils_API icl32f to32f(const std::string &s); 00240 00242 ICLUtils_API icl64f to64f(const std::string &s); 00243 00245 template<class T> 00246 inline std::vector<T> parseVec(const std::vector<std::string> &v){ 00247 std::vector<T> r(v.size()); 00248 std::transform(v.begin(),v.end(),r.begin(),parse<T>); 00249 return r; 00250 } 00251 00253 template<class T> 00254 inline std::vector<T> parseVecStr(const std::string &vecStr, const std::string &delims = ","){ 00255 return parseVec<T>(tok(vecStr,delims)); 00256 } 00257 00259 template<class T> 00260 inline std::vector<std::string> strVec(const std::vector<T> &v){ 00261 std::vector<std::string> r(v.size()); 00262 std::transform(v.begin(),v.end(),r.begin(),str<T>); 00263 return r; 00264 } 00265 00266 00268 00269 struct MatchResult{ 00270 bool matched; 00271 00272 struct Match{ 00273 int begin; 00274 int end; 00275 }; 00276 std::vector<std::string> submatches; 00277 00279 00280 operator bool()const{ return matched; } 00281 }; 00282 00284 00292 ICLUtils_API MatchResult match(const std::string &text, const std::string ®ex, int numSubMatches = 0) 00293 throw (InvalidRegularExpressionException); 00294 00295 00297 ICLUtils_API std::string time2str(Time::value_type x); 00298 00300 ICLUtils_API std::string skipWhitespaces(const std::string &s); 00301 00302 00304 ICLUtils_API bool endsWith(const std::string &s, const std::string &suffix); 00305 00307 ICLUtils_API bool startsWith(const std::string &s, const std::string &prefix); 00308 00310 00316 ICLUtils_API void analyseHashes(const std::string &sFileName, unsigned int& nHashes, std::string::size_type& iPostfixPos); 00317 00318 } // namespace utils 00319 } 00320