8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering This file is part of systemd.
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering Copyright 2010 Lennart Poettering
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering systemd is free software; you can redistribute it and/or modify it
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering under the terms of the GNU Lesser General Public License as published by
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering (at your option) any later version.
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering systemd is distributed in the hope that it will be useful, but
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering Lesser General Public License for more details.
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering You should have received a copy of the GNU Lesser General Public License
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poetteringssize_t string_table_lookup(const char * const *table, size_t len, const char *key);
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering/* For basic lookup tables with strictly enumerated entries */
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering#define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering scope const char *name##_to_string(type i) { \
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering scope type name##_from_string(const char *s) { \
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
b18b8662154b46f03ddeac00e52d157c87d474e3Lennart Poettering#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) \
b18b8662154b46f03ddeac00e52d157c87d474e3Lennart Poettering scope type name##_from_string(const char *s) { \
b18b8662154b46f03ddeac00e52d157c87d474e3Lennart Poettering else if (b > 0) \
b18b8662154b46f03ddeac00e52d157c87d474e3Lennart Poettering return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering#define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering struct __useless_struct_to_allow_trailing_semicolon__
b18b8662154b46f03ddeac00e52d157c87d474e3Lennart Poettering#define _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,scope) \
b18b8662154b46f03ddeac00e52d157c87d474e3Lennart Poettering _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \
b18b8662154b46f03ddeac00e52d157c87d474e3Lennart Poettering _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(name,type,yes,scope) \
b18b8662154b46f03ddeac00e52d157c87d474e3Lennart Poettering struct __useless_struct_to_allow_trailing_semicolon__
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering#define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
b18b8662154b46f03ddeac00e52d157c87d474e3Lennart Poettering#define DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes) _DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(name,type,yes,)
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering/* For string conversions where numbers are also acceptable */
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering#define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering int name##_to_string_alloc(type i, char **str) { \
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering if (i < 0 || i > max) \
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering if (i < (type) ELEMENTSOF(name##_table)) { \
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering type name##_from_string(const char *s) { \
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering unsigned u = 0; \
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering for (i = 0; i < (type) ELEMENTSOF(name##_table); i++) \