]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/string-table.h
e78a6dbdef1e41b0c4665130390ae02897e0f53c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 This file is part of systemd.
8 Copyright 2010 Lennart Poettering
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include <sys/types.h>
31 #include "parse-util.h"
32 #include "string-util.h"
34 ssize_t
string_table_lookup(const char * const *table
, size_t len
, const char *key
);
36 /* For basic lookup tables with strictly enumerated entries */
37 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
38 scope const char *name##_to_string(type i) { \
39 if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
41 return name##_table[i]; \
44 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
45 scope type name##_from_string(const char *s) { \
46 return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
49 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) \
50 scope type name##_from_string(const char *s) { \
54 b = parse_boolean(s); \
59 return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
62 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,scope) \
63 scope int name##_to_string_alloc(type i, char **str) { \
65 if (i < 0 || i > max) \
67 if (i < (type) ELEMENTSOF(name##_table)) { \
68 s = strdup(name##_table[i]); \
72 if (asprintf(&s, "%i", i) < 0) \
79 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,scope) \
80 type name##_from_string(const char *s) { \
85 for (i = 0; i < (type) ELEMENTSOF(name##_table); i++) \
86 if (streq_ptr(name##_table[i], s)) \
88 if (safe_atou(s, &u) >= 0 && u <= max) \
94 #define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
95 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
96 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)
98 #define _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,scope) \
99 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
100 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope)
102 #define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
103 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
104 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
105 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
107 #define DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,)
109 /* For string conversions where numbers are also acceptable */
110 #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
111 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,) \
112 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,)
114 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max) \
115 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,static)
116 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max) \
117 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,static)