1
0
mirror of https://github.com/irssi/irssi.git synced 2024-11-03 04:27:19 -05:00
irssi/src/lib-config/get.c

255 lines
6.9 KiB
C
Raw Normal View History

Sorry for a big update - I still don't have internet connection at home and this is what I've been doing a few weeks now.. :) You really shouldn't upgrade to this version without keeping a backup of the working one, since this will break everything and at least notify list is broken - probably something else too. * On the way to 0.8.0 .. Major rewriting/rearranging code. There's some changes in behaviour because I'm trying to make Irssi a bit more compatible with EPIC. * libPropList isn't needed anymore - I'm using my own configuration library. This is mostly because different proplists worked a bit differently everywhere and several people had problems with it. It's also yet another extra library that you needed to compile Irssi. New configuration library has several advantages: You can add comments to configuration file and they also stay there when it's saved. It's not nearly as vulnerable as proplist. If some error occurs, instead of just not reading anything it will try to continue if possible. Also the error messages are written to irssi's text window instead of stdout. It can be managed more easily than proplist - setting/getting the configuration is a lot more easier. * Coding style changes - I'm not using gint, gchar etc. anymore, they're just extra pain when moving code to non-glib projects and syntax hilighting doesn't work by default with most editors ;) Indentation style was also changed to K&R because of some political reasons ;) And I'm already starting to like it.. :) It forces me to split code to different functions more often and the result is that the code gets more readable. And finally I'm also using nst' all over the place. + /EVAL <commands> - Expand all the special variables from string and run it. Commands can be split with ; character. See docs/SPECIAL_VARS for more info. + Aliases are parsed just like /EVAL - arguments are in $0..$9. + Text formats are also parsed like /EVAL, arguments used to be in $1..$9, now they're in $0..$8 so it messes up existing themes.. + /SET [key [value]] - no more the '=' character. Boolean values also need to be changed with ON/OFF/TOGGLE values (not yes/no). Settings aren't saved to disk until you use /SAVE. + /TOGGLE <key> [ON/OFF] - same as /SET <key> TOGGLE git-svn-id: http://svn.irssi.org/repos/irssi/trunk@163 dbcabf3a-b0e7-0310-adc4-f8d773084564
2000-04-14 07:27:14 -04:00
/*
get.c : irssi configuration - get settings from memory
Copyright (C) 1999 Timo Sirainen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "module.h"
CONFIG_NODE *config_node_find(CONFIG_NODE *node, const char *key)
{
GSList *tmp;
g_return_val_if_fail(node != NULL, NULL);
g_return_val_if_fail(key != NULL, NULL);
g_return_val_if_fail(is_node_list(node), NULL);
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
CONFIG_NODE *node = tmp->data;
if (node->key != NULL && g_strcasecmp(node->key, key) == 0)
return node;
}
return NULL;
}
/* find the section from node - if not found create it unless new_type is -1.
you can also specify in new_type if it's NODE_TYPE_LIST or NODE_TYPE_BLOCK */
CONFIG_NODE *config_node_section(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int new_type)
{
CONFIG_NODE *node;
g_return_val_if_fail(rec != NULL, NULL);
g_return_val_if_fail(parent != NULL, NULL);
g_return_val_if_fail(is_node_list(parent), NULL);
node = key == NULL ? NULL : config_node_find(parent, key);
if (node != NULL) {
g_return_val_if_fail(new_type == -1 || new_type == node->type, NULL);
return node;
}
if (new_type == -1)
return NULL;
node = g_new0(CONFIG_NODE, 1);
parent->value = g_slist_append(parent->value, node);
node->type = new_type;
node->key = key == NULL ? NULL : g_strdup(key);
return node;
}
/* find the section with the whole path.
create the path if necessary `create' is TRUE. */
CONFIG_NODE *config_node_traverse(CONFIG_REC *rec, const char *section, int create)
{
CONFIG_NODE *node;
char **list, **tmp;
int is_list, new_type;
g_return_val_if_fail(rec != NULL, NULL);
if (section == NULL || *section == '\0')
return rec->mainnode;
/* check if it already exists in cache */
node = g_hash_table_lookup(rec->cache, section);
if (node != NULL) return node;
new_type = -1;
node = rec->mainnode;
list = g_strsplit(section, "/", -1);
for (tmp = list; *tmp != NULL; tmp++) {
is_list = **tmp == '(';
if (create) new_type = is_list ? NODE_TYPE_LIST : NODE_TYPE_BLOCK;
node = config_node_section(rec, node, *tmp + is_list, new_type);
if (node == NULL) return NULL;
}
g_strfreev(list);
/* save to cache */
g_hash_table_insert(rec->cache, g_strdup(section), node);
return node;
}
char *config_get_str(CONFIG_REC *rec, const char *section, const char *key, const char *def)
{
CONFIG_NODE *parent, *node;
char *path;
g_return_val_if_fail(rec != NULL, (char *) def);
g_return_val_if_fail(key != NULL, (char *) def);
/* check if it already exists in cache */
path = g_strconcat(section == NULL ? "" : section, "/", key, NULL);
Sorry for a big update - I still don't have internet connection at home and this is what I've been doing a few weeks now.. :) You really shouldn't upgrade to this version without keeping a backup of the working one, since this will break everything and at least notify list is broken - probably something else too. * On the way to 0.8.0 .. Major rewriting/rearranging code. There's some changes in behaviour because I'm trying to make Irssi a bit more compatible with EPIC. * libPropList isn't needed anymore - I'm using my own configuration library. This is mostly because different proplists worked a bit differently everywhere and several people had problems with it. It's also yet another extra library that you needed to compile Irssi. New configuration library has several advantages: You can add comments to configuration file and they also stay there when it's saved. It's not nearly as vulnerable as proplist. If some error occurs, instead of just not reading anything it will try to continue if possible. Also the error messages are written to irssi's text window instead of stdout. It can be managed more easily than proplist - setting/getting the configuration is a lot more easier. * Coding style changes - I'm not using gint, gchar etc. anymore, they're just extra pain when moving code to non-glib projects and syntax hilighting doesn't work by default with most editors ;) Indentation style was also changed to K&R because of some political reasons ;) And I'm already starting to like it.. :) It forces me to split code to different functions more often and the result is that the code gets more readable. And finally I'm also using nst' all over the place. + /EVAL <commands> - Expand all the special variables from string and run it. Commands can be split with ; character. See docs/SPECIAL_VARS for more info. + Aliases are parsed just like /EVAL - arguments are in $0..$9. + Text formats are also parsed like /EVAL, arguments used to be in $1..$9, now they're in $0..$8 so it messes up existing themes.. + /SET [key [value]] - no more the '=' character. Boolean values also need to be changed with ON/OFF/TOGGLE values (not yes/no). Settings aren't saved to disk until you use /SAVE. + /TOGGLE <key> [ON/OFF] - same as /SET <key> TOGGLE git-svn-id: http://svn.irssi.org/repos/irssi/trunk@163 dbcabf3a-b0e7-0310-adc4-f8d773084564
2000-04-14 07:27:14 -04:00
node = g_hash_table_lookup(rec->cache, path);
if (node != NULL)
g_free(path);
else {
parent = config_node_traverse(rec, section, FALSE);
node = parent == NULL ? NULL :
config_node_find(parent, key);
/* save to cache */
if (node != NULL)
g_hash_table_insert(rec->cache, path, node);
else
g_free(path);
}
return (node == NULL || !has_node_value(node)) ? (char *) def : node->value;
}
int config_get_int(CONFIG_REC *rec, const char *section, const char *key, int def)
{
char *str;
str = config_get_str(rec, section, key, NULL);
if (str == NULL) return def;
return atoi(str);
}
int config_get_bool(CONFIG_REC *rec, const char *section, const char *key, int def)
{
char *str;
str = config_get_str(rec, section, key, NULL);
if (str == NULL) return def;
return toupper(*str) == 'T' || toupper(*str) == 'Y';
}
/* Return value of key `value_key' from list item where `key' is `value' */
const char *config_list_find(CONFIG_REC *rec, const char *section, const char *key, const char *value, const char *value_key)
{
CONFIG_NODE *node;
node = config_list_find_node(rec, section, key, value, value_key);
return node != NULL && node->type == NODE_TYPE_KEY ?
node->value : NULL;
}
/* Like config_list_find(), but return node instead of it's value */
CONFIG_NODE *config_list_find_node(CONFIG_REC *rec, const char *section, const char *key, const char *value, const char *value_key)
{
CONFIG_NODE *node, *keynode;
GSList *tmp;
g_return_val_if_fail(rec != NULL, NULL);
g_return_val_if_fail(key != NULL, NULL);
g_return_val_if_fail(value_key != NULL, NULL);
node = config_node_traverse(rec, section, FALSE);
if (node == NULL || !is_node_list(node)) return NULL;
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
node = tmp->data;
if (node->type != NODE_TYPE_BLOCK)
continue;
/* key matches value? */
keynode = config_node_find(node, key);
if (keynode == NULL || keynode->type != NODE_TYPE_KEY ||
g_strcasecmp(keynode->value, value) != 0) continue;
return config_node_find(node, value_key);
}
return NULL;
}
char *config_node_get_str(CONFIG_NODE *parent, const char *key, const char *def)
{
CONFIG_NODE *node;
node = config_node_find(parent, key);
return (node == NULL || !has_node_value(node)) ? def : node->value;
}
int config_node_get_int(CONFIG_NODE *parent, const char *key, int def)
{
char *str;
str = config_node_get_str(parent, key, NULL);
if (str == NULL) return def;
return atoi(str);
}
int config_node_get_bool(CONFIG_NODE *parent, const char *key, int def)
{
char *str;
str = config_node_get_str(parent, key, NULL);
if (str == NULL) return def;
return toupper(*str) == 'T' || toupper(*str) == 'Y' ||
(toupper(*str) == 'O' && toupper(str[1]) == 'N');
}
/* Get the value of keys `key' and `key_value' and put them to
`ret_key' and `ret_value'. Returns -1 if not found. */
int config_node_get_keyvalue(CONFIG_NODE *node, const char *key, const char *value_key, char **ret_key, char **ret_value)
{
CONFIG_NODE *keynode, *valuenode;
GSList *tmp;
g_return_val_if_fail(node != NULL, -1);
g_return_val_if_fail(key != NULL, -1);
g_return_val_if_fail(value_key != NULL, -1);
g_return_val_if_fail(ret_key != NULL, -1);
g_return_val_if_fail(ret_value != NULL, -1);
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
node = tmp->data;
if (node->type != NODE_TYPE_BLOCK)
continue;
keynode = config_node_find(node, key);
if (keynode == NULL || keynode->type != NODE_TYPE_KEY)
continue;
valuenode = config_node_find(node, value_key);
*ret_key = keynode->key;
*ret_value = valuenode != NULL && valuenode->type == NODE_TYPE_KEY ?
valuenode->value : NULL;
return 0;
}
return -1;
}