fe714b53d78c37c645ce009d167a7b450434ac6f
[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(c) <2016>, AT&T Intellectual Property.  All other rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:  This product includes
20  *    software developed by the AT&T.
21  * 4. Neither the name of AT&T nor the names of its contributors may be used to
22  *    endorse or promote products derived from this software without specific
23  *    prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY AT&T INTELLECTUAL PROPERTY ''AS IS'' AND ANY
26  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  * DISCLAIMED. IN NO EVENT SHALL AT&T INTELLECTUAL PROPERTY BE LIABLE FOR ANY
29  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *****************************************************************************/
36
37 #include <assert.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "evel_internal.h"
42
43 /**************************************************************************//**
44  * Free the underlying resources of an ::EVEL_OPTION_STRING.
45  *
46  * @param option        Pointer to the ::EVEL_OPTION_STRING.
47  *****************************************************************************/
48 void evel_free_option_string(EVEL_OPTION_STRING * const option)
49 {
50   EVEL_ENTER();
51
52   /***************************************************************************/
53   /* Check preconditions.                                                    */
54   /***************************************************************************/
55   assert(option != NULL);
56
57   if (option->is_set)
58   {
59     free(option->value);
60     option->value = NULL;
61     option->is_set = EVEL_FALSE;
62   }
63
64   EVEL_EXIT();
65 }
66
67 /**************************************************************************//**
68  * Initialize an ::EVEL_OPTION_STRING to a not-set state.
69  *
70  * @param option        Pointer to the ::EVEL_OPTION_STRING.
71  *****************************************************************************/
72 void evel_init_option_string(EVEL_OPTION_STRING * const option)
73 {
74   EVEL_ENTER();
75
76   /***************************************************************************/
77   /* Check preconditions.                                                    */
78   /***************************************************************************/
79   assert(option != NULL);
80
81   option->value = NULL;
82   option->is_set = EVEL_FALSE;
83
84   EVEL_EXIT();
85 }
86
87 /**************************************************************************//**
88  * Set the value of an ::EVEL_OPTION_STRING.
89  *
90  * @param option        Pointer to the ::EVEL_OPTION_STRING.
91  * @param value         The value to set.
92  * @param description   Description to be used in logging.
93  *****************************************************************************/
94 void evel_set_option_string(EVEL_OPTION_STRING * const option,
95                             const char * const value,
96                             const char * const description)
97 {
98   EVEL_ENTER();
99
100   /***************************************************************************/
101   /* Check preconditions.                                                    */
102   /***************************************************************************/
103   assert(option != NULL);
104   assert(value != NULL);
105   assert(description != NULL);
106
107   if (option->is_set)
108   {
109     EVEL_ERROR("Ignoring attempt to update %s to %s. %s already set to %s",
110                description, value, description, option->value);
111   }
112   else
113   {
114     EVEL_DEBUG("Setting %s to %s", description, value);
115     option->value = strdup(value);
116     option->is_set = EVEL_TRUE;
117   }
118
119   EVEL_EXIT();
120 }
121
122 /**************************************************************************//**
123  * Force the value of an ::EVEL_OPTION_STRING.
124  *
125  * @param option        Pointer to the ::EVEL_OPTION_STRING.
126  * @param value         The value to set.
127  *****************************************************************************/
128 void evel_force_option_string(EVEL_OPTION_STRING * const option,
129                               const char * const value)
130 {
131   EVEL_ENTER();
132
133   /***************************************************************************/
134   /* Check preconditions.                                                    */
135   /***************************************************************************/
136   assert(option != NULL);
137   assert(option->is_set == EVEL_FALSE);
138   assert(option->value == NULL);
139
140   option->value = strdup(value);
141   option->is_set = EVEL_TRUE;
142
143   EVEL_EXIT();
144 }
145
146 /**************************************************************************//**
147  * Initialize an ::EVEL_OPTION_INT to a not-set state.
148  *
149  * @param option        Pointer to the ::EVEL_OPTION_INT.
150  *****************************************************************************/
151 void evel_init_option_int(EVEL_OPTION_INT * const option)
152 {
153   EVEL_ENTER();
154
155   /***************************************************************************/
156   /* Check preconditions.                                                    */
157   /***************************************************************************/
158   assert(option != NULL);
159
160   option->value = 0;
161   option->is_set = EVEL_FALSE;
162
163   EVEL_EXIT();
164 }
165
166 /**************************************************************************//**
167  * Force the value of an ::EVEL_OPTION_INT.
168  *
169  * @param option        Pointer to the ::EVEL_OPTION_INT.
170  * @param value         The value to set.
171  *****************************************************************************/
172 void evel_force_option_int(EVEL_OPTION_INT * const option,
173                            const int value)
174 {
175   EVEL_ENTER();
176
177   /***************************************************************************/
178   /* Check preconditions.                                                    */
179   /***************************************************************************/
180   assert(option != NULL);
181
182   option->value = value;
183   option->is_set = EVEL_TRUE;
184
185   EVEL_EXIT();
186 }
187
188 /**************************************************************************//**
189  * Set the value of an ::EVEL_OPTION_INT.
190  *
191  * @param option        Pointer to the ::EVEL_OPTION_INT.
192  * @param value         The value to set.
193  * @param description   Description to be used in logging.
194  *****************************************************************************/
195 void evel_set_option_int(EVEL_OPTION_INT * const option,
196                          const int value,
197                          const char * const description)
198 {
199   EVEL_ENTER();
200
201   /***************************************************************************/
202   /* Check preconditions.                                                    */
203   /***************************************************************************/
204   assert(option != NULL);
205   assert(description != NULL);
206
207   if (option->is_set)
208   {
209     EVEL_ERROR("Ignoring attempt to update %s to %d. %s already set to %d",
210                description, value, description, option->value);
211   }
212   else
213   {
214     EVEL_DEBUG("Setting %s to %d", description, value);
215     option->value = value;
216     option->is_set = EVEL_TRUE;
217   }
218
219   EVEL_EXIT();
220 }
221
222 /**************************************************************************//**
223  * Initialize an ::EVEL_OPTION_DOUBLE to a not-set state.
224  *
225  * @param option        Pointer to the ::EVEL_OPTION_DOUBLE.
226  *****************************************************************************/
227 void evel_init_option_double(EVEL_OPTION_DOUBLE * const option)
228 {
229   EVEL_ENTER();
230
231   /***************************************************************************/
232   /* Check preconditions.                                                    */
233   /***************************************************************************/
234   assert(option != NULL);
235
236   option->value = 0.0;
237   option->is_set = EVEL_FALSE;
238
239   EVEL_EXIT();
240 }
241
242 /**************************************************************************//**
243  * Force the value of an ::EVEL_OPTION_DOUBLE.
244  *
245  * @param option        Pointer to the ::EVEL_OPTION_DOUBLE.
246  * @param value         The value to set.
247  *****************************************************************************/
248 void evel_force_option_double(EVEL_OPTION_DOUBLE * const option,
249                               const double value)
250 {
251   EVEL_ENTER();
252
253   /***************************************************************************/
254   /* Check preconditions.                                                    */
255   /***************************************************************************/
256   assert(option != NULL);
257
258   option->value = value;
259   option->is_set = EVEL_TRUE;
260
261   EVEL_EXIT();
262 }
263
264 /**************************************************************************//**
265  * Set the value of an ::EVEL_OPTION_DOUBLE.
266  *
267  * @param option        Pointer to the ::EVEL_OPTION_DOUBLE.
268  * @param value         The value to set.
269  * @param description   Description to be used in logging.
270  *****************************************************************************/
271 void evel_set_option_double(EVEL_OPTION_DOUBLE * const option,
272                             const double value,
273                             const char * const description)
274 {
275   EVEL_ENTER();
276
277   /***************************************************************************/
278   /* Check preconditions.                                                    */
279   /***************************************************************************/
280   assert(option != NULL);
281   assert(description != NULL);
282
283   if (option->is_set)
284   {
285     EVEL_ERROR("Ignoring attempt to update %s to %lf. %s already set to %lf",
286                description, value, description, option->value);
287   }
288   else
289   {
290     EVEL_DEBUG("Setting %s to %lf", description, value);
291     option->value = value;
292     option->is_set = EVEL_TRUE;
293   }
294
295   EVEL_EXIT();
296 }
297
298 /**************************************************************************//**
299  * Initialize an ::EVEL_OPTION_ULL to a not-set state.
300  *
301  * @param option        Pointer to the ::EVEL_OPTION_ULL.
302  *****************************************************************************/
303 void evel_init_option_ull(EVEL_OPTION_ULL * const option)
304 {
305   EVEL_ENTER();
306
307   /***************************************************************************/
308   /* Check preconditions.                                                    */
309   /***************************************************************************/
310   assert(option != NULL);
311   option->value = 0;
312   option->is_set = EVEL_FALSE;
313   EVEL_EXIT();
314 }
315
316 /**************************************************************************//**
317  * Force the value of an ::EVEL_OPTION_ULL.
318  *
319  * @param option        Pointer to the ::EVEL_OPTION_ULL.
320  * @param value         The value to set.
321  *****************************************************************************/
322 void evel_force_option_ull(EVEL_OPTION_ULL * const option,
323                            const unsigned long long value)
324 {
325   EVEL_ENTER();
326
327   /***************************************************************************/
328   /* Check preconditions.                                                    */
329   /***************************************************************************/
330   assert(option != NULL);
331
332   option->value = value;
333   option->is_set = EVEL_TRUE;
334
335   EVEL_EXIT();
336 }
337
338 /**************************************************************************//**
339  * Set the value of an ::EVEL_OPTION_ULL.
340  *
341  * @param option        Pointer to the ::EVEL_OPTION_ULL.
342  * @param value         The value to set.
343  * @param description   Description to be used in logging.
344  *****************************************************************************/
345 void evel_set_option_ull(EVEL_OPTION_ULL * const option,
346                          const unsigned long long value,
347                          const char * const description)
348 {
349   EVEL_ENTER();
350
351   /***************************************************************************/
352   /* Check preconditions.                                                    */
353   /***************************************************************************/
354   assert(option != NULL);
355   assert(description != NULL);
356
357   if (option->is_set)
358   {
359     EVEL_ERROR("Ignoring attempt to update %s to %llu. %s already set to %llu",
360                description, value, description, option->value);
361   }
362   else
363   {
364     EVEL_DEBUG("Setting %s to %llu", description, value);
365     option->value = value;
366     option->is_set = EVEL_TRUE;
367   }
368   EVEL_EXIT();
369 }
370
371 /**************************************************************************//**
372  * Initialize an ::EVEL_OPTION_TIME to a not-set state.
373  *
374  * @param option        Pointer to the ::EVEL_OPTION_TIME.
375  *****************************************************************************/
376 void evel_init_option_time(EVEL_OPTION_TIME * const option)
377 {
378   EVEL_ENTER();
379
380   /***************************************************************************/
381   /* Check preconditions.                                                    */
382   /***************************************************************************/
383   assert(option != NULL);
384   option->value = 0;
385   option->is_set = EVEL_FALSE;
386   EVEL_EXIT();
387 }
388
389 /**************************************************************************//**
390  * Force the value of an ::EVEL_OPTION_TIME.
391  *
392  * @param option        Pointer to the ::EVEL_OPTION_TIME.
393  * @param value         The value to set.
394  *****************************************************************************/
395 void evel_force_option_time(EVEL_OPTION_TIME * const option,
396                             const time_t value)
397 {
398   EVEL_ENTER();
399
400   /***************************************************************************/
401   /* Check preconditions.                                                    */
402   /***************************************************************************/
403   assert(option != NULL);
404
405   option->value = value;
406   option->is_set = EVEL_TRUE;
407
408   EVEL_EXIT();
409 }
410
411 /**************************************************************************//**
412  * Set the value of an ::EVEL_OPTION_TIME.
413  *
414  * @param option        Pointer to the ::EVEL_OPTION_TIME.
415  * @param value         The value to set.
416  * @param description   Description to be used in logging.
417  *****************************************************************************/
418 void evel_set_option_time(EVEL_OPTION_TIME * const option,
419                           const time_t value,
420                           const char * const description)
421 {
422   EVEL_ENTER();
423
424   /***************************************************************************/
425   /* Check preconditions.                                                    */
426   /***************************************************************************/
427   assert(option != NULL);
428   assert(description != NULL);
429
430   if (option->is_set)
431   {
432     EVEL_ERROR("Ignoring attempt to update %s to %d. %s already set to %d",
433                description, value, description, option->value);
434   }
435   else
436   {
437     EVEL_DEBUG("Setting %s to %d", description, value);
438     option->value = value;
439     option->is_set = EVEL_TRUE;
440   }
441   EVEL_EXIT();
442 }