1 | |
---|
2 | /* |
---|
3 | Copyright 2013 Willem Vermin, SURFsara |
---|
4 | |
---|
5 | Licensed under the Apache License, Version 2.0 (the "License"); |
---|
6 | you may not use this file except in compliance with the License. |
---|
7 | You may obtain a copy of the License at |
---|
8 | |
---|
9 | http://www.apache.org/licenses/LICENSE-2.0 |
---|
10 | |
---|
11 | Unless required by applicable law or agreed to in writing, software |
---|
12 | distributed under the License is distributed on an "AS IS" BASIS, |
---|
13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
14 | See the License for the specific language governing permissions and |
---|
15 | limitations under the License. |
---|
16 | */ |
---|
17 | #include <string> |
---|
18 | #include <vector> |
---|
19 | #include <string.h> |
---|
20 | #include <iostream> |
---|
21 | #include <algorithm> |
---|
22 | #include <sys/time.h> |
---|
23 | #include "wutils.h" |
---|
24 | #include <sys/stat.h> |
---|
25 | #include <sys/types.h> |
---|
26 | #include <errno.h> |
---|
27 | #include <stdlib.h> |
---|
28 | #include "wtd.h" |
---|
29 | |
---|
30 | // |
---|
31 | // splits string s to vector v, using separator character sep |
---|
32 | // return value: v |
---|
33 | // It seems that it follows the python implementation of split() |
---|
34 | // |
---|
35 | std::vector <std::string> split(std::vector <std::string> &v, const std::string s, const char sep) |
---|
36 | { |
---|
37 | v.clear(); |
---|
38 | for (size_t p=0, q=0; q != s.npos; p = q + 1) |
---|
39 | v.push_back(s.substr(p,(q=s.find(sep,p))-p)); |
---|
40 | return v; |
---|
41 | } |
---|
42 | |
---|
43 | // converts string to char * |
---|
44 | // resulting string is created using malloc. Use free to free. |
---|
45 | char* str_to_charp(const std::string s) |
---|
46 | { |
---|
47 | return strdup(s.c_str()); |
---|
48 | } |
---|
49 | |
---|
50 | /* |
---|
51 | * converts string to hex representation |
---|
52 | */ |
---|
53 | std::string strtohex(const std::string s) |
---|
54 | { |
---|
55 | std::string a; |
---|
56 | const std::string c= |
---|
57 | "000102030405060708090a0b0c0d0e0f" |
---|
58 | "101112131415161718191a1b1c1d1e1f" |
---|
59 | "202122232425262728292a2b2c2d2e2f" |
---|
60 | "303132333435363738393a3b3c3d3e3f" |
---|
61 | "404142434445464748494a4b4c4d4e4f" |
---|
62 | "505152535455565758595a5b5c5d5e5f" |
---|
63 | "606162636465666768696a6b6c6d6e6f" |
---|
64 | "707172737475767778797a7b7c7d7e7f" |
---|
65 | "808182838485868788898a8b8c8d8e8f" |
---|
66 | "909192939495969798999a9b9c9d9e9f" |
---|
67 | "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf" |
---|
68 | "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" |
---|
69 | "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf" |
---|
70 | "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf" |
---|
71 | "e0e1e2e3e4e5e6e7e8e9eaebecedeeef" |
---|
72 | "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"; |
---|
73 | |
---|
74 | a.resize(2*s.size()); |
---|
75 | int k = 0; |
---|
76 | for (unsigned int i=0; i<s.size(); i++) |
---|
77 | { |
---|
78 | int p = 2*(unsigned char)s[i]; |
---|
79 | a[k++] = c[p++]; |
---|
80 | a[k++] = c[p]; |
---|
81 | } |
---|
82 | |
---|
83 | return a; |
---|
84 | } |
---|
85 | |
---|
86 | /* |
---|
87 | * convert hex represented string to string |
---|
88 | * unknown hex numbers result in '?' |
---|
89 | */ |
---|
90 | std::string hextostr(const std:: string s) |
---|
91 | { |
---|
92 | const char c[] = { |
---|
93 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
94 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
95 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
96 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1, |
---|
97 | -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
98 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
99 | -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
100 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
101 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
102 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
103 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
104 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
105 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
106 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
107 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
108 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 |
---|
109 | }; |
---|
110 | |
---|
111 | std::string a; |
---|
112 | unsigned int l = s.size(); |
---|
113 | a.resize((l+1)/2); |
---|
114 | |
---|
115 | unsigned int k=0; |
---|
116 | unsigned int l2 = (l/2)*2; |
---|
117 | for (unsigned int i=0; i<l2; i+=2) |
---|
118 | { |
---|
119 | int p = c[(unsigned int)s[i]]; |
---|
120 | int q = c[(unsigned int)s[i+1]]; |
---|
121 | if (p >= 0 && q >= 0) |
---|
122 | a[k++] = 16*p + q; |
---|
123 | else |
---|
124 | a[k++] = '?'; |
---|
125 | } |
---|
126 | |
---|
127 | if (l2 < l) |
---|
128 | a[k] = '?'; |
---|
129 | |
---|
130 | return a; |
---|
131 | |
---|
132 | } |
---|
133 | |
---|
134 | std::string zstrtohex(const std::string s) |
---|
135 | { |
---|
136 | return ZBEGIN_+strtohex(s)+ZEND_; |
---|
137 | } |
---|
138 | |
---|
139 | std::string zhextostr(const std::string s) |
---|
140 | { |
---|
141 | size_t lb = ZBEGIN_.size(); |
---|
142 | size_t le = ZEND_.size(); |
---|
143 | size_t ls = s.size(); |
---|
144 | |
---|
145 | if (ls < lb + le) |
---|
146 | return s; |
---|
147 | |
---|
148 | if (s.substr(0,lb) != ZBEGIN_) |
---|
149 | return s; |
---|
150 | |
---|
151 | if (s.substr(ls-le) != ZEND_) |
---|
152 | return s; |
---|
153 | |
---|
154 | return hextostr(s.substr(lb,ls-lb-le)); |
---|
155 | } |
---|
156 | |
---|
157 | //generates random passwoord of length len |
---|
158 | std::string gen_random(const int len) { |
---|
159 | timeval t1; |
---|
160 | gettimeofday(&t1, NULL); |
---|
161 | srand(t1.tv_usec * t1.tv_sec); |
---|
162 | static const char alphanum[] = |
---|
163 | "0123456789" |
---|
164 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
---|
165 | "abcdefghijklmnopqrstuvwxyz"; |
---|
166 | |
---|
167 | std::string s; |
---|
168 | s.resize(len); |
---|
169 | for (int i = 0; i < len; ++i) |
---|
170 | { |
---|
171 | s[i] = alphanum[std::rand() % (sizeof(alphanum) - 1)]; |
---|
172 | } |
---|
173 | return s; |
---|
174 | } |
---|
175 | |
---|
176 | #include <fstream> |
---|
177 | #include <string> |
---|
178 | #include <cerrno> |
---|
179 | |
---|
180 | int get_file_contents(std::string &contents,const std::string &filename) |
---|
181 | { |
---|
182 | std::ifstream in(filename.c_str(), std::ios::in | std::ios::binary); |
---|
183 | if (in) |
---|
184 | { |
---|
185 | in.seekg(0, std::ios::end); |
---|
186 | contents.resize(in.tellg()); |
---|
187 | in.seekg(0, std::ios::beg); |
---|
188 | in.read(&contents[0], contents.size()); |
---|
189 | in.close(); |
---|
190 | return(0); |
---|
191 | } |
---|
192 | return -1; |
---|
193 | } |
---|
194 | |
---|
195 | // tries to mimic mkdir -p |
---|
196 | int mkdir_p(const std::string &d,mode_t mode) |
---|
197 | { |
---|
198 | size_t p=0; |
---|
199 | |
---|
200 | std::string s = d; |
---|
201 | if (s[s.size()-1] != '/') |
---|
202 | s += '/'; |
---|
203 | |
---|
204 | int rc=0; |
---|
205 | std::string t; |
---|
206 | while (( p = s.find_first_of('/',p)) != s.npos) |
---|
207 | { |
---|
208 | p++; |
---|
209 | t = s.substr(0,p); |
---|
210 | rc = mkdir (t.c_str(),mode); |
---|
211 | if (rc < 0) |
---|
212 | { |
---|
213 | if (errno == EEXIST) |
---|
214 | { |
---|
215 | struct stat buf; |
---|
216 | rc = lstat(t.c_str(),&buf); |
---|
217 | if (rc < 0) |
---|
218 | break; |
---|
219 | if (!S_ISDIR(buf.st_mode)) |
---|
220 | { |
---|
221 | rc = -1; |
---|
222 | break; |
---|
223 | } |
---|
224 | } |
---|
225 | else |
---|
226 | break; |
---|
227 | } |
---|
228 | if ( p == s.size()) |
---|
229 | break; |
---|
230 | } |
---|
231 | return rc; |
---|
232 | } |
---|
233 | |
---|
234 | std::string envtostr(const std::string &s) |
---|
235 | { |
---|
236 | char *a = getenv(s.c_str()); |
---|
237 | if (a == 0) |
---|
238 | return ""; |
---|
239 | |
---|
240 | return a; |
---|
241 | } |
---|
242 | |
---|
243 | int get_dir_list(std::list <std::string> &v, |
---|
244 | const std::string &d) |
---|
245 | { |
---|
246 | DIR *dir; |
---|
247 | struct dirent *ent; |
---|
248 | dir = opendir(d.c_str()); |
---|
249 | if (dir == 0) |
---|
250 | return 1; |
---|
251 | |
---|
252 | while ((ent = readdir (dir)) != 0) |
---|
253 | v.push_back(ent->d_name); |
---|
254 | closedir (dir); |
---|
255 | return 0; |
---|
256 | } |
---|
257 | |
---|
258 | size_t count_lines(std::ifstream &f) { |
---|
259 | int number_of_lines = 0; |
---|
260 | std::string line; |
---|
261 | |
---|
262 | std::streampos p = f.tellg(); |
---|
263 | |
---|
264 | while (std::getline(f, line)) |
---|
265 | ++number_of_lines; |
---|
266 | f.clear(); |
---|
267 | f.seekg(p); |
---|
268 | return number_of_lines; |
---|
269 | } |
---|
270 | |
---|
271 | #include <sys/times.h> |
---|
272 | #include <sys/time.h> |
---|
273 | #include <time.h> |
---|
274 | #include <stdlib.h> |
---|
275 | #include "clocks.h" |
---|
276 | #include <unistd.h> |
---|
277 | |
---|
278 | double progress_bar::wallc() |
---|
279 | { |
---|
280 | struct timeval t; |
---|
281 | |
---|
282 | gettimeofday(&t,0); |
---|
283 | return t.tv_sec+0.000001*(double)t.tv_usec; |
---|
284 | } |
---|
285 | |
---|
286 | progress_bar::progress_bar() |
---|
287 | { |
---|
288 | this->clear(); |
---|
289 | this->timer = 0.3; |
---|
290 | this->bar_length = 60; |
---|
291 | } |
---|
292 | |
---|
293 | void progress_bar::clear() |
---|
294 | { |
---|
295 | this->firsttime = 1; |
---|
296 | this->walltime = wallc(); |
---|
297 | } |
---|
298 | |
---|
299 | void progress_bar::set_timer(float t) |
---|
300 | { |
---|
301 | this->timer = t; |
---|
302 | } |
---|
303 | |
---|
304 | std::string progress_bar::show(size_t num, double frac) |
---|
305 | { |
---|
306 | bool c; |
---|
307 | return this->show(num,frac,c); |
---|
308 | } |
---|
309 | |
---|
310 | std::string progress_bar::show(size_t num, double frac, bool &changed) |
---|
311 | { |
---|
312 | changed = 0; |
---|
313 | if (!this->firsttime) |
---|
314 | { |
---|
315 | if (this->wallc() - this->walltime < this->timer) |
---|
316 | return this->pshow; |
---|
317 | } |
---|
318 | |
---|
319 | changed = this -> firsttime || this->pfrac != frac || this->pnum != num; |
---|
320 | |
---|
321 | if (! changed) |
---|
322 | return this->pshow; |
---|
323 | |
---|
324 | this->firsttime = 0; |
---|
325 | |
---|
326 | int f = 100*frac+0.5; |
---|
327 | |
---|
328 | std::string b((size_t)(frac*this->bar_length),'x'); |
---|
329 | b.resize(this->bar_length,'-'); |
---|
330 | |
---|
331 | this->pshow = NumberToString(num) + ' ' + NumberToString(f) + "% [" |
---|
332 | + b + ']'; |
---|
333 | |
---|
334 | this->pnum = num; |
---|
335 | this->pfrac = frac; |
---|
336 | this->walltime = this->wallc(); |
---|
337 | |
---|
338 | return this->pshow; |
---|
339 | |
---|
340 | } |
---|
341 | |
---|
342 | std::string progress_bar::show(size_t num) |
---|
343 | { |
---|
344 | bool c; |
---|
345 | return this->show(num,c); |
---|
346 | } |
---|
347 | |
---|
348 | std::string progress_bar::show(size_t num, bool &changed) |
---|
349 | { |
---|
350 | changed = 0; |
---|
351 | if (!this->firsttime) |
---|
352 | { |
---|
353 | if (this->wallc() - this->walltime < this->timer) |
---|
354 | return this->pshow; |
---|
355 | } |
---|
356 | |
---|
357 | changed = this -> firsttime || this->pnum != num; |
---|
358 | |
---|
359 | if (! changed) |
---|
360 | return this->pshow; |
---|
361 | |
---|
362 | this->firsttime = 0; |
---|
363 | |
---|
364 | this->pshow = NumberToString(num); |
---|
365 | this->pnum = num; |
---|
366 | this->walltime = this->wallc(); |
---|
367 | |
---|
368 | return this->pshow; |
---|
369 | |
---|
370 | } |
---|