--- smsdaemon/util.cpp 2008/06/09 18:15:53 26 +++ smsdaemon/util.cpp 2008/06/16 07:39:56 86 @@ -2,10 +2,12 @@ #include #include #include +#include + #include #include #include - +#include #include #include "util.h" @@ -28,15 +30,56 @@ - void str_clean(string* str, string search, string replace) + string str_replace(string str, string search, string replace) { unsigned int pos = 0; - while ( (pos = str->find(search,pos)) != string::npos) + while ( (pos = str.find(search,pos)) != string::npos) { - str->replace(pos, search.size(), replace); + str.replace(pos, search.size(), replace); pos += replace.size(); } + return str; + } + + std::string str_replace_char(std::string str, char search, char replace) + { + unsigned int pos = 0; + + while ( (pos = str.find(search,pos)) != string::npos) + { + str.replace(pos, 1, 1, replace); + } + return str; + } + + bool my_isspace(char ch) + { + return (isspace(ch) || ch == 0); + } + + string str_trim(string str) + { + while (str.length() > 0 && my_isspace(str.at(0))) + str.erase(0,1); + while (str.length() > 0 && my_isspace(str.at(str.length()-1))) + str.erase(str.length()-1,1); + return str; + } + + string str_striptags(string str) + { + unsigned int pos=0; + + while ( (pos=str.find("<",pos)) != string::npos) + { + unsigned int endpos = str.find(">",pos); + if (endpos == string::npos) + break; + str.erase(pos, (endpos-pos)+1); + } + + return str; } vector str_split(string input, string delimiter) @@ -61,14 +104,102 @@ return retval; } + + string str_toupper(string str) + { + for (unsigned i=0; i0) + { + throw std::runtime_error("Command time out or document not found"); + } ifstream in( tempfile.c_str() ); @@ -87,7 +218,9 @@ return document; } - string convertToUnicode(string _input) + + + string iconv_wrapper(string _input, string to_format, string from_format) { char* input,*output,*input_ptr, *output_ptr; input = input_ptr = (char*) malloc(_input.size()+1); @@ -99,7 +232,7 @@ unsigned int realinsize,insize,outsize,realsize; - iconv_t icv = iconv_open("UTF8", "ISO8859-1"); + iconv_t icv = iconv_open(to_format.c_str(), from_format.c_str()); if (icv == (iconv_t)-1) { perror(0); @@ -135,6 +268,16 @@ return returnstr; } + std::string convertToUnicode(std::string str) + { + return iconv_wrapper(str, "UTF-8", "ISO8859-1"); + } + + std::string convertFromUnicode(std::string str) + { + return iconv_wrapper(str, "ISO8859-1", "UTF-8"); + } + int uTimeDiff(const timeval& then, const timeval& now) { return ( (now.tv_sec - then.tv_sec)*1000000) + (now.tv_usec-then.tv_usec); @@ -142,7 +285,16 @@ int mTimeDiff(const timeval& then, const timeval& now) { - return uTimeDiff(then,now) / 1000; + return ( (now.tv_sec - then.tv_sec)*1000) + + ((now.tv_usec-then.tv_usec)/1000); + } + + int my_system(const char* cmd) + { + FILE* p; + if ((p = popen(cmd,"w")) == NULL) + return (-1); + return (pclose(p)); } }