Changeset 4603
- Timestamp:
- 11/07/08 16:08:53 (2 months ago)
- Files:
-
- trunk/src/libstrongswan/settings.c (modified) (9 diffs)
- trunk/src/libstrongswan/settings.h (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/libstrongswan/settings.c
r4333 r4603 89 89 }; 90 90 91 static char *find(section_t *section, char *key) 92 { 93 char *name, *pos, *value = NULL; 91 /** 92 * find a section by a given key 93 */ 94 static section_t *find_section(section_t *section, char *key, va_list args) 95 { 96 char name[512], *pos; 97 enumerator_t *enumerator; 98 section_t *current, *found = NULL; 99 100 if (section == NULL) 101 { 102 return NULL; 103 } 104 if (vsnprintf(name, sizeof(name), key, args) >= sizeof(name)) 105 { 106 return NULL; 107 } 108 109 pos = strchr(name, '.'); 110 if (pos) 111 { 112 *pos = '\0'; 113 pos++; 114 } 115 enumerator = section->sections->create_enumerator(section->sections); 116 while (enumerator->enumerate(enumerator, ¤t)) 117 { 118 if (streq(current->name, name)) 119 { 120 found = current; 121 break; 122 } 123 } 124 enumerator->destroy(enumerator); 125 if (found && pos) 126 { 127 return find_section(found, pos, args); 128 } 129 return found; 130 } 131 132 static char *find_value(section_t *section, char *key, va_list args) 133 { 134 char name[512], *pos, *value = NULL; 94 135 enumerator_t *enumerator; 95 136 kv_t *kv; … … 101 142 } 102 143 103 name = strdupa(key); 144 if (vsnprintf(name, sizeof(name), key, args) >= sizeof(name)) 145 { 146 return NULL; 147 } 104 148 105 149 pos = strchr(name, '.'); … … 120 164 if (found) 121 165 { 122 return find (found, pos);166 return find_value(found, pos, args); 123 167 } 124 168 } … … 142 186 * Implementation of settings_t.get. 143 187 */ 144 static char* get_str(private_settings_t *this, char *key, char *def )188 static char* get_str(private_settings_t *this, char *key, char *def, ...) 145 189 { 146 190 char *value; 147 148 value = find(this->top, key); 191 va_list args; 192 193 va_start(args, def); 194 value = find_value(this->top, key, args); 195 va_end(args); 149 196 if (value) 150 197 { … … 157 204 * Implementation of settings_t.get_bool. 158 205 */ 159 static bool get_bool(private_settings_t *this, char *key, bool def )206 static bool get_bool(private_settings_t *this, char *key, bool def, ...) 160 207 { 161 208 char *value; 162 163 value = find(this->top, key); 209 va_list args; 210 211 va_start(args, def); 212 value = find_value(this->top, key, args); 213 va_end(args); 164 214 if (value) 165 215 { … … 185 235 * Implementation of settings_t.get_int. 186 236 */ 187 static int get_int(private_settings_t *this, char *key, int def )237 static int get_int(private_settings_t *this, char *key, int def, ...) 188 238 { 189 239 char *value; 190 240 int intval; 191 192 value = find(this->top, key); 241 va_list args; 242 243 va_start(args, def); 244 value = find_value(this->top, key, args); 245 va_end(args); 193 246 if (value) 194 247 { … … 206 259 * Implementation of settings_t.get_time. 207 260 */ 208 static u_int32_t get_time(private_settings_t *this, char *key, u_int32_t def )261 static u_int32_t get_time(private_settings_t *this, char *key, u_int32_t def, ...) 209 262 { 210 263 char *value, *endptr; 211 264 u_int32_t timeval; 212 213 value = find(this->top, key); 265 va_list args; 266 267 va_start(args, def); 268 value = find_value(this->top, key, args); 269 va_end(args); 214 270 if (value) 215 271 { … … 237 293 } 238 294 return def; 295 } 296 297 /** 298 * Enumerate section names, not sections 299 */ 300 static bool section_filter(void *null, section_t **in, char **out) 301 { 302 *out = (*in)->name; 303 return TRUE; 304 } 305 306 /** 307 * Implementation of settings_t.create_section_enumerator 308 */ 309 static enumerator_t* create_section_enumerator(private_settings_t *this, 310 char *key, ...) 311 { 312 section_t *section; 313 va_list args; 314 315 va_start(args, key); 316 section = find_section(this->top, key, args); 317 va_end(args); 318 319 if (!section) 320 { 321 return enumerator_create_empty(); 322 } 323 return enumerator_create_filter( 324 section->sections->create_enumerator(section->sections), 325 (void*)section_filter, NULL, NULL); 239 326 } 240 327 … … 401 488 private_settings_t *this = malloc_thing(private_settings_t); 402 489 403 this->public.get_str = (char*(*)(settings_t*, char *key, char* def))get_str; 404 this->public.get_int = (int(*)(settings_t*, char *key, int def))get_int; 405 this->public.get_time = (u_int32_t(*)(settings_t*, char *key, u_int32_t def))get_time; 406 this->public.get_bool = (bool(*)(settings_t*, char *key, bool def))get_bool; 490 this->public.get_str = (char*(*)(settings_t*, char *key, char* def, ...))get_str; 491 this->public.get_int = (int(*)(settings_t*, char *key, int def, ...))get_int; 492 this->public.get_time = (u_int32_t(*)(settings_t*, char *key, u_int32_t def, ...))get_time; 493 this->public.get_bool = (bool(*)(settings_t*, char *key, bool def, ...))get_bool; 494 this->public.create_section_enumerator = (enumerator_t*(*)(settings_t*,char *section, ...))create_section_enumerator; 407 495 this->public.destroy = (void(*)(settings_t*))destroy; 408 496 trunk/src/libstrongswan/settings.h
r4333 r4603 27 27 28 28 #include <library.h> 29 #include <utils/enumerator.h> 29 30 30 31 /** 31 32 * Generic configuration options read from a config file. 32 33 * 33 * The sy tax is quite simple:34 * The syntax is quite simple: 34 35 * 35 36 * settings := (section|keyvalue)* … … 39 40 * E.g.: 40 41 * @code 41 a = b42 section-one {42 a = b 43 section-one { 43 44 somevalue = asdf 44 45 subsection { … … 59 60 * Get a settings value as a string. 60 61 * 61 * @param key key including sections 62 * @param key key including sections, printf style format 62 63 * @param def value returned if key not found 64 * @param ... argument list for key 63 65 * @return value pointing to internal string 64 66 */ 65 char* (*get_str)(settings_t *this, char *key, char *def );67 char* (*get_str)(settings_t *this, char *key, char *def, ...); 66 68 67 69 /** 68 70 * Get a boolean yes|no, true|false value. 69 71 * 70 * @param jey key including sections 71 * @param def default value returned if key not found 72 * @param key key including sections, printf style format 73 * @param def value returned if key not found 74 * @param ... argument list for key 72 75 * @return value of the key 73 76 */ 74 bool (*get_bool)(settings_t *this, char *key, bool def );77 bool (*get_bool)(settings_t *this, char *key, bool def, ...); 75 78 76 79 /** 77 80 * Get an integer value. 78 81 * 79 * @param key key including sections 80 * @param def default value to return if key not found 82 * @param key key including sections, printf style format 83 * @param def value returned if key not found 84 * @param ... argument list for key 81 85 * @return value of the key 82 86 */ 83 int (*get_int)(settings_t *this, char *key, int def );87 int (*get_int)(settings_t *this, char *key, int def, ...); 84 88 85 89 /** 86 90 * Get a time value. 87 91 * 88 * @param key key including sections 89 * @param def default value to return if key not found 92 * @param key key including sections, printf style format 93 * @param def value returned if key not found 94 * @param ... argument list for key 90 95 * @return value of the key 91 96 */ 92 u_int32_t (*get_time)(settings_t *this, char *key, u_int32_t def );93 97 u_int32_t (*get_time)(settings_t *this, char *key, u_int32_t def, ...); 98 94 99 /** 95 * Destroy a settings instance. 96 */ 97 void (*destroy)(settings_t *this); 100 * Create an enumerator over subsection names of a section. 101 * 102 * @param section section including parents, printf style format 103 * @param ... argument list for key 104 * @return enumerator over subsection names 105 */ 106 enumerator_t* (*create_section_enumerator)(settings_t *this, 107 char *section, ...); 108 /** 109 * Destroy a settings instance. 110 */ 111 void (*destroy)(settings_t *this); 98 112 }; 99 113
