toolbox.cpp
Go to the documentation of this file.
1 
5 #include <stdio.h> /* for printf() and fprintf() */
6 #include <sys/socket.h> /* for socket(), bind(), and connect() */
7 #include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
8 #include <stdlib.h> /* for atoi() and exit() */
9 #include <string.h> /* for memset() */
11 #include <iostream>
12 #include <iomanip> // for std::setprecision
13 #include <sstream> // for std::stringstream
15 
16 #ifdef _MSC_VER
17 #pragma warning(disable: 4267)
18 #pragma warning(disable: 4996)
19 #endif
20 //
21 // Write a binary trace output, e.g. of buffer contents.
22 // Can be used for debugging.
23 //
24 void traceBuffer(std::string headerText, BYTE* buffer, UINT32 len)
25 {
26  // Table header
27  printInfoMessage(headerText, true);
28 
29  // Length
30  std::string line;
31  line = "Length= " + toString(len) + " bytes.";
32  printInfoMessage(line, true);
33 
34  // Contents
35  UINT32 pos = 0;
36  while (pos < len)
37  {
38  line = toHexString(pos) + ": ";
39  for (UINT16 i=0; i< 16; i++)
40  {
41  line += toHexString(buffer[pos]) + " ";
42  pos++;
43  if (pos >= len)
44  {
45  break;
46  }
47  }
48  printInfoMessage(line, true);
49  }
50 }
51 
52 //
53 // String conversion: Values to a hex string.
54 // 0..16 --> 0..F
55 //
56 std::string toHexStringNibble(UINT8 val)
57 {
58  std::string s = "0123456789ABCDEF";
59  std::string c;
60  if (val < 16)
61  {
62  c = s.substr(val, 1);
63  }
64  else
65  {
66  c = "x";
67  }
68  return c;
69 }
70 
71 //
72 // UINT32-value to Hex-String
73 // Result: "xxxxxxxx"
74 //
75 std::string toHexString(UINT32 val)
76 {
77  std::string s = toHexString((UINT16)(val >> 16));
78  s += toHexString((UINT16)(val & 0xFFFF));
79  return s;
80 }
81 
82 // Ergebnis: "xxxx"
83 std::string toHexString(UINT16 val)
84 {
85  std::string s = toHexStringNibble((UINT8)(val >> 12));
86  s += toHexStringNibble((UINT8)((val >> 8) & 0xF));
87  s += toHexStringNibble((UINT8)((val >> 4) & 0xF));
88  s += toHexStringNibble((UINT8)(val & 0xF));
89  return s;
90 }
91 
92 // Ergebnis: "xx"
93 std::string toHexString(UINT8 val)
94 {
95  std::string s1 = toHexStringNibble((UINT8)(val >> 4));
96  std::string s2 = toHexStringNibble((UINT8)(val & 0x0F));
97  std::string s = s1 + s2;
98  return s;
99 }
100 
101 
102 //
103 // Konvertiert einen String in Kleinbuchstaben
104 //
105 std::string toLower(const std::string& text)
106 {
107  std::string low;
108  UINT32 i;
109  unsigned char c;
110  for (i=0; i < text.length(); i++)
111  {
112  c = text.at(i);
113  if ((c >= 'A') || (c <= 'Z'))
114  {
115  // Grossbuchstabe umwandeln
116  c += ('a' - 'A');
117  }
118  low += c;
119  }
120 
121  return low;
122 }
123 
124 
125 //
126 // Konvertiere eine Angabe in [m] in Fuesse und Inches, als Text.
127 // Ausgabeformat ist <<ff' ii">>
128 //
129 std::string convertMeterToFeetAndInch(double m)
130 {
131  std::ostringstream os;
132  std::string text;
133 
134  // Vorzeichen verarbeiten
135  if (m < 0.0)
136  {
137  os << "-";
138  m = -m;
139  }
140 
141  INT32 feet = (INT32)(m / 0.3048);
142  INT32 inch = (INT32)((m - ((double)feet * 0.3048)) / 0.0254);
143  if (feet > 0)
144  {
145  os << feet << "'";
146  }
147  if ((inch > 0) || (feet == 0))
148  {
149  os << inch << "\"";
150  }
151  text = os.str();
152 
153  // Ausgabe
154  return text;
155 }
156 
157 
158 
159 //
160 // String --> UINT16
161 //
162 UINT16 fromString(const std::string& text)
163 {
164  int value;
165  int conversions = sscanf(text.c_str(), "%d", &value);
166  if (conversions == 1)
167  {
168  return (UINT16)value;
169  }
170 
171  return 0;
172 }
173 
177 int hexCharToValue(char c)
178 {
179  int value = 0;
180 
181  if ((c >= '0') && (c <= '9'))
182  {
183  value = c - '0';
184  }
185  else if ((c >= 'A') && (c <= 'F'))
186  {
187  value = c - 'A' + 10;
188  }
189  else if ((c >= 'a') && (c <= 'f'))
190  {
191  value = c - 'a' + 10;
192  }
193 
194  return value;
195 }
196 
197 
201 char convertNibbleToHexChar(int value, bool useLowerCaseLetters)
202 {
203  char c;
204 
205  if (value < 10)
206  {
207  c = '0' + value;
208  }
209  else
210  {
211  if (useLowerCaseLetters == false)
212  {
213  // Grossbuchstaben
214  c = 'A' + value - 10;
215  }
216  else
217  {
218  // Kleinbuchstaben
219  c = 'a' + value - 10;
220  }
221  }
222 
223  return c;
224 }
225 
231 void convertUINT8toHexString(UINT8 byte, char* buffer)
232 {
233  UINT8 value = (byte >> 4);
234  buffer[0] = convertNibbleToHexChar(value);
235  value = byte & 0x0F;
236  buffer[1] = convertNibbleToHexChar(value);
237 }
238 
244 void convertRGBtoHexString(UINT8 r, UINT8 g, UINT8 b, char* buffer)
245 {
246  convertUINT8toHexString(r, buffer);
247  convertUINT8toHexString(g, &buffer[2]);
248  convertUINT8toHexString(b, &buffer[4]);
249 }
250 
251 
252 
256 double makeAngleValid(double angle)
257 {
258  const double twoPi = (2.0 * PI);
259 
260  while (angle >= PI)
261  {
262  angle -= twoPi;
263  }
264  while (angle < -PI)
265  {
266  angle += twoPi;
267  }
268 
269  return angle;
270 }
271 
275 std::string toString(INT32 value)
276 {
277  char c[16];
278  sprintf(c, "%i", value);
279  return (std::string(c));
280 }
281 
285 std::string toString(UINT32 value)
286 {
287  char c[16];
288  sprintf(c, "%i", value);
289  return (std::string(c));
290 }
291 
292 #if INTPTR_MAX != INT32_MAX
293 std::string toString(size_t value)
294 {
295  char c[16];
296  sprintf(c, "%zu", value);
297  return (std::string(c));
298 }
299 #endif
300 
301 /*
302  * Konvertiere Zahl in formatierten String.
303  * digits_before_decimal_point = 0..20
304  * digits_after_decimal_point = 0..20
305  *
306  * Der Ergebnisstring ist formatiert gem. den Vorgaben, d.h. er hat
307  * exakt die Laenge digits_before_decimal_point + digits_after_decimal_point + 1,
308  * ausser, die Vorkommazahl passte nicht in die Laengenvorgabe
309  * "digits_before_decimal_point", dann ist er entsprechend laenger.
310  */
311 std::string doubleToString(double val,
312  std::string::size_type digits_before_decimal_point,
313  std::string::size_type digits_after_decimal_point)
314 {
315  // Konvertierung in String
316  std::string text = doubleToString(val, digits_after_decimal_point);
317 
318  // Laengen festlegen: Zuerst vor dem Dezimalpunkt
319  const std::string::size_type dotPosition = text.find_first_of('.', 0);
320  if (dotPosition != std::string::npos)
321  {
322  // Punkt gefunden
323  if (dotPosition < digits_before_decimal_point)
324  {
325  // Zu kurz, also vorne auffuellen
326  std::string::size_type numExtraSpaces = digits_before_decimal_point - dotPosition;
327  text = std::string(numExtraSpaces, ' ') + text;
328  }
329  }
330 
331  // Gesamtlaenge pruefen und ggf. verlaengern. NOTE: This will
332  // never happen because the first doubleToString() will always fill
333  // up with zero at the end, doesn't it?
334  if (text.length() < (digits_before_decimal_point + digits_after_decimal_point + 1))
335  {
336  // Spaces hinten anfuegen
337  std::string::size_type numExtraSpaces =
338  (digits_before_decimal_point + digits_after_decimal_point + 1) - text.length();
339  text += std::string(numExtraSpaces, ' ');
340  }
341 
342  return text;
343 }
344 
348 std::string doubleToString(double val,
349  int digits_after_decimal_point)
350 {
351  // Konvertierung in String
352  std::stringstream sstr;
353  sstr << std::fixed << std::setprecision(digits_after_decimal_point) << val;
354 
355  return sstr.str();
356 }
357 
358 std::string toString(double val, int digits_after_decimal_point)
359 {
360  return doubleToString(val, digits_after_decimal_point);
361 }
362 
363 
364 //
365 // Konvertiere einen String in seine Adresse und seinen Port
366 //
367 // Beispiel: "192.168.0.1:1234" -> 0x0100A8C0
368 //
369 void stringToIpTarget(std::string ipAdrStr, UINT32& ipAddress, UINT16& port)
370 {
371  std::string addrStr;
372  std::string portStr;
373 
374  if (ipAdrStr.length() < 3)
375  {
376  // Ungueltig
377  return;
378  }
379 
380  UINT32 adrVal = INADDR_NONE;
381  UINT16 portVal = 0;
382 
383  // Port extrahieren
384  size_t pos = ipAdrStr.find_first_of(':');
385  if ((pos > 0) && (pos < (ipAdrStr.length() - 1)))
386  {
387  addrStr = ipAdrStr.substr(0, pos);
388  portStr = ipAdrStr.substr(pos+1);
389  }
390  else
391  {
392  addrStr = ipAdrStr;
393  }
394 
395  // Adresse
396  adrVal = (UINT32)inet_addr(addrStr.c_str()); // inet_addr("127.0.0.1");
397  ipAddress = adrVal;
398 
399  // Port
400  if (portStr.length() > 0)
401  {
402  portVal = fromString(portStr);
403  port = portVal;
404  }
405 }
406 
407 
408 //
409 // Konvertiere die IP-Adresse (IPv4) in einen String der Form a.b.c.d:port
410 //
411 std::string ipTargetToString(UINT32 ipAddress, UINT16 port)
412 {
413  std::string addr;
414  addr = ipAdrToString(ipAddress);
415 
416  // Port
417  addr += ":";
418  addr += toString((UINT16)port);
419 
420  return addr;
421 }
422 
423 
424 //
425 // Konvertiere die IP-Adresse (IPv4) in einen String der Form a.b.c.d.
426 // OHNE PORT!
427 //
428 std::string ipAdrToString(UINT32 ipAddress)
429 {
430  std::string addr;
431  addr = toString((UINT16)((ipAddress >> 0 ) & 0xFF)) + "." +
432  toString((UINT16)((ipAddress >> 8 ) & 0xFF)) + "." +
433  toString((UINT16)((ipAddress >> 16) & 0xFF)) + "." +
434  toString((UINT16)((ipAddress >> 24) & 0xFF));
435 
436  return addr;
437 }
438 
439 
440 //
441 // Read an UINT32 from a buffer. The buffer has the value in Big Endian format.
442 //
444 {
445  UINT32 value = (((UINT32)buffer[0]) << 24) +
446  (((UINT32)buffer[1]) << 16) +
447  (((UINT32)buffer[2]) << 8 ) +
448  (((UINT32)buffer[3]) );
449  buffer += 4;
450  return value;
451 }
452 
453 
454 //
455 // Read an UINT16 from a buffer. The buffer has the value in Big Endian format.
456 //
458 {
459  UINT16 value = (((UINT16)buffer[0]) << 8) +
460  ((UINT16)buffer[1]);
461  buffer += 2;
462  return value;
463 }
464 
465 //
466 // Read an UINT8 from a buffer.
467 //
469 {
470  UINT8 value = buffer[0];
471  buffer++;
472  return value;
473 }
474 
475 //
476 // Read an INT16 from a buffer. The buffer has the value in Big Endian format.
477 //
479 {
480  UINT16 value = (((UINT16)buffer[0]) << 8) +
481  ((UINT16)buffer[1]);
482  buffer += 2;
483  return (INT16)value;
484 }
485 
486 //
487 // Read an INT32 from a buffer. The buffer has the value in Big Endian format.
488 //
490 {
491  UINT32 value = (((UINT32)buffer[0]) << 24) +
492  (((UINT32)buffer[1]) << 16) +
493  (((UINT32)buffer[2]) << 8 ) +
494  (((UINT32)buffer[3]) );
495  buffer += 4;
496  return (INT32)value;
497 }
498 
499 //
500 // Read a string (with fixed length) from a buffer.
501 //
502 std::string memread_string(BYTE*& buffer, UINT16 length)
503 {
504  std::string text;
505 
506  for (UINT16 i = 0; i<length; i++)
507  {
508  text += buffer[i];
509  }
510  buffer += length;
511 
512  return text;
513 }
514 
515 
517 union FloatInt
518 {
519  float value_float;
521 };
522 
523 //
524 // Read a float value from a buffer. The buffer has the value in Big Endian format, so it
525 // cannot be read directly on x86 machines.
526 //
527 float memread_float(BYTE*& buffer)
528 {
529  FloatInt floatint;
530  floatint.value_int = memread_UINT32(buffer);
531  return floatint.value_float;
532 }
533 
534 //
535 // Write a float value to a buffer, and advance the buffer pointer.
536 // After writing, the buffer has the value in Big Endian format.
537 //
538 void memwrite_float(BYTE*& buffer, float value)
539 {
540  FloatInt floatint;
541  floatint.value_float = value;
542  memwrite_UINT32(buffer, floatint.value_int);
543 }
544 
545 //
546 // Write an INT32 to a buffer, and advance the buffer pointer.
547 // The buffer has the value in Big Endian format.
548 //
549 void memwrite_INT32(BYTE*& buffer, INT32 value)
550 {
551  memwrite_UINT32(buffer, (UINT32)value);
552 }
553 
554 //
555 // Write an UINT32 to a buffer, and advance the buffer pointer.
556 // The buffer has the value in Big Endian format.
557 //
558 void memwrite_UINT32(BYTE*& buffer, UINT32 value)
559 {
560  buffer[0] = ((value >> 24) & 0xFF);
561  buffer[1] = ((value >> 16) & 0xFF);
562  buffer[2] = ((value >> 8 ) & 0xFF);
563  buffer[3] = ((value ) & 0xFF);
564  buffer += 4;
565 }
566 
567 //
568 // Write an INT16 to a buffer, and advance the buffer pointer.
569 // The buffer has the value in Big Endian format.
570 //
571 void memwrite_INT16(BYTE*& buffer, INT16 value)
572 {
573  memwrite_UINT16(buffer, (UINT16)value);
574 }
575 
576 //
577 // Write an UINT16 to a buffer, and advance the buffer pointer.
578 // The buffer has the value in Big Endian format.
579 //
580 void memwrite_UINT16(BYTE*& buffer, UINT16 value)
581 {
582  buffer[0] = ((value >> 8 ) & 0xFF);
583  buffer[1] = ((value ) & 0xFF);
584  buffer += 2;
585 }
586 
587 //
588 // Write an UINT8 to a buffer, and advance the buffer pointer.
589 //
590 void memwrite_UINT8(BYTE*& buffer, UINT8 value)
591 {
592  buffer[0] = value;
593  buffer++;
594 }
595 
596 //
597 // Write an INT8 to a buffer, and advance the buffer pointer.
598 //
599 void memwrite_INT8(BYTE*& buffer, INT8 value)
600 {
601  buffer[0] = value;
602  buffer++;
603 }
604 
605 //
606 // Write a string to a buffer, and advance the buffer pointer.
607 //
608 void memwrite_string(BYTE*& buffer, std::string text)
609 {
610  strncpy((char*)buffer, text.c_str(), text.length());
611  buffer += text.length();
612 }
std::string convertMeterToFeetAndInch(double m)
Definition: toolbox.cpp:129
std::string toHexStringNibble(UINT8 val)
Definition: toolbox.cpp:56
std::string toHexString(UINT32 val)
Definition: toolbox.cpp:75
unsigned char BYTE
uint16_t UINT16
void memwrite_INT16(BYTE *&buffer, INT16 value)
Definition: toolbox.cpp:571
void traceBuffer(std::string headerText, BYTE *buffer, UINT32 len)
Definition: toolbox.cpp:24
void memwrite_UINT32(BYTE *&buffer, UINT32 value)
Definition: toolbox.cpp:558
Internal: Structure for reading/writing a float as an UINT32.
Definition: toolbox.cpp:517
std::string doubleToString(double val, std::string::size_type digits_before_decimal_point, std::string::size_type digits_after_decimal_point)
Definition: toolbox.cpp:311
#define printInfoMessage(a, b)
uint32_t UINT32
void convertUINT8toHexString(UINT8 byte, char *buffer)
Definition: toolbox.cpp:231
std::string ipTargetToString(UINT32 ipAddress, UINT16 port)
Definition: toolbox.cpp:411
int hexCharToValue(char c)
Definition: toolbox.cpp:177
float value_float
Definition: toolbox.cpp:519
void memwrite_float(BYTE *&buffer, float value)
Definition: toolbox.cpp:538
float memread_float(BYTE *&buffer)
Definition: toolbox.cpp:527
void memwrite_INT8(BYTE *&buffer, INT8 value)
Definition: toolbox.cpp:599
UINT8 memread_UINT8(BYTE *&buffer)
Definition: toolbox.cpp:468
std::string toLower(const std::string &text)
Definition: toolbox.cpp:105
double makeAngleValid(double angle)
Definition: toolbox.cpp:256
std::string ipAdrToString(UINT32 ipAddress)
Definition: toolbox.cpp:428
INT32 memread_INT32(BYTE *&buffer)
Definition: toolbox.cpp:489
void memwrite_string(BYTE *&buffer, std::string text)
Definition: toolbox.cpp:608
int32_t INT32
void memwrite_UINT16(BYTE *&buffer, UINT16 value)
Definition: toolbox.cpp:580
UINT16 fromString(const std::string &text)
Definition: toolbox.cpp:162
void convertRGBtoHexString(UINT8 r, UINT8 g, UINT8 b, char *buffer)
Definition: toolbox.cpp:244
std::string toString(INT32 value)
Definition: toolbox.cpp:275
void memwrite_UINT8(BYTE *&buffer, UINT8 value)
Definition: toolbox.cpp:590
int8_t INT8
UINT32 memread_UINT32(BYTE *&buffer)
Definition: toolbox.cpp:443
int16_t INT16
TIXML_STRING value
Definition: tinyxml.h:761
void stringToIpTarget(std::string ipAdrStr, UINT32 &ipAddress, UINT16 &port)
Definition: toolbox.cpp:369
UINT16 memread_UINT16(BYTE *&buffer)
Definition: toolbox.cpp:457
UINT32 value_int
Definition: toolbox.cpp:520
static sick_scan::SickScanCommonTcp * s
#define PI
void memwrite_INT32(BYTE *&buffer, INT32 value)
Definition: toolbox.cpp:549
std::string memread_string(BYTE *&buffer, UINT16 length)
Definition: toolbox.cpp:502
uint8_t UINT8
INT16 memread_INT16(BYTE *&buffer)
Definition: toolbox.cpp:478
char convertNibbleToHexChar(int value, bool useLowerCaseLetters)
Definition: toolbox.cpp:201


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