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