Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*!
* \file converter.hh
* \brief Helper class for types converter.
* \author ETSI STF525
* \copyright ETSI Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#pragma once
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdint>
#include <cctype>
#include <climits> // LONG_MAX, LLONG_MAX
#include <ctime> // time_t, struct tm, difftime, time, mktime
/*!
* \class converter
* \brief This class provide a set of methods for types conversions
* \remark Singleton pattern
*/
class converter {
/*!
* \brief Unique static object reference of this class
*/
static converter * instance;
/*!
* \brief Default private ctor
*/
converter() {};
/*!
* \brief Default private dtor
*/
~converter() {
if (instance != NULL) {
delete instance;
instance = NULL;
}
};
public: /*! \publicsection */
/*!
* \brief Public accessor to the single object reference
*/
inline static converter & get_instance() {
if (instance == NULL) instance = new converter();
return *instance;
};
public:
/*!
* \enum endian_t
* \brief Endianess style
*/
typedef enum {
big_endian,
little_endian
} endian_t;
public:
/*!
* \brief Convert a Binary Coded Decimal value into a binary value
* \param[in] p_value The BDC value
* \return The binary value
* \inline
*/
inline uint8_t bcd_to_bin(const uint8_t p_value) {
return ((p_value / 16 * 10) + (p_value % 16));
};
/*!
* \brief Convert a binary value into a Binary Coded Decimal value
* \param[in] p_value The binary value
* \return The BCD value
* \inline
*/
inline uint8_t bin_to_bcd(const uint8_t p_value) {
return ((p_value / 10 * 16) + (p_value % 10));
};
/*!
* \brief Swap two bytes length value (e.g. 0xCAFE becomes 0xFECA)
* \param[in] p_value The value to swap
* \return The swapped value
* \inline
*/
uint16_t swap(const uint16_t p_value);
inline int16_t swap(const int16_t p_value) {
return static_cast<short>(swap(static_cast<uint16_t>(p_value)));
};
/*!
* \brief Swap four bytes length value (used for littel endian / big endian)
* \param[in] p_value The value to swap
* \return The swapped value
*/
uint32_t swap(const uint32_t p_value);
inline int32_t swap(const int32_t p_value) {
return static_cast<int>(swap(static_cast<uint32_t>(p_value)));
};
/*!
* \brief Convert a string into an hexadecimal string
* \param[in] p_value The string value
* \return The hexadecimal value
*/
std::string string_to_hexa(const std::string & p_value);
/*!
* \brief Convert a bytes array int32_t an hexadecimal string
* \param[in] p_value The bytes array value
* \return The hexadecimal value
*/
std::string bytes_to_hexa(const std::vector<uint8_t> & p_value);
/*!
* \brief Convert an hexadecimal string into a bytes array
* \param[in] p_value The hexadecimal value
* \return The bytes array value
*/
std::vector<uint8_t> hexa_to_bytes(const std::string & p_value);
/*!
* \brief Convert a time in time_t format into a string formated according to RFC 822, 1036, 1123, 2822
* \param[in] p_time The time to convert in time_t format
* \return The time string formated
* \see http://www.unixtimestamp.com/
* @code
* std::string result = time_to_string(1489755780);
* result.compare("Fri, 17 Mar 2017 13:03:00 +0000") == 0 // When time zone is set to UTC
* @endcode
* \remark Use commands 1) timedatectl to change your machine timezone (e.g. sudo timedatectl set-timezone UTC to change machine timezone to UTC, 2) timedatectl list-timezones to get the list of the timezones)
*/
std::string time_to_string(const time_t p_time);
/*!
* \brief Convert a time in struct tm format into a string formated according to RFC 822, 1036, 1123, 2822
* \param[in] p_time The time to convert in struct tm format
* \return The time string formated
* \see http://www.unixtimestamp.com/
*/
std::string time_to_string(const struct tm & p_time);
/*!
* \brief Convert a 16-bits integer (int16_t) into a bytes array
* \param[in] p_value The 16-bits integer value
* \param[in] p_endianess Endianess style. Default: big_endian
* \return The bytes array value
*/
inline std::vector<uint8_t> short_to_bytes(const int16_t p_value, const endian_t p_endianess = big_endian) const {
std::vector<uint8_t> result(sizeof(short), 0x00);
for (int i = sizeof(short) - 1; i >= 0; i--) {
int offset = (sizeof(short) - 1 - i) * 8;
result[i] = static_cast<uint8_t>((p_value >> offset) & 0xFF);
} // End of 'for' statement
return result;
}; // End of short_to_bytes
/*!
* \brief Convert a bytes array into a 16-bits integer (int16_t)
* \param[in] p_value The bytes array
* \param[in] p_endianess Endianess style. Default: big_endian
* \return The 16-bits integer on success, SHRT_MAX on error (wrong bytes array size)
*/
inline int16_t bytes_to_short(const std::vector<uint8_t> & p_value, const endian_t p_endianess = big_endian) const {
// Sanity check
if (p_value.size() > sizeof(short)) {
return SHRT_MAX;
}
int16_t value = 0;
for (size_t i = 0; i < p_value.size(); i++) {
value = (value << 8) + (p_value[i] & 0xff);
} // End of 'for' statement
return value;
}; // End of bytes_to_short
/*!
* \brief Convert a 32-bits integer (int32_t) into a bytes array
* \param[in] p_value The 32-bits integer value
* \param[in] p_endianess Endianess style. Default: big_endian
* \return The bytes array value
*/
inline std::vector<uint8_t> int_to_bytes(const int32_t p_value, const endian_t p_endianess = big_endian) const {
/*uint8_t bytes[sizeof(p_value)];
std::copy(
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)),
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)) + sizeof(p_value),
bytes
);
std::vector<uint8_t> result(bytes, bytes + sizeof(bytes) / sizeof(uint8_t));*/
std::vector<uint8_t> result(sizeof(int), 0x00);
for (int i = sizeof(int) - 1; i >= 0; i--) {
int offset = (sizeof(int) - 1 - i) * 8;
result[i] = static_cast<uint8_t>((p_value >> offset) & 0xFF);
} // End of 'for' statement
return result;
}; // End of int_to_bytes
/*!
* \brief Convert a bytes array into a 32-bits integer (int32_t)
* \param[in] p_value The bytes array
* \param[in] p_endianess Endianess style. Default: big_endian
* \return The 32-bits integer on success, LONG_MAX on error (wrong bytes array size)
*/
inline int32_t bytes_to_int(const std::vector<uint8_t> & p_value, const endian_t p_endianess = big_endian) const {
// Sanity check
if (p_value.size() > sizeof(int)) {
return INT_MAX;
}
int32_t value = 0;
for (size_t i = 0; i < p_value.size(); i++) {
value = (value << 8) + (p_value[i] & 0xff);
} // End of 'for' statement
return value;
// return *((int *)(&p_value[0]));
}; // End of bytes_to_int
/*!
* \brief Convert a 64-bits integer (int64_t) into a bytes array
* \param[in] p_value The 64-bits integer value
* \param[in] p_endianess Endianess style. Default: big_endian
* \return The bytes array value
*/
inline std::vector<uint8_t> long_to_bytes(const int64_t p_value, const endian_t p_endianess = big_endian) const {
/*uint8_t bytes[sizeof(p_value)];
std::copy(
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)),
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)) + sizeof(p_value),
bytes
);
std::vector<uint8_t> result(bytes, bytes + sizeof(bytes) / sizeof(uint8_t));*/
std::vector<uint8_t> result(sizeof(int64_t), 0x00);
for (int i = sizeof(int64_t) - 1; i >= 0; i--) {
int offset = (sizeof(int64_t) - 1 - i) * 8;
result[i] = static_cast<uint8_t>((p_value >> offset) & 0xFF);
} // End of 'for' statement
return result;
}; // End of long_to_bytes
/*!
* \brief Convert a bytes array into a 64-bits integer (int64_t)
* \param[in] p_value The bytes array
* \param[in] p_endianess Endianess style. Default: big_endian
* \return The 64-bits integer on success, LLONG_MAX on error (wrong bytes array size)
*/
inline int64_t bytes_to_long(const std::vector<uint8_t> & p_value, const endian_t p_endianess = big_endian) const {
// Sanity check
if (p_value.size() > sizeof(int64_t)) {
return LLONG_MAX;
}
int64_t value = 0;
for (size_t i = 0; i < p_value.size(); i++) {
value = (value << 8) + (p_value[i] & 0xff);
} // End of 'for' statement
return value;
// return *((long *)(&p_value[0]));
}; // End of bytes_to_long
/*!
* \brief Convert a float value into a bytes array
* \param[in] p_value The float value
* \return The bytes array value
*/
inline std::vector<uint8_t> float_to_bytes(const float p_value) const {
uint8_t bytes[sizeof(p_value)];
std::copy(
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)),
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)) + sizeof(p_value),
bytes
);
std::vector<uint8_t> result(bytes, bytes + sizeof(bytes) / sizeof(uint8_t));
return result;
}; // End of float_to_long
/*!
* \brief Convert a bytes array into a float
* \param[in] p_value The bytes array
* \return The float value
*/
inline float bytes_to_float(const std::vector<uint8_t> & p_value) const {
return *((float *)(&p_value[0]));
}; // End of bytes_to_float
/*!
* \brief Convert a string into a bytes array
* \param[in] p_value The string value
* \return The bytes array value
*/
inline std::vector<uint8_t> string_to_bytes(const std::string & p_value) const {
return std::vector<uint8_t>(p_value.begin(), p_value.end());
}; // End of string_to_bytes
/*!
* \brief Convert a bytes array into a string
* \param[in] p_value The bytes array value
* \return The string value
*/
inline std::string bytes_to_string(const std::vector<uint8_t> & p_value) const {
return std::string(p_value.begin(), p_value.end());
}; // End of bytes_to_string
public:
/*!
* \brief Convert a string into an integer
* \param[in] p_value The string value
* \return The integer value
*/
inline int32_t string_to_int(const std::string & p_value) const {
return std::stoi(p_value);
//return atoi(p_value.c_str());
}; // End of string_to_int
/*!
* \brief Convert an integer into a string
* \param[in] p_value The integer value
* \return The string value
*/
inline std::string int_to_string(const int32_t & p_value) const {
std::ostringstream ss;
ss << p_value;
return ss.str();
}; // End of string_to_bytes
public:
/*!
* \brief Returns a copy of the string, with leading and trailing special characters omitted
* \param[in] p_value The string value
* \param[in] p_trim_chars The special characters to be omitted. Default: ' ' and TAB
* \return The new string value
*/
std::string trim(const std::string& p_value, const std::string& p_trim_chars = " \t");
/*!
* \brief Convert the provided string into a list of arguments
* \param[in] p_value The string value
* \param[in] p_separator The separator sequence to use for the spliting process
* \return The item list
* \code{.cc}
* std::string str = "This is a test for spliting a string with a white spave";
* std::vector<std::string> tokens = converter::get_instance().split(str, " ");
* std::clog << "Tokens: " << std::endl;
* for (auto it = tokens.begin(); it != tokens.end(); ++it) {
* std::clog << " " << *it << std::endl;
* }
* \endcode
*/
std::vector<std::string> split(const std::string & p_value, const std::string& p_separator);
/*!
* \brief Convert the provided string into a list of arguments
* \param[in] p_value The string value
* \return The arguments list
* \code{.cc}
* std::string str = "--host localhost --port 12345 --duration -1";
* std::vector<std::string> tokens = converter::get_instance().split_arguments_line(str);
* std::clog << "Tokens: " << std::endl;
* for (auto it = tokens.begin(); it != tokens.end(); ++it) {
* std::clog << " " << *it << std::endl;
* }
* \endcode
*/
std::vector<std::string> split_arguments_line(const std::string & p_value);