summaryrefslogtreecommitdiff
path: root/lib/iniparser/test/iniexample.c
blob: 5e8e71cdf008a6c9b2bfabdf8e930dd728ed7e59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "iniparser.h"

void create_example_ini_file(void);
int  parse_ini_file(char * ini_name);

int main(int argc, char * argv[])
{
	int		status ;

	if (argc<2) {
		create_example_ini_file();
		status = parse_ini_file("example.ini");
	} else {
		status = parse_ini_file(argv[1]);
	}
	return status ;
}

void create_example_ini_file(void)
{
	FILE	*	ini ;

	ini = fopen("example.ini", "w");
	fprintf(ini, "\n\
#\n\
# This is an example of ini file\n\
#\n\
\n\
[Pizza]\n\
\n\
Ham       = yes ;\n\
Mushrooms = TRUE ;\n\
Capres    = 0 ;\n\
Cheese    = NO ;\n\
\n\
\n\
[Wine]\n\
\n\
Grape     = Cabernet Sauvignon ;\n\
Year      = 1989 ;\n\
Country   = Spain ;\n\
Alcohol   = 12.5  ;\n\
\n\
#\n\
# end of file\n\
#\n");

	fclose(ini);
}


int parse_ini_file(char * ini_name)
{
	dictionary	*	ini ;

	/* Some temporary variables to hold query results */
	int				b ;
	int				i ;
	double			d ;
	char		*	s ;

	ini = iniparser_load(ini_name);
	if (ini==NULL) {
		fprintf(stderr, "cannot parse file [%s]", ini_name);
		return -1 ;
	}
	iniparser_dump(ini, stderr);

	/* Get pizza attributes */
	printf("Pizza:\n");

	b = iniparser_getboolean(ini, "pizza:ham", -1);
	printf("Ham:       [%d]\n", b);
	b = iniparser_getboolean(ini, "pizza:mushrooms", -1);
	printf("Mushrooms: [%d]\n", b);
	b = iniparser_getboolean(ini, "pizza:capres", -1);
	printf("Capres:    [%d]\n", b);
	b = iniparser_getboolean(ini, "pizza:cheese", -1);
	printf("Cheese:    [%d]\n", b);

	/* Get wine attributes */
	printf("Wine:\n");
	s = iniparser_getstr(ini, "wine:grape");
	if (s) {
		printf("grape:     [%s]\n", s);
	} else {
		printf("grape:     not found\n");
	}
	i = iniparser_getint(ini, "wine:year", -1);
	if (i>0) {
		printf("year:      [%d]\n", i);
	} else {
		printf("year:      not found\n");
	}
	s = iniparser_getstr(ini, "wine:country");
	if (s) {
		printf("country:   [%s]\n", s);
	} else {
		printf("country:   not found\n");
	}
	d = iniparser_getdouble(ini, "wine:alcohol", -1.0);
	if (d>0.0) {
		printf("alcohol:   [%g]\n", d);
	} else {
		printf("alcohol:   not found\n");
	}

	iniparser_freedict(ini);
	return 0 ;
}