]>
Commit | Line | Data |
---|---|---|
8b43440b | 1 | |
8b43440b LP |
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 | ||
11c3a366 | 23 | #include <errno.h> |
8b43440b LP |
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 | ||
b18b8662 LP |
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; \ | |
35ca4ce0 LP |
51 | if (!s) \ |
52 | return -1; \ | |
b18b8662 LP |
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 | ||
8e98476e ZJS |
61 | #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,scope) \ |
62 | scope int name##_to_string_alloc(type i, char **str) { \ | |
8b43440b LP |
63 | char *s; \ |
64 | if (i < 0 || i > max) \ | |
65 | return -ERANGE; \ | |
66 | if (i < (type) ELEMENTSOF(name##_table)) { \ | |
67 | s = strdup(name##_table[i]); \ | |
68 | if (!s) \ | |
69 | return -ENOMEM; \ | |
70 | } else { \ | |
71 | if (asprintf(&s, "%i", i) < 0) \ | |
72 | return -ENOMEM; \ | |
73 | } \ | |
74 | *str = s; \ | |
75 | return 0; \ | |
8e98476e ZJS |
76 | } |
77 | ||
78 | #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,scope) \ | |
8b43440b LP |
79 | type name##_from_string(const char *s) { \ |
80 | type i; \ | |
81 | unsigned u = 0; \ | |
82 | if (!s) \ | |
83 | return (type) -1; \ | |
84 | for (i = 0; i < (type) ELEMENTSOF(name##_table); i++) \ | |
85 | if (streq_ptr(name##_table[i], s)) \ | |
86 | return i; \ | |
87 | if (safe_atou(s, &u) >= 0 && u <= max) \ | |
88 | return (type) u; \ | |
89 | return (type) -1; \ | |
90 | } \ | |
8e98476e ZJS |
91 | |
92 | ||
93 | #define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \ | |
94 | _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \ | |
95 | _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \ | |
96 | struct __useless_struct_to_allow_trailing_semicolon__ | |
97 | ||
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) \ | |
101 | struct __useless_struct_to_allow_trailing_semicolon__ | |
102 | ||
103 | #define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,) | |
104 | #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static) | |
105 | #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static) | |
106 | #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static) | |
107 | ||
108 | #define DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,) | |
109 | ||
110 | /* For string conversions where numbers are also acceptable */ | |
111 | #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \ | |
112 | _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,) \ | |
113 | _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,) \ | |
8b43440b | 114 | struct __useless_struct_to_allow_trailing_semicolon__ |
8e98476e ZJS |
115 | |
116 | #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max) \ | |
117 | _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,static) | |
118 | #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max) \ | |
119 | _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,static) |