]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/string-table.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
12 #include "parse-util.h"
13 #include "string-util.h"
15 ssize_t
string_table_lookup(const char * const *table
, size_t len
, const char *key
);
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)) \
22 return name##_table[i]; \
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); \
30 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) \
31 scope type name##_from_string(const char *s) { \
35 b = parse_boolean(s); \
40 return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
43 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,scope) \
44 scope int name##_to_string_alloc(type i, char **str) { \
46 if (i < 0 || i > max) \
48 if (i < (type) ELEMENTSOF(name##_table)) { \
49 s = strdup(name##_table[i]); \
53 if (asprintf(&s, "%i", i) < 0) \
60 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,scope) \
61 scope type name##_from_string(const char *s) { \
66 for (i = 0; i < (type) ELEMENTSOF(name##_table); i++) \
67 if (streq_ptr(name##_table[i], s)) \
69 if (safe_atou(s, &u) >= 0 && u <= max) \
74 #define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
75 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
76 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)
78 #define _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,scope) \
79 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
80 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope)
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)
87 #define DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,)
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,) \
92 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,)
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)
99 #define DUMP_STRING_TABLE(name,type,max) \
103 for (_k = 0; _k < (max); _k++) { \
105 _t = name##_to_string(_k); \
108 fputs_unlocked(_t, stdout); \
109 fputc_unlocked('\n', stdout); \
111 funlockfile(stdout); \