Rev 1380 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1380 | Rev 1402 | ||
|---|---|---|---|
| 1 | 1 | ||
| 2 | #include <string> |
2 | #include <string> |
| 3 | 3 | ||
| 4 | /*************************************************************************** |
4 | /*************************************************************************** |
| 5 | * Copyright (C) 2004 by Mattias Runge * |
5 | * Copyright (C) 2004 by Mattias Runge * |
| 6 | * mattias@mrunge.se * |
6 | * mattias@mrunge.se * |
| 7 | * * |
7 | * * |
| 8 | * This program is free software; you can redistribute it and/or modify * |
8 | * This program is free software; you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published by * |
9 | * it under the terms of the GNU General Public License as published by * |
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
10 | * the Free Software Foundation; either version 2 of the License, or * |
| 11 | * (at your option) any later version. * |
11 | * (at your option) any later version. * |
| 12 | * * |
12 | * * |
| 13 | * This program is distributed in the hope that it will be useful, * |
13 | * This program is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 | * GNU General Public License for more details. * |
16 | * GNU General Public License for more details. * |
| 17 | * * |
17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
18 | * You should have received a copy of the GNU General Public License * |
| 19 | * along with this program; if not, write to the * |
19 | * along with this program; if not, write to the * |
| 20 | * Free Software Foundation, Inc., * |
20 | * Free Software Foundation, Inc., * |
| 21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 22 | ***************************************************************************/ |
22 | ***************************************************************************/ |
| 23 | #include "tools.h" |
23 | #include "tools.h" |
| 24 | 24 | ||
| 25 | string niceTime() |
25 | string niceTime() |
| 26 | { |
26 | { |
| 27 | string result; |
27 | string result; |
| 28 | struct tm tmStruct; |
28 | struct tm tmStruct; |
| 29 | time_t t = time(NULL); |
29 | time_t t = time(NULL); |
| 30 | localtime_r(&t, &tmStruct); |
30 | localtime_r(&t, &tmStruct); |
| 31 | 31 | ||
| 32 | result += lpad(itos(tmStruct.tm_hour), 2, '0'); |
32 | result += lpad(itos(tmStruct.tm_hour), 2, '0'); |
| 33 | result += ":"; |
33 | result += ":"; |
| 34 | result += lpad(itos(tmStruct.tm_min), 2, '0'); |
34 | result += lpad(itos(tmStruct.tm_min), 2, '0'); |
| 35 | result += ":"; |
35 | result += ":"; |
| 36 | result += lpad(itos(tmStruct.tm_sec), 2, '0'); |
36 | result += lpad(itos(tmStruct.tm_sec), 2, '0'); |
| 37 | 37 | ||
| 38 | return result; |
38 | return result; |
| 39 | } |
39 | } |
| 40 | 40 | ||
| 41 | unsigned int hex2uint(string hex) |
41 | unsigned int hex2uint(string hex) |
| 42 | { |
42 | { |
| 43 | return bin2uint(hex2bin(hex)); |
43 | return bin2uint(hex2bin(hex)); |
| 44 | } |
44 | } |
| 45 | 45 | ||
| 46 | unsigned int bin2uint(string bin) |
46 | unsigned int bin2uint(string bin) |
| 47 | { |
47 | { |
| 48 | unsigned int num = 0; |
48 | unsigned int num = 0; |
| 49 | 49 | ||
| 50 | for (int n = bin.length()-1; n >= 0; n--) |
50 | for (int n = bin.length()-1; n >= 0; n--) |
| 51 | { |
51 | { |
| 52 | if (bin[n] == '1') |
52 | if (bin[n] == '1') |
| 53 | num += (unsigned int)pow(2.0f, (int)(bin.length()-1-n)); |
53 | num += (unsigned int)pow(2.0f, (int)(bin.length()-1-n)); |
| 54 | } |
54 | } |
| 55 | 55 | ||
| 56 | return num; |
56 | return num; |
| 57 | } |
57 | } |
| 58 | 58 | ||
| 59 | int bin2int(string bin) |
59 | int bin2int(string bin) |
| 60 | { |
60 | { |
| 61 | int num = 0; |
61 | int num = 0; |
| 62 | if (bin[0] == '1') { |
62 | if (bin[0] == '1') { |
| 63 | for (int n = bin.length()-1; n >= 0; n--) |
63 | for (int n = bin.length()-1; n >= 0; n--) |
| 64 | { |
64 | { |
| 65 | if (bin[n] == '0') |
65 | if (bin[n] == '0') |
| 66 | num += (unsigned int)pow(2.0f, (int)(bin.length()-1-n)); |
66 | num += (unsigned int)pow(2.0f, (int)(bin.length()-1-n)); |
| 67 | } |
67 | } |
| 68 | num = (num*(-1)) -1; |
68 | num = (num*(-1)) -1; |
| 69 | } else { |
69 | } else { |
| 70 | for (int n = bin.length()-1; n >= 0; n--) |
70 | for (int n = bin.length()-1; n >= 0; n--) |
| 71 | { |
71 | { |
| 72 | if (bin[n] == '1') |
72 | if (bin[n] == '1') |
| 73 | num += (unsigned int)pow(2.0f, (int)(bin.length()-1-n)); |
73 | num += (unsigned int)pow(2.0f, (int)(bin.length()-1-n)); |
| 74 | } |
74 | } |
| 75 | } |
75 | } |
| 76 | return num; |
76 | return num; |
| 77 | } |
77 | } |
| 78 | 78 | ||
| 79 | string invert(string bin) |
79 | string invert(string bin) |
| 80 | { |
80 | { |
| 81 | string inverted; |
81 | string inverted; |
| 82 | 82 | ||
| 83 | for (int n = 0; n < bin.length(); n++) |
83 | for (int n = 0; n < bin.length(); n++) |
| 84 | inverted += (bin[n] == '1' ? '0' : '1'); |
84 | inverted += (bin[n] == '1' ? '0' : '1'); |
| 85 | 85 | ||
| 86 | return inverted; |
86 | return inverted; |
| 87 | } |
87 | } |
| 88 | 88 | ||
| 89 | float bin2float(string bin) |
89 | float bin2float(string bin) |
| 90 | { |
90 | { |
| 91 | float num = 0; |
91 | float num = 0; |
| 92 | 92 | ||
| 93 | if (bin.length() == 0) |
93 | if (bin.length() == 0) |
| 94 | return 0.0f; |
94 | return 0.0f; |
| 95 | 95 | ||
| 96 | bool isNegative = false;; |
96 | bool isNegative = false;; |
| 97 | 97 | ||
| 98 | if (bin[0] == '1') |
98 | if (bin[0] == '1') |
| 99 | { |
99 | { |
| 100 | bin = invert(bin); |
100 | bin = invert(bin); |
| 101 | isNegative = true; |
101 | isNegative = true; |
| 102 | } |
102 | } |
| 103 | 103 | ||
| 104 | for (int n = bin.length()-1; n > 0; n--) |
104 | for (int n = bin.length()-1; n > 0; n--) |
| 105 | { |
105 | { |
| 106 | if (bin[n] == '1') |
106 | if (bin[n] == '1') |
| 107 | num += pow(2.0f, (int)(bin.length()-1-n)); |
107 | num += pow(2.0f, (int)(bin.length()-1-n)); |
| 108 | } |
108 | } |
| 109 | 109 | ||
| 110 | num /= 64.0f; |
110 | num /= 64.0f; |
| 111 | 111 | ||
| 112 | if (isNegative) |
112 | if (isNegative) |
| 113 | num = -num; |
113 | num = -num; |
| 114 | 114 | ||
| 115 | return num; |
115 | return num; |
| 116 | } |
116 | } |
| 117 | 117 | ||
| 118 | string bin2hex(string bin) |
118 | string bin2hex(string bin) |
| 119 | { |
119 | { |
| 120 | string hex; |
120 | string hex; |
| 121 | 121 | ||
| 122 | while (bin.size() > 0) |
122 | while (bin.size() > 0) |
| 123 | { |
123 | { |
| 124 | string part; |
124 | string part; |
| 125 | 125 | ||
| 126 | try |
126 | try |
| 127 | { |
127 | { |
| 128 | part = bin.substr(0, 4); |
128 | part = bin.substr(0, 4); |
| 129 | } |
129 | } |
| 130 | catch (std::out_of_range& e) |
130 | catch (std::out_of_range& e) |
| 131 | { |
131 | { |
| 132 | cout << "DEBUG: bin2hex exception: " << e.what() << "\n"; |
132 | cout << "DEBUG: bin2hex exception: " << e.what() << "\n"; |
| 133 | cout << "DEBUG: A bin=" << bin << endl; |
133 | cout << "DEBUG: A bin=" << bin << endl; |
| 134 | continue; |
134 | continue; |
| 135 | } |
135 | } |
| 136 | 136 | ||
| 137 | bin.erase(0, part.size()); |
137 | bin.erase(0, part.size()); |
| 138 | 138 | ||
| 139 | while (part.size() < 4) |
139 | while (part.size() < 4) |
| 140 | part = "0" + part; |
140 | part = "0" + part; |
| 141 | 141 | ||
| 142 | if (part == "0000") |
142 | if (part == "0000") |
| 143 | hex += '0'; |
143 | hex += '0'; |
| 144 | else if (part == "0001") |
144 | else if (part == "0001") |
| 145 | hex += '1'; |
145 | hex += '1'; |
| 146 | else if (part == "0010") |
146 | else if (part == "0010") |
| 147 | hex += '2'; |
147 | hex += '2'; |
| 148 | else if (part == "0011") |
148 | else if (part == "0011") |
| 149 | hex += '3'; |
149 | hex += '3'; |
| 150 | else if (part == "0100") |
150 | else if (part == "0100") |
| 151 | hex += '4'; |
151 | hex += '4'; |
| 152 | else if (part == "0101") |
152 | else if (part == "0101") |
| 153 | hex += '5'; |
153 | hex += '5'; |
| 154 | else if (part == "0110") |
154 | else if (part == "0110") |
| 155 | hex += '6'; |
155 | hex += '6'; |
| 156 | else if (part == "0111") |
156 | else if (part == "0111") |
| 157 | hex += '7'; |
157 | hex += '7'; |
| 158 | else if (part == "1000") |
158 | else if (part == "1000") |
| 159 | hex += '8'; |
159 | hex += '8'; |
| 160 | else if (part == "1001") |
160 | else if (part == "1001") |
| 161 | hex += '9'; |
161 | hex += '9'; |
| 162 | else if (part == "1010") |
162 | else if (part == "1010") |
| 163 | hex += 'a'; |
163 | hex += 'a'; |
| 164 | else if (part == "1011") |
164 | else if (part == "1011") |
| 165 | hex += 'b'; |
165 | hex += 'b'; |
| 166 | else if (part == "1100") |
166 | else if (part == "1100") |
| 167 | hex += 'c'; |
167 | hex += 'c'; |
| 168 | else if (part == "1101") |
168 | else if (part == "1101") |
| 169 | hex += 'd'; |
169 | hex += 'd'; |
| 170 | else if (part == "1110") |
170 | else if (part == "1110") |
| 171 | hex += 'e'; |
171 | hex += 'e'; |
| 172 | else if (part == "1111") |
172 | else if (part == "1111") |
| 173 | hex += 'f'; |
173 | hex += 'f'; |
| 174 | } |
174 | } |
| 175 | 175 | ||
| 176 | return hex; |
176 | return hex; |
| 177 | } |
177 | } |
| 178 | 178 | ||
| 179 | string float2bin(float num, int length) |
179 | string float2bin(float num, int length) |
| 180 | { |
180 | { |
| 181 | // This is 16 bits, 6 bits is the decimal |
181 | // This is 16 bits, 6 bits is the decimal |
| 182 | string bin; |
182 | string bin; |
| 183 | num = num*64; |
183 | num = num*64; |
| 184 | int16_t temp = (int16_t)num; |
184 | int16_t temp = (int16_t)num; |
| 185 | return int2bin(temp, length); |
185 | return int2bin(temp, length); |
| 186 | } |
186 | } |
| 187 | 187 | ||
| 188 | string hex2bin(string hex) |
188 | string hex2bin(string hex) |
| 189 | { |
189 | { |
| 190 | hex = strtolower(hex); |
190 | hex = strtolower(hex); |
| 191 | 191 | ||
| 192 | string bin; |
192 | string bin; |
| 193 | 193 | ||
| 194 | for (int n = 0; n < hex.length(); n++) |
194 | for (int n = 0; n < hex.length(); n++) |
| 195 | { |
195 | { |
| 196 | switch (hex[n]) |
196 | switch (hex[n]) |
| 197 | { |
197 | { |
| 198 | case '0': bin += "0000"; break; |
198 | case '0': bin += "0000"; break; |
| 199 | case '1': bin += "0001"; break; |
199 | case '1': bin += "0001"; break; |
| 200 | case '2': bin += "0010"; break; |
200 | case '2': bin += "0010"; break; |
| 201 | case '3': bin += "0011"; break; |
201 | case '3': bin += "0011"; break; |
| 202 | case '4': bin += "0100"; break; |
202 | case '4': bin += "0100"; break; |
| 203 | case '5': bin += "0101"; break; |
203 | case '5': bin += "0101"; break; |
| 204 | case '6': bin += "0110"; break; |
204 | case '6': bin += "0110"; break; |
| 205 | case '7': bin += "0111"; break; |
205 | case '7': bin += "0111"; break; |
| 206 | case '8': bin += "1000"; break; |
206 | case '8': bin += "1000"; break; |
| 207 | case '9': bin += "1001"; break; |
207 | case '9': bin += "1001"; break; |
| 208 | case 'a': bin += "1010"; break; |
208 | case 'a': bin += "1010"; break; |
| 209 | case 'b': bin += "1011"; break; |
209 | case 'b': bin += "1011"; break; |
| 210 | case 'c': bin += "1100"; break; |
210 | case 'c': bin += "1100"; break; |
| 211 | case 'd': bin += "1101"; break; |
211 | case 'd': bin += "1101"; break; |
| 212 | case 'e': bin += "1110"; break; |
212 | case 'e': bin += "1110"; break; |
| 213 | case 'f': bin += "1111"; break; |
213 | case 'f': bin += "1111"; break; |
| 214 | } |
214 | } |
| 215 | } |
215 | } |
| 216 | 216 | ||
| 217 | return bin; |
217 | return bin; |
| 218 | } |
218 | } |
| 219 | 219 | ||
| 220 | string uint2bin(unsigned int num, int length) |
220 | string uint2bin(unsigned int num, int length) |
| 221 | { |
221 | { |
| 222 | string bin; |
222 | string bin; |
| 223 | while (num) |
223 | while (num) |
| 224 | { |
224 | { |
| 225 | bin = char((num&1)+'0') + bin; |
225 | bin = char((num&1)+'0') + bin; |
| 226 | num >>= 1; |
226 | num >>= 1; |
| 227 | } |
227 | } |
| 228 | 228 | ||
| 229 | while (bin.size() < length) |
229 | while (bin.size() < length) |
| 230 | bin = "0" + bin; |
230 | bin = "0" + bin; |
| 231 | 231 | ||
| 232 | string bin2; |
232 | string bin2; |
| 233 | for (int i = 0; i < length; i++) |
233 | for (int i = 0; i < length; i++) |
| 234 | { |
234 | { |
| 235 | bin2 = bin2 + bin[i]; |
235 | bin2 = bin2 + bin[i]; |
| 236 | } |
236 | } |
| 237 | 237 | ||
| 238 | return bin2; |
238 | return bin2; |
| 239 | } |
239 | } |
| 240 | 240 | ||
| 241 | string int2bin(int num, int length) |
241 | string int2bin(int num, int length) |
| 242 | { |
242 | { |
| 243 | string bin; |
243 | string bin; |
| 244 | if (num < 0) { |
244 | if (num < 0) { |
| 245 | num *= -1; |
245 | num *= -1; |
| 246 | num -= 1; |
246 | num -= 1; |
| 247 | while (num) |
247 | while (num) |
| 248 | { |
248 | { |
| 249 | bin = char((num&1)+'0') + bin; |
249 | bin = char((num&1)+'0') + bin; |
| 250 | num >>= 1; |
250 | num >>= 1; |
| 251 | } |
251 | } |
| 252 | 252 | ||
| 253 | while (bin.size() < length) |
253 | while (bin.size() < length) |
| 254 | bin = "0" + bin; |
254 | bin = "0" + bin; |
| 255 | for (int i = 0; i < length; i++) |
255 | for (int i = 0; i < length; i++) |
| 256 | { |
256 | { |
| 257 | if (bin[i] == '0') |
257 | if (bin[i] == '0') |
| 258 | bin[i] = '1'; |
258 | bin[i] = '1'; |
| 259 | else |
259 | else |
| 260 | bin[i] = '0'; |
260 | bin[i] = '0'; |
| 261 | } |
261 | } |
| 262 | } else { |
262 | } else { |
| 263 | while (num) |
263 | while (num) |
| 264 | { |
264 | { |
| 265 | bin = char((num&1)+'0') + bin; |
265 | bin = char((num&1)+'0') + bin; |
| 266 | num >>= 1; |
266 | num >>= 1; |
| 267 | } |
267 | } |
| 268 | 268 | ||
| 269 | while (bin.size() < length) |
269 | while (bin.size() < length) |
| 270 | bin = "0" + bin; |
270 | bin = "0" + bin; |
| 271 | 271 | ||
| 272 | } |
272 | } |
| 273 | string bin2; |
273 | string bin2; |
| 274 | for (int i = 0; i < length; i++) |
274 | for (int i = 0; i < length; i++) |
| 275 | { |
275 | { |
| 276 | bin2 = bin2 + bin[i]; |
276 | bin2 = bin2 + bin[i]; |
| 277 | } |
277 | } |
| 278 | 278 | ||
| 279 | return bin2; |
279 | return bin2; |
| 280 | } |
280 | } |
| 281 | 281 | ||
| 282 | string int2hex(int num, int length) |
282 | string int2hex(int num, int length) |
| 283 | { |
283 | { |
| 284 | return bin2hex(int2bin(num, length)); |
284 | return bin2hex(int2bin(num, length)); |
| 285 | } |
285 | } |
| 286 | 286 | ||
| 287 | string uint2hex(unsigned int num, int length) |
287 | string uint2hex(unsigned int num, int length) |
| 288 | { |
288 | { |
| 289 | return bin2hex(uint2bin(num, length)); |
289 | return bin2hex(uint2bin(num, length)); |
| 290 | } |
290 | } |
| 291 | 291 | ||
| 292 | vector<string> explode(string delimiter, string str) |
292 | vector<string> explode(string delimiter, string str) |
| 293 | { |
293 | { |
| 294 | vector<string> parts; |
294 | vector<string> parts; |
| 295 | string::size_type startPos = 0; |
295 | string::size_type startPos = 0; |
| 296 | string::size_type endPos = 0; |
296 | string::size_type endPos = 0; |
| 297 | 297 | ||
| 298 | while ((endPos = str.find(delimiter, startPos)) != string::npos) |
298 | while ((endPos = str.find(delimiter, startPos)) != string::npos) |
| 299 | { |
299 | { |
| 300 | try |
300 | try |
| 301 | { |
301 | { |
| 302 | parts.push_back(str.substr(startPos, endPos-startPos)); |
302 | parts.push_back(str.substr(startPos, endPos-startPos)); |
| 303 | } |
303 | } |
| 304 | catch (std::out_of_range& e) |
304 | catch (std::out_of_range& e) |
| 305 | { |
305 | { |
| 306 | cout << "DEBUG: explode exception: " << e.what() << "\n"; |
306 | cout << "DEBUG: explode exception: " << e.what() << "\n"; |
| 307 | cout << "DEBUG: A str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl; |
307 | cout << "DEBUG: A str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl; |
| 308 | continue; |
308 | continue; |
| 309 | } |
309 | } |
| 310 | 310 | ||
| 311 | startPos = endPos+delimiter.length(); |
311 | startPos = endPos+delimiter.length(); |
| 312 | } |
312 | } |
| 313 | 313 | ||
| 314 | if (startPos < str.length()) |
314 | if (startPos < str.length()) |
| 315 | { |
315 | { |
| 316 | try |
316 | try |
| 317 | { |
317 | { |
| 318 | parts.push_back(str.substr(startPos, str.length()-startPos)); |
318 | parts.push_back(str.substr(startPos, str.length()-startPos)); |
| 319 | } |
319 | } |
| 320 | catch (std::out_of_range& e) |
320 | catch (std::out_of_range& e) |
| 321 | { |
321 | { |
| 322 | cout << "DEBUG: explode exception: " << e.what() << "\n"; |
322 | cout << "DEBUG: explode exception: " << e.what() << "\n"; |
| 323 | cout << "DEBUG: B str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl; |
323 | cout << "DEBUG: B str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl; |
| 324 | } |
324 | } |
| 325 | } |
325 | } |
| 326 | 326 | ||
| 327 | return parts; |
327 | return parts; |
| 328 | } |
328 | } |
| 329 | 329 | ||
| 330 | string strtoupper(string s) |
330 | string strtoupper(string s) |
| 331 | { |
331 | { |
| 332 | for (unsigned int n = 0; n < s.size(); n++) |
332 | for (unsigned int n = 0; n < s.size(); n++) |
| 333 | s[n] = (char)toupper(s[n]); |
333 | s[n] = (char)toupper(s[n]); |
| 334 | 334 | ||
| 335 | return s; |
335 | return s; |
| 336 | } |
336 | } |
| 337 | 337 | ||
| 338 | string strtolower(string s) |
338 | string strtolower(string s) |
| 339 | { |
339 | { |
| 340 | for (unsigned int n = 0; n < s.size(); n++) |
340 | for (unsigned int n = 0; n < s.size(); n++) |
| 341 | s[n] = (char)tolower(s[n]); |
341 | s[n] = (char)tolower(s[n]); |
| 342 | 342 | ||
| 343 | return s; |
343 | return s; |
| 344 | } |
344 | } |
| 345 | 345 | ||
| 346 | string trim(string s) |
346 | string trim(string s) |
| 347 | { |
347 | { |
| 348 | return trim(s, ' '); |
348 | return trim(s, ' '); |
| 349 | } |
349 | } |
| 350 | 350 | ||
| 351 | string trim(const string s, char c) |
351 | string trim(const string s, char c) |
| 352 | { |
352 | { |
| 353 | string result = s; |
353 | string result = s; |
| 354 | while (result[0] == c) |
354 | while (result[0] == c) |
| 355 | result.erase(0, 1); |
355 | result.erase(0, 1); |
| 356 | 356 | ||
| 357 | while (result[result.length()-1] == c) |
357 | while (result[result.length()-1] == c) |
| 358 | result.erase(result.length()-1, 1); |
358 | result.erase(result.length()-1, 1); |
| 359 | 359 | ||
| 360 | return result; |
360 | return result; |
| 361 | } |
361 | } |
| 362 | 362 | ||
| 363 | int stoi(const string s) |
363 | int stoi(const string s) |
| 364 | { |
364 | { |
| 365 | istringstream in(s); |
365 | istringstream in(s); |
| 366 | int i; |
366 | int i; |
| 367 | in >> i; |
367 | in >> i; |
| 368 | return i; |
368 | return i; |
| 369 | } |
369 | } |
| 370 | 370 | ||
| 371 | string itos(int i) |
371 | string itos(int i) |
| 372 | { |
372 | { |
| 373 | ostringstream out; |
373 | ostringstream out; |
| 374 | out << i; |
374 | out << i; |
| 375 | return out.str(); |
375 | return out.str(); |
| 376 | } |
376 | } |
| 377 | 377 | ||
| 378 | unsigned int stou(const string s) |
378 | unsigned int stou(const string s) |
| 379 | { |
379 | { |
| 380 | istringstream in(s); |
380 | istringstream in(s); |
| 381 | unsigned int i; |
381 | unsigned int i; |
| 382 | in >> i; |
382 | in >> i; |
| 383 | return i; |
383 | return i; |
| 384 | } |
384 | } |
| 385 | 385 | ||
| 386 | string utos(unsigned int i) |
386 | string utos(unsigned int i) |
| 387 | { |
387 | { |
| 388 | ostringstream out; |
388 | ostringstream out; |
| 389 | out << i; |
389 | out << i; |
| 390 | return out.str(); |
390 | return out.str(); |
| 391 | } |
391 | } |
| 392 | 392 | ||
| 393 | float stof(const string s) |
393 | float stof(const string s) |
| 394 | { |
394 | { |
| 395 | istringstream in(s); |
395 | istringstream in(s); |
| 396 | float f; |
396 | float f; |
| 397 | in >> f; |
397 | in >> f; |
| 398 | return f; |
398 | return f; |
| 399 | } |
399 | } |
| 400 | 400 | ||
| 401 | string ftos(float f) |
401 | string ftos(float f) |
| 402 | { |
402 | { |
| 403 | ostringstream out; |
403 | ostringstream out; |
| 404 | out << f; |
404 | out << f; |
| 405 | return out.str(); |
405 | return out.str(); |
| 406 | } |
406 | } |
| 407 | 407 | ||
| 408 | string str_replace(string search, string replace, string str) |
408 | string str_replace(string search, string replace, string str) |
| 409 | { |
409 | { |
| 410 | vector<string> parts = explode(search, str); |
410 | vector<string> parts = explode(search, str); |
| 411 | 411 | ||
| 412 | string result; |
412 | string result; |
| 413 | for (int n = 0; n < parts.size(); n++) |
413 | for (int n = 0; n < parts.size(); n++) |
| 414 | { |
414 | { |
| 415 | result += parts[n]; |
415 | result += parts[n]; |
| 416 | 416 | ||
| 417 | if (n < parts.size()-1) |
417 | if (n < parts.size()-1) |
| 418 | result += replace; |
418 | result += replace; |
| 419 | } |
419 | } |
| 420 | 420 | ||
| 421 | return result; |
421 | return result; |
| 422 | } |
422 | } |
| 423 | 423 | ||
| 424 | string file_get_contents(string filename) |
424 | string file_get_contents(string filename) |
| 425 | { |
425 | { |
| 426 | ifstream srcfile(filename.c_str()); |
426 | ifstream srcfile(filename.c_str()); |
| 427 | 427 | ||
| 428 | if (!srcfile.is_open()) |
428 | if (!srcfile.is_open()) |
| 429 | { |
429 | { |
| 430 | srcfile.close(); |
430 | srcfile.close(); |
| 431 | return ""; |
431 | return ""; |
| 432 | } |
432 | } |
| 433 | 433 | ||
| 434 | string buffer = ""; |
434 | string buffer = ""; |
| 435 | string line; |
435 | string line; |
| 436 | 436 | ||
| 437 | while (!srcfile.eof()) |
437 | while (!srcfile.eof()) |
| 438 | { |
438 | { |
| 439 | getline(srcfile, line); |
439 | getline(srcfile, line); |
| 440 | 440 | ||
| 441 | if (srcfile.eof()) |
441 | if (srcfile.eof()) |
| 442 | break; |
442 | break; |
| 443 | 443 | ||
| 444 | buffer += line + "\n"; |
444 | buffer += line + "\n"; |
| 445 | }; |
445 | }; |
| 446 | 446 | ||
| 447 | srcfile.close(); |
447 | srcfile.close(); |
| 448 | 448 | ||
| 449 | return buffer; |
449 | return buffer; |
| 450 | } |
450 | } |
| 451 | 451 | ||
| 452 | bool file_exists(string filename) |
452 | bool file_exists(string filename) |
| 453 | { |
453 | { |
| 454 | ifstream file(filename.c_str()); |
454 | ifstream file(filename.c_str()); |
| 455 | if (file.is_open()) |
455 | if (file.is_open()) |
| 456 | { |
456 | { |
| 457 | file.close(); |
457 | file.close(); |
| 458 | return true; |
458 | return true; |
| 459 | } |
459 | } |
| 460 | 460 | ||
| 461 | return false; |
461 | return false; |
| 462 | } |
462 | } |
| 463 | 463 | ||
| 464 | string escape(string in) |
464 | string escape(string in) |
| 465 | { |
465 | { |
| 466 | string out; |
466 | string out; |
| 467 | 467 | ||
| 468 | for (int n = 0; n < in.length(); n++) |
468 | for (int n = 0; n < in.length(); n++) |
| 469 | { |
469 | { |
| 470 | switch (in[n]) |
470 | switch (in[n]) |
| 471 | { |
471 | { |
| 472 | case '\r': |
472 | case '\r': |
| 473 | break; |
473 | break; |
| 474 | 474 | ||
| 475 | case '\n': |
475 | case '\n': |
| 476 | out += "\\n"; |
476 | out += "\\n"; |
| 477 | break; |
477 | break; |
| 478 | 478 | ||
| 479 | case '\'': |
479 | case '\'': |
| 480 | out += "\\'"; |
480 | out += "\\'"; |
| - | 481 | break; |
|
| - | 482 | ||
| - | 483 | case '\\': |
|
| - | 484 | out += "\\\\"; |
|
| 481 | break; |
485 | break; |
| 482 | 486 | ||
| 483 | case '"': |
487 | case '"': |
| 484 | out += "\\\""; |
488 | out += "\\\""; |
| 485 | break; |
489 | break; |
| 486 | 490 | ||
| 487 | default: |
491 | default: |
| 488 | out += in[n]; |
492 | out += in[n]; |
| 489 | break; |
493 | break; |
| 490 | } |
494 | } |
| 491 | } |
495 | } |
| 492 | 496 | ||
| 493 | return out; |
497 | return out; |
| 494 | } |
498 | } |
| 495 | 499 | ||
| 496 | string rpad(string in, int length, char c) |
500 | string rpad(string in, int length, char c) |
| 497 | { |
501 | { |
| 498 | while (in.length() < length) |
502 | while (in.length() < length) |
| 499 | { |
503 | { |
| 500 | in += c; |
504 | in += c; |
| 501 | } |
505 | } |
| 502 | 506 | ||
| 503 | return in; |
507 | return in; |
| 504 | } |
508 | } |
| 505 | 509 | ||
| 506 | string lpad(string in, int length, char c) |
510 | string lpad(string in, int length, char c) |
| 507 | { |
511 | { |
| 508 | while (in.length() < length) |
512 | while (in.length() < length) |
| 509 | { |
513 | { |
| 510 | in = c + in; |
514 | in = c + in; |
| 511 | } |
515 | } |
| 512 | 516 | ||
| 513 | return in; |
517 | return in; |
| 514 | } |
518 | } |
| 515 | 519 | ||