imported irssi.
[silc.git] / apps / irssi / src / lib-config / iconfig.h
1 #ifndef __ICONFIG_H
2 #define __ICONFIG_H
3
4 enum {
5         NODE_TYPE_KEY,
6         NODE_TYPE_VALUE,
7         NODE_TYPE_BLOCK,
8         NODE_TYPE_LIST,
9         NODE_TYPE_COMMENT
10 };
11
12 #define has_node_value(a) \
13         ((a)->type == NODE_TYPE_KEY || (a)->type == NODE_TYPE_VALUE)
14 #define is_node_list(a) \
15         ((a)->type == NODE_TYPE_BLOCK || (a)->type == NODE_TYPE_LIST)
16
17 struct _CONFIG_NODE {
18         int type;
19         char *key;
20         void *value;
21 };
22
23 /* a = { x=y; y=z; }
24
25    node1: type = NODE_TYPE_BLOCK, key = "a", value = (GSList *) nodes
26    nodes: (node2, node3)
27      node2: type = NODE_TYPE_KEY, key = "x", value = (char *) "y"
28      node3: type = NODE_TYPE_KEY, key = "y", value = (char *) "z"
29
30    b = ( a, { b=c; d=e; } )
31
32    node1: type = NODE_TYPE_LIST, key = "b", value = (GSList *) nodes
33    nodes: (node2, node3)
34      node2: type = NODE_TYPE_VALUE, key = NULL, value = (char *) "a"
35      node4: type = NODE_TYPE_BLOCK, key = NULL, value = (GSList *) nodes2
36      nodes2: (node4, node5)
37        node4: type = NODE_TYPE_KEY, key = "b", value = (char *) "c"
38        node5: type = NODE_TYPE_KEY, key = "d", value = (char *) "e"
39
40    Comments node has key=NULL and value is the comment line. Empty lines are
41    also in comments so they won't be forgotten when the config file is
42    written.
43
44 */
45
46 struct _CONFIG_REC {
47         char *fname;
48         int handle;
49         int create_mode;
50         int modifycounter; /* increase every time something is changed */
51
52         char *last_error;
53         CONFIG_NODE *mainnode;
54         GHashTable *cache; /* path -> node (for querying) */
55         GHashTable *cache_nodes; /* node -> path (for removing) */
56
57         GScanner *scanner;
58
59         /* while writing to configuration file.. */
60         int tmp_indent_level; /* indentation position */
61         int tmp_last_lf; /* last character was a line feed */
62 };
63
64 /* Open configuration. The file is created if it doesn't exist, unless
65    `create_mode' is -1. `fname' can be NULL if you just want to use
66    config_parse_data() */
67 CONFIG_REC *config_open(const char *fname, int create_mode);
68 /* Release all memory used by configuration */
69 void config_close(CONFIG_REC *rec);
70 /* Change file name of config file */
71 void config_change_file_name(CONFIG_REC *rec, const char *fname, int create_mode);
72
73 /* Parse configuration file */
74 int config_parse(CONFIG_REC *rec);
75 /* Parse configuration found from `data'. `input_name' is specifies the
76    "configuration name" which is displayed in error messages. */
77 int config_parse_data(CONFIG_REC *rec, const char *data, const char *input_name);
78
79 /* Write configuration file. Write to `fname' if it's not NULL.
80    If `create_mode' is -1, use the one that was given to config_open(). */
81 int config_write(CONFIG_REC *rec, const char *fname, int create_mode);
82
83 #define config_last_error(rec) \
84     (rec)->last_error
85
86 /* Getting values
87
88    `section' is something like "maingroup/key/subkey", or with lists
89    "maingroup/(list/subkey"
90
91    `def' is returned if the value is not found. */
92 char *config_get_str(CONFIG_REC *rec, const char *section, const char *key, const char *def);
93 int config_get_int(CONFIG_REC *rec, const char *section, const char *key, int def);
94 int config_get_bool(CONFIG_REC *rec, const char *section, const char *key, int def);
95
96 /* Return value of key `value_key' from list item where `key' is `value' */
97 const char *config_list_find(CONFIG_REC *rec, const char *section, const char *key, const char *value, const char *value_key);
98 /* Like config_list_find(), but return node instead of it's value */
99 CONFIG_NODE *config_list_find_node(CONFIG_REC *rec, const char *section, const char *key, const char *value, const char *value_key);
100 /* Returns n'th node from list. */
101 CONFIG_NODE *config_node_index(CONFIG_NODE *node, int index);
102
103 /* Setting values */
104 int config_set_str(CONFIG_REC *rec, const char *section, const char *key, const char *value);
105 int config_set_int(CONFIG_REC *rec, const char *section, const char *key, int value);
106 int config_set_bool(CONFIG_REC *rec, const char *section, const char *key, int value);
107
108 /* Handling the configuration directly with nodes -
109    useful when you need to read all values in a block/list. */
110 CONFIG_NODE *config_node_find(CONFIG_NODE *node, const char *key);
111 /* Find the section from node - if not found create it unless new_type is -1.
112    You can also specify in new_type if it's NODE_TYPE_LIST or NODE_TYPE_BLOCK */
113 CONFIG_NODE *config_node_section(CONFIG_NODE *parent, const char *key, int new_type);
114 /* Find the section with the whole path.
115    Create the path if necessary `create' is TRUE. */
116 CONFIG_NODE *config_node_traverse(CONFIG_REC *rec, const char *section, int create);
117 /* Get the value of keys `key' and `key_value' and put them to
118    `ret_key' and `ret_value'. Returns -1 if not found. */
119 int config_node_get_keyvalue(CONFIG_NODE *node, const char *key, const char *value_key, char **ret_key, char **ret_value);
120 /* Return all values from from the list `node' in a g_strsplit() array */
121 char **config_node_get_list(CONFIG_NODE *node);
122 /* Add all values in `array' to `node' */
123 void config_node_add_list(CONFIG_REC *rec, CONFIG_NODE *node, char **array);
124
125 char *config_node_get_str(CONFIG_NODE *parent, const char *key, const char *def);
126 int config_node_get_int(CONFIG_NODE *parent, const char *key, int def);
127 int config_node_get_bool(CONFIG_NODE *parent, const char *key, int def);
128
129 void config_node_set_str(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, const char *value);
130 void config_node_set_int(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value);
131 void config_node_set_bool(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value);
132
133 /* Remove one node from block/list.
134    ..set_str() with value = NULL does the same. */
135 void config_node_remove(CONFIG_REC *rec, CONFIG_NODE *parent, CONFIG_NODE *node);
136 /* Remove n'th node from a list */
137 void config_node_list_remove(CONFIG_REC *rec, CONFIG_NODE *node, int index);
138
139 /* Clear all data inside node, but leave the node */
140 void config_node_clear(CONFIG_REC *rec, CONFIG_NODE *node);
141 /* Clear the entire configuration */
142 void config_nodes_remove_all(CONFIG_REC *rec);
143
144 #endif