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.GPL ** 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 <string> 00034 #include <vector> 00035 #include <algorithm> 00036 #include <iterator> 00037 #include <sstream> 00038 #include <iostream> 00039 00040 #include <ICLUtils/BasicTypes.h> 00041 #include <ICLUtils/Exception.h> 00042 #include <ICLUtils/Time.h> 00043 00044 namespace icl{ 00045 namespace utils{ 00046 00048 00049 template<class T> 00050 inline std::ostream &icl_to_stream(std::ostream &s, T t){ 00051 return s << t; 00052 } 00053 00055 00056 template<class T> 00057 inline std::istream &icl_from_stream(std::istream &s, T &t){ 00058 return s >> t; 00059 } 00060 00061 00063 template<> inline std::ostream &icl_to_stream(std::ostream &s, icl8u t){ 00064 return s << (int)t; 00065 } 00066 00067 template<> inline std::istream &icl_from_stream(std::istream &s, icl8u &t){ 00068 int tmp; 00069 s >> tmp; 00070 t = (icl8u)tmp; 00071 return s; 00072 } 00073 00074 template<> inline std::ostream &icl_to_stream(std::ostream &s, bool b){ 00075 return s << (int)b; 00076 } 00077 00078 template<> inline std::istream &icl_from_stream(std::istream &s, bool &b){ 00079 int tmp; 00080 s >> tmp; 00081 b = (bool)tmp; 00082 return s; 00083 } 00087 00088 std::string &toLowerI(std::string &s); 00089 00091 std::string &toUpperI(std::string &s); 00092 00094 std::string toLower(const std::string &s); 00095 00097 std::string toUpper(const std::string &s); 00098 00100 std::vector<std::string> tok(const std::string &s, const std::string &delims=" ", 00101 bool singleCharDelims=true, char escapeChar='\0'); 00102 00104 std::vector<std::string> &tok(const std::string &s, const std::string &delim, std::vector<std::string> &dst, 00105 bool singleCharDelims=true, char escapeChar='\0'); 00106 00108 std::string cat(const std::vector<std::string> &v); 00109 00111 00115 std::string toStr(int i, const char* format, char *buf=0); 00116 00118 00122 std::string toStr(double d, const char* format, char *buf=0); 00123 00125 00126 std::string toStr(int i, char *buf=0); 00127 00129 00130 std::string toStr(double d, char *buf=0); 00131 00132 00134 template<class T> 00135 inline std::string str(const T &t){ 00136 std::ostringstream s; 00137 s << t; 00138 return s.str(); 00139 } 00140 00142 template<> 00143 inline std::string str(const icl8u &t){ 00144 std::ostringstream s; 00145 s << (int)t; 00146 return s.str(); 00147 } 00148 00150 template<> 00151 inline std::string str(const bool &b){ 00152 return b ? "true" : "false"; 00153 } 00154 00156 template<> inline std::string str(const std::string &s) { return s; } 00157 00159 template<> inline std::string str(char* const &pc) { return pc; } 00160 00162 template<> inline std::string str(const char* const &pc) { return pc; } 00163 00164 00166 00169 template<class T> 00170 std::string cat(const std::vector<T> &v, const std::string &delim = ","){ 00171 if(!v.size()) return ""; 00172 std::ostringstream s; s << v[0]; 00173 for(unsigned int i=1;i<v.size();++i){ s << delim << v[i]; } 00174 return s.str(); 00175 } 00176 00177 00178 00180 00181 template<class T> 00182 inline T parse(const std::string &s){ 00183 std::istringstream str(s); 00184 T t; 00185 str >> t; 00186 return t; 00187 } 00189 // we use this support functions here to avoid massive header code blow! 00190 icl8u parse_icl8u(const std::string &s); 00191 icl32f parse_icl32f(const std::string &s); 00192 icl64f parse_icl64f(const std::string &s); 00193 bool parse_bool(const std::string &s); 00194 00195 template<> 00196 inline icl8u parse<icl8u>(const std::string &s){ 00197 return parse_icl8u(s); 00198 } 00199 template<> 00200 inline icl32f parse<icl32f>(const std::string &s){ 00201 return parse_icl32f(s); 00202 } 00203 template<> 00204 inline icl64f parse<icl64f>(const std::string &s){ 00205 return parse_icl64f(s); 00206 } 00207 template<> 00208 inline bool parse<bool>(const std::string &s){ 00209 return parse_bool(s); 00210 } 00211 template<> 00212 inline std::string parse<std::string>(const std::string &s){ 00213 return s; 00214 } 00215 00219 00220 icl8u to8u(const std::string &s); 00221 00223 icl16s to16s(const std::string &s); 00224 00226 icl32s to32s(const std::string &s); 00227 00229 icl32f to32f(const std::string &s); 00230 00232 icl64f to64f(const std::string &s); 00233 00235 template<class T> 00236 inline std::vector<T> parseVec(const std::vector<std::string> &v){ 00237 std::vector<T> r(v.size()); 00238 std::transform(v.begin(),v.end(),r.begin(),parse<T>); 00239 return r; 00240 } 00241 00243 template<class T> 00244 inline std::vector<T> parseVecStr(const std::string &vecStr, const std::string &delims=","){ 00245 return parseVec<T>(tok(vecStr,delims)); 00246 } 00247 00249 template<class T> 00250 inline std::vector<std::string> strVec(const std::vector<T> &v){ 00251 std::vector<std::string> r(v.size()); 00252 std::transform(v.begin(),v.end(),r.begin(),str<T>); 00253 return r; 00254 } 00255 00256 00258 00259 struct MatchResult{ 00260 bool matched; 00261 00262 struct Match{ 00263 int begin; 00264 int end; 00265 }; 00266 std::vector<std::string> submatches; 00267 00269 00270 operator bool()const{ return matched; } 00271 }; 00272 00274 00282 MatchResult match(const std::string &text, const std::string ®ex, int numSubMatches=0) 00283 throw (InvalidRegularExpressionException); 00284 00285 00287 std::string time2str(Time::value_type x); 00288 00290 std::string skipWhitespaces(const std::string &s); 00291 00292 00294 bool endsWith(const std::string &s,const std::string &suffix); 00295 00297 bool startsWith(const std::string &s, const std::string &prefix); 00298 00300 00306 void analyseHashes (const std::string &sFileName, unsigned int& nHashes, std::string::size_type& iPostfixPos); 00307 00308 } // namespace utils 00309 } 00310