diff options
author | Timo Sirainen <cras@irssi.org> | 2000-05-10 13:57:42 +0000 |
---|---|---|
committer | cras <cras@dbcabf3a-b0e7-0310-adc4-f8d773084564> | 2000-05-10 13:57:42 +0000 |
commit | bb4d7566c29162976f1d47ce51c1ffd1ce2041d6 (patch) | |
tree | 796489749de7f4b7bc5db0376f800df0ad434732 /src/lib-config | |
parent | afc4fbc2238f9b76df9e704446ec9be64ee2f57b (diff) | |
download | irssi-bb4d7566c29162976f1d47ce51c1ffd1ce2041d6.zip |
iconfig's caching was a bit buggy - it didn't notice if some config node was
removed.
git-svn-id: http://svn.irssi.org/repos/irssi/trunk@213 dbcabf3a-b0e7-0310-adc4-f8d773084564
Diffstat (limited to 'src/lib-config')
-rw-r--r-- | src/lib-config/get.c | 14 | ||||
-rw-r--r-- | src/lib-config/iconfig.h | 11 | ||||
-rw-r--r-- | src/lib-config/parse.c | 29 | ||||
-rw-r--r-- | src/lib-config/set.c | 37 |
4 files changed, 66 insertions, 25 deletions
diff --git a/src/lib-config/get.c b/src/lib-config/get.c index 8ac3e667..07a328ce 100644 --- a/src/lib-config/get.c +++ b/src/lib-config/get.c @@ -70,7 +70,7 @@ CONFIG_NODE *config_node_section(CONFIG_NODE *parent, const char *key, int new_t CONFIG_NODE *config_node_traverse(CONFIG_REC *rec, const char *section, int create) { CONFIG_NODE *node; - char **list, **tmp; + char **list, **tmp, *str; int is_list, new_type; g_return_val_if_fail(rec != NULL, NULL); @@ -96,7 +96,9 @@ CONFIG_NODE *config_node_traverse(CONFIG_REC *rec, const char *section, int crea g_strfreev(list); /* save to cache */ - g_hash_table_insert(rec->cache, g_strdup(section), node); + str = g_strdup(section); + g_hash_table_insert(rec->cache, str, node); + g_hash_table_insert(rec->cache_nodes, node, str); return node; } @@ -120,10 +122,12 @@ char *config_get_str(CONFIG_REC *rec, const char *section, const char *key, cons config_node_find(parent, key); /* save to cache */ - if (node != NULL) + if (node == NULL) + g_free(path); + else { g_hash_table_insert(rec->cache, path, node); - else - g_free(path); + g_hash_table_insert(rec->cache_nodes, node, path); + } } return (node == NULL || !has_node_value(node)) ? (char *) def : node->value; diff --git a/src/lib-config/iconfig.h b/src/lib-config/iconfig.h index a45ffdec..86659a13 100644 --- a/src/lib-config/iconfig.h +++ b/src/lib-config/iconfig.h @@ -50,7 +50,8 @@ struct _config_rec { char *last_error; CONFIG_NODE *mainnode; - GHashTable *cache; + GHashTable *cache; /* path -> node (for querying) */ + GHashTable *cache_nodes; /* node -> path (for removing) */ GScanner *scanner; @@ -124,17 +125,15 @@ char *config_node_get_str(CONFIG_NODE *parent, const char *key, const char *def) int config_node_get_int(CONFIG_NODE *parent, const char *key, int def); int config_node_get_bool(CONFIG_NODE *parent, const char *key, int def); -void config_node_set_str(CONFIG_NODE *parent, const char *key, const char *value); +void config_node_set_str(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, const char *value); void config_node_set_int(CONFIG_NODE *parent, const char *key, int value); void config_node_set_bool(CONFIG_NODE *parent, const char *key, int value); -/* Add/change the value of the `key' */ -void config_node_set_str(CONFIG_NODE *parent, const char *key, const char *value); /* Remove one node from block/list. ..set_str() with value = NULL does the same. */ -void config_node_remove(CONFIG_NODE *parent, CONFIG_NODE *node); +void config_node_remove(CONFIG_REC *rec, CONFIG_NODE *parent, CONFIG_NODE *node); /* Remove n'th node from a list */ -void config_node_list_remove(CONFIG_NODE *node, int index); +void config_node_list_remove(CONFIG_REC *rec, CONFIG_NODE *node, int index); /* Clear the entire configuration */ void config_nodes_remove_all(CONFIG_REC *rec); diff --git a/src/lib-config/parse.c b/src/lib-config/parse.c index 25f9ccec..0032b824 100644 --- a/src/lib-config/parse.c +++ b/src/lib-config/parse.c @@ -20,6 +20,29 @@ #include "module.h" +static int g_istr_equal(gconstpointer v, gconstpointer v2) +{ + return g_strcasecmp((const char *) v, (const char *) v2) == 0; +} + +/* a char* hash function from ASU */ +static unsigned int g_istr_hash(gconstpointer v) +{ + const char *s = (char *) v; + unsigned int h = 0, g; + + while (*s != '\0') { + h = (h << 4) + toupper(*s); + if ((g = h & 0xf0000000)) { + h = h ^ (g >> 24); + h = h ^ g; + } + s++; + } + + return h /* % M */; +} + int config_error(CONFIG_REC *rec, const char *msg) { g_free_and_null(rec->last_error); @@ -138,7 +161,7 @@ static int config_parse_symbol(CONFIG_REC *rec, CONFIG_NODE *node) switch (rec->scanner->token) { case G_TOKEN_STRING: /* value */ - config_node_set_str(node, key, rec->scanner->value.v_string); + config_node_set_str(rec, node, key, rec->scanner->value.v_string); g_free_not_null(key); print_warning = TRUE; @@ -303,7 +326,8 @@ CONFIG_REC *config_open(const char *fname, int create_mode) rec->create_mode = create_mode; rec->mainnode = g_new0(CONFIG_NODE, 1); rec->mainnode->type = NODE_TYPE_BLOCK; - rec->cache = g_hash_table_new((GHashFunc) g_str_hash, (GCompareFunc) g_str_equal); + rec->cache = g_hash_table_new((GHashFunc) g_istr_hash, (GCompareFunc) g_istr_equal); + rec->cache_nodes = g_hash_table_new((GHashFunc) g_direct_hash, (GCompareFunc) g_direct_equal); return rec; } @@ -319,6 +343,7 @@ void config_close(CONFIG_REC *rec) if (rec->handle != -1) close(rec->handle); g_hash_table_foreach(rec->cache, (GHFunc) g_free, NULL); g_hash_table_destroy(rec->cache); + g_hash_table_destroy(rec->cache_nodes); g_free_not_null(rec->last_error); g_free(rec->fname); g_free(rec); diff --git a/src/lib-config/set.c b/src/lib-config/set.c index ce078f38..55607c4c 100644 --- a/src/lib-config/set.c +++ b/src/lib-config/set.c @@ -20,11 +20,24 @@ #include "module.h" -void config_node_remove(CONFIG_NODE *parent, CONFIG_NODE *node) +static void cache_remove(CONFIG_REC *rec, CONFIG_NODE *node) +{ + char *path; + + path = g_hash_table_lookup(rec->cache_nodes, node); + if (path != NULL) { + g_hash_table_remove(rec->cache, path); + g_hash_table_remove(rec->cache_nodes, node); + g_free(path); + } +} + +void config_node_remove(CONFIG_REC *rec, CONFIG_NODE *parent, CONFIG_NODE *node) { g_return_if_fail(parent != NULL); g_return_if_fail(node != NULL); + cache_remove(rec, node); parent->value = g_slist_remove(parent->value, node); switch (node->type) { @@ -36,7 +49,7 @@ void config_node_remove(CONFIG_NODE *parent, CONFIG_NODE *node) case NODE_TYPE_BLOCK: case NODE_TYPE_LIST: while (node->value != NULL) - config_node_remove(node, ((GSList *) node->value)->data); + config_node_remove(rec, node, ((GSList *) node->value)->data); break; } g_free_not_null(node->key); @@ -44,7 +57,7 @@ void config_node_remove(CONFIG_NODE *parent, CONFIG_NODE *node) } /* Remove n'th node from a list */ -void config_node_list_remove(CONFIG_NODE *node, int index) +void config_node_list_remove(CONFIG_REC *rec, CONFIG_NODE *node, int index) { GSList *tmp; @@ -53,7 +66,7 @@ void config_node_list_remove(CONFIG_NODE *node, int index) for (tmp = node->value; tmp != NULL; tmp = tmp->next, index--) { if (index == 0) { - config_node_remove(node, tmp->data); + config_node_remove(rec, node, tmp->data); break; } } @@ -64,15 +77,15 @@ void config_nodes_remove_all(CONFIG_REC *rec) g_return_if_fail(rec != NULL); while (rec->mainnode->value != NULL) - config_node_remove(rec->mainnode, ((GSList *) rec->mainnode->value)->data); + config_node_remove(rec, rec->mainnode, ((GSList *) rec->mainnode->value)->data); } - -void config_node_set_str(CONFIG_NODE *parent, const char *key, const char *value) +void config_node_set_str(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, const char *value) { CONFIG_NODE *node; int no_key; + g_return_if_fail(rec != NULL || value != NULL); g_return_if_fail(parent != NULL); no_key = key == NULL; @@ -80,7 +93,7 @@ void config_node_set_str(CONFIG_NODE *parent, const char *key, const char *value if (value == NULL) { /* remove the key */ - if (node != NULL) config_node_remove(parent, node); + if (node != NULL) config_node_remove(rec, parent, node); return; } @@ -102,12 +115,12 @@ void config_node_set_int(CONFIG_NODE *parent, const char *key, int value) char str[MAX_INT_STRLEN]; g_snprintf(str, sizeof(str), "%d", value); - config_node_set_str(parent, key, str); + config_node_set_str(NULL, parent, key, str); } void config_node_set_bool(CONFIG_NODE *parent, const char *key, int value) { - config_node_set_str(parent, key, value ? "yes" : "no"); + config_node_set_str(NULL, parent, key, value ? "yes" : "no"); } int config_set_str(CONFIG_REC *rec, const char *section, const char *key, const char *value) @@ -120,7 +133,7 @@ int config_set_str(CONFIG_REC *rec, const char *section, const char *key, const parent = config_node_traverse(rec, section, TRUE); if (parent == NULL) return -1; - config_node_set_str(parent, key, value); + config_node_set_str(rec, parent, key, value); return 0; } @@ -143,5 +156,5 @@ void config_node_add_list(CONFIG_NODE *node, char **array) char **tmp; for (tmp = array; *tmp != NULL; tmp++) - config_node_set_str(node, NULL, *tmp); + config_node_set_str(NULL, node, NULL, *tmp); } |