]> git.proxmox.com Git - systemd.git/blame - src/basic/string-table.h
Imported Upstream version 229
[systemd.git] / src / basic / string-table.h
CommitLineData
db2df898 1
db2df898
MP
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
4c89c718 23#include <errno.h>
db2df898
MP
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
33ssize_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
4c89c718
MP
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
db2df898
MP
59#define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
60 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
61 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
62 struct __useless_struct_to_allow_trailing_semicolon__
63
4c89c718
MP
64#define _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,scope) \
65 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
66 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) \
67 struct __useless_struct_to_allow_trailing_semicolon__
68
db2df898
MP
69#define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
70#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
71#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
72#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
73
4c89c718
MP
74#define DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,)
75
db2df898
MP
76/* For string conversions where numbers are also acceptable */
77#define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
78 int name##_to_string_alloc(type i, char **str) { \
79 char *s; \
80 if (i < 0 || i > max) \
81 return -ERANGE; \
82 if (i < (type) ELEMENTSOF(name##_table)) { \
83 s = strdup(name##_table[i]); \
84 if (!s) \
85 return -ENOMEM; \
86 } else { \
87 if (asprintf(&s, "%i", i) < 0) \
88 return -ENOMEM; \
89 } \
90 *str = s; \
91 return 0; \
92 } \
93 type name##_from_string(const char *s) { \
94 type i; \
95 unsigned u = 0; \
96 if (!s) \
97 return (type) -1; \
98 for (i = 0; i < (type) ELEMENTSOF(name##_table); i++) \
99 if (streq_ptr(name##_table[i], s)) \
100 return i; \
101 if (safe_atou(s, &u) >= 0 && u <= max) \
102 return (type) u; \
103 return (type) -1; \
104 } \
105 struct __useless_struct_to_allow_trailing_semicolon__