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