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 |
|
11 |
#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 |
string str_replace(string str, string search, string replace) |
34 |
{ |
35 |
unsigned int pos = 0; |
36 |
|
37 |
while ( (pos = str.find(search,pos)) != string::npos) |
38 |
{ |
39 |
str.replace(pos, search.size(), replace); |
40 |
pos += replace.size(); |
41 |
} |
42 |
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 |
|
57 |
|
58 |
string str_trim(string str) |
59 |
{ |
60 |
while (str.length() > 0 && isspace(str.at(0))) |
61 |
str.erase(0,1); |
62 |
while (str.length() > 0 && isspace(str.at(str.length()-1))) |
63 |
str.erase(str.length()-1,1); |
64 |
return str; |
65 |
} |
66 |
|
67 |
string str_striptags(string str) |
68 |
{ |
69 |
unsigned int pos=0; |
70 |
|
71 |
while ( (pos=str.find("<",pos)) != string::npos) |
72 |
{ |
73 |
unsigned int endpos = str.find(">",pos); |
74 |
if (endpos == string::npos) |
75 |
break; |
76 |
str.erase(pos, (endpos-pos)+1); |
77 |
} |
78 |
|
79 |
return str; |
80 |
} |
81 |
|
82 |
vector<string> str_split(string input, string delimiter) |
83 |
{ |
84 |
vector<string> retval; |
85 |
|
86 |
while ( 1 ) |
87 |
{ |
88 |
unsigned int endpos = input.find(delimiter); |
89 |
|
90 |
string entry = input.substr(0,endpos); |
91 |
retval.push_back( entry ); |
92 |
|
93 |
|
94 |
if (endpos == string::npos) |
95 |
break; |
96 |
|
97 |
endpos += delimiter.length(); |
98 |
input = input.substr(endpos, input.length() - endpos); |
99 |
} |
100 |
|
101 |
return retval; |
102 |
} |
103 |
|
104 |
|
105 |
string str_toupper(string str) |
106 |
{ |
107 |
for (unsigned i=0; i<str.length(); ++i) |
108 |
{ |
109 |
str.replace(i, 1 ,1, ::toupper(str.at(i))); |
110 |
} |
111 |
return str; |
112 |
} |
113 |
|
114 |
string str_tolower(string str) |
115 |
{ |
116 |
for (unsigned i=0; i<str.length(); ++i) |
117 |
{ |
118 |
str.replace(i, 1 ,1, ::tolower(str.at(i))); |
119 |
} |
120 |
return str; |
121 |
} |
122 |
|
123 |
|
124 |
string str_characters(string str) |
125 |
{ |
126 |
string rep; |
127 |
rep.append(1,197); |
128 |
str = str_replace(str, "Å", rep); |
129 |
|
130 |
rep.at(0) = 198; |
131 |
str = str_replace(str, "Æ", rep); |
132 |
|
133 |
rep.at(0) = 216; |
134 |
str = str_replace(str, "Ø", rep); |
135 |
|
136 |
|
137 |
rep.at(0) = 229; |
138 |
str = str_replace(str, "å", rep); |
139 |
|
140 |
rep.at(0) = 230; |
141 |
str = str_replace(str, "æ", rep); |
142 |
|
143 |
rep.at(0) = 248; |
144 |
str = str_replace(str, "ø", rep); |
145 |
|
146 |
return str; |
147 |
} |
148 |
|
149 |
|
150 |
string str_gsm2latin(string str) |
151 |
{ |
152 |
str = str_replace_char(str, 0x1c, 198); //AE |
153 |
str = str_replace_char(str, 0x0b, 216); //OE |
154 |
str = str_replace_char(str, 0x0e, 197); //AA |
155 |
|
156 |
str = str_replace_char(str, 0x1d, 230); //ae |
157 |
str = str_replace_char(str, 0x0c, 248); //oe |
158 |
str = str_replace_char(str, 0x0f, 229); //aa |
159 |
return str; |
160 |
} |
161 |
|
162 |
string str_latin2gsm(string str) |
163 |
{ |
164 |
str = str_replace_char(str, 198, 0x1c); //AE |
165 |
str = str_replace_char(str, 216, 0x0b); //OE |
166 |
str = str_replace_char(str, 197, 0x0e); //AA |
167 |
|
168 |
str = str_replace_char(str, 230, 0x1d); //ae |
169 |
str = str_replace_char(str, 248, 0x0c); //oe |
170 |
str = str_replace_char(str, 229, 0x0f); //aa |
171 |
return str; |
172 |
} |
173 |
|
174 |
string readUrl(string url, string tempfile) |
175 |
{ |
176 |
char buf[128000]; |
177 |
string document; |
178 |
|
179 |
ostringstream command; |
180 |
command << "wget -O " << tempfile << " -o /dev/null \"" << url << "\""; |
181 |
system( command.str().c_str() ); |
182 |
|
183 |
ifstream in( tempfile.c_str() ); |
184 |
|
185 |
if (!in) |
186 |
return ""; |
187 |
|
188 |
while (!in.eof() ) |
189 |
{ |
190 |
in.getline(buf,128000-1); |
191 |
document += buf; |
192 |
document += "\n"; |
193 |
} |
194 |
in.close(); |
195 |
unlink(tempfile.c_str()); |
196 |
|
197 |
return document; |
198 |
} |
199 |
|
200 |
|
201 |
|
202 |
string iconv_wrapper(string _input, string to_format, string from_format) |
203 |
{ |
204 |
char* input,*output,*input_ptr, *output_ptr; |
205 |
input = input_ptr = (char*) malloc(_input.size()+1); |
206 |
strcpy(input, _input.c_str()); |
207 |
|
208 |
output = output_ptr = (char*) malloc(_input.size()*2); |
209 |
|
210 |
|
211 |
|
212 |
unsigned int realinsize,insize,outsize,realsize; |
213 |
|
214 |
iconv_t icv = iconv_open(to_format.c_str(), from_format.c_str()); |
215 |
if (icv == (iconv_t)-1) |
216 |
{ |
217 |
perror(0); |
218 |
return ""; |
219 |
} |
220 |
|
221 |
|
222 |
realsize = outsize = _input.size()*2; |
223 |
realinsize = insize = _input.size(); |
224 |
|
225 |
iconv(icv, |
226 |
&input_ptr, |
227 |
&insize, |
228 |
&output_ptr, |
229 |
&outsize); |
230 |
|
231 |
|
232 |
perror(0); |
233 |
/* cout << "len=" << len << endl; |
234 |
cout << "outsize=" << outsize << endl; |
235 |
cout << "realsize=" << realsize << endl; |
236 |
cout << "insize=" << insize << endl; |
237 |
cout << "realinsize=" << realinsize << endl;*/ |
238 |
iconv_close(icv); |
239 |
|
240 |
string returnstr; |
241 |
for (unsigned int i=0; i<realsize - outsize; i++) |
242 |
{ |
243 |
//cout << i << ":" << (unsigned short) output[i] << "|" << (unsigned short) input[i]; |
244 |
//cout << " (" << output[i] << ")" << endl; |
245 |
returnstr += output[i]; |
246 |
} |
247 |
return returnstr; |
248 |
} |
249 |
|
250 |
std::string convertToUnicode(std::string str) |
251 |
{ |
252 |
return iconv_wrapper(str, "UTF-8", "ISO8859-1"); |
253 |
} |
254 |
|
255 |
std::string convertFromUnicode(std::string str) |
256 |
{ |
257 |
return iconv_wrapper(str, "ISO8859-1", "UTF-8"); |
258 |
} |
259 |
|
260 |
int uTimeDiff(const timeval& then, const timeval& now) |
261 |
{ |
262 |
return ( (now.tv_sec - then.tv_sec)*1000000) + (now.tv_usec-then.tv_usec); |
263 |
} |
264 |
|
265 |
int mTimeDiff(const timeval& then, const timeval& now) |
266 |
{ |
267 |
return uTimeDiff(then,now) / 1000; |
268 |
} |
269 |
|
270 |
} |