#include "iclStringUtils.h" #include "iclStrTok.h" #include #include #include using std::string; using std::vector; namespace icl{ std::string toStr(int i, const char* format, char *buf){ if(buf){ sprintf(buf,format,i); return buf; }else{ char buf2[32]; sprintf(buf2,format,i); return buf2; } } std::string toStr(double d, const char* format, char *buf){ if(buf){ sprintf(buf,format,d); return buf; }else{ char buf2[64]; snprintf(buf2,64,format,d); return buf2; } } std::string toStr(int i, char *buf){ return toStr(i,"%d",buf); } std::string toStr(double d, char *buf){ return toStr(d,"%f",buf); } /** ICL wrapper for the C-style tolower function \ingroup STRUTILS */ inline static char toLowerX(const char &c){ return tolower(c); } /** ICL wrapper for the C-style toupper function \ingroup STRUTILS */ inline static char toUpperX(const char &c){ return toupper(c); } /// inplace lower case conversion \ingroup STRUTILS std::string &toLowerI(std::string &s){ std::for_each(s.begin(),s.end(),icl::toLowerX); return s; } /// inplace upper case conversion \ingroup STRUTILS std::string &toUpperI(std::string &s){ std::for_each(s.begin(),s.end(),icl::toUpperX); return s; } /// lower case conversion \ingroup STRUTILS std::string toLower(const std::string &s){ std::string cpy(s); return toLowerI(cpy); } /// upper case conversion \ingroup STRUTILS std::string toUpper(const std::string &s){ std::string cpy(s); return toUpperI(cpy); } std::string cat(const std::vector &v){ std::ostringstream s; std::copy(v.begin(),v.end(),std::ostream_iterator(s)); return s.str(); } std::string strFmt(int i, const char* format, char *buf=0){ if(buf){ sprintf(buf,format,i); return buf; }else{ char buf2[32]; sprintf(buf2,format,i); return buf2; } } std::string strFmt(double d, const char* format, char *buf=0){ if(buf){ sprintf(buf,format,d); return buf; }else{ char buf2[64]; snprintf(buf2,64,format,d); return buf2; } } std::string strFmt(int i, char *buf=0){ return strFmt(i,"%d",buf); } std::string strFmt(double d, char *buf=0){ return strFmt(d,"%f",buf); } vector tok(const string &s, const string &delims, bool singleCharDelims, char escapeChar){ return StrTok(s,delims,singleCharDelims,escapeChar).allTokens(); } vector &tok(const string &s, const string &delims,bool singleCharDelims, char escapeChar,vector &dst){ // todo optimize dst = tok(s,delims,singleCharDelims,escapeChar); return dst; } icl8u parse_icl8u(const std::string &s){ std::istringstream str(s); int t; str >> t; return icl8u(t); } icl32f parse_icl32f(const std::string &s){ if(s == "inf") return std::numeric_limits::infinity(); if(s == "-inf") return -std::numeric_limits::infinity(); std::istringstream str(s); icl32f f; str >> f; return f; } icl64f parse_icl64f(const std::string &s){ if(s == "inf") return std::numeric_limits::infinity(); if(s == "-inf") return -std::numeric_limits::infinity(); std::istringstream str(s); icl64f f; str >> f; return f; } bool parse_bool(const std::string &s){ std::string s2=toLower(s); if(s == "true" || s == "yes" || s == "1") return true; if(s == "false" || s == "no" || s == "0") return false; return (bool)parse_icl8u(s); } icl8u to8u(const std::string &s) { return parse(s); } icl16s to16s(const std::string &s) { return parse(s); } icl32s to32s(const std::string &s) { return parse(s); } icl32f to32f(const std::string &s) { return parse(s); } icl64f to64f(const std::string &s) { return parse(s); } MatchResult match(const std::string &text, const std::string ®exIn, int num) throw (InvalidRegularExpressionException){ string regexSave = regexIn; char *regex = const_cast(regexSave.c_str()); regex_t re; int cflags = num ? REG_EXTENDED : (REG_EXTENDED|REG_NOSUB); int status = regcomp(&re, regex, cflags); if(status != 0){ char buf[256]; regerror(status,&re,buf,256); throw InvalidRegularExpressionException(regexIn + "[Error: " + str(buf) + "]"); } std::vector matchList(num); status = regexec(&re, text.c_str(), num, num ? matchList.data() : NULL, 0); // char buf[256]; // regerror(status,&re,buf,256); //throw InvalidRegularExpressionException(regexIn + "[Error: " + str(buf) + "]"); MatchResult mr; mr.matched = !status; for(int i=0;i