]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-settings: Add settings_get_bool
authorAki Tuomi <aki.tuomi@open-xchange.com>
Thu, 16 Sep 2021 08:53:20 +0000 (11:53 +0300)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Mon, 17 Jan 2022 11:52:09 +0000 (13:52 +0200)
Parses string as boolean. Accepts yes/y/1 and no.

Preferred ones are 'yes' and 'no'.

src/lib-settings/settings-parser.c
src/lib-settings/settings-parser.h

index 7b63b30f25f4145f8498f9e46c7687b6b76b19f2..0fb287c8ba0560d54b90c853dc1f334723fc8de1 100644 (file)
@@ -299,8 +299,8 @@ setting_define_find(const struct setting_parser_info *info, const char *key)
        return NULL;
 }
 
-static int
-get_bool(struct setting_parser_context *ctx, const char *value, bool *result_r)
+int settings_get_bool(const char *value, bool *result_r,
+                     const char **error_r)
 {
        /* FIXME: eventually we'd want to support only yes/no */
        if (strcasecmp(value, "yes") == 0 ||
@@ -309,14 +309,23 @@ get_bool(struct setting_parser_context *ctx, const char *value, bool *result_r)
        else if (strcasecmp(value, "no") == 0)
                *result_r = FALSE;
        else {
-               ctx->error = p_strdup_printf(ctx->parser_pool,
-                       "Invalid boolean value: %s (use yes or no)", value);
+               *error_r = t_strdup_printf("Invalid boolean value: %s (use yes or no)",
+                                          value);
                return -1;
        }
 
        return 0;
 }
 
+static int
+get_bool(struct setting_parser_context *ctx, const char *value, bool *result_r)
+{
+       int ret;
+       if ((ret = settings_get_bool(value, result_r, &ctx->error)) < 0)
+               ctx->error = p_strdup(ctx->parser_pool, ctx->error);
+       return ret;
+}
+
 static int
 get_uint(struct setting_parser_context *ctx, const char *value,
         unsigned int *result_r)
index d7ffcb593a79d179c7e22fae78214017280ba15e..21d00604d6cf1d4b2e98785b800785a0fd02801e 100644 (file)
@@ -277,5 +277,7 @@ int settings_get_time_msecs(const char *str, unsigned int *msecs_r,
 /* Parse size string, return as bytes. */
 int settings_get_size(const char *str, uoff_t *bytes_r,
                      const char **error_r);
-
+/* Parse boolean string, return as boolean */
+int settings_get_bool(const char *value, bool *result_r,
+                     const char **error_r);
 #endif