diff options
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/cgroup-util.c | 1 | ||||
-rw-r--r-- | src/basic/locale-util.c | 1 | ||||
-rw-r--r-- | src/basic/log.c | 1 | ||||
-rw-r--r-- | src/basic/rlimit-util.c | 1 | ||||
-rw-r--r-- | src/basic/signal-util.c | 1 | ||||
-rw-r--r-- | src/basic/smack-util.c | 1 | ||||
-rw-r--r-- | src/basic/socket-util.c | 1 | ||||
-rw-r--r-- | src/basic/string-table.c | 35 | ||||
-rw-r--r-- | src/basic/string-table.h | 88 | ||||
-rw-r--r-- | src/basic/unit-name.c | 5 | ||||
-rw-r--r-- | src/basic/util.c | 14 | ||||
-rw-r--r-- | src/basic/util.h | 56 | ||||
-rw-r--r-- | src/basic/virt.c | 1 |
13 files changed, 135 insertions, 71 deletions
diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c index 84aea8afb8..d416b0b41e 100644 --- a/src/basic/cgroup-util.c +++ b/src/basic/cgroup-util.c @@ -44,6 +44,7 @@ #include "process-util.h" #include "set.h" #include "special.h" +#include "string-table.h" #include "string-util.h" #include "unit-name.h" #include "user-util.h" diff --git a/src/basic/locale-util.c b/src/basic/locale-util.c index 08da4b98c5..9db906316b 100644 --- a/src/basic/locale-util.c +++ b/src/basic/locale-util.c @@ -26,6 +26,7 @@ #include "locale-util.h" #include "path-util.h" #include "set.h" +#include "string-table.h" #include "string-util.h" #include "strv.h" #include "utf8.h" diff --git a/src/basic/log.c b/src/basic/log.c index 2c0abdc0bd..dcb24cfab7 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -39,6 +39,7 @@ #include "process-util.h" #include "signal-util.h" #include "socket-util.h" +#include "string-table.h" #include "string-util.h" #include "terminal-util.h" #include "util.h" diff --git a/src/basic/rlimit-util.c b/src/basic/rlimit-util.c index 7f9d63224d..2627c813fc 100644 --- a/src/basic/rlimit-util.c +++ b/src/basic/rlimit-util.c @@ -21,6 +21,7 @@ #include "missing.h" #include "rlimit-util.h" +#include "string-table.h" #include "util.h" int setrlimit_closest(int resource, const struct rlimit *rlim) { diff --git a/src/basic/signal-util.c b/src/basic/signal-util.c index e5ac6317d4..8038bc891d 100644 --- a/src/basic/signal-util.c +++ b/src/basic/signal-util.c @@ -21,6 +21,7 @@ #include "parse-util.h" #include "signal-util.h" +#include "string-table.h" #include "string-util.h" #include "util.h" diff --git a/src/basic/smack-util.c b/src/basic/smack-util.c index 5ada621ca3..a96fb5b594 100644 --- a/src/basic/smack-util.c +++ b/src/basic/smack-util.c @@ -27,6 +27,7 @@ #include "path-util.h" #include "process-util.h" #include "smack-util.h" +#include "string-table.h" #include "util.h" #include "xattr-util.h" diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index c4af6aa941..71a52a840a 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -38,6 +38,7 @@ #include "parse-util.h" #include "path-util.h" #include "socket-util.h" +#include "string-table.h" #include "string-util.h" #include "util.h" diff --git a/src/basic/string-table.c b/src/basic/string-table.c new file mode 100644 index 0000000000..a860324fc9 --- /dev/null +++ b/src/basic/string-table.c @@ -0,0 +1,35 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include "string-table.h" + +ssize_t string_table_lookup(const char * const *table, size_t len, const char *key) { + size_t i; + + if (!key) + return -1; + + for (i = 0; i < len; ++i) + if (streq_ptr(table[i], key)) + return (ssize_t) i; + + return -1; +} diff --git a/src/basic/string-table.h b/src/basic/string-table.h new file mode 100644 index 0000000000..51b6007214 --- /dev/null +++ b/src/basic/string-table.h @@ -0,0 +1,88 @@ + +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +#pragma once + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <stddef.h> +#include <stdio.h> +#include <string.h> +#include <sys/types.h> + +#include "macro.h" +#include "parse-util.h" +#include "string-util.h" + +ssize_t string_table_lookup(const char * const *table, size_t len, const char *key); + +/* For basic lookup tables with strictly enumerated entries */ +#define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \ + scope const char *name##_to_string(type i) { \ + if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \ + return NULL; \ + return name##_table[i]; \ + } + +#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \ + scope type name##_from_string(const char *s) { \ + return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \ + } + +#define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \ + _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \ + _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \ + struct __useless_struct_to_allow_trailing_semicolon__ + +#define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,) +#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static) +#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static) +#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static) + +/* For string conversions where numbers are also acceptable */ +#define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \ + int name##_to_string_alloc(type i, char **str) { \ + char *s; \ + if (i < 0 || i > max) \ + return -ERANGE; \ + if (i < (type) ELEMENTSOF(name##_table)) { \ + s = strdup(name##_table[i]); \ + if (!s) \ + return -ENOMEM; \ + } else { \ + if (asprintf(&s, "%i", i) < 0) \ + return -ENOMEM; \ + } \ + *str = s; \ + return 0; \ + } \ + type name##_from_string(const char *s) { \ + type i; \ + unsigned u = 0; \ + if (!s) \ + return (type) -1; \ + for (i = 0; i < (type) ELEMENTSOF(name##_table); i++) \ + if (streq_ptr(name##_table[i], s)) \ + return i; \ + if (safe_atou(s, &u) >= 0 && u <= max) \ + return (type) u; \ + return (type) -1; \ + } \ + struct __useless_struct_to_allow_trailing_semicolon__ diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c index c6ba9afb5a..43f52ce7c8 100644 --- a/src/basic/unit-name.c +++ b/src/basic/unit-name.c @@ -24,12 +24,13 @@ #include "bus-label.h" #include "def.h" +#include "hexdecoct.h" #include "path-util.h" +#include "string-table.h" #include "string-util.h" #include "strv.h" -#include "util.h" #include "unit-name.h" -#include "hexdecoct.h" +#include "util.h" #define VALID_CHARS \ DIGITS LETTERS \ diff --git a/src/basic/util.c b/src/basic/util.c index 30691dea6b..0878adbfac 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -94,6 +94,7 @@ #include "random-util.h" #include "signal-util.h" #include "sparse-endian.h" +#include "string-table.h" #include "string-util.h" #include "strv.h" #include "terminal-util.h" @@ -1450,19 +1451,6 @@ int syslog_parse_priority(const char **p, int *priority, bool with_facility) { return 1; } -ssize_t string_table_lookup(const char * const *table, size_t len, const char *key) { - size_t i; - - if (!key) - return -1; - - for (i = 0; i < len; ++i) - if (streq_ptr(table[i], key)) - return (ssize_t) i; - - return -1; -} - int version(void) { puts(PACKAGE_STRING "\n" SYSTEMD_FEATURES); diff --git a/src/basic/util.h b/src/basic/util.h index 6c6920c2a4..d18b151d88 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -83,62 +83,6 @@ static inline const char* one_zero(bool b) { return b ? "1" : "0"; } -/* For basic lookup tables with strictly enumerated entries */ -#define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \ - scope const char *name##_to_string(type i) { \ - if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \ - return NULL; \ - return name##_table[i]; \ - } - -ssize_t string_table_lookup(const char * const *table, size_t len, const char *key); - -#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \ - scope type name##_from_string(const char *s) { \ - return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \ - } - -#define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \ - _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \ - _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \ - struct __useless_struct_to_allow_trailing_semicolon__ - -#define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,) -#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static) -#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static) -#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static) - -/* For string conversions where numbers are also acceptable */ -#define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \ - int name##_to_string_alloc(type i, char **str) { \ - char *s; \ - if (i < 0 || i > max) \ - return -ERANGE; \ - if (i < (type) ELEMENTSOF(name##_table)) { \ - s = strdup(name##_table[i]); \ - if (!s) \ - return -ENOMEM; \ - } else { \ - if (asprintf(&s, "%i", i) < 0) \ - return -ENOMEM; \ - } \ - *str = s; \ - return 0; \ - } \ - type name##_from_string(const char *s) { \ - type i; \ - unsigned u = 0; \ - if (!s) \ - return (type) -1; \ - for (i = 0; i < (type) ELEMENTSOF(name##_table); i++) \ - if (streq_ptr(name##_table[i], s)) \ - return i; \ - if (safe_atou(s, &u) >= 0 && u <= max) \ - return (type) u; \ - return (type) -1; \ - } \ - struct __useless_struct_to_allow_trailing_semicolon__ - bool fstype_is_network(const char *fstype); #define xsprintf(buf, fmt, ...) \ diff --git a/src/basic/virt.c b/src/basic/virt.c index 9267a2730b..9076cf618f 100644 --- a/src/basic/virt.c +++ b/src/basic/virt.c @@ -25,6 +25,7 @@ #include "fileio.h" #include "process-util.h" +#include "string-table.h" #include "string-util.h" #include "util.h" #include "virt.h" |