1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 Copyright 2010 Lennart Poettering
13 #include <sys/types.h>
16 #include "parse-util.h"
17 #include "string-util.h"
19 ssize_t string_table_lookup(const char * const *table, size_t len, const char *key);
21 /* For basic lookup tables with strictly enumerated entries */
22 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
23 scope const char *name##_to_string(type i) { \
24 if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
26 return name##_table[i]; \
29 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
30 scope type name##_from_string(const char *s) { \
31 return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
34 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) \
35 scope type name##_from_string(const char *s) { \
39 b = parse_boolean(s); \
44 return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
47 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,scope) \
48 scope int name##_to_string_alloc(type i, char **str) { \
50 if (i < 0 || i > max) \
52 if (i < (type) ELEMENTSOF(name##_table)) { \
53 s = strdup(name##_table[i]); \
57 if (asprintf(&s, "%i", i) < 0) \
64 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,scope) \
65 type name##_from_string(const char *s) { \
70 for (i = 0; i < (type) ELEMENTSOF(name##_table); i++) \
71 if (streq_ptr(name##_table[i], s)) \
73 if (safe_atou(s, &u) >= 0 && u <= max) \
78 #define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
79 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
80 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)
82 #define _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,scope) \
83 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
84 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope)
86 #define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
87 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
88 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
89 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
91 #define DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,)
93 /* For string conversions where numbers are also acceptable */
94 #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
95 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,) \
96 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,)
98 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max) \
99 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,static)
100 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max) \
101 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,static)
103 #define DUMP_STRING_TABLE(name,type,max) \
107 for (_k = 0; _k < (max); _k++) { \
109 _t = name##_to_string(_k); \
112 fputs_unlocked(_t, stdout); \
113 fputc_unlocked('\n', stdout); \
115 funlockfile(stdout); \