Added SILC Thread Queue API
[runtime.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 /* Returns n'th node from list. */
97 CONFIG_NODE *config_node_nth(CONFIG_NODE *node, int index);
98 /* Returns index for given key */
99 int config_node_index(CONFIG_NODE *parent, const char *key);
100
101 /* Returns the first non-comment node in list */
102 GSList *config_node_first(GSList *list);
103 /* Returns the next non-comment node in list */
104 GSList *config_node_next(GSList *list);
105
106 /* Setting values */
107 int config_set_str(CONFIG_REC *rec, const char *section, const char *key, const char *value);
108 int config_set_int(CONFIG_REC *rec, const char *section, const char *key, int value);
109 int config_set_bool(CONFIG_REC *rec, const char *section, const char *key, int value);
110
111 /* Handling the configuration directly with nodes -
112    useful when you need to read all values in a block/list. */
113 CONFIG_NODE *config_node_find(CONFIG_NODE *node, const char *key);
114 /* Find the section from node - if not found create it unless new_type is -1.
115    You can also specify in new_type if it's NODE_TYPE_LIST or NODE_TYPE_BLOCK */
116 CONFIG_NODE *config_node_section(CONFIG_NODE *parent, const char *key, int new_type);
117 CONFIG_NODE *config_node_section_index(CONFIG_NODE *parent, const char *key,
118                                        int index, int new_type);
119 /* Find the section with the whole path.
120    Create the path if necessary `create' is TRUE. */
121 CONFIG_NODE *config_node_traverse(CONFIG_REC *rec, const char *section, int create);
122 /* Get the value of keys `key' and `key_value' and put them to
123    `ret_key' and `ret_value'. Returns -1 if not found. */
124 int config_node_get_keyvalue(CONFIG_NODE *node, const char *key, const char *value_key, char **ret_key, char **ret_value);
125 /* Return all values from from the list `node' in a g_strsplit() array */
126 char **config_node_get_list(CONFIG_NODE *node);
127 /* Add all values in `array' to `node' */
128 void config_node_add_list(CONFIG_REC *rec, CONFIG_NODE *node, char **array);
129
130 char *config_node_get_str(CONFIG_NODE *parent, const char *key, const char *def);
131 int config_node_get_int(CONFIG_NODE *parent, const char *key, int def);
132 int config_node_get_bool(CONFIG_NODE *parent, const char *key, int def);
133
134 void config_node_set_str(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, const char *value);
135 void config_node_set_int(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value);
136 void config_node_set_bool(CONFIG_REC *rec, CONFIG_NODE *parent, const char *key, int value);
137
138 /* Remove one node from block/list.
139    ..set_str() with value = NULL does the same. */
140 void config_node_remove(CONFIG_REC *rec, CONFIG_NODE *parent, CONFIG_NODE *node);
141 /* Remove n'th node from a list */
142 void config_node_list_remove(CONFIG_REC *rec, CONFIG_NODE *node, int index);
143
144 /* Clear all data inside node, but leave the node */
145 void config_node_clear(CONFIG_REC *rec, CONFIG_NODE *node);
146 /* Clear the entire configuration */
147 void config_nodes_remove_all(CONFIG_REC *rec);
148
149 #endif