1 |
torben |
26 |
|
2 |
|
|
#include <string> |
3 |
|
|
#include <sstream> |
4 |
|
|
#include <vector> |
5 |
torben |
38 |
#include <cctype> |
6 |
|
|
|
7 |
torben |
26 |
#include <iostream> |
8 |
|
|
#include <fstream> |
9 |
|
|
#include <iconv.h> |
10 |
torben |
49 |
#include <stdexcept> |
11 |
torben |
26 |
#include <time.h> |
12 |
torben |
89 |
#include <sys/time.h> |
13 |
torben |
203 |
#include <sys/types.h> |
14 |
|
|
#include <sys/wait.h> |
15 |
|
|
#include <errno.h> |
16 |
torben |
114 |
#include <stdlib.h> |
17 |
torben |
26 |
|
18 |
torben |
114 |
#include <cstring> |
19 |
torben |
158 |
#include "Util.h" |
20 |
torben |
26 |
|
21 |
|
|
using namespace std; |
22 |
|
|
|
23 |
|
|
namespace Util |
24 |
|
|
{ |
25 |
torben |
141 |
const int GN_CHAR_ALPHABET_SIZE = 128; |
26 |
|
|
unsigned char gsm_default_alphabet[GN_CHAR_ALPHABET_SIZE] = |
27 |
|
|
{ |
28 |
|
|
|
29 |
|
|
/* ETSI GSM 03.38, version 6.0.1, section 6.2.1; Default alphabet */ |
30 |
|
|
/* Characters in hex position 10, [12 to 1a] and 24 are not present on |
31 |
|
|
* latin1 charset, so we cannot reproduce on the screen, however they are |
32 |
|
|
* greek symbol not present even on my Nokia */ |
33 |
|
|
|
34 |
|
|
'@', 0xa3, '$', 0xa5, 0xe8, 0xe9, 0xf9, 0xec, |
35 |
|
|
0xf2, 0xc7, '\n', 0xd8, 0xf8, '\r', 0xc5, 0xe5, |
36 |
|
|
'?', '_', '?', '?', '?', '?', '?', '?', |
37 |
|
|
'?', '?', '?', '?', 0xc6, 0xe6, 0xdf, 0xc9, |
38 |
|
|
' ', '!', '\"', '#', 0xa4, '%', '&', '\'', |
39 |
|
|
'(', ')', '*', '+', ',', '-', '.', '/', |
40 |
|
|
'0', '1', '2', '3', '4', '5', '6', '7', |
41 |
|
|
'8', '9', ':', ';', '<', '=', '>', '?', |
42 |
|
|
0xa1, 'A', 'B', 'C', 'D', 'E', 'F', 'G', |
43 |
|
|
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
44 |
|
|
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', |
45 |
|
|
'X', 'Y', 'Z', 0xc4, 0xd6, 0xd1, 0xdc, 0xa7, |
46 |
|
|
0xbf, 'a', 'b', 'c', 'd', 'e', 'f', 'g', |
47 |
|
|
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', |
48 |
|
|
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', |
49 |
|
|
'x', 'y', 'z', 0xe4, 0xf6, 0xf1, 0xfc, 0xe0 |
50 |
|
|
}; |
51 |
|
|
|
52 |
|
|
static unsigned char gsm_reverse_default_alphabet[256]; |
53 |
|
|
static bool reversed = false; |
54 |
|
|
|
55 |
|
|
static void tbl_setup_reverse() |
56 |
|
|
{ |
57 |
|
|
int i; |
58 |
|
|
|
59 |
|
|
if (reversed) return; |
60 |
|
|
memset(gsm_reverse_default_alphabet, 0x3f, 256); |
61 |
|
|
for (i = GN_CHAR_ALPHABET_SIZE - 1; i >= 0; i--) |
62 |
|
|
gsm_reverse_default_alphabet[ gsm_default_alphabet[i] ] = i; |
63 |
|
|
gsm_reverse_default_alphabet['?'] = 0x3f; |
64 |
|
|
reversed = true; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
unsigned char char_def_alphabet_encode(unsigned char value) |
68 |
|
|
{ |
69 |
|
|
tbl_setup_reverse(); |
70 |
|
|
return gsm_reverse_default_alphabet[value]; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
unsigned char char_def_alphabet_decode(unsigned char value) |
74 |
|
|
{ |
75 |
|
|
if (value < GN_CHAR_ALPHABET_SIZE) |
76 |
|
|
{ |
77 |
|
|
return gsm_default_alphabet[value]; |
78 |
|
|
} |
79 |
|
|
else |
80 |
|
|
{ |
81 |
|
|
return '?'; |
82 |
|
|
} |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
|
86 |
torben |
26 |
void str_dump(const string& str) |
87 |
|
|
{ |
88 |
torben |
141 |
for (unsigned i=0; i<str.length(); ++i) |
89 |
|
|
{ |
90 |
torben |
26 |
cout.width(2); |
91 |
|
|
cout.fill('0'); |
92 |
|
|
cout << hex << static_cast<int>(str.at(i)) << " "; |
93 |
|
|
} |
94 |
|
|
cout.width(1); |
95 |
|
|
cout << dec << endl; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
torben |
38 |
string str_replace(string str, string search, string replace) |
101 |
torben |
26 |
{ |
102 |
|
|
unsigned int pos = 0; |
103 |
torben |
141 |
|
104 |
torben |
38 |
while ( (pos = str.find(search,pos)) != string::npos) |
105 |
torben |
26 |
{ |
106 |
torben |
38 |
str.replace(pos, search.size(), replace); |
107 |
torben |
26 |
pos += replace.size(); |
108 |
|
|
} |
109 |
torben |
38 |
return str; |
110 |
torben |
26 |
} |
111 |
|
|
|
112 |
torben |
141 |
std::string str_replace_char(std::string str, char search, char replace) |
113 |
torben |
42 |
{ |
114 |
|
|
unsigned int pos = 0; |
115 |
|
|
|
116 |
|
|
while ( (pos = str.find(search,pos)) != string::npos) |
117 |
|
|
{ |
118 |
|
|
str.replace(pos, 1, 1, replace); |
119 |
|
|
} |
120 |
|
|
return str; |
121 |
|
|
} |
122 |
|
|
|
123 |
torben |
78 |
bool my_isspace(char ch) |
124 |
|
|
{ |
125 |
|
|
return (isspace(ch) || ch == 0); |
126 |
|
|
} |
127 |
torben |
38 |
|
128 |
|
|
string str_trim(string str) |
129 |
|
|
{ |
130 |
torben |
78 |
while (str.length() > 0 && my_isspace(str.at(0))) |
131 |
torben |
38 |
str.erase(0,1); |
132 |
torben |
78 |
while (str.length() > 0 && my_isspace(str.at(str.length()-1))) |
133 |
torben |
38 |
str.erase(str.length()-1,1); |
134 |
|
|
return str; |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
string str_striptags(string str) |
138 |
|
|
{ |
139 |
|
|
unsigned int pos=0; |
140 |
|
|
|
141 |
|
|
while ( (pos=str.find("<",pos)) != string::npos) |
142 |
|
|
{ |
143 |
|
|
unsigned int endpos = str.find(">",pos); |
144 |
|
|
if (endpos == string::npos) |
145 |
|
|
break; |
146 |
|
|
str.erase(pos, (endpos-pos)+1); |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
return str; |
150 |
|
|
} |
151 |
|
|
|
152 |
torben |
185 |
|
153 |
|
|
vector<string> str_split(string input) |
154 |
|
|
{ |
155 |
|
|
string buf; |
156 |
|
|
stringstream ss(input); |
157 |
|
|
vector<string> tokens; |
158 |
|
|
while (ss >> buf) |
159 |
|
|
tokens.push_back(buf); |
160 |
|
|
return tokens; |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
|
164 |
torben |
26 |
vector<string> str_split(string input, string delimiter) |
165 |
|
|
{ |
166 |
torben |
141 |
vector<string> retval; |
167 |
torben |
26 |
|
168 |
|
|
while ( 1 ) |
169 |
|
|
{ |
170 |
|
|
unsigned int endpos = input.find(delimiter); |
171 |
|
|
|
172 |
|
|
string entry = input.substr(0,endpos); |
173 |
|
|
retval.push_back( entry ); |
174 |
|
|
|
175 |
|
|
|
176 |
|
|
if (endpos == string::npos) |
177 |
|
|
break; |
178 |
|
|
|
179 |
|
|
endpos += delimiter.length(); |
180 |
|
|
input = input.substr(endpos, input.length() - endpos); |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
return retval; |
184 |
|
|
} |
185 |
|
|
|
186 |
torben |
183 |
char danish_map[3][2] = { {198,230}, {216,248}, {197,229} }; // aelig, oslash, aring |
187 |
|
|
|
188 |
|
|
unsigned char my_toupper(unsigned char ch) |
189 |
torben |
108 |
{ |
190 |
torben |
183 |
if (ch == 230) |
191 |
|
|
return 198; |
192 |
|
|
if (ch == 248) |
193 |
|
|
return 216; |
194 |
|
|
if (ch == 229) |
195 |
|
|
return 197; |
196 |
torben |
43 |
|
197 |
torben |
108 |
return ::toupper(ch); |
198 |
|
|
} |
199 |
|
|
|
200 |
torben |
183 |
unsigned char my_tolower(unsigned char ch) |
201 |
torben |
108 |
{ |
202 |
torben |
183 |
if (ch == 198) |
203 |
|
|
return 230; |
204 |
|
|
if (ch == 216) |
205 |
|
|
return 248; |
206 |
|
|
if (ch == 197) |
207 |
|
|
return 229; |
208 |
torben |
108 |
|
209 |
|
|
return ::tolower(ch); |
210 |
|
|
} |
211 |
torben |
141 |
string str_toupper(string str) |
212 |
torben |
43 |
{ |
213 |
|
|
for (unsigned i=0; i<str.length(); ++i) |
214 |
|
|
{ |
215 |
torben |
108 |
str.replace(i, 1 ,1, my_toupper(str.at(i))); |
216 |
torben |
43 |
} |
217 |
|
|
return str; |
218 |
|
|
} |
219 |
|
|
|
220 |
|
|
string str_tolower(string str) |
221 |
|
|
{ |
222 |
|
|
for (unsigned i=0; i<str.length(); ++i) |
223 |
|
|
{ |
224 |
torben |
108 |
str.replace(i, 1 ,1, my_tolower(str.at(i))); |
225 |
torben |
43 |
} |
226 |
|
|
return str; |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
|
230 |
torben |
59 |
string str_formatint(int i) |
231 |
|
|
{ |
232 |
|
|
std::ostringstream os; |
233 |
|
|
os << i; |
234 |
|
|
return os.str(); |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
|
238 |
torben |
38 |
string str_characters(string str) |
239 |
|
|
{ |
240 |
|
|
string rep; |
241 |
|
|
rep.append(1,197); |
242 |
|
|
str = str_replace(str, "Å", rep); |
243 |
torben |
186 |
str = str_replace(str, "Å", rep); |
244 |
torben |
38 |
|
245 |
|
|
rep.at(0) = 198; |
246 |
|
|
str = str_replace(str, "Æ", rep); |
247 |
torben |
186 |
str = str_replace(str, "Æ", rep); |
248 |
torben |
38 |
|
249 |
|
|
rep.at(0) = 216; |
250 |
|
|
str = str_replace(str, "Ø", rep); |
251 |
torben |
186 |
str = str_replace(str, "Ø", rep); |
252 |
torben |
38 |
|
253 |
|
|
|
254 |
|
|
rep.at(0) = 229; |
255 |
|
|
str = str_replace(str, "å", rep); |
256 |
torben |
186 |
str = str_replace(str, "å", rep); |
257 |
torben |
38 |
|
258 |
|
|
rep.at(0) = 230; |
259 |
|
|
str = str_replace(str, "æ", rep); |
260 |
torben |
186 |
str = str_replace(str, "æ", rep); |
261 |
torben |
38 |
|
262 |
|
|
rep.at(0) = 248; |
263 |
|
|
str = str_replace(str, "ø", rep); |
264 |
torben |
186 |
str = str_replace(str, "ø", rep); |
265 |
torben |
38 |
|
266 |
|
|
return str; |
267 |
|
|
} |
268 |
|
|
|
269 |
torben |
42 |
|
270 |
torben |
43 |
string str_gsm2latin(string str) |
271 |
torben |
42 |
{ |
272 |
torben |
196 |
for (unsigned i=0; i<str.size(); i++) |
273 |
|
|
{ |
274 |
torben |
141 |
str.at(i) = char_def_alphabet_decode(str.at(i)); |
275 |
|
|
} |
276 |
torben |
42 |
return str; |
277 |
|
|
} |
278 |
|
|
|
279 |
torben |
43 |
string str_latin2gsm(string str) |
280 |
|
|
{ |
281 |
torben |
196 |
for (unsigned i=0; i<str.size(); i++) |
282 |
|
|
{ |
283 |
torben |
141 |
str.at(i) = char_def_alphabet_encode(str.at(i)); |
284 |
|
|
} |
285 |
torben |
43 |
return str; |
286 |
|
|
} |
287 |
|
|
|
288 |
torben |
193 |
/* deprecated, use HttpClient instead |
289 |
torben |
26 |
string readUrl(string url, string tempfile) |
290 |
|
|
{ |
291 |
|
|
char buf[128000]; |
292 |
|
|
string document; |
293 |
torben |
141 |
|
294 |
torben |
26 |
ostringstream command; |
295 |
torben |
49 |
command << "wget -O " << tempfile << " --tries=1 --timeout=15 -o /dev/null \"" << url << "\""; |
296 |
torben |
86 |
int res = my_system( command.str().c_str() ); |
297 |
torben |
79 |
|
298 |
torben |
86 |
if (res<0) |
299 |
|
|
{ |
300 |
|
|
throw( std::runtime_error("Error retrieving document")); |
301 |
|
|
} |
302 |
|
|
|
303 |
torben |
141 |
if (res>0) |
304 |
torben |
79 |
{ |
305 |
torben |
86 |
throw std::runtime_error("Command time out or document not found"); |
306 |
torben |
80 |
} |
307 |
torben |
26 |
|
308 |
|
|
ifstream in( tempfile.c_str() ); |
309 |
|
|
|
310 |
|
|
if (!in) |
311 |
|
|
return ""; |
312 |
|
|
|
313 |
|
|
while (!in.eof() ) |
314 |
|
|
{ |
315 |
|
|
in.getline(buf,128000-1); |
316 |
|
|
document += buf; |
317 |
|
|
document += "\n"; |
318 |
|
|
} |
319 |
|
|
in.close(); |
320 |
|
|
unlink(tempfile.c_str()); |
321 |
torben |
141 |
|
322 |
torben |
26 |
return document; |
323 |
torben |
193 |
}*/ |
324 |
torben |
26 |
|
325 |
torben |
42 |
|
326 |
torben |
141 |
|
327 |
torben |
42 |
string iconv_wrapper(string _input, string to_format, string from_format) |
328 |
torben |
26 |
{ |
329 |
|
|
char* input,*output,*input_ptr, *output_ptr; |
330 |
|
|
input = input_ptr = (char*) malloc(_input.size()+1); |
331 |
|
|
strcpy(input, _input.c_str()); |
332 |
torben |
141 |
|
333 |
torben |
26 |
output = output_ptr = (char*) malloc(_input.size()*2); |
334 |
|
|
|
335 |
|
|
|
336 |
torben |
141 |
|
337 |
torben |
26 |
unsigned int realinsize,insize,outsize,realsize; |
338 |
torben |
141 |
|
339 |
torben |
42 |
iconv_t icv = iconv_open(to_format.c_str(), from_format.c_str()); |
340 |
torben |
26 |
if (icv == (iconv_t)-1) |
341 |
|
|
{ |
342 |
|
|
perror(0); |
343 |
|
|
return ""; |
344 |
|
|
} |
345 |
|
|
|
346 |
torben |
141 |
|
347 |
torben |
26 |
realsize = outsize = _input.size()*2; |
348 |
|
|
realinsize = insize = _input.size(); |
349 |
torben |
141 |
|
350 |
torben |
26 |
iconv(icv, |
351 |
torben |
141 |
&input_ptr, |
352 |
|
|
&insize, |
353 |
|
|
&output_ptr, |
354 |
|
|
&outsize); |
355 |
torben |
26 |
|
356 |
|
|
|
357 |
|
|
perror(0); |
358 |
torben |
141 |
/* cout << "len=" << len << endl; |
359 |
|
|
cout << "outsize=" << outsize << endl; |
360 |
|
|
cout << "realsize=" << realsize << endl; |
361 |
|
|
cout << "insize=" << insize << endl; |
362 |
|
|
cout << "realinsize=" << realinsize << endl;*/ |
363 |
torben |
26 |
iconv_close(icv); |
364 |
|
|
|
365 |
|
|
string returnstr; |
366 |
|
|
for (unsigned int i=0; i<realsize - outsize; i++) |
367 |
|
|
{ |
368 |
|
|
//cout << i << ":" << (unsigned short) output[i] << "|" << (unsigned short) input[i]; |
369 |
|
|
//cout << " (" << output[i] << ")" << endl; |
370 |
|
|
returnstr += output[i]; |
371 |
|
|
} |
372 |
torben |
141 |
return returnstr; |
373 |
torben |
26 |
} |
374 |
|
|
|
375 |
torben |
42 |
std::string convertToUnicode(std::string str) |
376 |
|
|
{ |
377 |
|
|
return iconv_wrapper(str, "UTF-8", "ISO8859-1"); |
378 |
|
|
} |
379 |
|
|
|
380 |
|
|
std::string convertFromUnicode(std::string str) |
381 |
|
|
{ |
382 |
|
|
return iconv_wrapper(str, "ISO8859-1", "UTF-8"); |
383 |
|
|
} |
384 |
|
|
|
385 |
torben |
26 |
int uTimeDiff(const timeval& then, const timeval& now) |
386 |
|
|
{ |
387 |
|
|
return ( (now.tv_sec - then.tv_sec)*1000000) + (now.tv_usec-then.tv_usec); |
388 |
|
|
} |
389 |
|
|
|
390 |
|
|
int mTimeDiff(const timeval& then, const timeval& now) |
391 |
|
|
{ |
392 |
torben |
84 |
return ( (now.tv_sec - then.tv_sec)*1000) + |
393 |
torben |
141 |
((now.tv_usec-then.tv_usec)/1000); |
394 |
torben |
26 |
} |
395 |
|
|
|
396 |
torben |
89 |
|
397 |
|
|
timeval GetTimeOfDay() |
398 |
|
|
{ |
399 |
|
|
timeval now; |
400 |
|
|
gettimeofday(&now,0); |
401 |
|
|
return now; |
402 |
|
|
} |
403 |
|
|
|
404 |
torben |
107 |
int my_system(const char* cmd, std::string* response) |
405 |
torben |
86 |
{ |
406 |
|
|
FILE* p; |
407 |
torben |
107 |
if ((p = popen(cmd,"r")) == NULL) |
408 |
torben |
86 |
return (-1); |
409 |
torben |
107 |
|
410 |
|
|
if (response) |
411 |
|
|
{ |
412 |
|
|
std::string output; |
413 |
|
|
char buf[256]; |
414 |
torben |
141 |
while (!feof(p)) |
415 |
torben |
107 |
{ |
416 |
|
|
int len = fread(buf,1,255, p); |
417 |
|
|
buf[len] = 0; |
418 |
|
|
output += buf; |
419 |
|
|
} |
420 |
|
|
*response = output; |
421 |
|
|
} |
422 |
|
|
|
423 |
torben |
86 |
return (pclose(p)); |
424 |
|
|
} |
425 |
|
|
|
426 |
torben |
203 |
int my_system2 (const char *command) |
427 |
|
|
{ |
428 |
|
|
int pid, status; |
429 |
|
|
|
430 |
|
|
if (command == 0) |
431 |
|
|
return 1; |
432 |
|
|
// printf("forking ...\n"); |
433 |
|
|
pid = fork(); |
434 |
|
|
if (pid == -1) |
435 |
|
|
return -1; |
436 |
|
|
if (pid == 0) |
437 |
|
|
{ |
438 |
|
|
char *argv[4]; |
439 |
|
|
argv[0] = (char*) "sh"; |
440 |
|
|
argv[1] = (char*) "-c"; |
441 |
|
|
argv[2] = (char *) command; |
442 |
|
|
argv[3] = 0; |
443 |
|
|
// printf("chaining ...\n"); |
444 |
|
|
execve("/bin/sh", argv, environ); |
445 |
|
|
// printf("Chaining failed!\n"); |
446 |
|
|
exit(127); |
447 |
|
|
} |
448 |
|
|
do |
449 |
|
|
{ |
450 |
|
|
// printf("Waiting on PID: %d\n",pid); |
451 |
|
|
if (waitpid(pid, &status, 0) == -1) |
452 |
|
|
{ |
453 |
|
|
// printf("erc: %d\n",errno); |
454 |
|
|
if (errno != EINTR) |
455 |
|
|
return -1; |
456 |
|
|
} |
457 |
|
|
else |
458 |
|
|
{ |
459 |
|
|
// printf("status: %d\n",status); |
460 |
|
|
return status; |
461 |
|
|
} |
462 |
|
|
} |
463 |
|
|
while (1); |
464 |
|
|
} |
465 |
|
|
|
466 |
|
|
|
467 |
torben |
132 |
string readfile(string filename) |
468 |
|
|
{ |
469 |
|
|
string str; |
470 |
torben |
141 |
ifstream in(filename.c_str()); |
471 |
|
|
if (in) |
472 |
|
|
{ |
473 |
torben |
151 |
char buffer[4096]; |
474 |
|
|
in.read(buffer, 4095); |
475 |
torben |
132 |
buffer[ in.gcount() ] = 0; |
476 |
torben |
141 |
str = string(buffer); |
477 |
|
|
in.close(); |
478 |
|
|
} |
479 |
|
|
else |
480 |
|
|
{ |
481 |
torben |
151 |
string message = "Could not open "; |
482 |
torben |
141 |
message += filename; |
483 |
|
|
throw std::runtime_error(message); |
484 |
|
|
} |
485 |
|
|
return str; |
486 |
torben |
132 |
} |
487 |
|
|
|
488 |
|
|
|
489 |
|
|
|
490 |
torben |
26 |
} |