Debian Codegraph @

src/basic/string-table.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <stdio.h>
5
6 #include "basic-forward.h"
7
8 const char* string_table_lookup_to_string(const char * const *table, size_t len, ssize_t i) _const_;
9
10 ssize_t string_table_lookup_from_string(const char * const *table, size_t len, const char *key) _pure_;
11 ssize_t string_table_lookup_from_string_with_boolean(const char * const *table, size_t len, const char *key, ssize_t yes) _pure_;
12
13 int string_table_lookup_to_string_fallback(const char * const *table, size_t len, ssize_t i, size_t max, char **ret);
14 ssize_t string_table_lookup_from_string_fallback(const char * const *table, size_t len, const char *s, size_t max);
15
16 /* For basic lookup tables with strictly enumerated entries */
17 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name, type, scope) \
18 scope const char* name##_to_string(type i) { \
19 return string_table_lookup_to_string(name##_table, ELEMENTSOF(name##_table), i); \
20 }
21
22 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name, type, scope) \
23 scope type name##_from_string(const char *s) { \
24 return (type) string_table_lookup_from_string(name##_table, ELEMENTSOF(name##_table), s); \
25 }
26
27 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name, type, yes, scope) \
28 scope type name##_from_string(const char *s) { \
29 return (type) string_table_lookup_from_string_with_boolean(name##_table, ELEMENTSOF(name##_table), s, yes); \
30 }
31
32 #define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name, type, max, scope) \
33 scope int name##_to_string_alloc(type i, char **ret) { \
34 return string_table_lookup_to_string_fallback(name##_table, ELEMENTSOF(name##_table), i, max, ret); \
35 }
36
37 #define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name, type, max, scope) \
38 scope type name##_from_string(const char *s) { \
39 return (type) string_table_lookup_from_string_fallback(name##_table, ELEMENTSOF(name##_table), s, max); \
40 }
41
42 #define _DEFINE_STRING_TABLE_LOOKUP(name, type, scope) \
43 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name, type, scope) \
44 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name, type, scope)
45
46 #define _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name, type, yes, scope) \
47 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name, type, scope) \
48 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name, type, yes, scope)
49
50 #define DEFINE_STRING_TABLE_LOOKUP(name, type) _DEFINE_STRING_TABLE_LOOKUP(name, type,)
51 #define DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name, type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name, type,)
52 #define DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name, type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name, type,)
53 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name, type) _DEFINE_STRING_TABLE_LOOKUP(name, type, static)
54 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name, type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name, type, static)
55 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name, type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name, type, static)
56
57 #define DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name, type, yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name, type, yes,)
58 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name, type, yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name, type, yes, static)
59 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name, type, yes) \
60 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name, type, yes, static)
61
62 /* For string conversions where numbers are also acceptable */
63 #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name, type, max) \
64 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name, type, max,) \
65 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name, type, max,)
66 #define DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_FALLBACK(name, type, max) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name, type, max,)
67
68 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name, type, max) \
69 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING_FALLBACK(name, type, max, static)
70 #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name, type, max) \
71 _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_FALLBACK(name, type, max, static)
72
73 #define DUMP_STRING_TABLE(name, type, max) \
74 ({ \
75 flockfile(stdout); \
76 for (type _k = 0; _k < (max); _k++) { \
77 const char *_t; \
78 _t = name##_to_string(_k); \
79 if (!_t) \
80 continue; \
81 fputs_unlocked(_t, stdout); \
82 fputc_unlocked('\n', stdout); \
83 } \
84 funlockfile(stdout); \
85 0; \
86 })