Hello,

attached patch improves "settings" code to handle boolean yes/no parameters.

Comments are welcomed.

Regards, Adam
>From 2e21de21d5c7b67f6117689f401234d65bfe59fd Mon Sep 17 00:00:00 2001
From: Adam Tkac <at...@redhat.com>
Date: Wed, 29 Jun 2011 18:34:25 +0200
Subject: [PATCH] Improve settings_t code to handle yes/no boolean values.

Signed-off-by: Adam Tkac <at...@redhat.com>
---
 src/settings.c |   28 ++++++++++++++++++++++++----
 src/settings.h |    6 ++++++
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/src/settings.c b/src/settings.c
index aa29a18..de9c773 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -24,6 +24,7 @@
 
 #include <ctype.h>
 #include <stdlib.h>
+#include <strings.h>
 
 #include "log.h"
 #include "settings.h"
@@ -110,10 +111,12 @@ set_value(setting_t *setting, const char *value)
        isc_result_t result;
        int numeric_value;
 
-       if (setting->type == ST_LD_STRING) {
+       switch (setting->type) {
+       case ST_LD_STRING:
                CHECK(str_init_char((ld_string_t *)setting->target, value));
-       } else if (setting->type == ST_SIGNED_INTEGER ||
-                  setting->type == ST_UNSIGNED_INTEGER) {
+               break;
+       case ST_SIGNED_INTEGER:
+       case ST_UNSIGNED_INTEGER:
                if (*value == '\0') {
                        result = ISC_R_FAILURE;
                        goto cleanup;
@@ -130,7 +133,20 @@ set_value(setting_t *setting, const char *value)
                } else {
                        (*(unsigned *)setting->target) = 
(unsigned)numeric_value;
                }
-       } else {
+               break;
+       case ST_BOOLEAN:
+               if (strncasecmp(value, "yes", 3) == 0)
+                       (*(isc_boolean_t *)setting->target) = ISC_TRUE;
+               else if (strncasecmp(value, "no", 2) == 0)
+                       (*(isc_boolean_t *)setting->target) = ISC_FALSE;
+               else {
+                       log_error("unknown boolean expression (%s: %s)",
+                                 setting->name, value);
+                       result = ISC_R_FAILURE;
+                       goto cleanup;
+               }
+               break;
+       default:
                fatal_error("unknown type in function set_value()");
                result = ISC_R_FAILURE;
                goto cleanup;
@@ -157,6 +173,10 @@ set_default_value(setting_t *setting)
        case ST_UNSIGNED_INTEGER:
                *(unsigned *)setting->target = 
setting->default_value.value_uint;
                break;
+       case ST_BOOLEAN:
+               *(isc_boolean_t *)setting->target =
+                       setting->default_value.value_boolean;
+               break;
        default:
                fatal_error("unknown type in function set_default_value()");
                return ISC_R_FAILURE;
diff --git a/src/settings.h b/src/settings.h
index 0ffaf0c..f184669 100644
--- a/src/settings.h
+++ b/src/settings.h
@@ -21,12 +21,15 @@
 #ifndef _LD_SETTINGS_H_
 #define _LD_SETTINGS_H_
 
+#include <isc/types.h>
+
 typedef struct setting setting_t;
 
 typedef enum {
        ST_LD_STRING,
        ST_SIGNED_INTEGER,
        ST_UNSIGNED_INTEGER,
+       ST_BOOLEAN,
 } setting_type_t;
 
 struct setting {
@@ -38,6 +41,7 @@ struct setting {
                const char      *value_char;
                signed int      value_sint;
                unsigned int    value_uint;
+               isc_boolean_t   value_boolean;
        } default_value;
        void            *target;
 };
@@ -57,10 +61,12 @@ struct setting {
 #define default_string(val)    0, 1, ST_LD_STRING, { .value_char = (val) }, 
NULL
 #define default_sint(val)      0, 1, ST_SIGNED_INTEGER, { .value_sint = (val) 
}, NULL
 #define default_uint(val)      0, 1, ST_UNSIGNED_INTEGER, { .value_uint = 
(val) }, NULL
+#define default_boolean(val)   0, 1, ST_BOOLEAN, { .value_boolean = (val) }, 
NULL
 /* No defaults. */
 #define no_default_string      0, 0, ST_LD_STRING, { .value_char = NULL }, NULL
 #define no_default_sint                0, 0, ST_SIGNED_INTEGER, { .value_sint 
= 0 }, NULL
 #define no_default_uint                0, 0, ST_UNSIGNED_INTEGER, { 
.value_uint = 0 }, NULL
+#define no_default_boolean     0, 1, ST_BOOLEAN, { .value_boolean = ISC_FALSE 
}, NULL
 
 /* This is used in the end of setting_t arrays. */
 #define end_of_settings        { NULL, default_sint(0) }
-- 
1.7.6

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to