Skip to content

Commit 081a0c7

Browse files
committed
volatile-mode: use the usual string table macros for implementing volatile_mode_from_string()
Let's shorten our code a bit, and also add the matching _to_string() call.
1 parent abdf29f commit 081a0c7

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

src/shared/volatile-util.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,10 @@
1111
#include "macro.h"
1212
#include "parse-util.h"
1313
#include "proc-cmdline.h"
14+
#include "string-table.h"
1415
#include "string-util.h"
1516
#include "volatile-util.h"
1617

17-
VolatileMode volatile_mode_from_string(const char *s) {
18-
int b;
19-
20-
if (isempty(s))
21-
return _VOLATILE_MODE_INVALID;
22-
23-
b = parse_boolean(s);
24-
if (b > 0)
25-
return VOLATILE_YES;
26-
if (b == 0)
27-
return VOLATILE_NO;
28-
29-
if (streq(s, "state"))
30-
return VOLATILE_STATE;
31-
32-
return _VOLATILE_MODE_INVALID;
33-
}
34-
3518
int query_volatile_mode(VolatileMode *ret) {
3619
_cleanup_free_ char *mode = NULL;
3720
VolatileMode m = VOLATILE_NO;
@@ -56,3 +39,11 @@ int query_volatile_mode(VolatileMode *ret) {
5639
*ret = m;
5740
return r;
5841
}
42+
43+
static const char* const volatile_mode_table[_VOLATILE_MODE_MAX] = {
44+
[VOLATILE_NO] = "no",
45+
[VOLATILE_YES] = "yes",
46+
[VOLATILE_STATE] = "state",
47+
};
48+
49+
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(volatile_mode, VolatileMode, VOLATILE_YES);

src/shared/volatile-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ typedef enum VolatileMode {
1616
} VolatileMode;
1717

1818
VolatileMode volatile_mode_from_string(const char *s);
19+
const char* volatile_mode_to_string(VolatileMode m);
1920

2021
int query_volatile_mode(VolatileMode *ret);

0 commit comments

Comments
 (0)