--- smsdaemon/util.cpp 2008/06/10 20:25:15 38 +++ smsdaemon/util.cpp 2008/06/16 07:39:56 86 @@ -7,7 +7,7 @@ #include #include #include - +#include #include #include "util.h" @@ -42,13 +42,27 @@ 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 && isspace(str.at(0))) + while (str.length() > 0 && my_isspace(str.at(0))) str.erase(0,1); - while (str.length() > 0 && isspace(str.at(str.length()-1))) + while (str.length() > 0 && my_isspace(str.at(str.length()-1))) str.erase(str.length()-1,1); return str; } @@ -90,6 +104,34 @@ 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() ); @@ -141,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); @@ -153,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); @@ -189,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); @@ -196,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)); } }