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