File: empty_string.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 (50 lines) | stat: -rw-r--r-- 1,117 bytes parent folder | download | duplicates (3)
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
#include "check_confuse.h"
#include <stdio.h>
#include <string.h>

#ifndef HAVE_FMEMOPEN
extern FILE *fmemopen(void *buf, size_t size, const char *type);
#endif

int main(void)
{
	cfg_opt_t opts[] = {
		CFG_STR("string", "hello", CFGF_NONE),
		CFG_END()
	};
	cfg_t *cfg;
	char buf[100]; /* should be enough */
	FILE *f;

	/*
	 * override the default with a config with an empty string
	 * and then generate a temporary config file with that
	 */
	cfg = cfg_init(opts, 0);
	fail_unless(cfg_parse_buf(cfg, "string = ''") == CFG_SUCCESS);
	fail_unless(strcmp(cfg_getstr(cfg, "string"), "") == 0);
	f = fmemopen(buf, sizeof(buf), "w+");
	fail_unless(f != NULL);
	cfg_print(cfg, f);
	cfg_free(cfg);

	/*
	 * try to reload the generated temporary config file to check
	 * that the default is indeed overridden by an empty string
	 */
	cfg = cfg_init(opts, 0);
	fseek(f, 0L, SEEK_SET);
	fail_unless(cfg_parse_fp(cfg, f) == CFG_SUCCESS);
	fclose(f);
	fail_unless(strcmp(cfg_getstr(cfg, "string"), "") == 0);
	cfg_free(cfg);

	return 0;
}

/**
 * Local Variables:
 *  indent-tabs-mode: t
 *  c-file-style: "linux"
 * End:
 */