--- smsdaemon/util.cpp 2008/06/11 06:46:14 43 +++ smsdaemon/util.cpp 2008/06/16 10:09:17 89 @@ -7,8 +7,9 @@ #include #include #include - +#include #include +#include #include "util.h" @@ -53,13 +54,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; } @@ -121,6 +125,14 @@ } + string str_formatint(int i) + { + std::ostringstream os; + os << i; + return os.str(); + } + + string str_characters(string str) { string rep; @@ -177,8 +189,18 @@ string document; ostringstream command; - command << "wget -O " << tempfile << " -o /dev/null \"" << url << "\""; - system( command.str().c_str() ); + command << "wget -O " << tempfile << " --tries=1 --timeout=15 -o /dev/null \"" << url << "\""; + int res = my_system( command.str().c_str() ); + + if (res<0) + { + throw( std::runtime_error("Error retrieving document")); + } + + if (res>0) + { + throw std::runtime_error("Command time out or document not found"); + } ifstream in( tempfile.c_str() ); @@ -264,7 +286,24 @@ 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) + { + FILE* p; + if ((p = popen(cmd,"w")) == NULL) + return (-1); + return (pclose(p)); } }