1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 This file is part of systemd.
8 Copyright 2010 Lennart Poettering
15 #include <sys/types.h>
18 #include "parse-util.h"
19 #include "string-util.h"
21 ssize_t string_table_lookup(const char * const *table, size_t len, const char *key);
23 /* For basic lookup tables with strictly enumerated entries */
24 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
25 scope const char *name##_to_string(type i) { \
26 if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
28 return name##_table[i]; \
31 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
32 scope type name##_from_string(const char *s) { \
33 return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
36 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) \
37 scope type name##_from_string(const char *s) { \
41 b = parse_boolean(s); \
46 return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
49 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,scope) \
50 scope int name##_to_string_alloc(type i, char **str) { \
52 if (i < 0 || i > max) \
54 if (i < (type) ELEMENTSOF(name##_table)) { \
55 s = strdup(name##_table[i]); \
59 if (asprintf(&s, "%i", i) < 0) \
66 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,scope) \
67 type name##_from_string(const char *s) { \
72 for (i = 0; i < (type) ELEMENTSOF(name##_table); i++) \
73 if (streq_ptr(name##_table[i], s)) \
75 if (safe_atou(s, &u) >= 0 && u <= max) \
81 #define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
82 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
83 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)
85 #define _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,scope) \
86 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
87 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope)
89 #define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
90 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
91 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
92 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
94 #define DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,)
96 /* For string conversions where numbers are also acceptable */
97 #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
98 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,) \
99 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,)
101 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max) \
102 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,static)
103 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max) \
104 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,static)
106 #define DUMP_STRING_TABLE(name,type,max) \
110 for (_k = 0; _k < (max); _k++) { \
112 _t = name##_to_string(_k); \
115 fputs_unlocked(_t, stdout); \
116 fputc_unlocked('\n', stdout); \
118 funlockfile(stdout); \