Line data Source code
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 :
12 : OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
13 :
14 : namespace OpenDDS {
15 : namespace DCPS {
16 :
17 0 : String to_dds_string(unsigned short to_convert)
18 : {
19 0 : const char* fmt = "%hu";
20 0 : const int buff_size = 5 + 1; // note +1 for null terminator
21 : char buf[buff_size];
22 0 : ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
23 0 : return String(buf);
24 : }
25 :
26 0 : String to_dds_string(int to_convert)
27 : {
28 0 : const char* fmt = "%d";
29 0 : const int buff_size = 20 + 1; // note +1 for null terminator
30 : char buf[buff_size];
31 0 : ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
32 0 : return String(buf);
33 : }
34 :
35 0 : String to_dds_string(unsigned int to_convert, bool as_hex)
36 : {
37 : const char* fmt;
38 0 : if (as_hex) {
39 0 : fmt = "%02x";
40 0 : const int buff_size = 3; // note +1 for null terminator
41 : char buf[buff_size];
42 0 : ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
43 0 : return String(buf);
44 : } else {
45 0 : fmt = "%u";
46 0 : const int buff_size = 20 + 1; // note +1 for null terminator
47 : char buf[buff_size];
48 0 : ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
49 0 : return String(buf);
50 : }
51 : }
52 :
53 64 : String to_dds_string(long to_convert)
54 : {
55 64 : const char* fmt = "%ld";
56 64 : const int buff_size = 20 + 1; // note +1 for null terminator
57 : char buf[buff_size];
58 64 : ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
59 64 : return String(buf);
60 : }
61 :
62 0 : String to_dds_string(long long to_convert)
63 : {
64 0 : const char* fmt = "%lld";
65 0 : const int buff_size = 20 + 1; // note +1 for null terminator
66 : char buf[buff_size];
67 0 : ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
68 0 : return String(buf);
69 : }
70 :
71 0 : String to_dds_string(unsigned long long to_convert, bool as_hex)
72 : {
73 : const char* fmt;
74 0 : if (as_hex) {
75 0 : fmt = "%0llx";
76 : } else {
77 0 : fmt = "%llu";
78 : }
79 0 : const int buff_size = 20 + 1; // note +1 for null terminator
80 : char buf[buff_size];
81 0 : ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
82 0 : return String(buf);
83 : }
84 :
85 0 : String to_dds_string(unsigned long to_convert, bool as_hex)
86 : {
87 : const char* fmt;
88 0 : if (as_hex) {
89 0 : fmt = "%0.8lx";
90 : } else {
91 0 : fmt = "%lu";
92 : }
93 0 : const int buff_size = 20 + 1; // note +1 for null terminator
94 : char buf[buff_size];
95 0 : ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
96 0 : return String(buf);
97 : }
98 :
99 0 : 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 0 : String ret(length * bytes_per_elt, '\0');
103 0 : char* buf = &ret[0];
104 0 : size_t total_used = 0;
105 0 : for (size_t i = 0; i < length; ++i) {
106 0 : const int used = ACE_OS::snprintf(buf, bytes_per_elt + 1,
107 0 : i < length - 1 ? "%d, " : "%d", array[i]);
108 0 : if (used < 1) {
109 0 : return "";
110 : }
111 0 : buf += used;
112 0 : total_used += static_cast<size_t>(used);
113 : }
114 :
115 0 : ret.resize(total_used);
116 0 : return ret;
117 0 : }
118 :
119 0 : String to_hex_dds_string(
120 : const unsigned char* data, const size_t size, const char delim, const size_t delim_every)
121 : {
122 0 : return to_hex_dds_string(reinterpret_cast<const char*>(data), size, delim, delim_every);
123 : }
124 :
125 0 : static inline char nibble_to_hex_char(char nibble)
126 : {
127 0 : nibble &= 0x0F;
128 0 : return ((nibble < 0xA) ? '0' : ('a' - 0xA)) + nibble;
129 : }
130 :
131 0 : String to_hex_dds_string(
132 : const char* data, size_t size, const char delim, const size_t delim_every)
133 : {
134 0 : const bool valid_delim = delim && delim_every;
135 0 : size_t l = size * 2;
136 0 : if (valid_delim && size > 1) {
137 0 : l += size / delim_every;
138 0 : if (!(size % delim_every)) {
139 0 : l--;
140 : }
141 : }
142 :
143 0 : String rv;
144 0 : rv.reserve(l);
145 0 : for (size_t i = 0; i < size; i++) {
146 0 : if (valid_delim && i && !(i % delim_every)) {
147 0 : rv.push_back(delim);
148 : }
149 0 : rv.push_back(nibble_to_hex_char(data[i] >> 4));
150 0 : rv.push_back(nibble_to_hex_char(data[i]));
151 : }
152 0 : return rv;
153 0 : }
154 :
155 : } // namespace DCPS
156 : } // namespace OpenDDS
157 :
158 : OPENDDS_END_VERSIONED_NAMESPACE_DECL
|