7 |
#include <iostream> |
#include <iostream> |
8 |
#include <fstream> |
#include <fstream> |
9 |
#include <iconv.h> |
#include <iconv.h> |
10 |
|
#include <stdexcept> |
11 |
#include <time.h> |
#include <time.h> |
12 |
|
|
13 |
#include "util.h" |
#include "util.h" |
42 |
return str; |
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) |
string str_trim(string str) |
101 |
return retval; |
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_formatint(int i) |
125 |
|
{ |
126 |
|
std::ostringstream os; |
127 |
|
os << i; |
128 |
|
return os.str(); |
129 |
|
} |
130 |
|
|
131 |
|
|
132 |
string str_characters(string str) |
string str_characters(string str) |
133 |
{ |
{ |
134 |
string rep; |
string rep; |
154 |
return str; |
return str; |
155 |
} |
} |
156 |
|
|
157 |
|
|
158 |
|
string str_gsm2latin(string str) |
159 |
|
{ |
160 |
|
str = str_replace_char(str, 0x1c, 198); //AE |
161 |
|
str = str_replace_char(str, 0x0b, 216); //OE |
162 |
|
str = str_replace_char(str, 0x0e, 197); //AA |
163 |
|
|
164 |
|
str = str_replace_char(str, 0x1d, 230); //ae |
165 |
|
str = str_replace_char(str, 0x0c, 248); //oe |
166 |
|
str = str_replace_char(str, 0x0f, 229); //aa |
167 |
|
return str; |
168 |
|
} |
169 |
|
|
170 |
|
string str_latin2gsm(string str) |
171 |
|
{ |
172 |
|
str = str_replace_char(str, 198, 0x1c); //AE |
173 |
|
str = str_replace_char(str, 216, 0x0b); //OE |
174 |
|
str = str_replace_char(str, 197, 0x0e); //AA |
175 |
|
|
176 |
|
str = str_replace_char(str, 230, 0x1d); //ae |
177 |
|
str = str_replace_char(str, 248, 0x0c); //oe |
178 |
|
str = str_replace_char(str, 229, 0x0f); //aa |
179 |
|
return str; |
180 |
|
} |
181 |
|
|
182 |
string readUrl(string url, string tempfile) |
string readUrl(string url, string tempfile) |
183 |
{ |
{ |
184 |
char buf[128000]; |
char buf[128000]; |
185 |
string document; |
string document; |
186 |
|
|
187 |
ostringstream command; |
ostringstream command; |
188 |
command << "wget -O " << tempfile << " -o /dev/null \"" << url << "\""; |
command << "wget -O " << tempfile << " --tries=1 --timeout=15 -o /dev/null \"" << url << "\""; |
189 |
system( command.str().c_str() ); |
int res = system( command.str().c_str() ); |
190 |
|
if (res) |
191 |
|
throw std::runtime_error("Command time out"); |
192 |
|
|
193 |
ifstream in( tempfile.c_str() ); |
ifstream in( tempfile.c_str() ); |
194 |
|
|
207 |
return document; |
return document; |
208 |
} |
} |
209 |
|
|
210 |
string convertToUnicode(string _input) |
|
211 |
|
|
212 |
|
string iconv_wrapper(string _input, string to_format, string from_format) |
213 |
{ |
{ |
214 |
char* input,*output,*input_ptr, *output_ptr; |
char* input,*output,*input_ptr, *output_ptr; |
215 |
input = input_ptr = (char*) malloc(_input.size()+1); |
input = input_ptr = (char*) malloc(_input.size()+1); |
221 |
|
|
222 |
unsigned int realinsize,insize,outsize,realsize; |
unsigned int realinsize,insize,outsize,realsize; |
223 |
|
|
224 |
iconv_t icv = iconv_open("UTF8", "ISO8859-1"); |
iconv_t icv = iconv_open(to_format.c_str(), from_format.c_str()); |
225 |
if (icv == (iconv_t)-1) |
if (icv == (iconv_t)-1) |
226 |
{ |
{ |
227 |
perror(0); |
perror(0); |
257 |
return returnstr; |
return returnstr; |
258 |
} |
} |
259 |
|
|
260 |
|
std::string convertToUnicode(std::string str) |
261 |
|
{ |
262 |
|
return iconv_wrapper(str, "UTF-8", "ISO8859-1"); |
263 |
|
} |
264 |
|
|
265 |
|
std::string convertFromUnicode(std::string str) |
266 |
|
{ |
267 |
|
return iconv_wrapper(str, "ISO8859-1", "UTF-8"); |
268 |
|
} |
269 |
|
|
270 |
int uTimeDiff(const timeval& then, const timeval& now) |
int uTimeDiff(const timeval& then, const timeval& now) |
271 |
{ |
{ |
272 |
return ( (now.tv_sec - then.tv_sec)*1000000) + (now.tv_usec-then.tv_usec); |
return ( (now.tv_sec - then.tv_sec)*1000000) + (now.tv_usec-then.tv_usec); |