]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/string-table.h
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / basic / string-table.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
8b43440b 2
8b43440b
LP
3#pragma once
4
5/***
6 This file is part of systemd.
7
8 Copyright 2010 Lennart Poettering
9
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.
14
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.
19
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/>.
22***/
23
11c3a366 24#include <errno.h>
8b43440b
LP
25#include <stddef.h>
26#include <stdio.h>
27#include <string.h>
28#include <sys/types.h>
29
30#include "macro.h"
31#include "parse-util.h"
32#include "string-util.h"
33
34ssize_t string_table_lookup(const char * const *table, size_t len, const char *key);
35
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)) \
40 return NULL; \
41 return name##_table[i]; \
42 }
43
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); \
47 }
48
b18b8662
LP
49#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) \
50 scope type name##_from_string(const char *s) { \
51 int b; \
35ca4ce0
LP
52 if (!s) \
53 return -1; \
b18b8662
LP
54 b = parse_boolean(s); \
55 if (b == 0) \
56 return (type) 0; \
57 else if (b > 0) \
58 return yes; \
59 return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
60 }
61
8e98476e
ZJS
62#define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,scope) \
63 scope int name##_to_string_alloc(type i, char **str) { \
8b43440b
LP
64 char *s; \
65 if (i < 0 || i > max) \
66 return -ERANGE; \
67 if (i < (type) ELEMENTSOF(name##_table)) { \
68 s = strdup(name##_table[i]); \
69 if (!s) \
70 return -ENOMEM; \
71 } else { \
72 if (asprintf(&s, "%i", i) < 0) \
73 return -ENOMEM; \
74 } \
75 *str = s; \
76 return 0; \
8e98476e
ZJS
77 }
78
79#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,scope) \
8b43440b
LP
80 type name##_from_string(const char *s) { \
81 type i; \
82 unsigned u = 0; \
83 if (!s) \
84 return (type) -1; \
85 for (i = 0; i < (type) ELEMENTSOF(name##_table); i++) \
86 if (streq_ptr(name##_table[i], s)) \
87 return i; \
88 if (safe_atou(s, &u) >= 0 && u <= max) \
89 return (type) u; \
90 return (type) -1; \
91 } \
8e98476e
ZJS
92
93
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) \
97 struct __useless_struct_to_allow_trailing_semicolon__
98
99#define _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,scope) \
100 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
101 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) \
102 struct __useless_struct_to_allow_trailing_semicolon__
103
104#define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
105#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
106#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
107#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
108
109#define DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,)
110
111/* For string conversions where numbers are also acceptable */
112#define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
113 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,) \
114 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,) \
8b43440b 115 struct __useless_struct_to_allow_trailing_semicolon__
8e98476e
ZJS
116
117#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max) \
118 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,static)
119#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max) \
120 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,static)