--- smsdaemon/util.cpp 2008/06/10 22:22:19 42 +++ 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; @@ -53,13 +56,16 @@ 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; } @@ -101,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() ); @@ -232,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; + } + + + }