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