1 /***********************************************************************************************************************************
2 Convert C Types
3 
4 Contains conversions to/from native C types. Conversions of project types should be defined in their respective type modules.
5 ***********************************************************************************************************************************/
6 #ifndef COMMON_TYPE_CONVERT_H
7 #define COMMON_TYPE_CONVERT_H
8 
9 #include <inttypes.h>
10 #include <stdbool.h>
11 #include <sys/types.h>
12 
13 /***********************************************************************************************************************************
14 Required buffer sizes
15 ***********************************************************************************************************************************/
16 #define CVT_BOOL_BUFFER_SIZE                                        6
17 #define CVT_BASE10_BUFFER_SIZE                                      64
18 
19 /***********************************************************************************************************************************
20 Functions
21 ***********************************************************************************************************************************/
22 // Convert char to zero-terminated string
23 size_t cvtCharToZ(char value, char *buffer, size_t bufferSize);
24 
25 // Convert double to zero-terminated string and vice versa
26 size_t cvtDoubleToZ(double value, char *buffer, size_t bufferSize);
27 double cvtZToDouble(const char *value);
28 
29 // Convert int to zero-terminated string and vice versa
30 size_t cvtIntToZ(int value, char *buffer, size_t bufferSize);
31 int cvtZToInt(const char *value);
32 int cvtZToIntBase(const char *value, int base);
33 
34 // Convert int64 to zero-terminated string and vice versa
35 size_t cvtInt64ToZ(int64_t value, char *buffer, size_t bufferSize);
36 int64_t cvtZToInt64(const char *value);
37 int64_t cvtZToInt64Base(const char *value, int base);
38 
39 // Convert int32/64 to uint32/64 using zigzag encoding and vice versa. Zigzag encoding places the sign in the least significant bit
40 // so that signed and unsigned values alternate, e.g. 0 = 0, -1 = 1, 1 = 2, -2 = 3, 2 = 4, -3 = 5, 3 = 6, etc. This moves as many
41 // bits as possible into the low order bits which is good for other types of encoding, e.g. base-128. See
42 // http://neurocline.github.io/dev/2015/09/17/zig-zag-encoding.html for details.
43 __attribute__((always_inline)) static inline uint32_t
cvtInt32ToZigZag(const int32_t value)44 cvtInt32ToZigZag(const int32_t value)
45 {
46     return ((uint32_t)value << 1) ^ (uint32_t)(value >> 31);
47 }
48 
49 __attribute__((always_inline)) static inline int32_t
cvtInt32FromZigZag(const uint32_t value)50 cvtInt32FromZigZag(const uint32_t value)
51 {
52     return (int32_t)((value >> 1) ^ (~(value & 1) + 1));
53 }
54 
55 __attribute__((always_inline)) static inline uint64_t
cvtInt64ToZigZag(const int64_t value)56 cvtInt64ToZigZag(const int64_t value)
57 {
58     return ((uint64_t)value << 1) ^ (uint64_t)(value >> 63);
59 }
60 
61 __attribute__((always_inline)) static inline int64_t
cvtInt64FromZigZag(const uint64_t value)62 cvtInt64FromZigZag(const uint64_t value)
63 {
64     return (int64_t)((value >> 1) ^ (~(value & 1) + 1));
65 }
66 
67 // Convert mode to zero-terminated string and vice versa
68 size_t cvtModeToZ(mode_t value, char *buffer, size_t bufferSize);
69 mode_t cvtZToMode(const char *value);
70 
71 // Convert size/ssize to zero-terminated string
72 size_t cvtSizeToZ(size_t value, char *buffer, size_t bufferSize);
73 size_t cvtSSizeToZ(ssize_t value, char *buffer, size_t bufferSize);
74 
75 // Convert time_t to zero-terminated string
76 size_t cvtTimeToZ(time_t value, char *buffer, size_t bufferSize);
77 
78 // Convert uint to zero-terminated string and vice versa
79 size_t cvtUIntToZ(unsigned int value, char *buffer, size_t bufferSize);
80 unsigned int cvtZToUInt(const char *value);
81 unsigned int cvtZToUIntBase(const char *value, int base);
82 
83 // Convert uint64 to zero-terminated string and visa versa
84 size_t cvtUInt64ToZ(uint64_t value, char *buffer, size_t bufferSize);
85 uint64_t cvtZToUInt64(const char *value);
86 uint64_t cvtZToUInt64Base(const char *value, int base);
87 
88 // Convert boolean to zero-terminated string. Use cvtBoolToConstZ() whenever possible since it is more efficient.
89 size_t cvtBoolToZ(bool value, char *buffer, size_t bufferSize);
90 const char *cvtBoolToConstZ(bool value);
91 
92 #endif
93