]>
Commit | Line | Data |
---|---|---|
52ad194e | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
db2df898 | 2 | |
db2df898 MP |
3 | #pragma once |
4 | ||
4c89c718 | 5 | #include <errno.h> |
db2df898 MP |
6 | #include <stddef.h> |
7 | #include <stdio.h> | |
8 | #include <string.h> | |
9 | #include <sys/types.h> | |
10 | ||
11 | #include "macro.h" | |
12 | #include "parse-util.h" | |
13 | #include "string-util.h" | |
14 | ||
15 | ssize_t string_table_lookup(const char * const *table, size_t len, const char *key); | |
16 | ||
17 | /* For basic lookup tables with strictly enumerated entries */ | |
18 | #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \ | |
19 | scope const char *name##_to_string(type i) { \ | |
20 | if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \ | |
21 | return NULL; \ | |
22 | return name##_table[i]; \ | |
23 | } | |
24 | ||
25 | #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \ | |
26 | scope type name##_from_string(const char *s) { \ | |
27 | return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \ | |
28 | } | |
29 | ||
4c89c718 MP |
30 | #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) \ |
31 | scope type name##_from_string(const char *s) { \ | |
32 | int b; \ | |
5a920b42 MP |
33 | if (!s) \ |
34 | return -1; \ | |
4c89c718 MP |
35 | b = parse_boolean(s); \ |
36 | if (b == 0) \ | |
37 | return (type) 0; \ | |
38 | else if (b > 0) \ | |
39 | return yes; \ | |
40 | return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \ | |
41 | } | |
42 | ||
aa27b158 MP |
43 | #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,scope) \ |
44 | scope int name##_to_string_alloc(type i, char **str) { \ | |
db2df898 MP |
45 | char *s; \ |
46 | if (i < 0 || i > max) \ | |
47 | return -ERANGE; \ | |
48 | if (i < (type) ELEMENTSOF(name##_table)) { \ | |
49 | s = strdup(name##_table[i]); \ | |
50 | if (!s) \ | |
51 | return -ENOMEM; \ | |
52 | } else { \ | |
53 | if (asprintf(&s, "%i", i) < 0) \ | |
54 | return -ENOMEM; \ | |
55 | } \ | |
56 | *str = s; \ | |
57 | return 0; \ | |
aa27b158 MP |
58 | } |
59 | ||
60 | #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,scope) \ | |
6e866b33 | 61 | scope type name##_from_string(const char *s) { \ |
db2df898 | 62 | unsigned u = 0; \ |
bb4f798a | 63 | type i; \ |
db2df898 MP |
64 | if (!s) \ |
65 | return (type) -1; \ | |
bb4f798a MB |
66 | i = (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \ |
67 | if (i >= 0) \ | |
68 | return i; \ | |
db2df898 MP |
69 | if (safe_atou(s, &u) >= 0 && u <= max) \ |
70 | return (type) u; \ | |
71 | return (type) -1; \ | |
72 | } \ | |
aa27b158 | 73 | |
aa27b158 MP |
74 | #define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \ |
75 | _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \ | |
b012e921 | 76 | _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) |
aa27b158 MP |
77 | |
78 | #define _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,scope) \ | |
79 | _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \ | |
b012e921 | 80 | _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) |
aa27b158 MP |
81 | |
82 | #define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,) | |
83 | #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static) | |
84 | #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static) | |
85 | #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static) | |
86 | ||
87 | #define DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,) | |
88 | ||
89 | /* For string conversions where numbers are also acceptable */ | |
90 | #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \ | |
91 | _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,) \ | |
b012e921 | 92 | _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,) |
aa27b158 MP |
93 | |
94 | #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max) \ | |
95 | _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,static) | |
96 | #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max) \ | |
97 | _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,static) | |
b012e921 MB |
98 | |
99 | #define DUMP_STRING_TABLE(name,type,max) \ | |
100 | do { \ | |
101 | type _k; \ | |
102 | flockfile(stdout); \ | |
103 | for (_k = 0; _k < (max); _k++) { \ | |
104 | const char *_t; \ | |
105 | _t = name##_to_string(_k); \ | |
106 | if (!_t) \ | |
107 | continue; \ | |
108 | fputs_unlocked(_t, stdout); \ | |
109 | fputc_unlocked('\n', stdout); \ | |
110 | } \ | |
111 | funlockfile(stdout); \ | |
112 | } while(false) |