]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/string-table.h
Merge pull request #2921 from keszybz/do-not-report-masked-units-as-changed
[thirdparty/systemd.git] / src / basic / string-table.h
1
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2010 Lennart Poettering
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <errno.h>
24 #include <stddef.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/types.h>
28
29 #include "macro.h"
30 #include "parse-util.h"
31 #include "string-util.h"
32
33 ssize_t string_table_lookup(const char * const *table, size_t len, const char *key);
34
35 /* For basic lookup tables with strictly enumerated entries */
36 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
37 scope const char *name##_to_string(type i) { \
38 if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
39 return NULL; \
40 return name##_table[i]; \
41 }
42
43 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
44 scope type name##_from_string(const char *s) { \
45 return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
46 }
47
48 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) \
49 scope type name##_from_string(const char *s) { \
50 int b; \
51 b = parse_boolean(s); \
52 if (b == 0) \
53 return (type) 0; \
54 else if (b > 0) \
55 return yes; \
56 return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
57 }
58
59 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,scope) \
60 scope int name##_to_string_alloc(type i, char **str) { \
61 char *s; \
62 if (i < 0 || i > max) \
63 return -ERANGE; \
64 if (i < (type) ELEMENTSOF(name##_table)) { \
65 s = strdup(name##_table[i]); \
66 if (!s) \
67 return -ENOMEM; \
68 } else { \
69 if (asprintf(&s, "%i", i) < 0) \
70 return -ENOMEM; \
71 } \
72 *str = s; \
73 return 0; \
74 }
75
76 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,scope) \
77 type name##_from_string(const char *s) { \
78 type i; \
79 unsigned u = 0; \
80 if (!s) \
81 return (type) -1; \
82 for (i = 0; i < (type) ELEMENTSOF(name##_table); i++) \
83 if (streq_ptr(name##_table[i], s)) \
84 return i; \
85 if (safe_atou(s, &u) >= 0 && u <= max) \
86 return (type) u; \
87 return (type) -1; \
88 } \
89
90
91 #define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
92 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
93 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
94 struct __useless_struct_to_allow_trailing_semicolon__
95
96 #define _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,scope) \
97 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
98 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) \
99 struct __useless_struct_to_allow_trailing_semicolon__
100
101 #define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
102 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
103 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
104 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
105
106 #define DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,)
107
108 /* For string conversions where numbers are also acceptable */
109 #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
110 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,) \
111 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,) \
112 struct __useless_struct_to_allow_trailing_semicolon__
113
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)