chiark / gitweb /
Prep v228: Add remaining updates from upstream (3/3)
[elogind.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 <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
34 ssize_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(name,type,scope)                    \
50         _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope)          \
51         _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)        \
52         struct __useless_struct_to_allow_trailing_semicolon__
53
54 #define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
55 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
56 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
57 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
58
59 /* For string conversions where numbers are also acceptable */
60 #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max)         \
61         int name##_to_string_alloc(type i, char **str) {                \
62                 char *s;                                                \
63                 if (i < 0 || i > max)                                   \
64                         return -ERANGE;                                 \
65                 if (i < (type) ELEMENTSOF(name##_table)) {              \
66                         s = strdup(name##_table[i]);                    \
67                         if (!s)                                         \
68                                 return -ENOMEM;                         \
69                 } else {                                                \
70                         if (asprintf(&s, "%i", i) < 0)                  \
71                                 return -ENOMEM;                         \
72                 }                                                       \
73                 *str = s;                                               \
74                 return 0;                                               \
75         }                                                               \
76         type name##_from_string(const char *s) {                        \
77                 type i;                                                 \
78                 unsigned u = 0;                                         \
79                 if (!s)                                                 \
80                         return (type) -1;                               \
81                 for (i = 0; i < (type) ELEMENTSOF(name##_table); i++)   \
82                         if (streq_ptr(name##_table[i], s))              \
83                                 return i;                               \
84                 if (safe_atou(s, &u) >= 0 && u <= max)                  \
85                         return (type) u;                                \
86                 return (type) -1;                                       \
87         }                                                               \
88         struct __useless_struct_to_allow_trailing_semicolon__