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" |
42 |
return str; |
return str; |
43 |
} |
} |
44 |
|
|
45 |
|
std::string str_replace_char(std::string str, char search, char replace) |
46 |
|
{ |
47 |
|
unsigned int pos = 0; |
48 |
|
|
49 |
|
while ( (pos = str.find(search,pos)) != string::npos) |
50 |
|
{ |
51 |
|
str.replace(pos, 1, 1, replace); |
52 |
|
} |
53 |
|
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 |
} |
} |
104 |
return retval; |
return retval; |
105 |
} |
} |
106 |
|
|
107 |
|
|
108 |
|
string str_toupper(string str) |
109 |
|
{ |
110 |
|
for (unsigned i=0; i<str.length(); ++i) |
111 |
|
{ |
112 |
|
str.replace(i, 1 ,1, ::toupper(str.at(i))); |
113 |
|
} |
114 |
|
return str; |
115 |
|
} |
116 |
|
|
117 |
|
string str_tolower(string str) |
118 |
|
{ |
119 |
|
for (unsigned i=0; i<str.length(); ++i) |
120 |
|
{ |
121 |
|
str.replace(i, 1 ,1, ::tolower(str.at(i))); |
122 |
|
} |
123 |
|
return str; |
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; |
157 |
return str; |
return str; |
158 |
} |
} |
159 |
|
|
160 |
|
|
161 |
|
string str_gsm2latin(string str) |
162 |
|
{ |
163 |
|
str = str_replace_char(str, 0x1c, 198); //AE |
164 |
|
str = str_replace_char(str, 0x0b, 216); //OE |
165 |
|
str = str_replace_char(str, 0x0e, 197); //AA |
166 |
|
|
167 |
|
str = str_replace_char(str, 0x1d, 230); //ae |
168 |
|
str = str_replace_char(str, 0x0c, 248); //oe |
169 |
|
str = str_replace_char(str, 0x0f, 229); //aa |
170 |
|
return str; |
171 |
|
} |
172 |
|
|
173 |
|
string str_latin2gsm(string str) |
174 |
|
{ |
175 |
|
str = str_replace_char(str, 198, 0x1c); //AE |
176 |
|
str = str_replace_char(str, 216, 0x0b); //OE |
177 |
|
str = str_replace_char(str, 197, 0x0e); //AA |
178 |
|
|
179 |
|
str = str_replace_char(str, 230, 0x1d); //ae |
180 |
|
str = str_replace_char(str, 248, 0x0c); //oe |
181 |
|
str = str_replace_char(str, 229, 0x0f); //aa |
182 |
|
return str; |
183 |
|
} |
184 |
|
|
185 |
string readUrl(string url, string tempfile) |
string readUrl(string url, string tempfile) |
186 |
{ |
{ |
187 |
char buf[128000]; |
char buf[128000]; |
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 |
|
|
218 |
return document; |
return document; |
219 |
} |
} |
220 |
|
|
221 |
string convertToUnicode(string _input) |
|
222 |
|
|
223 |
|
string iconv_wrapper(string _input, string to_format, string from_format) |
224 |
{ |
{ |
225 |
char* input,*output,*input_ptr, *output_ptr; |
char* input,*output,*input_ptr, *output_ptr; |
226 |
input = input_ptr = (char*) malloc(_input.size()+1); |
input = input_ptr = (char*) malloc(_input.size()+1); |
232 |
|
|
233 |
unsigned int realinsize,insize,outsize,realsize; |
unsigned int realinsize,insize,outsize,realsize; |
234 |
|
|
235 |
iconv_t icv = iconv_open("UTF8", "ISO8859-1"); |
iconv_t icv = iconv_open(to_format.c_str(), from_format.c_str()); |
236 |
if (icv == (iconv_t)-1) |
if (icv == (iconv_t)-1) |
237 |
{ |
{ |
238 |
perror(0); |
perror(0); |
268 |
return returnstr; |
return returnstr; |
269 |
} |
} |
270 |
|
|
271 |
|
std::string convertToUnicode(std::string str) |
272 |
|
{ |
273 |
|
return iconv_wrapper(str, "UTF-8", "ISO8859-1"); |
274 |
|
} |
275 |
|
|
276 |
|
std::string convertFromUnicode(std::string str) |
277 |
|
{ |
278 |
|
return iconv_wrapper(str, "ISO8859-1", "UTF-8"); |
279 |
|
} |
280 |
|
|
281 |
int uTimeDiff(const timeval& then, const timeval& now) |
int uTimeDiff(const timeval& then, const timeval& now) |
282 |
{ |
{ |
283 |
return ( (now.tv_sec - then.tv_sec)*1000000) + (now.tv_usec-then.tv_usec); |
return ( (now.tv_sec - then.tv_sec)*1000000) + (now.tv_usec-then.tv_usec); |
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 |
} |
} |