]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/string-table.h
core: reduce scope of variants
[thirdparty/systemd.git] / src / basic / string-table.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #pragma once
4
5 #include <errno.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <sys/types.h>
9
10 #include "macro.h"
11 #include "parse-util.h"
12 #include "string-util.h"
13
14 ssize_t string_table_lookup(const char * const *table, size_t len, const char *key);
15
16 /* For basic lookup tables with strictly enumerated entries */
17 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
18 scope const char *name##_to_string(type i) { \
19 if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
20 return NULL; \
21 return name##_table[i]; \
22 }
23
24 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
25 scope type name##_from_string(const char *s) { \
26 return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
27 }
28
29 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) \
30 scope type name##_from_string(const char *s) { \
31 int b; \
32 if (!s) \
33 return -1; \
34 b = parse_boolean(s); \
35 if (b == 0) \
36 return (type) 0; \
37 else if (b > 0) \
38 return yes; \
39 return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
40 }
41
42 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,scope) \
43 scope int name##_to_string_alloc(type i, char **str) { \
44 char *s; \
45 if (i < 0 || i > max) \
46 return -ERANGE; \
47 if (i < (type) ELEMENTSOF(name##_table) && name##_table[i]) { \
48 s = strdup(name##_table[i]); \
49 if (!s) \
50 return -ENOMEM; \
51 } else { \
52 if (asprintf(&s, "%i", i) < 0) \
53 return -ENOMEM; \
54 } \
55 *str = s; \
56 return 0; \
57 }
58
59 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,scope) \
60 scope type name##_from_string(const char *s) { \
61 unsigned u = 0; \
62 type i; \
63 if (!s) \
64 return (type) -1; \
65 i = (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
66 if (i >= 0) \
67 return i; \
68 if (safe_atou(s, &u) >= 0 && u <= max) \
69 return (type) u; \
70 return (type) -1; \
71 } \
72
73 #define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
74 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
75 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)
76
77 #define _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,scope) \
78 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
79 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope)
80
81 #define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
82 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
83 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
84 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
85
86 #define DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,)
87
88 /* For string conversions where numbers are also acceptable */
89 #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
90 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,) \
91 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,)
92
93 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max) \
94 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,static)
95 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max) \
96 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,static)
97
98 #define DUMP_STRING_TABLE(name,type,max) \
99 do { \
100 type _k; \
101 flockfile(stdout); \
102 for (_k = 0; _k < (max); _k++) { \
103 const char *_t; \
104 _t = name##_to_string(_k); \
105 if (!_t) \
106 continue; \
107 fputs_unlocked(_t, stdout); \
108 fputc_unlocked('\n', stdout); \
109 } \
110 funlockfile(stdout); \
111 } while(false)