]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/string-table.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "parse-util.h"
12 #include "string-util.h"
14 ssize_t
string_table_lookup(const char * const *table
, size_t len
, const char *key
);
16 /* For basic lookup tables with strictly enumerated entries */
17 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
18 scope const char *name##_to_string(type i) { \
19 if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
21 return name##_table[i]; \
24 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
25 scope type name##_from_string(const char *s) { \
26 return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
29 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) \
30 scope type name##_from_string(const char *s) { \
34 b = parse_boolean(s); \
39 return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
42 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,scope) \
43 scope int name##_to_string_alloc(type i, char **str) { \
45 if (i < 0 || i > max) \
47 if (i < (type) ELEMENTSOF(name##_table) && name##_table[i]) { \
48 s = strdup(name##_table[i]); \
52 if (asprintf(&s, "%i", i) < 0) \
59 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,scope) \
60 scope type name##_from_string(const char *s) { \
65 i = (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
68 if (safe_atou(s, &u) >= 0 && u <= max) \
73 #define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
74 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
75 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)
77 #define _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,scope) \
78 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
79 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope)
81 #define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
82 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
83 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
84 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
86 #define DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,)
88 /* For string conversions where numbers are also acceptable */
89 #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
90 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,) \
91 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,)
93 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max) \
94 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,static)
95 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max) \
96 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,static)
98 #define DUMP_STRING_TABLE(name,type,max) \
102 for (_k = 0; _k < (max); _k++) { \
104 _t = name##_to_string(_k); \
107 fputs_unlocked(_t, stdout); \
108 fputc_unlocked('\n', stdout); \
110 funlockfile(stdout); \