X-Git-Url: http://git.openpandora.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fpnd_conf.c;h=824fbb33c80461ed0acbab3da679e30d227f6fac;hb=4b7531ad98d318b8071d595ce3e0d4113ee06f93;hp=e2114a5f052d59ad5d2e763293ff79cf6beea777;hpb=2deb0263a52b618a2bf0091252317975b6380a19;p=pandora-libraries.git diff --git a/lib/pnd_conf.c b/lib/pnd_conf.c index e2114a5..824fbb3 100644 --- a/lib/pnd_conf.c +++ b/lib/pnd_conf.c @@ -79,6 +79,7 @@ pnd_conf_handle pnd_conf_fetch_by_name ( char *filename, char *searchpath ) { conf = pnd_conf_fetch_by_path ( buffer ); if ( conf ) { + wordfree ( &_p ); return ( conf ); } @@ -233,7 +234,7 @@ pnd_conf_handle pnd_conf_fetch_by_path ( char *fullpath ) { } // key or key/value? } // section or key/value line? - + } // while // clean up a trifle @@ -257,3 +258,98 @@ int pnd_conf_get_as_int ( pnd_conf_handle c, char *key ) { 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 ); +}