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