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) \
80 #define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
81 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
82 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)
84 #define _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,scope) \
85 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
86 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope)
88 #define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
89 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
90 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
91 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
93 #define DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,)
95 /* For string conversions where numbers are also acceptable */
96 #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
97 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,) \
98 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,)
100 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max) \
101 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,static)
102 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max) \
103 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,static)
105 #define DUMP_STRING_TABLE(name,type,max) \
109 for (_k = 0; _k < (max); _k++) { \
111 _t = name##_to_string(_k); \
114 fputs_unlocked(_t, stdout); \
115 fputc_unlocked('\n', stdout); \
117 funlockfile(stdout); \