24705f0fb68f102bc133a0159ac9f0d5db99f1db
[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 *GNUNET_CONFIGURATION_dup (const struct
62                                                               GNUNET_CONFIGURATION_Handle
63                                                               *cfg);
64
65
66 /**
67  * Destroy configuration object.
68  *
69  * @param cfg configuration to destroy
70  */
71 void GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg);
72
73
74 /**
75  * Load configuration.  This function will first parse the
76  * defaults and then parse the specific configuration file
77  * to overwrite the defaults.
78  *
79  * @param cfg configuration to update
80  * @param filename name of the configuration file, NULL to load defaults
81  * @return GNUNET_OK on success, GNUNET_SYSERR on error
82  */
83 int GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
84                                const char *filename);
85
86
87 /**
88  * Parse a configuration file, add all of the options in the
89  * file to the configuration environment.
90  *
91  * @param cfg configuration to update
92  * @param filename name of the configuration file
93  * @return GNUNET_OK on success, GNUNET_SYSERR on error
94  */
95 int GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
96                                 const char *filename);
97
98
99 /**
100  * Write configuration file.
101  *
102  * @param cfg configuration to write
103  * @param filename where to write the configuration
104  * @return GNUNET_OK on success, GNUNET_SYSERR on error
105  */
106 int GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
107                                 const char *filename);
108
109 /**
110  * Write only configuration entries that have been changed to configuration file
111  * @param cfgDefault default configuration
112  * @param cfgNew new configuration
113  * @param filename where to write the configuration diff between default and new
114  * @return GNUNET_OK on success, GNUNET_SYSERR on error
115  */
116 int GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
117                                       *cfgDefault,
118                                       const struct GNUNET_CONFIGURATION_Handle
119                                       *cfgNew, const char *filename);
120
121 /**
122  * Test if there are configuration options that were
123  * changed since the last save.
124  *
125  * @param cfg configuration to inspect
126  * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
127  */
128 int GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle
129                                    *cfg);
130
131
132 /**
133  * Function to iterate over options.
134  *
135  * @param cls closure
136  * @param section name of the section
137  * @param option name of the option
138  * @param value value of the option
139  */
140 typedef void (*GNUNET_CONFIGURATION_Iterator) (void *cls, const char *section,
141                                                const char *option,
142                                                const char *value);
143
144
145 /**
146  * Function to iterate over section.
147  *
148  * @param cls closure
149  * @param section name of the section
150  */
151 typedef void (*GNUNET_CONFIGURATION_Section_Iterator) (void *cls,
152                                                        const char *section);
153
154
155 /**
156  * Iterate over all options in the configuration.
157  *
158  * @param cfg configuration to inspect
159  * @param iter function to call on each option
160  * @param iter_cls closure for iter
161  */
162 void GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle
163                                    *cfg, GNUNET_CONFIGURATION_Iterator iter,
164                                    void *iter_cls);
165
166
167 /**
168  * Iterate over all sections in the configuration.
169  *
170  * @param cfg configuration to inspect
171  * @param iter function to call on each section
172  * @param iter_cls closure for iter
173  */
174 void GNUNET_CONFIGURATION_iterate_sections (const struct
175                                             GNUNET_CONFIGURATION_Handle *cfg,
176                                             GNUNET_CONFIGURATION_Section_Iterator
177                                             iter, void *iter_cls);
178
179
180 /**
181  * Remove the given section and all options in it.
182  *
183  * @param cfg configuration to inspect
184  * @param section name of the section to remove
185  */
186 void GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle
187                                           *cfg, const char *section);
188
189 /**
190  * Get a configuration value that should be a number.
191  *
192  * @param cfg configuration to inspect
193  * @param section section of interest
194  * @param option option of interest
195  * @param number where to store the numeric value of the option
196  * @return GNUNET_OK on success, GNUNET_SYSERR on error
197  */
198 int GNUNET_CONFIGURATION_get_value_number (const struct
199                                            GNUNET_CONFIGURATION_Handle *cfg,
200                                            const char *section,
201                                            const char *option,
202                                            unsigned long long *number);
203
204
205 /**
206  * Get a configuration value that should be a relative time.
207  *
208  * @param cfg configuration to inspect
209  * @param section section of interest
210  * @param option option of interest
211  * @param time set to the time value stored in the configuration
212  * @return GNUNET_OK on success, GNUNET_SYSERR on error
213  */
214 int GNUNET_CONFIGURATION_get_value_time (const struct
215                                          GNUNET_CONFIGURATION_Handle *cfg,
216                                          const char *section,
217                                          const char *option,
218                                          struct GNUNET_TIME_Relative *time);
219
220
221 /**
222  * Test if we have a value for a particular option
223  *
224  * @param cfg configuration to inspect
225  * @param section section of interest
226  * @param option option of interest
227  * @return GNUNET_YES if so, GNUNET_NO if not.
228  */
229 int GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle
230                                      *cfg, const char *section,
231                                      const char *option);
232
233
234 /**
235  * Get a configuration value that should be a string.
236  *
237  * @param cfg configuration to inspect
238  * @param section section of interest
239  * @param option option of interest
240  * @param value will be set to a freshly allocated configuration
241  *        value, or NULL if option is not specified
242  * @return GNUNET_OK on success, GNUNET_SYSERR on error
243  */
244 int GNUNET_CONFIGURATION_get_value_string (const struct
245                                            GNUNET_CONFIGURATION_Handle *cfg,
246                                            const char *section,
247                                            const char *option, char **value);
248
249
250 /**
251  * Get a configuration value that should be the name of a file
252  * or directory.
253  *
254  * @param cfg configuration to inspect
255  * @param section section of interest
256  * @param option option of interest
257  * @param value will be set to a freshly allocated configuration
258  *        value, or NULL if option is not specified
259  * @return GNUNET_OK on success, GNUNET_SYSERR on error
260  */
261 int GNUNET_CONFIGURATION_get_value_filename (const struct
262                                              GNUNET_CONFIGURATION_Handle *cfg,
263                                              const char *section,
264                                              const char *option, char **value);
265
266 /**
267  * Iterate over the set of filenames stored in a configuration value.
268  *
269  * @param cfg configuration to inspect
270  * @param section section of interest
271  * @param option option of interest
272  * @param cb function to call on each filename
273  * @param cb_cls closure for cb
274  * @return number of filenames iterated over, -1 on error
275  */
276 int GNUNET_CONFIGURATION_iterate_value_filenames (const struct
277                                                   GNUNET_CONFIGURATION_Handle
278                                                   *cfg, const char *section,
279                                                   const char *option,
280                                                   GNUNET_FileNameCallback cb,
281                                                   void *cb_cls);
282
283 /**
284  * Iterate over values of a section in the configuration.
285  *
286  * @param cfg configuration to inspect
287  * @param section the section
288  * @param iter function to call on each option
289  * @param iter_cls closure for iter
290  */
291 void GNUNET_CONFIGURATION_iterate_section_values (const struct
292                                                   GNUNET_CONFIGURATION_Handle
293                                                   *cfg, const char *section,
294                                                   GNUNET_CONFIGURATION_Iterator
295                                                   iter, void *iter_cls);
296
297 /**
298  * Get a configuration value that should be in a set of
299  * predefined strings
300  *
301  * @param cfg configuration to inspect
302  * @param section section of interest
303  * @param option option of interest
304  * @param choices NULL-terminated list of legal values
305  * @param value will be set to an entry in the legal list,
306  *        or NULL if option is not specified and no default given
307  * @return GNUNET_OK on success, GNUNET_SYSERR on error
308  */
309 int GNUNET_CONFIGURATION_get_value_choice (const struct
310                                            GNUNET_CONFIGURATION_Handle *cfg,
311                                            const char *section,
312                                            const char *option,
313                                            const char **choices,
314                                            const char **value);
315
316 /**
317  * Get a configuration value that should be in a set of
318  * "YES" or "NO".
319  *
320  * @param cfg configuration to inspect
321  * @param section section of interest
322  * @param option option of interest
323  * @return GNUNET_YES, GNUNET_NO or if option has no valid value, GNUNET_SYSERR
324  */
325 int GNUNET_CONFIGURATION_get_value_yesno (const struct
326                                           GNUNET_CONFIGURATION_Handle *cfg,
327                                           const char *section,
328                                           const char *option);
329
330 /**
331  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
332  * where either in the "PATHS" section or the environtment
333  * "FOO" is set to "DIRECTORY".
334  *
335  * @param cfg configuration to use for path expansion
336  * @param orig string to $-expand (will be freed!)
337  * @return $-expanded string
338  */
339 char *GNUNET_CONFIGURATION_expand_dollar (const struct
340                                           GNUNET_CONFIGURATION_Handle *cfg,
341                                           char *orig);
342
343
344 /**
345  * Set a configuration value that should be a number.
346  *
347  * @param cfg configuration to update
348  * @param section section of interest
349  * @param option option of interest
350  * @param number value to set
351  */
352 void GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
353                                             *cfg, const char *section,
354                                             const char *option,
355                                             unsigned long long number);
356
357
358 /**
359  * Set a configuration value that should be a string.
360  *
361  * @param cfg configuration to update
362  * @param section section of interest
363  * @param option option of interest
364  * @param value value to set
365  */
366 void GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
367                                             *cfg, const char *section,
368                                             const char *option,
369                                             const char *value);
370
371 /**
372  * Remove a filename from a configuration value that
373  * represents a list of filenames
374  *
375  * @param cfg configuration to update
376  * @param section section of interest
377  * @param option option of interest
378  * @param value filename to remove
379  * @return GNUNET_OK on success,
380  *         GNUNET_SYSERR if the filename is not in the list
381  */
382 int GNUNET_CONFIGURATION_remove_value_filename (struct
383                                                 GNUNET_CONFIGURATION_Handle
384                                                 *cfg, const char *section,
385                                                 const char *option,
386                                                 const char *value);
387
388 /**
389  * Append a filename to a configuration value that
390  * represents a list of filenames
391  *
392  * @param cfg configuration to update
393  * @param section section of interest
394  * @param option option of interest
395  * @param value filename to append
396  * @return GNUNET_OK on success,
397  *         GNUNET_SYSERR if the filename already in the list
398  */
399 int GNUNET_CONFIGURATION_append_value_filename (struct
400                                                 GNUNET_CONFIGURATION_Handle
401                                                 *cfg, const char *section,
402                                                 const char *option,
403                                                 const char *value);
404
405
406 #if 0                           /* keep Emacsens' auto-indent happy */
407 {
408 #endif
409 #ifdef __cplusplus
410 }
411 #endif
412
413 #endif