libcfe  0.12.1
some useful C-functions
cfe_value.c
Go to the documentation of this file.
1 /* kate: indent-mode cstyle; tab-width 4; indent-width 4; */
2 #include "config.h"
3 
4 #include "cfe_value.h"
5 
6 #include <stdlib.h>
7 #include <string.h>
8 
9 #define C_TYPE_FIELD(T) T##_value
10 #define C_TYPE_FIELD_DEF(T) T T##_value;
11 #define C_TYPE_FIELD_DEF2(T, F) T F;
12 
13 #define getset(NAME, C_TYPE, CFE_TYPE) getset2(NAME, C_TYPE, C_TYPE_FIELD(C_TYPE), CFE_TYPE)
14 #define getset2(NAME, C_TYPE, FIELD, CFE_TYPE) \
15 uint8_t cfe_value_set_##NAME (cfe_value_t *v, C_TYPE value) \
16 { \
17  cfe_value_empty(v); \
18  v->type = CFE_TYPE; \
19  v->value.FIELD = value; \
20  return 1; \
21 } \
22 uint8_t cfe_value_get_##NAME (const cfe_value_t *v, C_TYPE *value) \
23 { \
24  if(!cfe_value_check_type(v, CFE_TYPE)) return 0; \
25  *value = (C_TYPE)v->value.FIELD; \
26  return 1; \
27 }
28 
29 struct _cfe_value
30 {
32  union
33  {
34  C_TYPE_FIELD_DEF(uint8_t)
35  C_TYPE_FIELD_DEF(int8_t)
36  C_TYPE_FIELD_DEF(uint16_t)
37  C_TYPE_FIELD_DEF(int16_t)
38  C_TYPE_FIELD_DEF(uint32_t)
39  C_TYPE_FIELD_DEF(int32_t)
40  C_TYPE_FIELD_DEF(uint64_t)
41  C_TYPE_FIELD_DEF(int64_t)
42  C_TYPE_FIELD_DEF(float)
43  C_TYPE_FIELD_DEF(double)
44  C_TYPE_FIELD_DEF2(void *, pointer)
45  C_TYPE_FIELD_DEF2(long double, ldouble)
46  C_TYPE_FIELD_DEF2(char *, str)
47  C_TYPE_FIELD_DEF2(signed char *, s_str)
48  C_TYPE_FIELD_DEF2(unsigned char *, u_str)
49  } value;
50 };
51 
52 static void cfe_value_empty(cfe_value_t *v);
53 
54 cfe_value_t *cfe_value_new(void)
55 {
56  cfe_value_t *value = malloc(sizeof(struct _cfe_value));
57  value->type = CFE_NIL;
58  return value;
59 }
60 
61 void cfe_value_free(cfe_value_t *v)
62 {
63  cfe_value_empty(v);
64  free(v);
65 }
66 
67 getset(uint8, uint8_t, CFE_UINT8)
68 getset(int8, int8_t, CFE_INT8)
69 getset(uint16, uint16_t, CFE_UINT16)
70 getset(int16, int16_t, CFE_INT16)
71 getset(uint32, uint32_t, CFE_UINT32)
72 getset(int32, int32_t, CFE_INT32)
73 getset(uint64, uint64_t, CFE_UINT64)
74 getset(int64, int64_t, CFE_INT64)
75 getset2(signed_int, signed int, C_TYPE_FIELD(int32_t), CFE_INT32)
76 getset2(unsigned_int, unsigned int, C_TYPE_FIELD(uint32_t), CFE_UINT32)
77 getset2(signed_long, signed long, C_TYPE_FIELD(int64_t), CFE_INT64)
78 getset2(unsigned_long, unsigned long, C_TYPE_FIELD(uint64_t), CFE_UINT64)
79 
80 getset2(schar, signed char, C_TYPE_FIELD(int8_t), CFE_INT8)
81 getset2(uchar, unsigned char, C_TYPE_FIELD(uint8_t), CFE_UINT8)
82 
83 getset(boolean, int8_t, CFE_BOOLEAN)
84 
85 getset(float, float, CFE_FLOAT)
86 getset(double, double, CFE_DOUBLE)
87 getset2(long_double, long double, ldouble, CFE_LDOUBLE)
88 
89 getset2(pointer, void *, pointer, CFE_POINTER)
90 
91 uint8_t cfe_value_get_string(const cfe_value_t *v, char **value)
92 {
93  if(!cfe_value_check_type(v, CFE_STRING)) return 0;
94  *value = v->value.str;
95  return 1;
96 }
97 
98 uint8_t cfe_value_set_string(cfe_value_t *v, char *value)
99 {
100  cfe_value_empty(v);
101  v->type = CFE_STRING;
102  v->value.str = strdup(value);
103  return 1;
104 }
105 /*
106 uint8_t cfe_value_get_signed_string(cfe_value_t *v, signed char **value)
107 {
108  if(!cfe_value_check_type(v, CFE_U_STRING)) return 0;
109  *value = (signed char *)v->value.s_str;
110  return 1;
111 }
112 
113 uint8_t cfe_value_set_signed_string(cfe_value_t *v, signed char *value)
114 {
115  cfe_value_empty(v);
116  v->type = CFE_S_STRING;
117  v->value.s_str = strdup((const signed char *)value);
118  return 1;
119 }
120 
121 uint8_t cfe_value_get_unsigned_string(cfe_value_t *v, unsigned char **value)
122 {
123  if(!cfe_value_check_type(v, CFE_U_STRING)) return 0;
124  *value = (unsigned char *)v->value.u_str;
125  return 1;
126 }
127 
128 uint8_t cfe_value_set_unsigned_string(cfe_value_t *v, unsigned char *value)
129 {
130  cfe_value_empty(v);
131  v->type = CFE_U_STRING;
132  v->value.u_str = strdup((const unsigned char *)value);
133  return 1;
134 }
135 */
136 uint8_t cfe_value_set_null(cfe_value_t *v)
137 {
138  cfe_value_empty(v);
139  v->type = CFE_NULL;
140  v->value.pointer = NULL;
141  return 1;
142 }
143 
144 uint8_t cfe_value_is_null(const cfe_value_t *v)
145 {
146  return cfe_value_check_type(v, CFE_NULL);
147 }
148 
149 uint8_t cfe_value_is_nil(const cfe_value_t *v)
150 {
151  return cfe_value_check_type(v, CFE_NIL);
152 }
153 
154 inline uint8_t cfe_value_check_type(const cfe_value_t *v, cfe_value_type_t type)
155 {
156  return (v->type == type);
157 }
158 
160 {
161  return v->type;
162 }
163 
164 static void cfe_value_empty(cfe_value_t *v)
165 {
166  if(v->type == CFE_STRING) free(v->value.str);
167  else if(v->type == CFE_S_STRING) free(v->value.s_str);
168  else if(v->type == CFE_U_STRING) free(v->value.u_str);
169  v->value.str = NULL;
170  v->type = CFE_NIL;
171 }
uint8_t cfe_value_get_string(const cfe_value_t *v, char **value)
#define getset2(NAME, C_TYPE, FIELD, CFE_TYPE)
Definition: cfe_value.c:14
uint8_t cfe_value_check_type(const cfe_value_t *v, cfe_value_type_t type)
Definition: cfe_value.c:154
cfe_value_type_t type
Definition: cfe_value.c:31
uint8_t cfe_value_is_null(const cfe_value_t *v)
Definition: cfe_value.c:144
union _cfe_value::@0 value
#define C_TYPE_FIELD_DEF(T)
Definition: cfe_value.c:10
enum cfe_value_type cfe_value_type_t
Definition: cfe_value.h:7
#define C_TYPE_FIELD(T)
Definition: cfe_value.c:9
#define getset(NAME, C_TYPE, CFE_TYPE)
Definition: cfe_value.c:13
uint8_t cfe_value_set_null(cfe_value_t *v)
Definition: cfe_value.c:136
uint8_t cfe_value_is_nil(const cfe_value_t *v)
Definition: cfe_value.c:149
uint8_t cfe_value_set_string(cfe_value_t *v, char *value)
Definition: cfe_value.c:98
cfe_value_type_t cfe_value_get_type(const cfe_value_t *v)
Definition: cfe_value.c:159
void cfe_value_free(cfe_value_t *v)
Definition: cfe_value.c:61
cfe_value_t * cfe_value_new(void)
Definition: cfe_value.c:54
#define C_TYPE_FIELD_DEF2(T, F)
Definition: cfe_value.c:11