]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
volatile-mode: use the usual string table macros for implementing volatile_mode_from_...
authorLennart Poettering <lennart@poettering.net>
Tue, 22 May 2018 10:09:59 +0000 (12:09 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 22 May 2018 11:14:18 +0000 (13:14 +0200)
Let's shorten our code a bit, and also add the matching _to_string()
call.

src/shared/volatile-util.c
src/shared/volatile-util.h

index d70d5856d328069ee0f78c82389d2bd5f9df086d..a20f0b43f45bd68dba1b3f5084364205ea96d794 100644 (file)
 #include "macro.h"
 #include "parse-util.h"
 #include "proc-cmdline.h"
+#include "string-table.h"
 #include "string-util.h"
 #include "volatile-util.h"
 
-VolatileMode volatile_mode_from_string(const char *s) {
-        int b;
-
-        if (isempty(s))
-                return _VOLATILE_MODE_INVALID;
-
-        b = parse_boolean(s);
-        if (b > 0)
-                return VOLATILE_YES;
-        if (b == 0)
-                return VOLATILE_NO;
-
-        if (streq(s, "state"))
-                return VOLATILE_STATE;
-
-        return _VOLATILE_MODE_INVALID;
-}
-
 int query_volatile_mode(VolatileMode *ret) {
         _cleanup_free_ char *mode = NULL;
         VolatileMode m = VOLATILE_NO;
@@ -56,3 +39,11 @@ finish:
         *ret = m;
         return r;
 }
+
+static const char* const volatile_mode_table[_VOLATILE_MODE_MAX] = {
+        [VOLATILE_NO] = "no",
+        [VOLATILE_YES] = "yes",
+        [VOLATILE_STATE] = "state",
+};
+
+DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(volatile_mode, VolatileMode, VOLATILE_YES);
index b275bff1767cc2c22eea1019c74cb024850d2ea6..4312c97a995b20f44936a6b14a7ffd244e6176bf 100644 (file)
@@ -16,5 +16,6 @@ typedef enum VolatileMode {
 } VolatileMode;
 
 VolatileMode volatile_mode_from_string(const char *s);
+const char* volatile_mode_to_string(VolatileMode m);
 
 int query_volatile_mode(VolatileMode *ret);