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