--- smsdaemon/util.cpp 2008/06/10 20:25:15 38 +++ smsdaemon/util.cpp 2008/12/07 00:59:05 132 @@ -7,9 +7,12 @@ #include #include #include - +#include #include +#include +#include +#include #include "util.h" using namespace std; @@ -42,13 +45,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 +107,56 @@ return retval; } + char my_toupper(char ch) + { + if (ch == 'æ') + return 'Æ'; + if (ch == 'ø') + return 'Ø'; + if (ch == 'å') + return 'Å'; + + return ::toupper(ch); + } + + char my_tolower(char ch) + { + if (ch == 'Æ') + return 'æ'; + if (ch == 'Ø') + return 'ø'; + if (ch == 'Å') + return 'å'; + + return ::tolower(ch); + } + 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 +243,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 +257,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 +293,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,9 +310,63 @@ 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); + } + + + timeval GetTimeOfDay() + { + timeval now; + gettimeofday(&now,0); + return now; } + int my_system(const char* cmd, std::string* response) + { + FILE* p; + if ((p = popen(cmd,"r")) == NULL) + return (-1); + + if (response) + { + std::string output; + char buf[256]; + while(!feof(p)) + { + int len = fread(buf,1,255, p); + buf[len] = 0; + output += buf; + } + *response = output; + } + + return (pclose(p)); + } + + string readfile(string filename) + { + string str; + ifstream in(filename.c_str()); + if (in) + { + char buffer[2048]; + in.read(buffer, 2047); + buffer[ in.gcount() ] = 0; + str = string(buffer); + in.close(); + } + else + { + string message = "Could no open "; + message += filename; + throw std::runtime_error(message); + } + return str; + } + + + }