]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - src/basic/string-table.h
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / basic / string-table.h
... / ...
CommitLineData
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
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
24#include <errno.h>
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
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; \
52 if (!s) \
53 return -1; \
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
62#define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name,type,max,scope) \
63 scope int name##_to_string_alloc(type i, char **str) { \
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; \
77 }
78
79#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name,type,max,scope) \
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 } \
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,) \
115 struct __useless_struct_to_allow_trailing_semicolon__
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)