1 /* ----------------------------------------------------------------------------
2    libconfig - A library for processing structured configuration files
3    Copyright (C) 2005-2018  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' and displays
28  * some of its contents.
29  */
30 
main(int argc,char ** argv)31 int main(int argc, char **argv)
32 {
33   config_t cfg;
34   config_setting_t *setting;
35   const char *str;
36 
37   config_init(&cfg);
38 
39   /* Read the file. If there is an error, report it and exit. */
40   if(! config_read_file(&cfg, "example.cfg"))
41   {
42     fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
43             config_error_line(&cfg), config_error_text(&cfg));
44     config_destroy(&cfg);
45     return(EXIT_FAILURE);
46   }
47 
48   /* Get the store name. */
49   if(config_lookup_string(&cfg, "name", &str))
50     printf("Store name: %s\n\n", str);
51   else
52     fprintf(stderr, "No 'name' setting in configuration file.\n");
53 
54   /* Output a list of all books in the inventory. */
55   setting = config_lookup(&cfg, "inventory.books");
56   if(setting != NULL)
57   {
58     int count = config_setting_length(setting);
59     int i;
60 
61     printf("%-30s  %-30s   %-6s  %s\n", "TITLE", "AUTHOR", "PRICE", "QTY");
62 
63     for(i = 0; i < count; ++i)
64     {
65       config_setting_t *book = config_setting_get_elem(setting, i);
66 
67       /* Only output the record if all of the expected fields are present. */
68       const char *title, *author;
69       double price;
70       int qty;
71 
72       if(!(config_setting_lookup_string(book, "title", &title)
73            && config_setting_lookup_string(book, "author", &author)
74            && config_setting_lookup_float(book, "price", &price)
75            && config_setting_lookup_int(book, "qty", &qty)))
76         continue;
77 
78       printf("%-30s  %-30s  $%6.2f  %3d\n", title, author, price, qty);
79     }
80     putchar('\n');
81   }
82 
83   /* Output a list of all movies in the inventory. */
84   setting = config_lookup(&cfg, "inventory.movies");
85   if(setting != NULL)
86   {
87     unsigned int count = config_setting_length(setting);
88     unsigned int i;
89 
90     printf("%-30s  %-10s   %-6s  %s\n", "TITLE", "MEDIA", "PRICE", "QTY");
91     for(i = 0; i < count; ++i)
92     {
93       config_setting_t *movie = config_setting_get_elem(setting, i);
94 
95       /* Only output the record if all of the expected fields are present. */
96       const char *title, *media;
97       double price;
98       int qty;
99 
100       if(!(config_setting_lookup_string(movie, "title", &title)
101            && config_setting_lookup_string(movie, "media", &media)
102            && config_setting_lookup_float(movie, "price", &price)
103            && config_setting_lookup_int(movie, "qty", &qty)))
104         continue;
105 
106       printf("%-30s  %-10s  $%6.2f  %3d\n", title, media, price, qty);
107     }
108     putchar('\n');
109   }
110 
111   config_destroy(&cfg);
112   return(EXIT_SUCCESS);
113 }
114 
115 /* eof */
116