Changeset 4603

Show
Ignore:
Timestamp:
11/07/08 16:08:53 (2 months ago)
Author:
martin
Message:

settings section enumeration
printf style key lookup

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/libstrongswan/settings.c

    r4333 r4603  
    8989}; 
    9090 
    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 */ 
     94static 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, &current)) 
     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 
     132static char *find_value(section_t *section, char *key, va_list args) 
     133
     134    char name[512], *pos, *value = NULL; 
    94135    enumerator_t *enumerator; 
    95136    kv_t *kv; 
     
    101142    } 
    102143     
    103     name = strdupa(key); 
     144    if (vsnprintf(name, sizeof(name), key, args) >= sizeof(name)) 
     145    { 
     146        return NULL; 
     147    } 
    104148     
    105149    pos = strchr(name, '.'); 
     
    120164        if (found) 
    121165        { 
    122             return find(found, pos); 
     166            return find_value(found, pos, args); 
    123167        } 
    124168    } 
     
    142186 * Implementation of settings_t.get. 
    143187 */ 
    144 static char* get_str(private_settings_t *this, char *key, char *def
     188static char* get_str(private_settings_t *this, char *key, char *def, ...
    145189{ 
    146190    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); 
    149196    if (value) 
    150197    { 
     
    157204 * Implementation of settings_t.get_bool. 
    158205 */ 
    159 static bool get_bool(private_settings_t *this, char *key, bool def
     206static bool get_bool(private_settings_t *this, char *key, bool def, ...
    160207{ 
    161208    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); 
    164214    if (value) 
    165215    { 
     
    185235 * Implementation of settings_t.get_int. 
    186236 */ 
    187 static int get_int(private_settings_t *this, char *key, int def
     237static int get_int(private_settings_t *this, char *key, int def, ...
    188238{ 
    189239    char *value; 
    190240    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); 
    193246    if (value) 
    194247    { 
     
    206259 * Implementation of settings_t.get_time. 
    207260 */ 
    208 static u_int32_t get_time(private_settings_t *this, char *key, u_int32_t def
     261static u_int32_t get_time(private_settings_t *this, char *key, u_int32_t def, ...
    209262{ 
    210263    char *value, *endptr; 
    211264    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); 
    214270    if (value) 
    215271    { 
     
    237293    } 
    238294    return def; 
     295} 
     296 
     297/** 
     298 * Enumerate section names, not sections 
     299 */ 
     300static 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 */ 
     309static 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); 
    239326} 
    240327 
     
    401488    private_settings_t *this = malloc_thing(private_settings_t); 
    402489     
    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; 
    407495    this->public.destroy = (void(*)(settings_t*))destroy; 
    408496     
  • trunk/src/libstrongswan/settings.h

    r4333 r4603  
    2727 
    2828#include <library.h> 
     29#include <utils/enumerator.h> 
    2930 
    3031/** 
    3132 * Generic configuration options read from a config file. 
    3233 * 
    33  * The sytax is quite simple: 
     34 * The syntax is quite simple: 
    3435 * 
    3536 * settings := (section|keyvalue)* 
     
    3940 * E.g.: 
    4041 * @code 
    41   a = b 
    42   section-one { 
     42  a = b 
     43  section-one { 
    4344        somevalue = asdf 
    4445        subsection { 
     
    5960     * Get a settings value as a string. 
    6061     * 
    61      * @param key       key including sections 
     62     * @param key       key including sections, printf style format 
    6263     * @param def       value returned if key not found 
     64     * @param ...       argument list for key 
    6365     * @return          value pointing to internal string 
    6466     */ 
    65     char* (*get_str)(settings_t *this, char *key, char *def); 
     67    char* (*get_str)(settings_t *this, char *key, char *def, ...); 
    6668     
    6769    /** 
    6870     * Get a boolean yes|no, true|false value. 
    6971     * 
    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 
    7275     * @return          value of the key 
    7376     */ 
    74     bool (*get_bool)(settings_t *this, char *key, bool def); 
     77    bool (*get_bool)(settings_t *this, char *key, bool def, ...); 
    7578     
    7679    /** 
    7780     * Get an integer value. 
    7881     * 
    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 
    8185     * @return          value of the key 
    8286     */ 
    83     int (*get_int)(settings_t *this, char *key, int def); 
     87    int (*get_int)(settings_t *this, char *key, int def, ...); 
    8488     
    8589    /** 
    8690     * Get a time value. 
    8791     * 
    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 
    9095     * @return          value of the key 
    9196     */ 
    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     
    9499    /** 
    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); 
    98112}; 
    99113