Add License to VES library
[demo.git] / vnfs / VES / code / evel_library / evel_option.c
1 /**************************************************************************//**
2  * @file
3  * Source module relating to EVEL_OPTION_ types.
4  *
5  * License
6  * -------
7  *
8  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *        http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *****************************************************************************/
21
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "evel_internal.h"
27
28 /**************************************************************************//**
29  * Free the underlying resources of an ::EVEL_OPTION_STRING.
30  *
31  * @param option        Pointer to the ::EVEL_OPTION_STRING.
32  *****************************************************************************/
33 void evel_free_option_string(EVEL_OPTION_STRING * const option)
34 {
35   EVEL_ENTER();
36
37   /***************************************************************************/
38   /* Check preconditions.                                                    */
39   /***************************************************************************/
40   assert(option != NULL);
41
42   if (option->is_set)
43   {
44     free(option->value);
45     option->value = NULL;
46     option->is_set = EVEL_FALSE;
47   }
48
49   EVEL_EXIT();
50 }
51
52 /**************************************************************************//**
53  * Initialize an ::EVEL_OPTION_STRING to a not-set state.
54  *
55  * @param option        Pointer to the ::EVEL_OPTION_STRING.
56  *****************************************************************************/
57 void evel_init_option_string(EVEL_OPTION_STRING * const option)
58 {
59   EVEL_ENTER();
60
61   /***************************************************************************/
62   /* Check preconditions.                                                    */
63   /***************************************************************************/
64   assert(option != NULL);
65
66   option->value = NULL;
67   option->is_set = EVEL_FALSE;
68
69   EVEL_EXIT();
70 }
71
72 /**************************************************************************//**
73  * Set the value of an ::EVEL_OPTION_STRING.
74  *
75  * @param option        Pointer to the ::EVEL_OPTION_STRING.
76  * @param value         The value to set.
77  * @param description   Description to be used in logging.
78  *****************************************************************************/
79 void evel_set_option_string(EVEL_OPTION_STRING * const option,
80                             const char * const value,
81                             const char * const description)
82 {
83   EVEL_ENTER();
84
85   /***************************************************************************/
86   /* Check preconditions.                                                    */
87   /***************************************************************************/
88   assert(option != NULL);
89   assert(value != NULL);
90   assert(description != NULL);
91
92   if (option->is_set)
93   {
94     EVEL_ERROR("Ignoring attempt to update %s to %s. %s already set to %s",
95                description, value, description, option->value);
96   }
97   else
98   {
99     EVEL_DEBUG("Setting %s to %s", description, value);
100     option->value = strdup(value);
101     option->is_set = EVEL_TRUE;
102   }
103
104   EVEL_EXIT();
105 }
106
107 /**************************************************************************//**
108  * Force the value of an ::EVEL_OPTION_STRING.
109  *
110  * @param option        Pointer to the ::EVEL_OPTION_STRING.
111  * @param value         The value to set.
112  *****************************************************************************/
113 void evel_force_option_string(EVEL_OPTION_STRING * const option,
114                               const char * const value)
115 {
116   EVEL_ENTER();
117
118   /***************************************************************************/
119   /* Check preconditions.                                                    */
120   /***************************************************************************/
121   assert(option != NULL);
122   assert(option->is_set == EVEL_FALSE);
123   assert(option->value == NULL);
124
125   option->value = strdup(value);
126   option->is_set = EVEL_TRUE;
127
128   EVEL_EXIT();
129 }
130
131 /**************************************************************************//**
132  * Initialize an ::EVEL_OPTION_INT to a not-set state.
133  *
134  * @param option        Pointer to the ::EVEL_OPTION_INT.
135  *****************************************************************************/
136 void evel_init_option_int(EVEL_OPTION_INT * const option)
137 {
138   EVEL_ENTER();
139
140   /***************************************************************************/
141   /* Check preconditions.                                                    */
142   /***************************************************************************/
143   assert(option != NULL);
144
145   option->value = 0;
146   option->is_set = EVEL_FALSE;
147
148   EVEL_EXIT();
149 }
150
151 /**************************************************************************//**
152  * Force the value of an ::EVEL_OPTION_INT.
153  *
154  * @param option        Pointer to the ::EVEL_OPTION_INT.
155  * @param value         The value to set.
156  *****************************************************************************/
157 void evel_force_option_int(EVEL_OPTION_INT * const option,
158                            const int value)
159 {
160   EVEL_ENTER();
161
162   /***************************************************************************/
163   /* Check preconditions.                                                    */
164   /***************************************************************************/
165   assert(option != NULL);
166
167   option->value = value;
168   option->is_set = EVEL_TRUE;
169
170   EVEL_EXIT();
171 }
172
173 /**************************************************************************//**
174  * Set the value of an ::EVEL_OPTION_INT.
175  *
176  * @param option        Pointer to the ::EVEL_OPTION_INT.
177  * @param value         The value to set.
178  * @param description   Description to be used in logging.
179  *****************************************************************************/
180 void evel_set_option_int(EVEL_OPTION_INT * const option,
181                          const int value,
182                          const char * const description)
183 {
184   EVEL_ENTER();
185
186   /***************************************************************************/
187   /* Check preconditions.                                                    */
188   /***************************************************************************/
189   assert(option != NULL);
190   assert(description != NULL);
191
192   if (option->is_set)
193   {
194     EVEL_ERROR("Ignoring attempt to update %s to %d. %s already set to %d",
195                description, value, description, option->value);
196   }
197   else
198   {
199     EVEL_DEBUG("Setting %s to %d", description, value);
200     option->value = value;
201     option->is_set = EVEL_TRUE;
202   }
203
204   EVEL_EXIT();
205 }
206
207 /**************************************************************************//**
208  * Initialize an ::EVEL_OPTION_DOUBLE to a not-set state.
209  *
210  * @param option        Pointer to the ::EVEL_OPTION_DOUBLE.
211  *****************************************************************************/
212 void evel_init_option_double(EVEL_OPTION_DOUBLE * const option)
213 {
214   EVEL_ENTER();
215
216   /***************************************************************************/
217   /* Check preconditions.                                                    */
218   /***************************************************************************/
219   assert(option != NULL);
220
221   option->value = 0.0;
222   option->is_set = EVEL_FALSE;
223
224   EVEL_EXIT();
225 }
226
227 /**************************************************************************//**
228  * Force the value of an ::EVEL_OPTION_DOUBLE.
229  *
230  * @param option        Pointer to the ::EVEL_OPTION_DOUBLE.
231  * @param value         The value to set.
232  *****************************************************************************/
233 void evel_force_option_double(EVEL_OPTION_DOUBLE * const option,
234                               const double value)
235 {
236   EVEL_ENTER();
237
238   /***************************************************************************/
239   /* Check preconditions.                                                    */
240   /***************************************************************************/
241   assert(option != NULL);
242
243   option->value = value;
244   option->is_set = EVEL_TRUE;
245
246   EVEL_EXIT();
247 }
248
249 /**************************************************************************//**
250  * Set the value of an ::EVEL_OPTION_DOUBLE.
251  *
252  * @param option        Pointer to the ::EVEL_OPTION_DOUBLE.
253  * @param value         The value to set.
254  * @param description   Description to be used in logging.
255  *****************************************************************************/
256 void evel_set_option_double(EVEL_OPTION_DOUBLE * const option,
257                             const double value,
258                             const char * const description)
259 {
260   EVEL_ENTER();
261
262   /***************************************************************************/
263   /* Check preconditions.                                                    */
264   /***************************************************************************/
265   assert(option != NULL);
266   assert(description != NULL);
267
268   if (option->is_set)
269   {
270     EVEL_ERROR("Ignoring attempt to update %s to %lf. %s already set to %lf",
271                description, value, description, option->value);
272   }
273   else
274   {
275     EVEL_DEBUG("Setting %s to %lf", description, value);
276     option->value = value;
277     option->is_set = EVEL_TRUE;
278   }
279
280   EVEL_EXIT();
281 }
282
283 /**************************************************************************//**
284  * Initialize an ::EVEL_OPTION_ULL to a not-set state.
285  *
286  * @param option        Pointer to the ::EVEL_OPTION_ULL.
287  *****************************************************************************/
288 void evel_init_option_ull(EVEL_OPTION_ULL * const option)
289 {
290   EVEL_ENTER();
291
292   /***************************************************************************/
293   /* Check preconditions.                                                    */
294   /***************************************************************************/
295   assert(option != NULL);
296   option->value = 0;
297   option->is_set = EVEL_FALSE;
298   EVEL_EXIT();
299 }
300
301 /**************************************************************************//**
302  * Force the value of an ::EVEL_OPTION_ULL.
303  *
304  * @param option        Pointer to the ::EVEL_OPTION_ULL.
305  * @param value         The value to set.
306  *****************************************************************************/
307 void evel_force_option_ull(EVEL_OPTION_ULL * const option,
308                            const unsigned long long value)
309 {
310   EVEL_ENTER();
311
312   /***************************************************************************/
313   /* Check preconditions.                                                    */
314   /***************************************************************************/
315   assert(option != NULL);
316
317   option->value = value;
318   option->is_set = EVEL_TRUE;
319
320   EVEL_EXIT();
321 }
322
323 /**************************************************************************//**
324  * Set the value of an ::EVEL_OPTION_ULL.
325  *
326  * @param option        Pointer to the ::EVEL_OPTION_ULL.
327  * @param value         The value to set.
328  * @param description   Description to be used in logging.
329  *****************************************************************************/
330 void evel_set_option_ull(EVEL_OPTION_ULL * const option,
331                          const unsigned long long value,
332                          const char * const description)
333 {
334   EVEL_ENTER();
335
336   /***************************************************************************/
337   /* Check preconditions.                                                    */
338   /***************************************************************************/
339   assert(option != NULL);
340   assert(description != NULL);
341
342   if (option->is_set)
343   {
344     EVEL_ERROR("Ignoring attempt to update %s to %llu. %s already set to %llu",
345                description, value, description, option->value);
346   }
347   else
348   {
349     EVEL_DEBUG("Setting %s to %llu", description, value);
350     option->value = value;
351     option->is_set = EVEL_TRUE;
352   }
353   EVEL_EXIT();
354 }
355
356 /**************************************************************************//**
357  * Initialize an ::EVEL_OPTION_TIME to a not-set state.
358  *
359  * @param option        Pointer to the ::EVEL_OPTION_TIME.
360  *****************************************************************************/
361 void evel_init_option_time(EVEL_OPTION_TIME * const option)
362 {
363   EVEL_ENTER();
364
365   /***************************************************************************/
366   /* Check preconditions.                                                    */
367   /***************************************************************************/
368   assert(option != NULL);
369   option->value = 0;
370   option->is_set = EVEL_FALSE;
371   EVEL_EXIT();
372 }
373
374 /**************************************************************************//**
375  * Force the value of an ::EVEL_OPTION_TIME.
376  *
377  * @param option        Pointer to the ::EVEL_OPTION_TIME.
378  * @param value         The value to set.
379  *****************************************************************************/
380 void evel_force_option_time(EVEL_OPTION_TIME * const option,
381                             const time_t value)
382 {
383   EVEL_ENTER();
384
385   /***************************************************************************/
386   /* Check preconditions.                                                    */
387   /***************************************************************************/
388   assert(option != NULL);
389
390   option->value = value;
391   option->is_set = EVEL_TRUE;
392
393   EVEL_EXIT();
394 }
395
396 /**************************************************************************//**
397  * Set the value of an ::EVEL_OPTION_TIME.
398  *
399  * @param option        Pointer to the ::EVEL_OPTION_TIME.
400  * @param value         The value to set.
401  * @param description   Description to be used in logging.
402  *****************************************************************************/
403 void evel_set_option_time(EVEL_OPTION_TIME * const option,
404                           const time_t value,
405                           const char * const description)
406 {
407   EVEL_ENTER();
408
409   /***************************************************************************/
410   /* Check preconditions.                                                    */
411   /***************************************************************************/
412   assert(option != NULL);
413   assert(description != NULL);
414
415   if (option->is_set)
416   {
417     EVEL_ERROR("Ignoring attempt to update %s to %d. %s already set to %d",
418                description, value, description, option->value);
419   }
420   else
421   {
422     EVEL_DEBUG("Setting %s to %d", description, value);
423     option->value = value;
424     option->is_set = EVEL_TRUE;
425   }
426   EVEL_EXIT();
427 }