00001
00005 #include <stdio.h>
00006 #include <sys/socket.h>
00007 #include <arpa/inet.h>
00008 #include <stdlib.h>
00009 #include <string.h>
00010 #include "toolbox.hpp"
00011 #include <iostream>
00012 #include <iomanip>
00013 #include <sstream>
00014 #include "errorhandler.hpp"
00015
00016
00017
00018
00019
00020 void traceBuffer(std::string headerText, BYTE* buffer, UINT32 len)
00021 {
00022
00023 printInfoMessage(headerText, true);
00024
00025
00026 std::string line;
00027 line = "Length= " + toString(len) + " bytes.";
00028 printInfoMessage(line, true);
00029
00030
00031 UINT32 pos = 0;
00032 while (pos < len)
00033 {
00034 line = toHexString(pos) + ": ";
00035 for (UINT16 i=0; i< 16; i++)
00036 {
00037 line += toHexString(buffer[pos]) + " ";
00038 pos++;
00039 if (pos >= len)
00040 {
00041 break;
00042 }
00043 }
00044 printInfoMessage(line, true);
00045 }
00046 }
00047
00048
00049
00050
00051
00052 std::string toHexStringNibble(UINT8 val)
00053 {
00054 std::string s = "0123456789ABCDEF";
00055 std::string c;
00056 if (val < 16)
00057 {
00058 c = s.substr(val, 1);
00059 }
00060 else
00061 {
00062 c = "x";
00063 }
00064 return c;
00065 }
00066
00067
00068
00069
00070
00071 std::string toHexString(UINT32 val)
00072 {
00073 std::string s = toHexString((UINT16)(val >> 16));
00074 s += toHexString((UINT16)(val & 0xFFFF));
00075 return s;
00076 }
00077
00078
00079 std::string toHexString(UINT16 val)
00080 {
00081 std::string s = toHexStringNibble((UINT8)(val >> 12));
00082 s += toHexStringNibble((UINT8)((val >> 8) & 0xF));
00083 s += toHexStringNibble((UINT8)((val >> 4) & 0xF));
00084 s += toHexStringNibble((UINT8)(val & 0xF));
00085 return s;
00086 }
00087
00088
00089 std::string toHexString(UINT8 val)
00090 {
00091 std::string s1 = toHexStringNibble((UINT8)(val >> 4));
00092 std::string s2 = toHexStringNibble((UINT8)(val & 0x0F));
00093 std::string s = s1 + s2;
00094 return s;
00095 }
00096
00097
00098
00099
00100
00101 std::string toLower(const std::string& text)
00102 {
00103 std::string low;
00104 UINT32 i;
00105 unsigned char c;
00106 for (i=0; i < text.length(); i++)
00107 {
00108 c = text.at(i);
00109 if ((c >= 'A') || (c <= 'Z'))
00110 {
00111
00112 c += ('a' - 'A');
00113 }
00114 low += c;
00115 }
00116
00117 return low;
00118 }
00119
00120
00121
00122
00123
00124
00125 std::string convertMeterToFeetAndInch(double m)
00126 {
00127 std::ostringstream os;
00128 std::string text;
00129
00130
00131 if (m < 0.0)
00132 {
00133 os << "-";
00134 m = -m;
00135 }
00136
00137 INT32 feet = (INT32)(m / 0.3048);
00138 INT32 inch = (INT32)((m - ((double)feet * 0.3048)) / 0.0254);
00139 if (feet > 0)
00140 {
00141 os << feet << "'";
00142 }
00143 if ((inch > 0) || (feet == 0))
00144 {
00145 os << inch << "\"";
00146 }
00147 text = os.str();
00148
00149
00150 return text;
00151 }
00152
00153
00154
00155
00156
00157
00158 UINT16 fromString(const std::string& text)
00159 {
00160 int value;
00161 int conversions = sscanf(text.c_str(), "%d", &value);
00162 if (conversions == 1)
00163 {
00164 return (UINT16)value;
00165 }
00166
00167 return 0;
00168 }
00169
00173 int hexCharToValue(char c)
00174 {
00175 int value = 0;
00176
00177 if ((c >= '0') && (c <= '9'))
00178 {
00179 value = c - '0';
00180 }
00181 else if ((c >= 'A') && (c <= 'F'))
00182 {
00183 value = c - 'A' + 10;
00184 }
00185 else if ((c >= 'a') && (c <= 'f'))
00186 {
00187 value = c - 'a' + 10;
00188 }
00189
00190 return value;
00191 }
00192
00193
00197 char convertNibbleToHexChar(int value, bool useLowerCaseLetters)
00198 {
00199 char c;
00200
00201 if (value < 10)
00202 {
00203 c = '0' + value;
00204 }
00205 else
00206 {
00207 if (useLowerCaseLetters == false)
00208 {
00209
00210 c = 'A' + value - 10;
00211 }
00212 else
00213 {
00214
00215 c = 'a' + value - 10;
00216 }
00217 }
00218
00219 return c;
00220 }
00221
00227 void convertUINT8toHexString(UINT8 byte, char* buffer)
00228 {
00229 UINT8 value = (byte >> 4);
00230 buffer[0] = convertNibbleToHexChar(value);
00231 value = byte & 0x0F;
00232 buffer[1] = convertNibbleToHexChar(value);
00233 }
00234
00240 void convertRGBtoHexString(UINT8 r, UINT8 g, UINT8 b, char* buffer)
00241 {
00242 convertUINT8toHexString(r, buffer);
00243 convertUINT8toHexString(g, &buffer[2]);
00244 convertUINT8toHexString(b, &buffer[4]);
00245 }
00246
00247
00248
00252 double makeAngleValid(double angle)
00253 {
00254 const double twoPi = (2.0 * PI);
00255
00256 while (angle >= PI)
00257 {
00258 angle -= twoPi;
00259 }
00260 while (angle < -PI)
00261 {
00262 angle += twoPi;
00263 }
00264
00265 return angle;
00266 }
00267
00271 std::string toString(INT32 value)
00272 {
00273 char c[16];
00274 sprintf(c, "%i", value);
00275 return (std::string(c));
00276 }
00277
00281 std::string toString(UINT32 value)
00282 {
00283 char c[16];
00284 sprintf(c, "%i", value);
00285 return (std::string(c));
00286 }
00287
00288 #if INTPTR_MAX != INT32_MAX
00289 std::string toString(size_t value)
00290 {
00291 char c[16];
00292 sprintf(c, "%zu", value);
00293 return (std::string(c));
00294 }
00295 #endif
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 std::string doubleToString(double val,
00308 std::string::size_type digits_before_decimal_point,
00309 std::string::size_type digits_after_decimal_point)
00310 {
00311
00312 std::string text = doubleToString(val, digits_after_decimal_point);
00313
00314
00315 const std::string::size_type dotPosition = text.find_first_of('.', 0);
00316 if (dotPosition != std::string::npos)
00317 {
00318
00319 if (dotPosition < digits_before_decimal_point)
00320 {
00321
00322 std::string::size_type numExtraSpaces = digits_before_decimal_point - dotPosition;
00323 text = std::string(numExtraSpaces, ' ') + text;
00324 }
00325 }
00326
00327
00328
00329
00330 if (text.length() < (digits_before_decimal_point + digits_after_decimal_point + 1))
00331 {
00332
00333 std::string::size_type numExtraSpaces =
00334 (digits_before_decimal_point + digits_after_decimal_point + 1) - text.length();
00335 text += std::string(numExtraSpaces, ' ');
00336 }
00337
00338 return text;
00339 }
00340
00344 std::string doubleToString(double val,
00345 int digits_after_decimal_point)
00346 {
00347
00348 std::stringstream sstr;
00349 sstr << std::fixed << std::setprecision(digits_after_decimal_point) << val;
00350
00351 return sstr.str();
00352 }
00353
00354 std::string toString(double val, int digits_after_decimal_point)
00355 {
00356 return doubleToString(val, digits_after_decimal_point);
00357 }
00358
00359
00360
00361
00362
00363
00364
00365 void stringToIpTarget(std::string ipAdrStr, UINT32& ipAddress, UINT16& port)
00366 {
00367 std::string addrStr;
00368 std::string portStr;
00369
00370 if (ipAdrStr.length() < 3)
00371 {
00372
00373 return;
00374 }
00375
00376 UINT32 adrVal = INADDR_NONE;
00377 UINT16 portVal = 0;
00378
00379
00380 size_t pos = ipAdrStr.find_first_of(':');
00381 if ((pos > 0) && (pos < (ipAdrStr.length() - 1)))
00382 {
00383 addrStr = ipAdrStr.substr(0, pos);
00384 portStr = ipAdrStr.substr(pos+1);
00385 }
00386 else
00387 {
00388 addrStr = ipAdrStr;
00389 }
00390
00391
00392 adrVal = (UINT32)inet_addr(addrStr.c_str());
00393 ipAddress = adrVal;
00394
00395
00396 if (portStr.length() > 0)
00397 {
00398 portVal = fromString(portStr);
00399 port = portVal;
00400 }
00401 }
00402
00403
00404
00405
00406
00407 std::string ipTargetToString(UINT32 ipAddress, UINT16 port)
00408 {
00409 std::string addr;
00410 addr = ipAdrToString(ipAddress);
00411
00412
00413 addr += ":";
00414 addr += toString((UINT16)port);
00415
00416 return addr;
00417 }
00418
00419
00420
00421
00422
00423
00424 std::string ipAdrToString(UINT32 ipAddress)
00425 {
00426 std::string addr;
00427 addr = toString((UINT16)((ipAddress >> 0 ) & 0xFF)) + "." +
00428 toString((UINT16)((ipAddress >> 8 ) & 0xFF)) + "." +
00429 toString((UINT16)((ipAddress >> 16) & 0xFF)) + "." +
00430 toString((UINT16)((ipAddress >> 24) & 0xFF));
00431
00432 return addr;
00433 }
00434
00435
00436
00437
00438
00439 UINT32 memread_UINT32(BYTE*& buffer)
00440 {
00441 UINT32 value = (((UINT32)buffer[0]) << 24) +
00442 (((UINT32)buffer[1]) << 16) +
00443 (((UINT32)buffer[2]) << 8 ) +
00444 (((UINT32)buffer[3]) );
00445 buffer += 4;
00446 return value;
00447 }
00448
00449
00450
00451
00452
00453 UINT16 memread_UINT16(BYTE*& buffer)
00454 {
00455 UINT16 value = (((UINT16)buffer[0]) << 8) +
00456 ((UINT16)buffer[1]);
00457 buffer += 2;
00458 return value;
00459 }
00460
00461
00462
00463
00464 UINT8 memread_UINT8(BYTE*& buffer)
00465 {
00466 UINT8 value = buffer[0];
00467 buffer++;
00468 return value;
00469 }
00470
00471
00472
00473
00474 INT16 memread_INT16(BYTE*& buffer)
00475 {
00476 UINT16 value = (((UINT16)buffer[0]) << 8) +
00477 ((UINT16)buffer[1]);
00478 buffer += 2;
00479 return (INT16)value;
00480 }
00481
00482
00483
00484
00485 INT32 memread_INT32(BYTE*& buffer)
00486 {
00487 UINT32 value = (((UINT32)buffer[0]) << 24) +
00488 (((UINT32)buffer[1]) << 16) +
00489 (((UINT32)buffer[2]) << 8 ) +
00490 (((UINT32)buffer[3]) );
00491 buffer += 4;
00492 return (INT32)value;
00493 }
00494
00495
00496
00497
00498 std::string memread_string(BYTE*& buffer, UINT16 length)
00499 {
00500 std::string text;
00501
00502 for (UINT16 i = 0; i<length; i++)
00503 {
00504 text += buffer[i];
00505 }
00506 buffer += length;
00507
00508 return text;
00509 }
00510
00511
00513 union FloatInt
00514 {
00515 float value_float;
00516 UINT32 value_int;
00517 };
00518
00519
00520
00521
00522
00523 float memread_float(BYTE*& buffer)
00524 {
00525 FloatInt floatint;
00526 floatint.value_int = memread_UINT32(buffer);
00527 return floatint.value_float;
00528 }
00529
00530
00531
00532
00533
00534 void memwrite_float(BYTE*& buffer, float value)
00535 {
00536 FloatInt floatint;
00537 floatint.value_float = value;
00538 memwrite_UINT32(buffer, floatint.value_int);
00539 }
00540
00541
00542
00543
00544
00545 void memwrite_INT32(BYTE*& buffer, INT32 value)
00546 {
00547 memwrite_UINT32(buffer, (UINT32)value);
00548 }
00549
00550
00551
00552
00553
00554 void memwrite_UINT32(BYTE*& buffer, UINT32 value)
00555 {
00556 buffer[0] = ((value >> 24) & 0xFF);
00557 buffer[1] = ((value >> 16) & 0xFF);
00558 buffer[2] = ((value >> 8 ) & 0xFF);
00559 buffer[3] = ((value ) & 0xFF);
00560 buffer += 4;
00561 }
00562
00563
00564
00565
00566
00567 void memwrite_INT16(BYTE*& buffer, INT16 value)
00568 {
00569 memwrite_UINT16(buffer, (UINT16)value);
00570 }
00571
00572
00573
00574
00575
00576 void memwrite_UINT16(BYTE*& buffer, UINT16 value)
00577 {
00578 buffer[0] = ((value >> 8 ) & 0xFF);
00579 buffer[1] = ((value ) & 0xFF);
00580 buffer += 2;
00581 }
00582
00583
00584
00585
00586 void memwrite_UINT8(BYTE*& buffer, UINT8 value)
00587 {
00588 buffer[0] = value;
00589 buffer++;
00590 }
00591
00592
00593
00594
00595 void memwrite_INT8(BYTE*& buffer, INT8 value)
00596 {
00597 buffer[0] = value;
00598 buffer++;
00599 }
00600
00601
00602
00603
00604 void memwrite_string(BYTE*& buffer, std::string text)
00605 {
00606 strncpy((char*)buffer, text.c_str(), text.length());
00607 buffer += text.length();
00608 }