This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
conversion.c
619 lines (549 loc) · 16 KB
/
conversion.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/**
* @file
*
* @brief Elektra High Level API.
*
* @copyright BSD License (see doc/LICENSE.md or http://www.libelektra.org)
*/
#include "kdbease.h"
#include "kdbhelper.h"
#include <ctype.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
#define CAT(X, Y) CAT_ (X, Y)
#define CAT_(X, Y) X##Y
/**
* Converts a Key to string.
*
* The variable pointed to by @p variable is unchanged,
* if an error occurs.
*
* @param key the key to convert
* @param variable pointer to the output variable
* @retval 1 on success
* @retval 0 otherwise
*/
int elektraKeyToString (const Key * key ELEKTRA_UNUSED, const char ** variable ELEKTRA_UNUSED)
{
#define TYPE_NAME String
#define TYPE const char *
#define KDB_TYPE KDB_TYPE_STRING
#define PRE_CHECK_FAIL_BLOCK
#define CHECK_FAIL_BLOCK
#define TO_VALUE (string)
#define PRE_CHECK_CONVERSION (keyIsString (key) == 1)
#define NAME_MACRO(TYPE_NAME) CAT (elektraKeyTo, TYPE_NAME)
#define KEY_PARAM_NAME key
#define VARIABLE_PARAM_NAME variable
#define CODE_ONLY 1
#include "macros/type_create_to_value.h"
#undef KDB_TYPE
}
/**
* Converts a Key to boolean.
*
* The value "1" is regarded as true, anything is as false.
*
* The variable pointed to by @p variable is unchanged,
* if an error occurs.
*
* @param key the key to convert
* @param variable pointer to the output variable
* @retval 1 on success
* @retval 0 otherwise
*/
int elektraKeyToBoolean (const Key * key ELEKTRA_UNUSED, kdb_boolean_t * variable ELEKTRA_UNUSED)
{
#define TYPE_NAME Boolean
#define TYPE kdb_boolean_t
#define KDB_TYPE KDB_TYPE_BOOLEAN
#define PRE_CHECK_CONVERSION ((string[0] == '0' || string[0] == '1') && string[1] == '\0')
#define PRE_CHECK_FAIL_BLOCK
#define CHECK_FAIL_BLOCK
#define TO_VALUE (string[0] == '1')
#define NAME_MACRO(TYPE_NAME) CAT (elektraKeyTo, TYPE_NAME)
#define CODE_ONLY 1
#define KEY_PARAM_NAME key
#define VARIABLE_PARAM_NAME variable
#include "macros/type_create_to_value.h"
#undef KDB_TYPE
}
/**
* Converts a Key to char.
*
* The variable pointed to by @p variable is unchanged,
* if an error occurs.
*
* @param key the key to convert
* @param variable pointer to the output variable
* @retval 1 on success
* @retval 0 otherwise
*/
int elektraKeyToChar (const Key * key ELEKTRA_UNUSED, kdb_char_t * variable ELEKTRA_UNUSED)
{
#define TYPE_NAME Char
#define TYPE kdb_char_t
#define KDB_TYPE KDB_TYPE_CHAR
#define PRE_CHECK_FAIL_BLOCK
#define CHECK_FAIL_BLOCK
#define TO_VALUE (string[0])
#define NAME_MACRO(TYPE_NAME) CAT (elektraKeyTo, TYPE_NAME)
#define CODE_ONLY 1
#define KEY_PARAM_NAME key
#define VARIABLE_PARAM_NAME variable
#include "macros/type_create_to_value.h"
#undef KDB_TYPE
}
/**
* Converts a Key to octet.
*
* The variable pointed to by @p variable is unchanged,
* if an error occurs.
*
* @param key the key to convert
* @param variable pointer to the output variable
* @retval 1 on success
* @retval 0 otherwise
*/
int elektraKeyToOctet (const Key * key ELEKTRA_UNUSED, kdb_octet_t * variable ELEKTRA_UNUSED)
{
#define TYPE_NAME Octet
#define TYPE kdb_octet_t
#define KDB_TYPE KDB_TYPE_OCTET
#define PRE_CHECK_BLOCK ELEKTRA_TYPE_NEGATIVE_PRE_CHECK_BLOCK
#define PRE_CHECK_CONVERSION (ELEKTRA_TYPE_NEGATIVE_PRE_CHECK)
#define PRE_CHECK_FAIL_BLOCK
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION_RANGE (value <= UINT8_MAX)
#define CHECK_FAIL_BLOCK
#define VALUE_TYPE unsigned long
#define TO_VALUE (strtoul (string, &end, 10))
#define NAME_MACRO(TYPE_NAME) CAT (elektraKeyTo, TYPE_NAME)
#define CODE_ONLY 1
#define KEY_PARAM_NAME key
#define VARIABLE_PARAM_NAME variable
#include "macros/type_create_to_value.h"
#undef KDB_TYPE
}
/**
* Converts a Key to short.
*
* The variable pointed to by @p variable is unchanged,
* if an error occurs.
*
* @param key the key to convert
* @param variable pointer to the output variable
* @retval 1 on success
* @retval 0 otherwise
*/
int elektraKeyToShort (const Key * key ELEKTRA_UNUSED, kdb_short_t * variable ELEKTRA_UNUSED)
{
#define TYPE_NAME Short
#define TYPE kdb_short_t
#define KDB_TYPE KDB_TYPE_SHORT
#define PRE_CHECK_FAIL_BLOCK
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION_RANGE (value <= INT16_MAX && value >= INT16_MIN)
#define CHECK_FAIL_BLOCK
#define VALUE_TYPE long
#define TO_VALUE (strtol (string, &end, 10))
#define NAME_MACRO(TYPE_NAME) CAT (elektraKeyTo, TYPE_NAME)
#define CODE_ONLY 1
#define KEY_PARAM_NAME key
#define VARIABLE_PARAM_NAME variable
#include "macros/type_create_to_value.h"
#undef KDB_TYPE
}
/**
* Converts a Key to unsigned_short.
*
* The variable pointed to by @p variable is unchanged,
* if an error occurs.
*
* @param key the key to convert
* @param variable pointer to the output variable
* @retval 1 on success
* @retval 0 otherwise
*/
int elektraKeyToUnsignedShort (const Key * key ELEKTRA_UNUSED, kdb_unsigned_short_t * variable ELEKTRA_UNUSED)
{
#define TYPE_NAME UnsignedShort
#define TYPE kdb_unsigned_short_t
#define KDB_TYPE KDB_TYPE_UNSIGNED_SHORT
#define PRE_CHECK_BLOCK ELEKTRA_TYPE_NEGATIVE_PRE_CHECK_BLOCK
#define PRE_CHECK_CONVERSION (ELEKTRA_TYPE_NEGATIVE_PRE_CHECK)
#define PRE_CHECK_FAIL_BLOCK
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION_RANGE (value <= UINT16_MAX)
#define CHECK_FAIL_BLOCK
#define VALUE_TYPE unsigned long
#define TO_VALUE (strtoul (string, &end, 10))
#define NAME_MACRO(TYPE_NAME) CAT (elektraKeyTo, TYPE_NAME)
#define CODE_ONLY 1
#define KEY_PARAM_NAME key
#define VARIABLE_PARAM_NAME variable
#include "macros/type_create_to_value.h"
#undef KDB_TYPE
}
/**
* Converts a Key to long.
*
* The variable pointed to by @p variable is unchanged,
* if an error occurs.
*
* @param key the key to convert
* @param variable pointer to the output variable
* @retval 1 on success
* @retval 0 otherwise
*/
int elektraKeyToLong (const Key * key ELEKTRA_UNUSED, kdb_long_t * variable ELEKTRA_UNUSED)
{
#define TYPE_NAME Long
#define TYPE kdb_long_t
#define KDB_TYPE KDB_TYPE_LONG
#define PRE_CHECK_FAIL_BLOCK
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION_RANGE (value <= INT32_MAX && value >= INT32_MIN)
#define CHECK_FAIL_BLOCK
#define VALUE_TYPE long long
#define TO_VALUE (ELEKTRA_LONG_LONG_S (string, &end, 10))
#define NAME_MACRO(TYPE_NAME) CAT (elektraKeyTo, TYPE_NAME)
#define CODE_ONLY 1
#define KEY_PARAM_NAME key
#define VARIABLE_PARAM_NAME variable
#include "macros/type_create_to_value.h"
#undef KDB_TYPE
}
/**
* Converts a Key to unsigned_long.
*
* The variable pointed to by @p variable is unchanged,
* if an error occurs.
*
* @param key the key to convert
* @param variable pointer to the output variable
* @retval 1 on success
* @retval 0 otherwise
*/
int elektraKeyToUnsignedLong (const Key * key ELEKTRA_UNUSED, kdb_unsigned_long_t * variable ELEKTRA_UNUSED)
{
#define TYPE_NAME UnsignedLong
#define TYPE kdb_unsigned_long_t
#define KDB_TYPE KDB_TYPE_UNSIGNED_LONG
#define PRE_CHECK_BLOCK ELEKTRA_TYPE_NEGATIVE_PRE_CHECK_BLOCK
#define PRE_CHECK_CONVERSION (ELEKTRA_TYPE_NEGATIVE_PRE_CHECK)
#define PRE_CHECK_FAIL_BLOCK
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION_RANGE (value <= UINT32_MAX)
#define CHECK_FAIL_BLOCK
#define VALUE_TYPE unsigned long long
#define TO_VALUE (ELEKTRA_UNSIGNED_LONG_LONG_S (string, &end, 10))
#define NAME_MACRO(TYPE_NAME) CAT (elektraKeyTo, TYPE_NAME)
#define CODE_ONLY 1
#define KEY_PARAM_NAME key
#define VARIABLE_PARAM_NAME variable
#include "macros/type_create_to_value.h"
#undef KDB_TYPE
}
/**
* Converts a Key to long_long.
*
* The variable pointed to by @p variable is unchanged,
* if an error occurs.
*
* @param key the key to convert
* @param variable pointer to the output variable
* @retval 1 on success
* @retval 0 otherwise
*/
int elektraKeyToLongLong (const Key * key ELEKTRA_UNUSED, kdb_long_long_t * variable ELEKTRA_UNUSED)
{
#define TYPE_NAME LongLong
#define TYPE kdb_long_long_t
#define KDB_TYPE KDB_TYPE_LONG_LONG
#define PRE_CHECK_FAIL_BLOCK
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION_RANGE (value <= INT64_MAX && value >= INT64_MIN)
#define CHECK_FAIL_BLOCK
#define VALUE_TYPE long long
#define TO_VALUE (ELEKTRA_LONG_LONG_S (string, &end, 10))
#define NAME_MACRO(TYPE_NAME) CAT (elektraKeyTo, TYPE_NAME)
#define CODE_ONLY 1
#define KEY_PARAM_NAME key
#define VARIABLE_PARAM_NAME variable
#include "macros/type_create_to_value.h"
#undef KDB_TYPE
}
/**
* Converts a Key to unsigned_long_long.
*
* The variable pointed to by @p variable is unchanged,
* if an error occurs.
*
* @param key the key to convert
* @param variable pointer to the output variable
* @retval 1 on success
* @retval 0 otherwise
*/
int elektraKeyToUnsignedLongLong (const Key * key ELEKTRA_UNUSED, kdb_unsigned_long_long_t * variable ELEKTRA_UNUSED)
{
#define TYPE_NAME UnsignedLongLong
#define TYPE kdb_unsigned_long_long_t
#define KDB_TYPE KDB_TYPE_UNSIGNED_LONG_LONG
#define PRE_CHECK_BLOCK ELEKTRA_TYPE_NEGATIVE_PRE_CHECK_BLOCK
#define PRE_CHECK_CONVERSION (ELEKTRA_TYPE_NEGATIVE_PRE_CHECK)
#define PRE_CHECK_FAIL_BLOCK
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION_RANGE (value <= UINT64_MAX)
#define CHECK_FAIL_BLOCK
#define VALUE_TYPE unsigned long long
#define TO_VALUE (ELEKTRA_UNSIGNED_LONG_LONG_S (string, &end, 10))
#define NAME_MACRO(TYPE_NAME) CAT (elektraKeyTo, TYPE_NAME)
#define CODE_ONLY 1
#define KEY_PARAM_NAME key
#define VARIABLE_PARAM_NAME variable
#include "macros/type_create_to_value.h"
#undef KDB_TYPE
}
/**
* Converts a Key to float.
*
* The variable pointed to by @p variable is unchanged,
* if an error occurs.
*
* @param key the key to convert
* @param variable pointer to the output variable
* @retval 1 on success
* @retval 0 otherwise
*/
int elektraKeyToFloat (const Key * key ELEKTRA_UNUSED, kdb_float_t * variable ELEKTRA_UNUSED)
{
#define TYPE_NAME Float
#define TYPE kdb_float_t
#define KDB_TYPE KDB_TYPE_FLOAT
#define PRE_CHECK_FAIL_BLOCK
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION
#define CHECK_FAIL_BLOCK
#define TO_VALUE (strtof (string, &end))
#define NAME_MACRO(TYPE_NAME) CAT (elektraKeyTo, TYPE_NAME)
#define CODE_ONLY 1
#define KEY_PARAM_NAME key
#define VARIABLE_PARAM_NAME variable
#include "macros/type_create_to_value.h"
#undef KDB_TYPE
}
/**
* Converts a Key to double.
*
* The variable pointed to by @p variable is unchanged,
* if an error occurs.
*
* @param key the key to convert
* @param variable pointer to the output variable
* @retval 1 on success
* @retval 0 otherwise
*/
int elektraKeyToDouble (const Key * key ELEKTRA_UNUSED, kdb_double_t * variable ELEKTRA_UNUSED)
{
#define TYPE_NAME Double
#define TYPE kdb_double_t
#define KDB_TYPE KDB_TYPE_DOUBLE
#define PRE_CHECK_FAIL_BLOCK
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION
#define CHECK_FAIL_BLOCK
#define TO_VALUE (strtod (string, &end))
#define NAME_MACRO(TYPE_NAME) CAT (elektraKeyTo, TYPE_NAME)
#define CODE_ONLY 1
#define KEY_PARAM_NAME key
#define VARIABLE_PARAM_NAME variable
#include "macros/type_create_to_value.h"
#undef KDB_TYPE
}
#ifdef ELEKTRA_HAVE_KDB_LONG_DOUBLE
/**
* Converts a Key to long_double.
*
* The variable pointed to by @p variable is unchanged,
* if an error occurs.
*
* @param key the key to convert
* @param variable pointer to the output variable
* @retval 1 on success
* @retval 0 otherwise
*/
int elektraKeyToLongDouble (const Key * key ELEKTRA_UNUSED, kdb_long_double_t * variable ELEKTRA_UNUSED)
{
#define TYPE_NAME LongDouble
#define TYPE kdb_long_double_t
#define KDB_TYPE KDB_TYPE_LONG_DOUBLE
#define PRE_CHECK_FAIL_BLOCK
#define CHECK_CONVERSION ELEKTRA_TYPE_CHECK_CONVERSION
#define CHECK_FAIL_BLOCK
#define TO_VALUE (strtold (string, &end))
#define NAME_MACRO(TYPE_NAME) CAT (elektraKeyTo, TYPE_NAME)
#define CODE_ONLY 1
#define KEY_PARAM_NAME key
#define VARIABLE_PARAM_NAME variable
#include "macros/type_create_to_value.h"
#undef KDB_TYPE
}
#endif // ELEKTRA_HAVE_KDB_LONG_DOUBLE
/**
* Converts a boolean to string
*
* The string is allocated with elektraMalloc() and must
* be disposed of with elektraFree().
*
* @param value the boolean to convert
* @return a new string allocated with elektraMalloc, or 0 on error
*/
char * elektraBooleanToString (kdb_boolean_t value)
{
return elektraFormat ("%s", value ? "1" : "0");
}
/**
* Converts a char to string
*
* The string is allocated with elektraMalloc() and must
* be disposed of with elektraFree().
*
* @param value the value to convert
* @return a new string allocated with elektraMalloc, or 0 on error
*/
char * elektraCharToString (kdb_char_t value)
{
return elektraFormat ("%c", value);
}
/**
* Converts an octet to string
*
* The string is allocated with elektraMalloc() and must
* be disposed of with elektraFree().
*
* @param value the value to convert
* @return a new string allocated with elektraMalloc, or 0 on error
*/
char * elektraOctetToString (kdb_octet_t value)
{
return elektraFormat ("%d", value);
}
/**
* Converts a short to string
*
* The string is allocated with elektraMalloc() and must
* be disposed of with elektraFree().
*
* @param value the value to convert
* @return a new string allocated with elektraMalloc, or 0 on error
*/
char * elektraShortToString (kdb_short_t value)
{
return elektraFormat ("%d", value);
}
/**
* Converts an unsigned short to string
*
* The string is allocated with elektraMalloc() and must
* be disposed of with elektraFree().
*
* @param value the value to convert
* @return a new string allocated with elektraMalloc, or 0 on error
*/
char * elektraUnsignedShortToString (kdb_unsigned_short_t value)
{
return elektraFormat ("%d", value);
}
/**
* Converts a long to string
*
* The string is allocated with elektraMalloc() and must
* be disposed of with elektraFree().
*
* @param value the value to convert
* @return a new string allocated with elektraMalloc, or 0 on error
*/
char * elektraLongToString (kdb_long_t value)
{
return elektraFormat (ELEKTRA_LONG_F, value);
}
/**
* Converts an unsigned long to string
*
* The string is allocated with elektraMalloc() and must
* be disposed of with elektraFree().
*
* @param value the value to convert
* @return a new string allocated with elektraMalloc, or 0 on error
*/
char * elektraUnsignedLongToString (kdb_unsigned_long_t value)
{
return elektraFormat (ELEKTRA_UNSIGNED_LONG_F, value);
}
/**
* Converts a long long to string
*
* The string is allocated with elektraMalloc() and must
* be disposed of with elektraFree().
*
* @param value the value to convert
* @return a new string allocated with elektraMalloc, or 0 on error
*/
char * elektraLongLongToString (kdb_long_long_t value)
{
return elektraFormat (ELEKTRA_LONG_LONG_F, value);
}
/**
* Converts an unsigned long long to string
*
* The string is allocated with elektraMalloc() and must
* be disposed of with elektraFree().
*
* @param value the value to convert
* @return a new string allocated with elektraMalloc, or 0 on error
*/
char * elektraUnsignedLongLongToString (kdb_unsigned_long_long_t value)
{
return elektraFormat (ELEKTRA_UNSIGNED_LONG_LONG_F, value);
}
/**
* Converts a float to string
*
* The string is allocated with elektraMalloc() and must
* be disposed of with elektraFree().
*
* @param value the value to convert
* @return a new string allocated with elektraMalloc, or 0 on error
*/
char * elektraFloatToString (kdb_float_t value)
{
return elektraFormat ("%.9g", value);
}
/**
* Converts a double to string
*
* The string is allocated with elektraMalloc() and must
* be disposed of with elektraFree().
*
* @param value the value to convert
* @return a new string allocated with elektraMalloc, or 0 on error
*/
char * elektraDoubleToString (kdb_double_t value)
{
return elektraFormat ("%.17g", value);
}
#ifdef ELEKTRA_HAVE_KDB_LONG_DOUBLE
/**
* Converts a long double to string
*
* The string is allocated with elektraMalloc() and must
* be disposed of with elektraFree().
*
* @param value the value to convert
* @return a new string allocated with elektraMalloc, or 0 on error
*/
char * elektraLongDoubleToString (kdb_long_double_t value)
{
return elektraFormat ("%.21Lg", value);
}
#endif // ELEKTRA_HAVE_KDB_LONG_DOUBLE
#ifdef __cplusplus
};
#endif