ETSI STF525 / Internal Testing test suite This project provides an internal testing test suite and its associated Test Adapter/Codec

converter.hh
Go to the documentation of this file.
1
11 #pragma once
12
13 #include <iostream>
14 #include <iomanip>
15 #include <sstream>
16
17 #include <string>
18 #include <vector>
19 #include <algorithm>
20
21 #include <cstdint>
22 #include <cctype>
23 #include <climits> // LONG_MAX, LLONG_MAX
24 #include <ctime> // time_t, struct tm, difftime, time, mktime
25
31 class converter {
32
36 static converter * instance;
37
41 converter() {};
45 ~converter() {
46 if (instance != NULL) {
47 delete instance;
48 instance = NULL;
49 }
50 };
51
52 public:
56 inline static converter & get_instance() {
57 if (instance == NULL) instance = new converter();
58 return *instance;
59 };
60
61 public:
66 typedef enum {
67 big_endian,
68 little_endian
69 } endian_t;
70
71 public:
78 inline uint8_t bcd_to_bin(const uint8_t p_value) {
79 return ((p_value / 16 * 10) + (p_value % 16));
80 };
81
88 inline uint8_t bin_to_bcd(const uint8_t p_value) {
89 return ((p_value / 10 * 16) + (p_value % 10));
90 };
91
98 uint16_t swap(const uint16_t p_value);
99 inline int16_t swap(const int16_t p_value) {
100 return static_cast<short>(swap(static_cast<uint16_t>(p_value)));
101 };
107 uint32_t swap(const uint32_t p_value);
108 inline int32_t swap(const int32_t p_value) {
109 return static_cast<int>(swap(static_cast<uint32_t>(p_value)));
110 };
111
117 std::string string_to_hexa(const std::string & p_value);
123 std::string bytes_to_hexa(const std::vector<uint8_t> & p_value);
129 std::vector<uint8_t> hexa_to_bytes(const std::string & p_value);
130
142 std::string time_to_string(const time_t p_time);
149 std::string time_to_string(const struct tm & p_time);
150
157 inline std::vector<uint8_t> short_to_bytes(const int16_t p_value, const endian_t p_endianess = big_endian) const {
158 std::vector<uint8_t> result(sizeof(short), 0x00);
159 for (int i = sizeof(short) - 1; i >= 0; i--) {
160 int offset = (sizeof(short) - 1 - i) * 8;
161 result[i] = static_cast<uint8_t>((p_value >> offset) & 0xFF);
162 } // End of 'for' statement
163 return result;
164 }; // End of short_to_bytes
165
172 inline int16_t bytes_to_short(const std::vector<uint8_t> & p_value, const endian_t p_endianess = big_endian) const {
173 // Sanity check
174 if (p_value.size() > sizeof(short)) {
175 return SHRT_MAX;
176 }
177 int16_t value = 0;
178 for (size_t i = 0; i < p_value.size(); i++) {
179 value = (value << 8) + (p_value[i] & 0xff);
180 } // End of 'for' statement
181 return value;
182 }; // End of bytes_to_short
183
190 inline std::vector<uint8_t> int_to_bytes(const int32_t p_value, const endian_t p_endianess = big_endian) const {
191 /*uint8_t bytes[sizeof(p_value)];
192 std::copy(
193 static_cast<const uint8_t *>(static_cast<const void *>(&p_value)),
194 static_cast<const uint8_t *>(static_cast<const void *>(&p_value)) + sizeof(p_value),
195 bytes
196 );
197 std::vector<uint8_t> result(bytes, bytes + sizeof(bytes) / sizeof(uint8_t));*/
198 std::vector<uint8_t> result(sizeof(int), 0x00);
199 for (int i = sizeof(int) - 1; i >= 0; i--) {
200 int offset = (sizeof(int) - 1 - i) * 8;
201 result[i] = static_cast<uint8_t>((p_value >> offset) & 0xFF);
202 } // End of 'for' statement
203 return result;
204 }; // End of int_to_bytes
205
212 inline int32_t bytes_to_int(const std::vector<uint8_t> & p_value, const endian_t p_endianess = big_endian) const {
213 // Sanity check
214 if (p_value.size() > sizeof(int)) {
215 return INT_MAX;
216 }
217 int32_t value = 0;
218 for (size_t i = 0; i < p_value.size(); i++) {
219 value = (value << 8) + (p_value[i] & 0xff);
220 } // End of 'for' statement
221 return value;
222 // return *((int *)(&p_value[0]));
223 }; // End of bytes_to_int
224
231 inline std::vector<uint8_t> long_to_bytes(const int64_t p_value, const endian_t p_endianess = big_endian) const {
232 /*uint8_t bytes[sizeof(p_value)];
233 std::copy(
234 static_cast<const uint8_t *>(static_cast<const void *>(&p_value)),
235 static_cast<const uint8_t *>(static_cast<const void *>(&p_value)) + sizeof(p_value),
236 bytes
237 );
238 std::vector<uint8_t> result(bytes, bytes + sizeof(bytes) / sizeof(uint8_t));*/
239 std::vector<uint8_t> result(sizeof(int64_t), 0x00);
240 for (int i = sizeof(int64_t) - 1; i >= 0; i--) {
241 int offset = (sizeof(int64_t) - 1 - i) * 8;
242 result[i] = static_cast<uint8_t>((p_value >> offset) & 0xFF);
243 } // End of 'for' statement
244 return result;
245 }; // End of long_to_bytes
246
253 inline int64_t bytes_to_long(const std::vector<uint8_t> & p_value, const endian_t p_endianess = big_endian) const {
254 // Sanity check
255 if (p_value.size() > sizeof(int64_t)) {
256 return LLONG_MAX;
257 }
258 int64_t value = 0;
259 for (size_t i = 0; i < p_value.size(); i++) {
260 value = (value << 8) + (p_value[i] & 0xff);
261 } // End of 'for' statement
262 return value;
263 // return *((long *)(&p_value[0]));
264 }; // End of bytes_to_long
265
271 inline std::vector<uint8_t> float_to_bytes(const float p_value) const {
272 uint8_t bytes[sizeof(p_value)];
273 std::copy(
274 static_cast<const uint8_t *>(static_cast<const void *>(&p_value)),
275 static_cast<const uint8_t *>(static_cast<const void *>(&p_value)) + sizeof(p_value),
276 bytes
277 );
278 std::vector<uint8_t> result(bytes, bytes + sizeof(bytes) / sizeof(uint8_t));
279 return result;
280 }; // End of float_to_long
281
287 inline float bytes_to_float(const std::vector<uint8_t> & p_value) const {
288 return *((float *)(&p_value[0]));
289 }; // End of bytes_to_float
290
296 inline std::vector<uint8_t> string_to_bytes(const std::string & p_value) const {
297 return std::vector<uint8_t>(p_value.begin(), p_value.end());
298 }; // End of string_to_bytes
299
305 inline std::string bytes_to_string(const std::vector<uint8_t> & p_value) const {
306 return std::string(p_value.begin(), p_value.end());
307 }; // End of bytes_to_string
308
309 public:
315 inline int32_t string_to_int(const std::string & p_value) const {
316 return std::stoi(p_value);
317 //return atoi(p_value.c_str());
318 }; // End of string_to_int
319
325 inline std::string int_to_string(const int32_t & p_value) const {
326 std::ostringstream ss;
327 ss << p_value;
328 return ss.str();
329 }; // End of string_to_bytes
330
331 public:
332
339 std::string trim(const std::string& p_value, const std::string& p_trim_chars = " \t");
340
354 std::vector<std::string> split_arguments_line(const std::string & p_value);
355
356 }; // End of class converter
converter::bytes_to_int
int32_t bytes_to_int(const std::vector< uint8_t > &p_value, const endian_t p_endianess=big_endian) const
Convert a bytes array into a 32-bits integer (int32_t)
Definition: converter.hh:212
converter::bcd_to_bin
uint8_t bcd_to_bin(const uint8_t p_value)
Convert a Binary Coded Decimal value into a binary value.
Definition: converter.hh:78
converter::string_to_int
int32_t string_to_int(const std::string &p_value) const
Convert a string into an integer.
Definition: converter.hh:315
converter::swap
int16_t swap(const int16_t p_value)
Definition: converter.hh:99
converter::endian_t
endian_t
Endianess style.
Definition: converter.hh:66
converter::big_endian
Definition: converter.hh:67
converter::split_arguments_line
std::vector< std::string > split_arguments_line(const std::string &p_value)
Convert the provided string into a list of arguments.
Definition: converter.cc:115
converter::trim
std::string trim(const std::string &p_value, const std::string &p_trim_chars=" \)
Returns a copy of the string, with leading and trailing special characters omitted.
Definition: converter.cc:104
converter::bin_to_bcd
uint8_t bin_to_bcd(const uint8_t p_value)
Convert a binary value into a Binary Coded Decimal value.
Definition: converter.hh:88
converter::~converter
~converter()
Default private dtor.
Definition: converter.hh:45
converter::int_to_bytes
std::vector< uint8_t > int_to_bytes(const int32_t p_value, const endian_t p_endianess=big_endian) const
Convert a 32-bits integer (int32_t) into a bytes array.
Definition: converter.hh:190
converter::converter
converter()
Default private ctor.
Definition: converter.hh:41
converter::time_to_string
std::string time_to_string(const time_t p_time)
Convert a time in time_t format into a string formated according to RFC 822, 1036, 1123, 2822.
Definition: converter.cc:92
converter::hexa_to_bytes
std::vector< uint8_t > hexa_to_bytes(const std::string &p_value)
Convert an hexadecimal string into a bytes array.
Definition: converter.cc:54
converter::instance
static converter * instance
Unique static object reference of this class.
Definition: converter.hh:36
converter::string_to_bytes
std::vector< uint8_t > string_to_bytes(const std::string &p_value) const
Convert a string into a bytes array.
Definition: converter.hh:296
converter::bytes_to_string
std::string bytes_to_string(const std::vector< uint8_t > &p_value) const
Convert a bytes array into a string.
Definition: converter.hh:305
converter::little_endian
Definition: converter.hh:68
converter::bytes_to_short
int16_t bytes_to_short(const std::vector< uint8_t > &p_value, const endian_t p_endianess=big_endian) const
Convert a bytes array into a 16-bits integer (int16_t)
Definition: converter.hh:172
converter::float_to_bytes
std::vector< uint8_t > float_to_bytes(const float p_value) const
Convert a float value into a bytes array.
Definition: converter.hh:271
converter::bytes_to_long
int64_t bytes_to_long(const std::vector< uint8_t > &p_value, const endian_t p_endianess=big_endian) const
Convert a bytes array into a 64-bits integer (int64_t)
Definition: converter.hh:253
converter::bytes_to_float
float bytes_to_float(const std::vector< uint8_t > &p_value) const
Convert a bytes array into a float.
Definition: converter.hh:287
converter::get_instance
static converter & get_instance()
Public accessor to the single object reference.
Definition: converter.hh:56
converter::swap
int32_t swap(const int32_t p_value)
Definition: converter.hh:108
converter::swap
uint16_t swap(const uint16_t p_value)
Swap two bytes length value (e.g. 0xCAFE becomes 0xFECA)
Definition: converter.cc:5
converter::bytes_to_hexa
std::string bytes_to_hexa(const std::vector< uint8_t > &p_value)
Convert a bytes array int32_t an hexadecimal string.
Definition: converter.cc:39
converter::long_to_bytes
std::vector< uint8_t > long_to_bytes(const int64_t p_value, const endian_t p_endianess=big_endian) const
Convert a 64-bits integer (int64_t) into a bytes array.
Definition: converter.hh:231
converter
This class provide a set of methods for types conversions.
Definition: converter.hh:31
converter::int_to_string
std::string int_to_string(const int32_t &p_value) const
Convert an integer into a string.
Definition: converter.hh:325
converter::short_to_bytes
std::vector< uint8_t > short_to_bytes(const int16_t p_value, const endian_t p_endianess=big_endian) const
Convert a 16-bits integer (int16_t) into a bytes array.
Definition: converter.hh:157
converter::string_to_hexa
std::string string_to_hexa(const std::string &p_value)
Convert a string into an hexadecimal string.
Definition: converter.cc:15