1
0
mirror of https://github.com/irssi/irssi.git synced 2024-10-27 05:20:20 -04:00

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
This commit is contained in:
Timo Sirainen 2000-05-10 13:57:42 +00:00 committed by cras
parent afc4fbc223
commit bb4d7566c2
12 changed files with 106 additions and 62 deletions

View File

@ -246,15 +246,15 @@ static void log_set_config(LOG_REC *log)
if (log->autoopen)
config_node_set_bool(node, "auto_open", TRUE);
else
config_node_set_str(node, "auto_open", NULL);
iconfig_node_set_str(node, "auto_open", NULL);
config_node_set_str(node, "rotate", log_rotate2str(log->rotate));
iconfig_node_set_str(node, "rotate", log_rotate2str(log->rotate));
levelstr = bits2level(log->level);
config_node_set_str(node, "level", levelstr);
iconfig_node_set_str(node, "level", levelstr);
g_free(levelstr);
config_node_set_str(node, "items", NULL);
iconfig_node_set_str(node, "items", NULL);
if (log->items != NULL && *log->items != NULL) {
node = config_node_section(node, "items", NODE_TYPE_LIST);

View File

@ -19,16 +19,19 @@ typedef struct {
} SETTINGS_REC;
/* macros for handling the default Irssi configuration */
#define iconfig_get_str(a, b, c) config_get_str(mainconfig, a, b,c)
#define iconfig_get_int(a, b, c) config_get_int(mainconfig, a, b,c)
#define iconfig_get_bool(a, b, c) config_get_bool(mainconfig, a, b,c)
#define iconfig_get_str(a, b, c) config_get_str(mainconfig, a, b, c)
#define iconfig_get_int(a, b, c) config_get_int(mainconfig, a, b, c)
#define iconfig_get_bool(a, b, c) config_get_bool(mainconfig, a, b, c)
#define iconfig_list_find(a, b, c, d) config_list_find(mainconfig, a, b, c, d)
#define iconfig_set_str(a, b, c) config_set_str(mainconfig, a, b,c)
#define iconfig_set_int(a, b, c) config_set_int(mainconfig, a, b,c)
#define iconfig_set_bool(a, b, c) config_set_bool(mainconfig, a, b,c)
#define iconfig_set_str(a, b, c) config_set_str(mainconfig, a, b, c)
#define iconfig_set_int(a, b, c) config_set_int(mainconfig, a, b, c)
#define iconfig_set_bool(a, b, c) config_set_bool(mainconfig, a, b, c)
#define iconfig_node_traverse(a, b) config_node_traverse(mainconfig, a, b)
#define iconfig_node_set_str(a, b, c) config_node_set_str(mainconfig, a, b, c)
#define iconfig_node_list_remove(a, b) config_node_list_remove(mainconfig, a, b)
#define iconfig_node_remove(a, b) config_node_remove(mainconfig, a, b)
extern CONFIG_REC *mainconfig;

View File

@ -46,9 +46,9 @@ static void hilight_add_config(HILIGHT_REC *rec)
node = iconfig_node_traverse("(hilights", TRUE);
node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
config_node_set_str(node, "text", rec->text);
iconfig_node_set_str(node, "text", rec->text);
if (rec->level > 0) config_node_set_int(node, "level", rec->level);
if (rec->color) config_node_set_str(node, "color", rec->color);
if (rec->color) iconfig_node_set_str(node, "color", rec->color);
if (rec->nickmask) config_node_set_bool(node, "nickmask", TRUE);
if (rec->fullword) config_node_set_bool(node, "fullword", TRUE);
if (rec->regexp) config_node_set_bool(node, "regexp", TRUE);
@ -66,7 +66,7 @@ static void hilight_remove_config(HILIGHT_REC *rec)
g_return_if_fail(rec != NULL);
node = iconfig_node_traverse("hilights", FALSE);
if (node != NULL) config_node_list_remove(node, g_slist_index(hilights, rec));
if (node != NULL) iconfig_node_list_remove(node, g_slist_index(hilights, rec));
}
static void hilight_destroy(HILIGHT_REC *rec)

View File

@ -205,7 +205,7 @@ void keyboard_save(void)
GSList *tmp, *tmp2;
/* remove old keyboard settings */
config_node_set_str(NULL, "(keyboard", NULL);
iconfig_node_set_str(NULL, "(keyboard", NULL);
keyboard = iconfig_node_traverse("(keyboard", TRUE);
for (tmp = keyinfos; tmp != NULL; tmp = tmp->next) {
@ -217,8 +217,8 @@ void keyboard_save(void)
listnode = config_node_section(node, NULL, NODE_TYPE_BLOCK);
if (key->data != NULL)
config_node_set_str(listnode, "data", key->data);
config_node_set_str(listnode, "key", key->key);
iconfig_node_set_str(listnode, "data", key->data);
iconfig_node_set_str(listnode, "key", key->key);
}
}
}

View File

