toolbox.cpp
Go to the documentation of this file.
00001 
00005 #include <stdio.h>      /* for printf() and fprintf() */
00006 #include <sys/socket.h> /* for socket(), bind(), and connect() */
00007 #include <arpa/inet.h>  /* for sockaddr_in and inet_ntoa() */
00008 #include <stdlib.h>     /* for atoi() and exit() */
00009 #include <string.h>     /* for memset() */
00010 #include "sick_scan/tcp/toolbox.hpp"
00011 #include <iostream>
00012 #include <iomanip>              // for std::setprecision
00013 #include <sstream>              // for std::stringstream
00014 #include "sick_scan/tcp/errorhandler.hpp"
00015 
00016 #ifdef _MSC_VER
00017 #pragma warning(disable: 4267)
00018 #pragma warning(disable: 4996)
00019 #endif
00020 //
00021 // Write a binary trace output, e.g. of buffer contents.
00022 // Can be used for debugging.
00023 //
00024 void traceBuffer(std::string headerText, BYTE* buffer, UINT32 len)
00025 {
00026         // Table header
00027         printInfoMessage(headerText, true);
00028 
00029         // Length
00030         std::string line;
00031         line = "Length= " + toString(len) + " bytes.";
00032         printInfoMessage(line, true);
00033         
00034         // Contents
00035         UINT32 pos = 0;
00036         while (pos < len)
00037         {
00038                 line = toHexString(pos) + ": ";
00039                 for (UINT16 i=0; i< 16; i++)
00040                 {
00041                         line += toHexString(buffer[pos]) +  " ";
00042                         pos++;
00043                         if (pos >= len)
00044                         {
00045                                 break;
00046                         }
00047                 }
00048                 printInfoMessage(line, true);
00049         }
00050 }
00051 
00052 //
00053 // String conversion: Values to a hex string.
00054 // 0..16 --> 0..F
00055 //
00056 std::string toHexStringNibble(UINT8 val)
00057 {
00058         std::string s = "0123456789ABCDEF";
00059         std::string c;
00060         if (val < 16)
00061         {
00062                 c = s.substr(val, 1);
00063         }
00064         else
00065         {
00066                 c = "x";
00067         }
00068         return c;
00069 }
00070 
00071 //
00072 // UINT32-value to Hex-String
00073 // Result: "xxxxxxxx"
00074 //
00075 std::string toHexString(UINT32 val)
00076 {
00077         std::string s = toHexString((UINT16)(val >> 16));
00078         s += toHexString((UINT16)(val & 0xFFFF));
00079         return s;
00080 }
00081 
00082 // Ergebnis: "xxxx"
00083 std::string toHexString(UINT16 val)
00084 {
00085         std::string s = toHexStringNibble((UINT8)(val >> 12));
00086         s += toHexStringNibble((UINT8)((val >> 8) & 0xF));
00087         s += toHexStringNibble((UINT8)((val >> 4) & 0xF));
00088         s += toHexStringNibble((UINT8)(val & 0xF));
00089         return s;
00090 }
00091 
00092 // Ergebnis: "xx"
00093 std::string toHexString(UINT8 val)
00094 {
00095         std::string s1 = toHexStringNibble((UINT8)(val >> 4));
00096         std::string s2 = toHexStringNibble((UINT8)(val & 0x0F));
00097         std::string s = s1 + s2;
00098         return s;
00099 }
00100 
00101 
00102 //
00103 // Konvertiert einen String in Kleinbuchstaben
00104 //
00105 std::string toLower(const std::string& text)
00106 {
00107         std::string low;
00108         UINT32 i;
00109         unsigned char c;
00110         for (i=0; i < text.length(); i++)
00111         {
00112                 c = text.at(i);
00113                 if ((c >= 'A') || (c <= 'Z'))
00114                 {
00115                         // Grossbuchstabe umwandeln
00116                         c += ('a' - 'A');
00117                 }
00118                 low += c;
00119         }
00120         
00121         return low;
00122 }
00123 
00124 
00125 //
00126 // Konvertiere eine Angabe in [m] in Fuesse und Inches, als Text.
00127 // Ausgabeformat ist <<ff' ii">>
00128 //
00129 std::string convertMeterToFeetAndInch(double m)
00130 {
00131         std::ostringstream os;
00132         std::string text;
00133 
00134         // Vorzeichen verarbeiten
00135         if (m < 0.0)
00136         {
00137                 os << "-";
00138                 m = -m;
00139         }
00140 
00141         INT32 feet = (INT32)(m / 0.3048);
00142         INT32 inch = (INT32)((m - ((double)feet * 0.3048)) / 0.0254);
00143         if (feet > 0)
00144         {
00145                 os << feet << "'";
00146         }
00147         if ((inch > 0) || (feet == 0))
00148         {
00149                 os << inch << "\"";
00150         }
00151         text = os.str();
00152 
00153         // Ausgabe
00154         return text;
00155 }
00156 
00157 
00158 
00159 //
00160 // String --> UINT16
00161 //
00162 UINT16 fromString(const std::string& text)
00163 {
00164         int value;
00165         int conversions = sscanf(text.c_str(), "%d", &value);
00166         if (conversions == 1)
00167         {
00168                 return (UINT16)value;
00169         }
00170         
00171         return 0;
00172 }
00173 
00177 int hexCharToValue(char c)
00178 {
00179         int value = 0;
00180         
00181         if ((c >= '0') && (c <= '9'))
00182         {
00183                 value = c - '0';
00184         }
00185         else if ((c >= 'A') && (c <= 'F'))
00186         {
00187                 value = c - 'A' + 10;
00188         }
00189         else if ((c >= 'a') && (c <= 'f'))
00190         {
00191                 value = c - 'a' + 10;
00192         }
00193         
00194         return value;
00195 }
00196 
00197 
00201 char convertNibbleToHexChar(int value, bool useLowerCaseLetters)
00202 {
00203         char c;
00204         
00205         if (value < 10)
00206         {
00207                 c = '0' + value;
00208         }
00209         else
00210         {
00211                 if (useLowerCaseLetters == false)
00212                 {
00213                         // Grossbuchstaben
00214                         c = 'A' + value - 10;
00215                 }
00216                 else
00217                 {
00218                         // Kleinbuchstaben
00219                         c = 'a' + value - 10;
00220                 }
00221         }
00222         
00223         return c;
00224 }
00225 
00231 void convertUINT8toHexString(UINT8 byte, char* buffer)
00232 {
00233         UINT8 value = (byte >> 4);
00234         buffer[0] = convertNibbleToHexChar(value);
00235         value = byte & 0x0F;
00236         buffer[1] = convertNibbleToHexChar(value);
00237 }
00238 
00244 void convertRGBtoHexString(UINT8 r, UINT8 g, UINT8 b, char* buffer)
00245 {
00246         convertUINT8toHexString(r, buffer);
00247         convertUINT8toHexString(g, &buffer[2]);
00248         convertUINT8toHexString(b, &buffer[4]);
00249 }
00250 
00251 
00252 
00256 double makeAngleValid(double angle)
00257 {
00258         const double twoPi = (2.0 * PI);
00259         
00260         while (angle >= PI)
00261         {
00262                 angle -= twoPi;
00263         }
00264         while (angle < -PI)
00265         {
00266                 angle += twoPi;
00267         }
00268         
00269         return angle;
00270 }
00271 
00275 std::string toString(INT32 value)
00276 {
00277         char c[16];
00278         sprintf(c, "%i", value);
00279         return (std::string(c));
00280 }
00281 
00285 std::string toString(UINT32 value)
00286 {
00287         char c[16];
00288         sprintf(c, "%i", value);
00289         return (std::string(c));
00290 }
00291 
00292 #if INTPTR_MAX != INT32_MAX
00293 std::string toString(size_t value)
00294 {
00295         char c[16];
00296         sprintf(c, "%zu", value);
00297         return (std::string(c));
00298 }
00299 #endif
00300 
00301 /*
00302  * Konvertiere Zahl in formatierten String.
00303  * digits_before_decimal_point = 0..20
00304  * digits_after_decimal_point = 0..20
00305  *
00306  * Der Ergebnisstring ist formatiert gem. den Vorgaben, d.h. er hat
00307  * exakt die Laenge digits_before_decimal_point + digits_after_decimal_point + 1,
00308  * ausser, die Vorkommazahl passte nicht in die Laengenvorgabe
00309  * "digits_before_decimal_point", dann ist er entsprechend laenger.
00310  */
00311 std::string doubleToString(double val,
00312                                                    std::string::size_type digits_before_decimal_point,
00313                                                    std::string::size_type digits_after_decimal_point)
00314 {
00315         // Konvertierung in String
00316         std::string text = doubleToString(val, digits_after_decimal_point);
00317 
00318         // Laengen festlegen: Zuerst vor dem Dezimalpunkt
00319         const std::string::size_type dotPosition = text.find_first_of('.', 0);
00320         if (dotPosition != std::string::npos)
00321         {
00322                 // Punkt gefunden
00323                 if (dotPosition < digits_before_decimal_point)
00324                 {
00325                         // Zu kurz, also vorne auffuellen
00326                         std::string::size_type numExtraSpaces = digits_before_decimal_point - dotPosition;
00327                         text = std::string(numExtraSpaces, ' ') + text;
00328                 }
00329         }
00330 
00331         // Gesamtlaenge pruefen und ggf. verlaengern. NOTE: This will
00332         // never happen because the first doubleToString() will always fill
00333         // up with zero at the end, doesn't it?
00334         if (text.length() < (digits_before_decimal_point + digits_after_decimal_point + 1))
00335         {
00336                 // Spaces hinten anfuegen
00337                 std::string::size_type numExtraSpaces =
00338                         (digits_before_decimal_point + digits_after_decimal_point + 1) -  text.length();
00339                 text += std::string(numExtraSpaces, ' ');
00340         }
00341 
00342         return text;
00343 }
00344 
00348 std::string doubleToString(double val,
00349                                                    int digits_after_decimal_point)
00350 {
00351         // Konvertierung in String
00352         std::stringstream sstr;
00353         sstr << std::fixed << std::setprecision(digits_after_decimal_point) << val;
00354 
00355         return sstr.str();
00356 }
00357 
00358 std::string toString(double val, int digits_after_decimal_point)
00359 {
00360         return doubleToString(val, digits_after_decimal_point);
00361 }
00362 
00363 
00364 //
00365 // Konvertiere einen String in seine Adresse und seinen Port
00366 //
00367 // Beispiel: "192.168.0.1:1234" -> 0x0100A8C0
00368 //
00369 void stringToIpTarget(std::string ipAdrStr, UINT32& ipAddress, UINT16& port)
00370 {
00371         std::string addrStr;
00372         std::string portStr;
00373         
00374         if (ipAdrStr.length() < 3)
00375         {
00376                 // Ungueltig
00377                 return;
00378         }
00379         
00380         UINT32 adrVal = INADDR_NONE;
00381         UINT16 portVal = 0;
00382         
00383         // Port extrahieren
00384         size_t pos = ipAdrStr.find_first_of(':');
00385         if ((pos > 0) && (pos < (ipAdrStr.length() - 1)))
00386         {
00387                 addrStr = ipAdrStr.substr(0, pos);
00388                 portStr = ipAdrStr.substr(pos+1);
00389         }
00390         else
00391         {
00392                 addrStr = ipAdrStr;
00393         }
00394 
00395         // Adresse
00396         adrVal = (UINT32)inet_addr(addrStr.c_str());    //      inet_addr("127.0.0.1");
00397         ipAddress = adrVal;
00398         
00399         // Port
00400         if (portStr.length() > 0)
00401         {
00402                 portVal = fromString(portStr);
00403                 port = portVal;
00404         }
00405 }
00406 
00407 
00408 //
00409 // Konvertiere die IP-Adresse (IPv4) in einen String der Form a.b.c.d:port
00410 //
00411 std::string ipTargetToString(UINT32 ipAddress, UINT16 port)
00412 {
00413         std::string addr;
00414         addr = ipAdrToString(ipAddress);
00415         
00416         // Port
00417         addr += ":";
00418         addr += toString((UINT16)port);
00419         
00420         return addr;
00421 }
00422 
00423 
00424 //
00425 // Konvertiere die IP-Adresse (IPv4) in einen String der Form a.b.c.d.
00426 // OHNE PORT!
00427 //
00428 std::string ipAdrToString(UINT32 ipAddress)
00429 {
00430         std::string addr;
00431         addr =  toString((UINT16)((ipAddress >> 0 ) & 0xFF)) + "." +
00432                         toString((UINT16)((ipAddress >> 8 ) & 0xFF)) + "." +
00433                         toString((UINT16)((ipAddress >> 16) & 0xFF)) + "." +
00434                         toString((UINT16)((ipAddress >> 24) & 0xFF));
00435 
00436         return addr;
00437 }
00438 
00439 
00440 //
00441 // Read an UINT32 from a buffer. The buffer has the value in Big Endian format.
00442 //
00443 UINT32 memread_UINT32(BYTE*& buffer)
00444 {
00445         UINT32 value = (((UINT32)buffer[0]) << 24) +
00446                                         (((UINT32)buffer[1]) << 16) +
00447                                         (((UINT32)buffer[2]) << 8 ) +
00448                                         (((UINT32)buffer[3])      );
00449         buffer += 4;
00450         return value;
00451 }
00452 
00453 
00454 //
00455 // Read an UINT16 from a buffer. The buffer has the value in Big Endian format.
00456 //
00457 UINT16 memread_UINT16(BYTE*& buffer)
00458 {
00459         UINT16 value = (((UINT16)buffer[0]) << 8) +
00460                                         ((UINT16)buffer[1]);
00461         buffer += 2;
00462         return value;
00463 }
00464 
00465 //
00466 // Read an UINT8 from a buffer.
00467 //
00468 UINT8 memread_UINT8(BYTE*& buffer)
00469 {
00470         UINT8 value = buffer[0];
00471         buffer++;
00472         return value;
00473 }
00474 
00475 //
00476 // Read an INT16 from a buffer. The buffer has the value in Big Endian format.
00477 //
00478 INT16 memread_INT16(BYTE*& buffer)
00479 {
00480         UINT16 value = (((UINT16)buffer[0]) << 8) +
00481                                         ((UINT16)buffer[1]);
00482         buffer += 2;
00483         return (INT16)value;
00484 }
00485 
00486 //
00487 // Read an INT32 from a buffer. The buffer has the value in Big Endian format.
00488 //
00489 INT32 memread_INT32(BYTE*& buffer)
00490 {
00491         UINT32 value = (((UINT32)buffer[0]) << 24) +
00492                                         (((UINT32)buffer[1]) << 16) +
00493                                         (((UINT32)buffer[2]) << 8 ) +
00494                                         (((UINT32)buffer[3])      );
00495         buffer += 4;
00496         return (INT32)value;
00497 }
00498 
00499 //
00500 // Read a string (with fixed length) from a buffer.
00501 //
00502 std::string memread_string(BYTE*& buffer, UINT16 length)
00503 {
00504         std::string text;
00505         
00506         for (UINT16 i = 0; i<length; i++)
00507         {
00508                 text += buffer[i];
00509         }
00510         buffer += length;
00511         
00512         return text;
00513 }
00514 
00515 
00517 union FloatInt
00518 {
00519         float value_float;
00520         UINT32 value_int;
00521 };
00522 
00523 //
00524 // Read a float value from a buffer. The buffer has the value in Big Endian format, so it
00525 // cannot be read directly on x86 machines.
00526 //
00527 float memread_float(BYTE*& buffer)
00528 {
00529         FloatInt floatint;
00530         floatint.value_int = memread_UINT32(buffer);
00531         return floatint.value_float;
00532 }
00533 
00534 //
00535 // Write a float value to a buffer, and advance the buffer pointer.
00536 // After writing, the buffer has the value in Big Endian format.
00537 //
00538 void memwrite_float(BYTE*& buffer, float value)
00539 {
00540         FloatInt floatint;
00541         floatint.value_float = value;
00542         memwrite_UINT32(buffer, floatint.value_int);
00543 }
00544 
00545 //
00546 // Write an INT32 to a buffer, and advance the buffer pointer.
00547 // The buffer has the value in Big Endian format.
00548 //
00549 void memwrite_INT32(BYTE*& buffer, INT32 value)
00550 {
00551         memwrite_UINT32(buffer, (UINT32)value);
00552 }
00553 
00554 //
00555 // Write an UINT32 to a buffer, and advance the buffer pointer.
00556 // The buffer has the value in Big Endian format.
00557 //
00558 void memwrite_UINT32(BYTE*& buffer, UINT32 value)
00559 {
00560         buffer[0] = ((value >> 24) & 0xFF);
00561         buffer[1] = ((value >> 16) & 0xFF);
00562         buffer[2] = ((value >> 8 ) & 0xFF);
00563         buffer[3] = ((value      ) & 0xFF);
00564         buffer += 4;
00565 }
00566 
00567 //
00568 // Write an INT16 to a buffer, and advance the buffer pointer.
00569 // The buffer has the value in Big Endian format.
00570 //
00571 void memwrite_INT16(BYTE*& buffer, INT16 value)
00572 {
00573         memwrite_UINT16(buffer, (UINT16)value);
00574 }
00575 
00576 //
00577 // Write an UINT16 to a buffer, and advance the buffer pointer.
00578 // The buffer has the value in Big Endian format.
00579 //
00580 void memwrite_UINT16(BYTE*& buffer, UINT16 value)
00581 {
00582         buffer[0] = ((value >> 8 ) & 0xFF);
00583         buffer[1] = ((value      ) & 0xFF);
00584         buffer += 2;
00585 }
00586 
00587 //
00588 // Write an UINT8 to a buffer, and advance the buffer pointer.
00589 //
00590 void memwrite_UINT8(BYTE*& buffer, UINT8 value)
00591 {
00592         buffer[0] = value;
00593         buffer++;
00594 }
00595 
00596 //
00597 // Write an INT8 to a buffer, and advance the buffer pointer.
00598 //
00599 void memwrite_INT8(BYTE*& buffer, INT8 value)
00600 {
00601         buffer[0] = value;
00602         buffer++;
00603 }
00604 
00605 //
00606 // Write a string to a buffer, and advance the buffer pointer.
00607 //
00608 void memwrite_string(BYTE*& buffer, std::string text)
00609 {
00610         strncpy((char*)buffer, text.c_str(), text.length());
00611         buffer += text.length();
00612 }


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Tue Jul 9 2019 05:05:35