7 |
#include <iostream> |
#include <iostream> |
8 |
#include <fstream> |
#include <fstream> |
9 |
#include <iconv.h> |
#include <iconv.h> |
10 |
|
#include <stdexcept> |
11 |
#include <time.h> |
#include <time.h> |
12 |
|
|
13 |
#include "util.h" |
#include "util.h" |
53 |
return str; |
return str; |
54 |
} |
} |
55 |
|
|
56 |
|
bool my_isspace(char ch) |
57 |
|
{ |
58 |
|
return (isspace(ch) || ch == 0); |
59 |
|
} |
60 |
|
|
61 |
string str_trim(string str) |
string str_trim(string str) |
62 |
{ |
{ |
63 |
while (str.length() > 0 && isspace(str.at(0))) |
while (str.length() > 0 && my_isspace(str.at(0))) |
64 |
str.erase(0,1); |
str.erase(0,1); |
65 |
while (str.length() > 0 && isspace(str.at(str.length()-1))) |
while (str.length() > 0 && my_isspace(str.at(str.length()-1))) |
66 |
str.erase(str.length()-1,1); |
str.erase(str.length()-1,1); |
67 |
return str; |
return str; |
68 |
} |
} |
124 |
} |
} |
125 |
|
|
126 |
|
|
127 |
|
string str_formatint(int i) |
128 |
|
{ |
129 |
|
std::ostringstream os; |
130 |
|
os << i; |
131 |
|
return os.str(); |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
string str_characters(string str) |
string str_characters(string str) |
136 |
{ |
{ |
137 |
string rep; |
string rep; |
188 |
string document; |
string document; |
189 |
|
|
190 |
ostringstream command; |
ostringstream command; |
191 |
command << "wget -O " << tempfile << " -o /dev/null \"" << url << "\""; |
command << "wget -O " << tempfile << " --tries=1 --timeout=15 -o /dev/null \"" << url << "\""; |
192 |
system( command.str().c_str() ); |
int res = my_system( command.str().c_str() ); |
193 |
|
|
194 |
|
if (res<0) |
195 |
|
{ |
196 |
|
throw( std::runtime_error("Error retrieving document")); |
197 |
|
} |
198 |
|
|
199 |
|
if (res>0) |
200 |
|
{ |
201 |
|
throw std::runtime_error("Command time out or document not found"); |
202 |
|
} |
203 |
|
|
204 |
ifstream in( tempfile.c_str() ); |
ifstream in( tempfile.c_str() ); |
205 |
|
|
285 |
|
|
286 |
int mTimeDiff(const timeval& then, const timeval& now) |
int mTimeDiff(const timeval& then, const timeval& now) |
287 |
{ |
{ |
288 |
return uTimeDiff(then,now) / 1000; |
return ( (now.tv_sec - then.tv_sec)*1000) + |
289 |
|
((now.tv_usec-then.tv_usec)/1000); |
290 |
|
} |
291 |
|
|
292 |
|
int my_system(const char* cmd) |
293 |
|
{ |
294 |
|
FILE* p; |
295 |
|
if ((p = popen(cmd,"w")) == NULL) |
296 |
|
return (-1); |
297 |
|
return (pclose(p)); |
298 |
} |
} |
299 |
|
|
300 |
} |
} |