From 28d8c4a1319ed2535acffccd08eb3afb31b86137 Mon Sep 17 00:00:00 2001 From: skeezix Date: Fri, 26 Mar 2010 14:32:11 -0400 Subject: [PATCH] Added conf set/save quick routines --- include/pnd_conf.h | 6 +++ include/pnd_container.h | 3 ++ lib/pnd_conf.c | 83 +++++++++++++++++++++++++++++++++++++++++ lib/pnd_container.c | 21 +++++++++++ 4 files changed, 113 insertions(+) diff --git a/include/pnd_conf.h b/include/pnd_conf.h index a522fd7..1d0b708 100644 --- a/include/pnd_conf.h +++ b/include/pnd_conf.h @@ -125,6 +125,12 @@ char *pnd_conf_get_as_char ( pnd_conf_handle c, char *key ); int pnd_conf_get_as_int ( pnd_conf_handle c, char *key ); int pnd_conf_get_as_int_d ( pnd_conf_handle c, char *key, int def ); // same as _as_int, but returns default instead of BADNUM +/* writes and saves + */ +int *pnd_conf_set_int ( pnd_conf_handle c, char *key, int v ); +char *pnd_conf_set_char ( pnd_conf_handle c, char *key, char *v ); +unsigned char pnd_conf_write ( pnd_conf_handle c, char *fullpath ); + #ifdef __cplusplus } /* "C" */ #endif diff --git a/include/pnd_container.h b/include/pnd_container.h index e0ba9fd..10247c7 100644 --- a/include/pnd_container.h +++ b/include/pnd_container.h @@ -39,6 +39,9 @@ void pnd_box_delete ( pnd_box_handle box ); */ void *pnd_box_allocinsert ( pnd_box_handle box, char *key, unsigned int size ); +/* use find_by_key() to find a value or iteration, and unlink it */ +void pnd_box_delete_node ( pnd_box_handle box, void *value ); + /* find_by_key() will attempt to locate a payload based on the specified key. * Returns the payload pointer or NULL if not found. * CASE INSENSITIVE. diff --git a/lib/pnd_conf.c b/lib/pnd_conf.c index 5f1901f..6b8f644 100644 --- a/lib/pnd_conf.c +++ b/lib/pnd_conf.c @@ -269,3 +269,86 @@ int pnd_conf_get_as_int_d ( pnd_conf_handle c, char *key, int def ) { 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 ) ); + + 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 ); +} diff --git a/lib/pnd_container.c b/lib/pnd_container.c index 38657ab..ca9dac8 100644 --- a/lib/pnd_container.c +++ b/lib/pnd_container.c @@ -193,3 +193,24 @@ unsigned char pnd_box_append ( pnd_box_handle box, pnd_box_handle append ) { return ( 1 ); } + +void pnd_box_delete_node ( pnd_box_handle box, void *value ) { + pnd_box_t *pbox = (pnd_box_t*) box; + pnd_box_node_t *p = PAYLOAD2NODE(value); + + if ( pbox -> head == p ) { + pbox -> head = p -> next; + free ( p ); + } else { + pnd_box_node_t *i = pbox -> head; + while ( i && i -> next != p ) { + i = i -> next; + } + if ( i -> next == p ) { + i -> next = p -> next; + free ( p ); + } + } + + return; +} -- 2.39.2