1 |
|
2 |
#include <string> |
3 |
#include <sstream> |
4 |
#include <vector> |
5 |
#include <cctype> |
6 |
|
7 |
#include <iostream> |
8 |
#include <fstream> |
9 |
#include <iconv.h> |
10 |
#include <stdexcept> |
11 |
#include <time.h> |
12 |
#include <sys/time.h> |
13 |
|
14 |
#include "util.h" |
15 |
|
16 |
using namespace std; |
17 |
|
18 |
namespace Util |
19 |
{ |
20 |
void str_dump(const string& str) |
21 |
{ |
22 |
for (unsigned i=0; i<str.length(); ++i) |
23 |
{ |
24 |
cout.width(2); |
25 |
cout.fill('0'); |
26 |
cout << hex << static_cast<int>(str.at(i)) << " "; |
27 |
} |
28 |
cout.width(1); |
29 |
cout << dec << endl; |
30 |
} |
31 |
|
32 |
|
33 |
|
34 |
string str_replace(string str, string search, string replace) |
35 |
{ |
36 |
unsigned int pos = 0; |
37 |
|
38 |
while ( (pos = str.find(search,pos)) != string::npos) |
39 |
{ |
40 |
str.replace(pos, search.size(), replace); |
41 |
pos += replace.size(); |
42 |
} |
43 |
return str; |
44 |
} |
45 |
|
46 |
std::string str_replace_char(std::string str, char search, char replace) |
47 |
{ |
48 |
unsigned int pos = 0; |
49 |
|
50 |
while ( (pos = str.find(search,pos)) != string::npos) |
51 |
{ |
52 |
str.replace(pos, 1, 1, replace); |
53 |
} |
54 |
return str; |
55 |
} |
56 |
|
57 |
bool my_isspace(char ch) |
58 |
{ |
59 |
return (isspace(ch) || ch == 0); |
60 |
} |
61 |
|
62 |
string str_trim(string str) |
63 |
{ |
64 |
while (str.length() > 0 && my_isspace(str.at(0))) |
65 |
str.erase(0,1); |
66 |
while (str.length() > 0 && my_isspace(str.at(str.length()-1))) |
67 |
str.erase(str.length()-1,1); |
68 |
return str; |
69 |
} |
70 |
|
71 |
string str_striptags(string str) |
72 |
{ |
73 |
unsigned int pos=0; |
74 |
|
75 |
while ( (pos=str.find("<",pos)) != string::npos) |
76 |
{ |
77 |
unsigned int endpos = str.find(">",pos); |
78 |
if (endpos == string::npos) |
79 |
break; |
80 |
str.erase(pos, (endpos-pos)+1); |
81 |
} |
82 |
|
83 |
return str; |
84 |
} |
85 |
|
86 |
vector<string> str_split(string input, string delimiter) |
87 |
{ |
88 |
vector<string> retval; |
89 |
|
90 |
while ( 1 ) |
91 |
{ |
92 |
unsigned int endpos = input.find(delimiter); |
93 |
|
94 |
string entry = input.substr(0,endpos); |
95 |
retval.push_back( entry ); |
96 |
|
97 |
|
98 |
if (endpos == string::npos) |
99 |
break; |
100 |
|
101 |
endpos += delimiter.length(); |
102 |
input = input.substr(endpos, input.length() - endpos); |
103 |
} |
104 |
|
105 |
return retval; |
106 |
} |
107 |
|
108 |
|
109 |
string str_toupper(string str) |
110 |
{ |
111 |
for (unsigned i=0; i<str.length(); ++i) |
112 |
{ |
113 |
str.replace(i, 1 ,1, ::toupper(str.at(i))); |
114 |
} |
115 |
return str; |
116 |
} |
117 |
|
118 |
string str_tolower(string str) |
119 |
{ |
120 |
for (unsigned i=0; i<str.length(); ++i) |
121 |
{ |
122 |
str.replace(i, 1 ,1, ::tolower(str.at(i))); |
123 |
} |
124 |
return str; |
125 |
} |
126 |
|
127 |
|
128 |
string str_formatint(int i) |
129 |
{ |
130 |
std::ostringstream os; |
131 |
os << i; |
132 |
return os.str(); |
133 |
} |
134 |
|
135 |
|
136 |
string str_characters(string str) |
137 |
{ |
138 |
string rep; |
139 |
rep.append(1,197); |
140 |
str = str_replace(str, "Å", rep); |
141 |
|
142 |
rep.at(0) = 198; |
143 |
str = str_replace(str, "Æ", rep); |
144 |
|
145 |
rep.at(0) = 216; |
146 |
str = str_replace(str, "Ø", rep); |
147 |
|
148 |
|
149 |
rep.at(0) = 229; |
150 |
str = str_replace(str, "å", rep); |
151 |
|
152 |
rep.at(0) = 230; |
153 |
str = str_replace(str, "æ", rep); |
154 |
|
155 |
rep.at(0) = 248; |
156 |
str = str_replace(str, "ø", rep); |
157 |
|
158 |
return str; |
159 |
} |
160 |
|
161 |
|
162 |
string str_gsm2latin(string str) |
163 |
{ |
164 |
str = str_replace_char(str, 0x1c, 198); //AE |
165 |
str = str_replace_char(str, 0x0b, 216); //OE |
166 |
str = str_replace_char(str, 0x0e, 197); //AA |
167 |
|
168 |
str = str_replace_char(str, 0x1d, 230); //ae |
169 |
str = str_replace_char(str, 0x0c, 248); //oe |
170 |
str = str_replace_char(str, 0x0f, 229); //aa |
171 |
return str; |
172 |
} |
173 |
|
174 |
string str_latin2gsm(string str) |
175 |
{ |
176 |
str = str_replace_char(str, 198, 0x1c); //AE |
177 |
str = str_replace_char(str, 216, 0x0b); //OE |
178 |
str = str_replace_char(str, 197, 0x0e); //AA |
179 |
|
180 |
str = str_replace_char(str, 230, 0x1d); //ae |
181 |
str = str_replace_char(str, 248, 0x0c); //oe |
182 |
str = str_replace_char(str, 229, 0x0f); //aa |
183 |
return str; |
184 |
} |
185 |
|
186 |
string readUrl(string url, string tempfile) |
187 |
{ |
188 |
char buf[128000]; |
189 |
string document; |
190 |
|
191 |
ostringstream command; |
192 |
command << "wget -O " << tempfile << " --tries=1 --timeout=15 -o /dev/null \"" << url << "\""; |
193 |
int res = my_system( command.str().c_str() ); |
194 |
|
195 |
if (res<0) |
196 |
{ |
197 |
throw( std::runtime_error("Error retrieving document")); |
198 |
} |
199 |
|
200 |
if (res>0) |
201 |
{ |
202 |
throw std::runtime_error("Command time out or document not found"); |
203 |
} |
204 |
|
205 |
ifstream in( tempfile.c_str() ); |
206 |
|
207 |
if (!in) |
208 |
return ""; |
209 |
|
210 |
while (!in.eof() ) |
211 |
{ |
212 |
in.getline(buf,128000-1); |
213 |
document += buf; |
214 |
document += "\n"; |
215 |
} |
216 |
in.close(); |
217 |
unlink(tempfile.c_str()); |
218 |
|
219 |
return document; |
220 |
} |
221 |
|
222 |
|
223 |
|
224 |
string iconv_wrapper(string _input, string to_format, string from_format) |
225 |
{ |
226 |
char* input,*output,*input_ptr, *output_ptr; |
227 |
input = input_ptr = (char*) malloc(_input.size()+1); |
228 |
strcpy(input, _input.c_str()); |
229 |
|
230 |
output = output_ptr = (char*) malloc(_input.size()*2); |
231 |
|
232 |
|
233 |
|
234 |
unsigned int realinsize,insize,outsize,realsize; |
235 |
|
236 |
iconv_t icv = iconv_open(to_format.c_str(), from_format.c_str()); |
237 |
if (icv == (iconv_t)-1) |
238 |
{ |
239 |
perror(0); |
240 |
return ""; |
241 |
} |
242 |
|
243 |
|
244 |
realsize = outsize = _input.size()*2; |
245 |
realinsize = insize = _input.size(); |
246 |
|
247 |
iconv(icv, |
248 |
&input_ptr, |
249 |
&insize, |
250 |
&output_ptr, |
251 |
&outsize); |
252 |
|
253 |
|
254 |
perror(0); |
255 |
/* cout << "len=" << len << endl; |
256 |
cout << "outsize=" << outsize << endl; |
257 |
cout << "realsize=" << realsize << endl; |
258 |
cout << "insize=" << insize << endl; |
259 |
cout << "realinsize=" << realinsize << endl;*/ |
260 |
iconv_close(icv); |
261 |
|
262 |
string returnstr; |
263 |
for (unsigned int i=0; i<realsize - outsize; i++) |
264 |
{ |
265 |
//cout << i << ":" << (unsigned short) output[i] << "|" << (unsigned short) input[i]; |
266 |
//cout << " (" << output[i] << ")" << endl; |
267 |
returnstr += output[i]; |
268 |
} |
269 |
return returnstr; |
270 |
} |
271 |
|
272 |
std::string convertToUnicode(std::string str) |
273 |
{ |
274 |
return iconv_wrapper(str, "UTF-8", "ISO8859-1"); |
275 |
} |
276 |
|
277 |
std::string convertFromUnicode(std::string str) |
278 |
{ |
279 |
return iconv_wrapper(str, "ISO8859-1", "UTF-8"); |
280 |
} |
281 |
|
282 |
int uTimeDiff(const timeval& then, const timeval& now) |
283 |
{ |
284 |
return ( (now.tv_sec - then.tv_sec)*1000000) + (now.tv_usec-then.tv_usec); |
285 |
} |
286 |
|
287 |
int mTimeDiff(const timeval& then, const timeval& now) |
288 |
{ |
289 |
return ( (now.tv_sec - then.tv_sec)*1000) + |
290 |
((now.tv_usec-then.tv_usec)/1000); |
291 |
} |
292 |
|
293 |
|
294 |
timeval GetTimeOfDay() |
295 |
{ |
296 |
timeval now; |
297 |
gettimeofday(&now,0); |
298 |
return now; |
299 |
} |
300 |
|
301 |
int my_system(const char* cmd, std::string* response) |
302 |
{ |
303 |
FILE* p; |
304 |
if ((p = popen(cmd,"r")) == NULL) |
305 |
return (-1); |
306 |
|
307 |
if (response) |
308 |
{ |
309 |
std::string output; |
310 |
char buf[256]; |
311 |
while(!feof(p)) |
312 |
{ |
313 |
int len = fread(buf,1,255, p); |
314 |
buf[len] = 0; |
315 |
output += buf; |
316 |
} |
317 |
*response = output; |
318 |
} |
319 |
|
320 |
return (pclose(p)); |
321 |
} |
322 |
|
323 |
} |