added config_set_options(), config_get_options(), setOptions(), getOptions().
[platform/upstream/libconfig.git] / examples / c / example2.c
1 /* ----------------------------------------------------------------------------
2    libconfig - A library for processing structured configuration files
3    Copyright (C) 2005-2010  Mark A Lindner
4
5    This file is part of libconfig.
6
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Lesser General Public License
9    as published by the Free Software Foundation; either version 2.1 of
10    the License, or (at your option) any later version.
11
12    This library is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Lesser General Public License for more details.
16
17    You should have received a copy of the GNU Library General Public
18    License along with this library; if not, see
19    <http://www.gnu.org/licenses/>.
20    ----------------------------------------------------------------------------
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <libconfig.h>
26
27 /* This example reads the configuration file 'example.cfg', adds a new
28  * movie record to the movies list, and writes the updated configuration to
29  * 'updated.cfg'.
30  */
31
32 int main(int argc, char **argv)
33 {
34   static const char *output_file = "updated.cfg";
35   config_t cfg;
36   config_setting_t *root, *setting, *movie;
37
38   config_init(&cfg);
39
40   /* Read the file. If there is an error, report it and exit. */
41   if(! config_read_file(&cfg, "example.cfg"))
42   {
43     fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
44             config_error_line(&cfg), config_error_text(&cfg));
45     config_destroy(&cfg);
46     return(EXIT_FAILURE);
47   }
48
49   /* Find the 'movies' setting. Add intermediate settings if they don't yet
50   * exist.
51   */
52   root = config_root_setting(&cfg);
53
54   setting = config_setting_get_member(root, "inventory");
55   if(!setting)
56     setting = config_setting_add(root, "inventory", CONFIG_TYPE_GROUP);
57
58   setting = config_setting_get_member(setting, "movies");
59   if(!setting)
60     setting = config_setting_add(setting, "movies", CONFIG_TYPE_LIST);
61
62   /* Create the new movie entry. */
63   movie = config_setting_add(setting, NULL, CONFIG_TYPE_GROUP);
64
65   setting = config_setting_add(movie, "title", CONFIG_TYPE_STRING);
66   config_setting_set_string(setting, "Buckaroo Banzai");
67
68   setting = config_setting_add(movie, "media", CONFIG_TYPE_STRING);
69   config_setting_set_string(setting, "DVD");
70
71   setting = config_setting_add(movie, "price", CONFIG_TYPE_FLOAT);
72   config_setting_set_float(setting, 12.99);
73
74   setting = config_setting_add(movie, "qty", CONFIG_TYPE_INT);
75   config_setting_set_float(setting, 20);
76
77   config_set_options(&cfg, 0);
78
79   /* Write out the updated configuration. */
80   if(! config_write_file(&cfg, output_file))
81   {
82     fprintf(stderr, "Error while writing file.\n");
83     config_destroy(&cfg);
84     return(EXIT_FAILURE);
85   }
86
87   fprintf(stderr, "Updated configuration successfully written to: %s\n",
88           output_file);
89
90   config_destroy(&cfg);
91   return(EXIT_SUCCESS);
92 }
93
94 /* eof */