various fixes
[oweals/gnunet.git] / src / include / gnunet_configuration_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_configuration_lib.h
23  * @brief configuration API
24  *
25  * @author Christian Grothoff
26  */
27
28 #ifndef GNUNET_CONFIGURATION_LIB_H
29 #define GNUNET_CONFIGURATION_LIB_H
30
31
32 #ifdef __cplusplus
33 extern "C"
34 {
35 #if 0                           /* keep Emacsens' auto-indent happy */
36 }
37 #endif
38 #endif
39
40 #include "gnunet_common.h"
41 #include "gnunet_time_lib.h"
42
43 /**
44  * A configuration object.
45  */
46 struct GNUNET_CONFIGURATION_Handle;
47
48 /**
49  * Create a new configuration object.
50  * @return fresh configuration object
51  */
52 struct GNUNET_CONFIGURATION_Handle *GNUNET_CONFIGURATION_create (void);
53
54
55 /**
56  * Duplicate an existing configuration object.
57  *
58  * @param cfg configuration to duplicate
59  * @return duplicate configuration
60  */
61 struct GNUNET_CONFIGURATION_Handle *
62 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg);
63
64
65 /**
66  * Destroy configuration object.
67  *
68  * @param cfg configuration to destroy
69  */
70 void GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg);
71
72
73 /**
74  * Load configuration.  This function will first parse the
75  * defaults and then parse the specific configuration file
76  * to overwrite the defaults.
77  *
78  * @param cfg configuration to update
79  * @param filename name of the configuration file
80  * @return GNUNET_OK on success, GNUNET_SYSERR on error
81  */
82 int GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
83                                const char *filename);
84
85
86 /**
87  * Parse a configuration file, add all of the options in the
88  * file to the configuration environment.
89  *
90  * @param cfg configuration to update
91  * @param filename name of the configuration file
92  * @return GNUNET_OK on success, GNUNET_SYSERR on error
93  */
94 int GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
95                                 const char *filename);
96
97
98 /**
99  * Write configuration file.
100  *
101  * @param cfg configuration to write
102  * @param filename where to write the configuration
103  * @return GNUNET_OK on success, GNUNET_SYSERR on error
104  */
105 int GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
106                                 const char *filename);
107
108
109 /**
110  * Test if there are configuration options that were
111  * changed since the last save.
112  *
113  * @param cfg configuration to inspect
114  * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
115  */
116 int GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg);
117
118
119 /**
120  * Function to iterate over options.
121  *
122  * @param cls closure
123  * @param section name of the section
124  * @param option name of the option
125  * @param value value of the option
126  */
127 typedef void (*GNUNET_CONFIGURATION_Iterator)(void *cls,
128                                               const char *section,
129                                               const char *option,
130                                               const char *value);
131
132
133 /**
134  * Iterate over all options in the configuration.
135  *
136  * @param cfg configuration to inspect
137  * @param iter function to call on each option
138  * @param iter_cls closure for iter
139  */
140 void GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
141                                    GNUNET_CONFIGURATION_Iterator iter,
142                                    void *iter_cls);
143
144
145 /**
146  * Get a configuration value that should be a number.
147  *
148  * @param cfg configuration to inspect
149  * @param section section of interest
150  * @param option option of interest
151  * @param number where to store the numeric value of the option
152  * @return GNUNET_OK on success, GNUNET_SYSERR on error
153  */
154 int GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
155                                            *cfg, const char *section,
156                                            const char *option,
157                                            unsigned long long *number);
158
159
160 /**
161  * Get a configuration value that should be a relative time.
162  *
163  * @param cfg configuration to inspect
164  * @param section section of interest
165  * @param option option of interest
166  * @param time set to the time value stored in the configuration
167  * @return GNUNET_OK on success, GNUNET_SYSERR on error
168  */
169 int GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
170                                          *cfg, const char *section,
171                                          const char *option,
172                                          struct GNUNET_TIME_Relative *time);
173
174
175 /**
176  * Test if we have a value for a particular option
177  *
178  * @param cfg configuration to inspect
179  * @param section section of interest
180  * @param option option of interest
181  * @return GNUNET_YES if so, GNUNET_NO if not.
182  */
183 int GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
184                                      const char *section, const char *option);
185
186
187 /**
188  * Get a configuration value that should be a string.
189  *
190  * @param cfg configuration to inspect
191  * @param section section of interest
192  * @param option option of interest
193  * @param value will be set to a freshly allocated configuration
194  *        value, or NULL if option is not specified
195  * @return GNUNET_OK on success, GNUNET_SYSERR on error
196  */
197 int GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
198                                            *cfg, const char *section,
199                                            const char *option, char **value);
200
201
202 /**
203  * Get a configuration value that should be the name of a file
204  * or directory.
205  *
206  * @param cfg configuration to inspect
207  * @param section section of interest
208  * @param option option of interest
209  * @param value will be set to a freshly allocated configuration
210  *        value, or NULL if option is not specified
211  * @return GNUNET_OK on success, GNUNET_SYSERR on error
212  */
213 int GNUNET_CONFIGURATION_get_value_filename (const struct
214                                              GNUNET_CONFIGURATION_Handle *cfg,
215                                              const char *section,
216                                              const char *option,
217                                              char **value);
218
219 /**
220  * Iterate over the set of filenames stored in a configuration value.
221  *
222  * @param cfg configuration to inspect
223  * @param section section of interest
224  * @param option option of interest
225  * @param cb function to call on each filename
226  * @param cb_cls closure for cb
227  * @return number of filenames iterated over, -1 on error
228  */
229 int GNUNET_CONFIGURATION_iterate_value_filenames (const struct
230                                                   GNUNET_CONFIGURATION_Handle
231                                                   *cfg,
232                                                   const char *section,
233                                                   const char *option,
234                                                   GNUNET_FileNameCallback
235                                                   cb, void *cb_cls);
236
237 /**
238  * Get a configuration value that should be in a set of
239  * predefined strings
240  *
241  * @param cfg configuration to inspect
242  * @param section section of interest
243  * @param option option of interest
244  * @param choices NULL-terminated list of legal values
245  * @param value will be set to an entry in the legal list,
246  *        or NULL if option is not specified and no default given
247  * @return GNUNET_OK on success, GNUNET_SYSERR on error
248  */
249 int GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
250                                            *cfg, const char *section,
251                                            const char *option,
252                                            const char **choices,
253                                            const char **value);
254
255 /**
256  * Get a configuration value that should be in a set of
257  * "YES" or "NO".
258  *
259  * @param cfg configuration to inspect
260  * @param section section of interest
261  * @param option option of interest
262  * @return GNUNET_YES, GNUNET_NO or if option has no valid value, GNUNET_SYSERR
263  */
264 int GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
265                                           *cfg, const char *section,
266                                           const char *option);
267
268 /**
269  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
270  * where either in the "PATHS" section or the environtment
271  * "FOO" is set to "DIRECTORY".
272  *
273  * @param cfg configuration to use for path expansion
274  * @param orig string to $-expand (will be freed!)
275  * @return $-expanded string
276  */
277 char *GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
278                                           *cfg, char *orig);
279
280
281 /**
282  * Set a configuration value that should be a number.
283  *
284  * @param cfg configuration to update
285  * @param section section of interest
286  * @param option option of interest
287  * @param number value to set
288  */
289 void
290 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
291                                        *cfg,
292                                        const char *section,
293                                        const char *option,
294                                        unsigned long long number);
295
296
297 /**
298  * Set a configuration value that should be a string.
299  *
300  * @param cfg configuration to update
301  * @param section section of interest
302  * @param option option of interest
303  * @param value value to set
304  */
305 void
306 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
307                                        *cfg,
308                                        const char *section,
309                                        const char *option, const char *value);
310
311 /**
312  * Remove a filename from a configuration value that
313  * represents a list of filenames
314  *
315  * @param cfg configuration to update
316  * @param section section of interest
317  * @param option option of interest
318  * @param value filename to remove
319  * @return GNUNET_OK on success,
320  *         GNUNET_SYSERR if the filename is not in the list
321  */
322 int GNUNET_CONFIGURATION_remove_value_filename (struct
323                                                 GNUNET_CONFIGURATION_Handle
324                                                 *cfg,
325                                                 const char *section,
326                                                 const char *option,
327                                                 const char *value);
328
329 /**
330  * Append a filename to a configuration value that
331  * represents a list of filenames
332  *
333  * @param cfg configuration to update
334  * @param section section of interest
335  * @param option option of interest
336  * @param value filename to append
337  * @return GNUNET_OK on success,
338  *         GNUNET_SYSERR if the filename already in the list
339  */
340 int GNUNET_CONFIGURATION_append_value_filename (struct
341                                                 GNUNET_CONFIGURATION_Handle
342                                                 *cfg, const char *section,
343                                                 const char *option,
344                                                 const char *value);
345
346
347 #if 0                           /* keep Emacsens' auto-indent happy */
348 {
349 #endif
350 #ifdef __cplusplus
351 }
352 #endif
353
354 #endif