settings: Add a set_default_str() to set a different default for a key
authorMartin Willi <martin@revosec.ch>
Thu, 11 Apr 2013 17:23:29 +0000 (19:23 +0200)
committerMartin Willi <martin@revosec.ch>
Mon, 6 May 2013 13:28:27 +0000 (15:28 +0200)
The value is set only if it is not configured in strongswan.conf or has
not been set() otherwise.

src/libstrongswan/utils/settings.c
src/libstrongswan/utils/settings.h

index 712ea6e..809ca10 100644 (file)
@@ -644,6 +644,26 @@ METHOD(settings_t, set_time, void,
        va_end(args);
 }
 
+METHOD(settings_t, set_default_str, bool,
+          private_settings_t *this, char *key, char *value, ...)
+{
+       char *old;
+       va_list args;
+
+       va_start(args, value);
+       old = find_value(this, this->top, key, args);
+       va_end(args);
+
+       if (!old)
+       {
+               va_start(args, value);
+               set_value(this, this->top, key, args, value);
+               va_end(args);
+               return TRUE;
+       }
+       return FALSE;
+}
+
 /**
  * Enumerate section names, not sections
  */
@@ -1209,6 +1229,7 @@ settings_t *settings_create(char *file)
                        .set_double = _set_double,
                        .set_time = _set_time,
                        .set_bool = _set_bool,
+                       .set_default_str = _set_default_str,
                        .create_section_enumerator = _create_section_enumerator,
                        .create_key_value_enumerator = _create_key_value_enumerator,
                        .load_files = _load_files,
index a861325..df0c534 100644 (file)
@@ -239,6 +239,16 @@ struct settings_t {
        void (*set_time)(settings_t *this, char *key, u_int32_t value, ...);
 
        /**
+        * Set a default for string value.
+        *
+        * @param key           key including sections, printf style format
+        * @param def           value to set if unconfigured
+        * @param ...           argument list for key
+        * @return                      TRUE if a new default value for key has been set
+        */
+       bool (*set_default_str)(settings_t *this, char *key, char *value, ...);
+
+       /**
         * Create an enumerator over subsection names of a section.
         *
         * @param section       section including parents, printf style format