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 |
|
|
|
13 |
|
|
#include "util.h" |
14 |
|
|
|
15 |
|
|
using namespace std; |
16 |
|
|
|
17 |
|
|
namespace Util |
18 |
|
|
{ |
19 |
|
|
void str_dump(const string& str) |
20 |
|
|
{ |
21 |
|
|
for (unsigned i=0; i<str.length(); ++i) |
22 |
|
|
{ |
23 |
|
|
cout.width(2); |
24 |
|
|
cout.fill('0'); |
25 |
|
|
cout << hex << static_cast<int>(str.at(i)) << " "; |
26 |
|
|
} |
27 |
|
|
cout.width(1); |
28 |
|
|
cout << dec << endl; |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
torben |
38 |
string str_replace(string str, string search, string replace) |
34 |
torben |
26 |
{ |
35 |
|
|
unsigned int pos = 0; |
36 |
|
|
|
37 |
torben |
38 |
while ( (pos = str.find(search,pos)) != string::npos) |
38 |
torben |
26 |
{ |
39 |
torben |
38 |
str.replace(pos, search.size(), replace); |
40 |
torben |
26 |
pos += replace.size(); |
41 |
|
|
} |
42 |
torben |
38 |
return str; |
43 |
torben |
26 |
} |
44 |
|
|
|
45 |
torben |
42 |
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 |
torben |
78 |
bool my_isspace(char ch) |
57 |
|
|
{ |
58 |
|
|
return (isspace(ch) || ch == 0); |
59 |
|
|
} |
60 |
torben |
38 |
|
61 |
|
|
string str_trim(string str) |
62 |
|
|
{ |
63 |
torben |
78 |
while (str.length() > 0 && my_isspace(str.at(0))) |
64 |
torben |
38 |
str.erase(0,1); |
65 |
torben |
78 |
while (str.length() > 0 && my_isspace(str.at(str.length()-1))) |
66 |
torben |
38 |
str.erase(str.length()-1,1); |
67 |
|
|
return str; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
string str_striptags(string str) |
71 |
|
|
{ |
72 |
|
|
unsigned int pos=0; |
73 |
|
|
|
74 |
|
|
while ( (pos=str.find("<",pos)) != string::npos) |
75 |
|
|
{ |
76 |
|
|
unsigned int endpos = str.find(">",pos); |
77 |
|
|
if (endpos == string::npos) |
78 |
|
|
break; |
79 |
|
|
str.erase(pos, (endpos-pos)+1); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
return str; |
83 |
|
|
} |
84 |
|
|
|
85 |
torben |
26 |
vector<string> str_split(string input, string delimiter) |
86 |
|
|
{ |
87 |
|
|
vector<string> retval; |
88 |
|
|
|
89 |
|
|
while ( 1 ) |
90 |
|
|
{ |
91 |
|
|
unsigned int endpos = input.find(delimiter); |
92 |
|
|
|
93 |
|
|
string entry = input.substr(0,endpos); |
94 |
|
|
retval.push_back( entry ); |
95 |
|
|
|
96 |
|
|
|
97 |
|
|
if (endpos == string::npos) |
98 |
|
|
break; |
99 |
|
|
|
100 |
|
|
endpos += delimiter.length(); |
101 |
|
|
input = input.substr(endpos, input.length() - endpos); |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
return retval; |
105 |
|
|
} |
106 |
|
|
|
107 |
torben |
43 |
|
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 |
torben |
59 |
string str_formatint(int i) |
128 |
|
|
{ |
129 |
|
|
std::ostringstream os; |
130 |
|
|
os << i; |
131 |
|
|
return os.str(); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
|
135 |
torben |
38 |
string str_characters(string str) |
136 |
|
|
{ |
137 |
|
|
string rep; |
138 |
|
|
rep.append(1,197); |
139 |
|
|
str = str_replace(str, "Å", rep); |
140 |
|
|
|
141 |
|
|
rep.at(0) = 198; |
142 |
|
|
str = str_replace(str, "Æ", rep); |
143 |
|
|
|
144 |
|
|
rep.at(0) = 216; |
145 |
|
|
str = str_replace(str, "Ø", rep); |
146 |
|
|
|
147 |
|
|
|
148 |
|
|
rep.at(0) = 229; |
149 |
|
|
str = str_replace(str, "å", rep); |
150 |
|
|
|
151 |
|
|
rep.at(0) = 230; |
152 |
|
|
str = str_replace(str, "æ", rep); |
153 |
|
|
|
154 |
|
|
rep.at(0) = 248; |
155 |
|
|
str = str_replace(str, "ø", rep); |
156 |
|
|
|
157 |
|
|
return str; |
158 |
|
|
} |
159 |
|
|
|
160 |
torben |
42 |
|
161 |
torben |
43 |
string str_gsm2latin(string str) |
162 |
torben |
42 |
{ |
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 |
torben |
43 |
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 |
torben |
26 |
string readUrl(string url, string tempfile) |
186 |
|
|
{ |
187 |
|
|
char buf[128000]; |
188 |
|
|
string document; |
189 |
|
|
|
190 |
|
|
ostringstream command; |
191 |
torben |
49 |
command << "wget -O " << tempfile << " --tries=1 --timeout=15 -o /dev/null \"" << url << "\""; |
192 |
|
|
int res = system( command.str().c_str() ); |
193 |
torben |
79 |
|
194 |
torben |
80 |
if (res>0) |
195 |
torben |
79 |
{ |
196 |
torben |
49 |
throw std::runtime_error("Command time out"); |
197 |
torben |
80 |
} |
198 |
torben |
26 |
|
199 |
|
|
ifstream in( tempfile.c_str() ); |
200 |
|
|
|
201 |
|
|
if (!in) |
202 |
|
|
return ""; |
203 |
|
|
|
204 |
|
|
while (!in.eof() ) |
205 |
|
|
{ |
206 |
|
|
in.getline(buf,128000-1); |
207 |
|
|
document += buf; |
208 |
|
|
document += "\n"; |
209 |
|
|
} |
210 |
|
|
in.close(); |
211 |
|
|
unlink(tempfile.c_str()); |
212 |
|
|
|
213 |
|
|
return document; |
214 |
|
|
} |
215 |
|
|
|
216 |
torben |
42 |
|
217 |
|
|
|
218 |
|
|
string iconv_wrapper(string _input, string to_format, string from_format) |
219 |
torben |
26 |
{ |
220 |
|
|
char* input,*output,*input_ptr, *output_ptr; |
221 |
|
|
input = input_ptr = (char*) malloc(_input.size()+1); |
222 |
|
|
strcpy(input, _input.c_str()); |
223 |
|
|
|
224 |
|
|
output = output_ptr = (char*) malloc(_input.size()*2); |
225 |
|
|
|
226 |
|
|
|
227 |
|
|
|
228 |
|
|
unsigned int realinsize,insize,outsize,realsize; |
229 |
|
|
|
230 |
torben |
42 |
iconv_t icv = iconv_open(to_format.c_str(), from_format.c_str()); |
231 |
torben |
26 |
if (icv == (iconv_t)-1) |
232 |
|
|
{ |
233 |
|
|
perror(0); |
234 |
|
|
return ""; |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
|
238 |
|
|
realsize = outsize = _input.size()*2; |
239 |
|
|
realinsize = insize = _input.size(); |
240 |
|
|
|
241 |
|
|
iconv(icv, |
242 |
|
|
&input_ptr, |
243 |
|
|
&insize, |
244 |
|
|
&output_ptr, |
245 |
|
|
&outsize); |
246 |
|
|
|
247 |
|
|
|
248 |
|
|
perror(0); |
249 |
|
|
/* cout << "len=" << len << endl; |
250 |
|
|
cout << "outsize=" << outsize << endl; |
251 |
|
|
cout << "realsize=" << realsize << endl; |
252 |
|
|
cout << "insize=" << insize << endl; |
253 |
|
|
cout << "realinsize=" << realinsize << endl;*/ |
254 |
|
|
iconv_close(icv); |
255 |
|
|
|
256 |
|
|
string returnstr; |
257 |
|
|
for (unsigned int i=0; i<realsize - outsize; i++) |
258 |
|
|
{ |
259 |
|
|
//cout << i << ":" << (unsigned short) output[i] << "|" << (unsigned short) input[i]; |
260 |
|
|
//cout << " (" << output[i] << ")" << endl; |
261 |
|
|
returnstr += output[i]; |
262 |
|
|
} |
263 |
|
|
return returnstr; |
264 |
|
|
} |
265 |
|
|
|
266 |
torben |
42 |
std::string convertToUnicode(std::string str) |
267 |
|
|
{ |
268 |
|
|
return iconv_wrapper(str, "UTF-8", "ISO8859-1"); |
269 |
|
|
} |
270 |
|
|
|
271 |
|
|
std::string convertFromUnicode(std::string str) |
272 |
|
|
{ |
273 |
|
|
return iconv_wrapper(str, "ISO8859-1", "UTF-8"); |
274 |
|
|
} |
275 |
|
|
|
276 |
torben |
26 |
int uTimeDiff(const timeval& then, const timeval& now) |
277 |
|
|
{ |
278 |
|
|
return ( (now.tv_sec - then.tv_sec)*1000000) + (now.tv_usec-then.tv_usec); |
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
int mTimeDiff(const timeval& then, const timeval& now) |
282 |
|
|
{ |
283 |
|
|
return uTimeDiff(then,now) / 1000; |
284 |
|
|
} |
285 |
|
|
|
286 |
|
|
} |