OpenDDS  Snapshot(2023/04/28-20:55)
SafetyProfileStreams.cpp
Go to the documentation of this file.
1 /*
2  * Distributed under the OpenDDS License.
3  * See: http://www.opendds.org/license.html
4  */
5 
6 #include <DCPS/DdsDcps_pch.h> // Only the _pch include should start with DCPS/
7 
8 #include "SafetyProfileStreams.h"
9 
10 #include "Definitions.h"
11 
13 
14 namespace OpenDDS {
15 namespace DCPS {
16 
17 String to_dds_string(unsigned short to_convert)
18 {
19  const char* fmt = "%hu";
20  const int buff_size = 5 + 1; // note +1 for null terminator
21  char buf[buff_size];
22  ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
23  return String(buf);
24 }
25 
26 String to_dds_string(int to_convert)
27 {
28  const char* fmt = "%d";
29  const int buff_size = 20 + 1; // note +1 for null terminator
30  char buf[buff_size];
31  ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
32  return String(buf);
33 }
34 
35 String to_dds_string(unsigned int to_convert, bool as_hex)
36 {
37  const char* fmt;
38  if (as_hex) {
39  fmt = "%02x";
40  const int buff_size = 3; // note +1 for null terminator
41  char buf[buff_size];
42  ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
43  return String(buf);
44  } else {
45  fmt = "%u";
46  const int buff_size = 20 + 1; // note +1 for null terminator
47  char buf[buff_size];
48  ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
49  return String(buf);
50  }
51 }
52 
53 String to_dds_string(long to_convert)
54 {
55  const char* fmt = "%ld";
56  const int buff_size = 20 + 1; // note +1 for null terminator
57  char buf[buff_size];
58  ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
59  return String(buf);
60 }
61 
62 String to_dds_string(long long to_convert)
63 {
64  const char* fmt = "%lld";
65  const int buff_size = 20 + 1; // note +1 for null terminator
66  char buf[buff_size];
67  ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
68  return String(buf);
69 }
70 
71 String to_dds_string(unsigned long long to_convert, bool as_hex)
72 {
73  const char* fmt;
74  if (as_hex) {
75  fmt = "%0llx";
76  } else {
77  fmt = "%llu";
78  }
79  const int buff_size = 20 + 1; // note +1 for null terminator
80  char buf[buff_size];
81  ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
82  return String(buf);
83 }
84 
85 String to_dds_string(unsigned long to_convert, bool as_hex)
86 {
87  const char* fmt;
88  if (as_hex) {
89  fmt = "%0.8lx";
90  } else {
91  fmt = "%lu";
92  }
93  const int buff_size = 20 + 1; // note +1 for null terminator
94  char buf[buff_size];
95  ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
96  return String(buf);
97 }
98 
99 String to_dds_string(const unsigned char* array, size_t length)
100 {
101  static const size_t bytes_per_elt = 5; // largest byte as decimal plus comma and space
102  String ret(length * bytes_per_elt, '\0');
103  char* buf = &ret[0];
104  size_t total_used = 0;
105  for (size_t i = 0; i < length; ++i) {
106  const int used = ACE_OS::snprintf(buf, bytes_per_elt + 1,
107  i < length - 1 ? "%d, " : "%d", array[i]);
108  if (used < 1) {
109  return "";
110  }
111  buf += used;
112  total_used += static_cast<size_t>(used);
113  }
114 
115  ret.resize(total_used);
116  return ret;
117 }
118 
120  const unsigned char* data, const size_t size, const char delim, const size_t delim_every)
121 {
122  return to_hex_dds_string(reinterpret_cast<const char*>(data), size, delim, delim_every);
123 }
124 
125 static inline char nibble_to_hex_char(char nibble)
126 {
127  nibble &= 0x0F;
128  return ((nibble < 0xA) ? '0' : ('a' - 0xA)) + nibble;
129 }
130 
132  const char* data, size_t size, const char delim, const size_t delim_every)
133 {
134  const bool valid_delim = delim && delim_every;
135  size_t l = size * 2;
136  if (valid_delim && size > 1) {
137  l += size / delim_every;
138  if (!(size % delim_every)) {
139  l--;
140  }
141  }
142 
143  String rv;
144  rv.reserve(l);
145  for (size_t i = 0; i < size; i++) {
146  if (valid_delim && i && !(i % delim_every)) {
147  rv.push_back(delim);
148  }
149  rv.push_back(nibble_to_hex_char(data[i] >> 4));
150  rv.push_back(nibble_to_hex_char(data[i]));
151  }
152  return rv;
153 }
154 
155 } // namespace DCPS
156 } // namespace OpenDDS
157 
std::string String
int snprintf(char *buf, size_t maxlen, const char *format,...) ACE_GCC_FORMAT_ATTRIBUTE(printf
String to_dds_string(unsigned short to_convert)
static char nibble_to_hex_char(char nibble)
String to_hex_dds_string(const unsigned char *data, const size_t size, const char delim, const size_t delim_every)
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28