@ -43,15 +43,15 @@ static void channel_config_add(SETUP_CHANNEL_REC *channel)
node = iconfig_node_traverse("(channels", TRUE);
node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
config_node_set_str(node, "name", channel->name);
config_node_set_str(node, "ircnet", channel->ircnet);
iconfig_node_set_str(node, "name", channel->name);
iconfig_node_set_str(node, "ircnet", channel->ircnet);
if (channel->autojoin)
config_node_set_bool(node, "autojoin", TRUE);
config_node_set_str(node, "password", channel->password);
config_node_set_str(node, "botmasks", channel->botmasks);
config_node_set_str(node, "autosendcmd", channel->autosendcmd);
config_node_set_str(node, "background", channel->background);
config_node_set_str(node, "font", channel->font);
iconfig_node_set_str(node, "password", channel->password);
iconfig_node_set_str(node, "botmasks", channel->botmasks);
iconfig_node_set_str(node, "autosendcmd", channel->autosendcmd);
iconfig_node_set_str(node, "background", channel->background);
iconfig_node_set_str(node, "font", channel->font);
}
static void channel_config_remove(SETUP_CHANNEL_REC *channel)
@ -59,7 +59,7 @@ static void channel_config_remove(SETUP_CHANNEL_REC *channel)
CONFIG_NODE *node;
node = iconfig_node_traverse("channels", FALSE);
if (node != NULL) config_node_list_remove(node, g_slist_index(setupchannels, channel));
if (node != NULL) iconfig_node_list_remove(node, g_slist_index(setupchannels, channel));
}
void channels_setup_create(SETUP_CHANNEL_REC *channel)

View File

@ -164,18 +164,18 @@ static void ignore_set_config(IGNORE_REC *rec)
node = iconfig_node_traverse("(ignores", TRUE);
node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
if (rec->mask != NULL) config_node_set_str(node, "mask", rec->mask);
if (rec->mask != NULL) iconfig_node_set_str(node, "mask", rec->mask);
if (rec->level) {
levelstr = bits2level(rec->level);
config_node_set_str(node, "level", levelstr);
iconfig_node_set_str(node, "level", levelstr);
g_free(levelstr);
}
if (rec->except_level) {
levelstr = bits2level(rec->except_level);
config_node_set_str(node, "except_level", levelstr);
iconfig_node_set_str(node, "except_level", levelstr);
g_free(levelstr);
}
config_node_set_str(node, "pattern", rec->pattern);
iconfig_node_set_str(node, "pattern", rec->pattern);
if (rec->regexp) config_node_set_bool(node, "regexp", TRUE);
if (rec->fullword) config_node_set_bool(node, "fullword", TRUE);
@ -210,7 +210,7 @@ static void ignore_remove_config(IGNORE_REC *rec)
CONFIG_NODE *node;
node = iconfig_node_traverse("ignores", FALSE);
if (node != NULL) config_node_list_remove(node, ignore_index(rec));
if (node != NULL) iconfig_node_list_remove(node, ignore_index(rec));
}
void ignore_add_rec(IGNORE_REC *rec)

View File

@ -261,12 +261,12 @@ void setupserver_config_add(SETUP_SERVER_REC *rec)
node = iconfig_node_traverse("(servers", TRUE);
node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
config_node_set_str(node, "address", rec->address);
config_node_set_str(node, "ircnet", rec->ircnet);
iconfig_node_set_str(node, "address", rec->address);
iconfig_node_set_str(node, "ircnet", rec->ircnet);
config_node_set_int(node, "port", rec->port);
config_node_set_str(node, "password", rec->password);
config_node_set_str(node, "own_host", rec->own_host);
iconfig_node_set_str(node, "password", rec->password);
iconfig_node_set_str(node, "own_host", rec->own_host);
if (rec->autoconnect)
config_node_set_bool(node, "autoconnect", TRUE);
@ -282,7 +282,7 @@ void setupserver_config_remove(SETUP_SERVER_REC *rec)
CONFIG_NODE *node;
node = iconfig_node_traverse("servers", FALSE);
if (node != NULL) config_node_list_remove(node, g_slist_index(setupservers, rec));
if (node != NULL) iconfig_node_list_remove(node, g_slist_index(setupservers, rec));
}
static void setupserver_destroy(SETUP_SERVER_REC *rec)

View File

@ -35,14 +35,14 @@ void notifylist_add_config(NOTIFYLIST_REC *rec)
if (rec->away_check)
config_node_set_bool(node, "away_check", TRUE);
else
config_node_set_str(node, "away_check", NULL);
iconfig_node_set_str(node, "away_check", NULL);
if (rec->idle_check_time > 0)
config_node_set_int(node, "idle_check_time", rec->idle_check_time/60);
else
config_node_set_str(node, "idle_check_time", NULL);
iconfig_node_set_str(node, "idle_check_time", NULL);
config_node_set_str(node, "ircnets", NULL);
iconfig_node_set_str(node, "ircnets", NULL);
if (rec->ircnets != NULL && *rec->ircnets != NULL) {
node = config_node_section(node, "ircnets", NODE_TYPE_LIST);
config_node_add_list(node, rec->ircnets);

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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);
}