1 |
|
2 |
#include <string> |
3 |
#include <sstream> |
4 |
#include <vector> |
5 |
#include <iostream> |
6 |
#include <fstream> |
7 |
#include <iconv.h> |
8 |
|
9 |
#include <time.h> |
10 |
|
11 |
#include "util.h" |
12 |
|
13 |
using namespace std; |
14 |
|
15 |
namespace Util |
16 |
{ |
17 |
void str_dump(const string& str) |
18 |
{ |
19 |
for (unsigned i=0; i<str.length(); ++i) |
20 |
{ |
21 |
cout.width(2); |
22 |
cout.fill('0'); |
23 |
cout << hex << static_cast<int>(str.at(i)) << " "; |
24 |
} |
25 |
cout.width(1); |
26 |
cout << dec << endl; |
27 |
} |
28 |
|
29 |
|
30 |
|
31 |
void str_clean(string* str, string search, string replace) |
32 |
{ |
33 |
unsigned int pos = 0; |
34 |
|
35 |
while ( (pos = str->find(search,pos)) != string::npos) |
36 |
{ |
37 |
str->replace(pos, search.size(), replace); |
38 |
pos += replace.size(); |
39 |
} |
40 |
} |
41 |
|
42 |
vector<string> str_split(string input, string delimiter) |
43 |
{ |
44 |
vector<string> retval; |
45 |
|
46 |
while ( 1 ) |
47 |
{ |
48 |
unsigned int endpos = input.find(delimiter); |
49 |
|
50 |
string entry = input.substr(0,endpos); |
51 |
retval.push_back( entry ); |
52 |
|
53 |
|
54 |
if (endpos == string::npos) |
55 |
break; |
56 |
|
57 |
endpos += delimiter.length(); |
58 |
input = input.substr(endpos, input.length() - endpos); |
59 |
} |
60 |
|
61 |
return retval; |
62 |
} |
63 |
|
64 |
string readUrl(string url, string tempfile) |
65 |
{ |
66 |
char buf[128000]; |
67 |
string document; |
68 |
|
69 |
ostringstream command; |
70 |
command << "wget -O " << tempfile << " -o /dev/null \"" << url << "\""; |
71 |
system( command.str().c_str() ); |
72 |
|
73 |
ifstream in( tempfile.c_str() ); |
74 |
|
75 |
if (!in) |
76 |
return ""; |
77 |
|
78 |
while (!in.eof() ) |
79 |
{ |
80 |
in.getline(buf,128000-1); |
81 |
document += buf; |
82 |
document += "\n"; |
83 |
} |
84 |
in.close(); |
85 |
unlink(tempfile.c_str()); |
86 |
|
87 |
return document; |
88 |
} |
89 |
|
90 |
string convertToUnicode(string _input) |
91 |
{ |
92 |
char* input,*output,*input_ptr, *output_ptr; |
93 |
input = input_ptr = (char*) malloc(_input.size()+1); |
94 |
strcpy(input, _input.c_str()); |
95 |
|
96 |
output = output_ptr = (char*) malloc(_input.size()*2); |
97 |
|
98 |
|
99 |
|
100 |
unsigned int realinsize,insize,outsize,realsize; |
101 |
|
102 |
iconv_t icv = iconv_open("UTF8", "ISO8859-1"); |
103 |
if (icv == (iconv_t)-1) |
104 |
{ |
105 |
perror(0); |
106 |
return ""; |
107 |
} |
108 |
|
109 |
|
110 |
realsize = outsize = _input.size()*2; |
111 |
realinsize = insize = _input.size(); |
112 |
|
113 |
iconv(icv, |
114 |
&input_ptr, |
115 |
&insize, |
116 |
&output_ptr, |
117 |
&outsize); |
118 |
|
119 |
|
120 |
perror(0); |
121 |
/* cout << "len=" << len << endl; |
122 |
cout << "outsize=" << outsize << endl; |
123 |
cout << "realsize=" << realsize << endl; |
124 |
cout << "insize=" << insize << endl; |
125 |
cout << "realinsize=" << realinsize << endl;*/ |
126 |
iconv_close(icv); |
127 |
|
128 |
string returnstr; |
129 |
for (unsigned int i=0; i<realsize - outsize; i++) |
130 |
{ |
131 |
//cout << i << ":" << (unsigned short) output[i] << "|" << (unsigned short) input[i]; |
132 |
//cout << " (" << output[i] << ")" << endl; |
133 |
returnstr += output[i]; |
134 |
} |
135 |
return returnstr; |
136 |
} |
137 |
|
138 |
int uTimeDiff(const timeval& then, const timeval& now) |
139 |
{ |
140 |
return ( (now.tv_sec - then.tv_sec)*1000000) + (now.tv_usec-then.tv_usec); |
141 |
} |
142 |
|
143 |
int mTimeDiff(const timeval& then, const timeval& now) |
144 |
{ |
145 |
return uTimeDiff(then,now) / 1000; |
146 |
} |
147 |
|
148 |
} |