File: simple.c

package info (click to toggle)
libconfuse 3.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,192 kB
  • sloc: ansic: 5,532; lex: 451; xml: 439; makefile: 213; sh: 39
file content (77 lines) | stat: -rw-r--r-- 1,944 bytes parent folder | download | duplicates (4)
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
#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <locale.h>
#include "confuse.h"

int main(void)
{
	static cfg_bool_t verbose = cfg_false;
	static char *server = NULL;
	static double delay = 1.356e-32;
	static char *username = NULL;

	/* Although the macro used to specify an integer option is called
	 * CFG_SIMPLE_INT(), it actually expects a long int. On a 64 bit system
	 * where ints are 32 bit and longs 64 bit (such as the x86-64 or amd64
	 * architectures), you will get weird effects if you use an int here.
	 *
	 * If you use the regular (non-"simple") options, ie CFG_INT() and use
	 * cfg_getint(), this is not a problem as the data types are implicitly
	 * cast.
	 */
	static long int debug = 1;

	cfg_opt_t opts[] = {
		CFG_SIMPLE_BOOL("verbose", &verbose),
		CFG_SIMPLE_STR("server", &server),
		CFG_SIMPLE_STR("user", &username),
		CFG_SIMPLE_INT("debug", &debug),
		CFG_SIMPLE_FLOAT("delay", &delay),
		CFG_END()
	};
	cfg_t *cfg;

	/* Localize messages & types according to environment, since v2.9 */
#ifdef LC_MESSAGES
	setlocale(LC_MESSAGES, "");
	setlocale(LC_CTYPE, "");
#endif

	/* set default value for the server option */
	server = strdup("gazonk");

	cfg = cfg_init(opts, 0);
	cfg_parse(cfg, "simple.conf");

	printf("verbose: %s\n", verbose ? "true" : "false");
	printf("server: %s\n", server);
	printf("username: %s\n", username);
	printf("debug: %ld\n", debug);
	printf("delay: %G\n", delay);

	printf("setting username to 'foo'\n");
	/* using cfg_setstr here is not necessary at all, the equivalent
	 * code is:
	 *   free(username);
	 *   username = strdup("foo");
	 */
	cfg_setstr(cfg, "user", "foo");
	printf("username: %s\n", username);

	/* print the parsed values to another file */
	{
		FILE *fp = fopen("simple.conf.out", "w");

		cfg_print(cfg, fp);
		fclose(fp);
	}

	cfg_free(cfg);

	/* You are responsible for freeing string values. */
	free(server);
	free(username);

	return 0;
}