X-Git-Url: http://git.openpandora.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fpnd_conf.c;h=88d0c46331edfef5cf46d6aedd54c44821d6cb49;hb=e7fba6b31c06ad5432bed398b02b704ed0e92131;hp=e3077c2d1c5271eb9a12ca16a4d81f6bb761a7bd;hpb=0258a5f326ad753a47bfc8189249c71748bed7fc;p=pandora-libraries.git diff --git a/lib/pnd_conf.c b/lib/pnd_conf.c index e3077c2..88d0c46 100644 --- a/lib/pnd_conf.c +++ b/lib/pnd_conf.c @@ -14,6 +14,7 @@ pnd_conf_filename_t pnd_conf_filenames[] = { { pnd_conf_startup, "startup" }, { pnd_conf_desktop, "desktop" }, { pnd_conf_categories, "categories" }, + { pnd_conf_evmap, "eventmap" }, { pnd_conf_nil, NULL }, }; @@ -34,7 +35,7 @@ char *pnd_conf_query_searchpath ( void ) { temp = pnd_conf_get_as_char ( ch, PND_CONF_KEY ); - if ( searchpath ) { + if ( temp ) { searchpath = strdup ( temp ); } else { searchpath = strdup ( PND_CONF_SEARCHPATH ); @@ -178,6 +179,11 @@ pnd_conf_handle pnd_conf_fetch_by_path ( char *fullpath ) { *mid = '\0'; mid++; + // skip past any heading space for the key + while ( *mid && isspace ( *mid ) ) { + mid++; + } + //printf ( "key head: '%s'\n", head ); //printf ( "key mid: '%s'\n", mid ); @@ -205,7 +211,7 @@ pnd_conf_handle pnd_conf_fetch_by_path ( char *fullpath ) { } } else { - // key/value pairing + // key only char *v; // form the actual new key @@ -239,3 +245,110 @@ pnd_conf_handle pnd_conf_fetch_by_path ( char *fullpath ) { char *pnd_conf_get_as_char ( pnd_conf_handle c, char *key ) { return ( pnd_box_find_by_key ( c, key ) ); } + +int pnd_conf_get_as_int ( pnd_conf_handle c, char *key ) { + char *t = pnd_box_find_by_key ( c, key ); + + if ( ! t ) { + return ( PND_CONF_BADNUM ); // non-existant + } + + int i = atoi ( t ); + + return ( i ); +} + +int pnd_conf_get_as_int_d ( pnd_conf_handle c, char *key, int def ) { + char *t = pnd_box_find_by_key ( c, key ); + + if ( ! t ) { + return ( def ); // non-existant + } + + int i = atoi ( t ); + + return ( i ); +} + +int *pnd_conf_set_int ( pnd_conf_handle c, char *key, int v ) { + + // key is already present? if so, delete it + void *kv = pnd_box_find_by_key ( c, key ); + + if ( kv ) { + pnd_box_delete_node ( c, kv ); + } + + // add the new node + int *nv = pnd_box_allocinsert ( c, key, sizeof(int) ); + + if ( nv ) { + *nv = v; + return ( nv ); + } + + return ( NULL ); +} + +char *pnd_conf_set_char ( pnd_conf_handle c, char *key, char *v ) { + + // key is already present? if so, delete it + char *kv = pnd_box_find_by_key ( c, key ); + + if ( kv ) { + pnd_box_delete_node ( c, kv ); + } + + // add the new node + char *nv = pnd_box_allocinsert ( c, key, strlen ( v ) + 1 ); + + if ( nv ) { + strcpy ( nv, v ); + return ( nv ); + } + + return ( NULL ); +} + +unsigned char pnd_conf_write ( pnd_conf_handle c, char *fullpath ) { + char *p = pnd_box_get_head ( c ); + FILE *f; + char lastcategory [ 100 ] = ""; + + if ( ! p ) { + return ( 1 ); // nothing to save, so.. success? + } + + f = fopen ( fullpath, "w" ); + + if ( ! f ) { + return ( 0 ); + } + + while ( p ) { + char *k = pnd_box_get_key ( p ); + char *c = strchr ( k, '.' ); + + if ( c ) { + if ( strncmp ( k, lastcategory, c - k ) == 0 ) { + // same category + } else { + strncpy ( lastcategory, k, c - k ); + fprintf ( f, "[%s]\n", lastcategory ); + } + fprintf ( f, "%s\t%s\n", c + 1, p ); + } else { + if ( lastcategory [ 0 ] ) { + fprintf ( f, "[]\n" ); + } + lastcategory [ 0 ] = '\0'; + fprintf ( f, "%s\t%s\n", k, p ); + } + + p = pnd_box_get_next ( p ); + } + + fclose ( f ); + + return ( 1 ); +}