File: test.c

package info (click to toggle)
libiio 0.26-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,832 kB
  • sloc: ansic: 21,880; python: 1,844; cs: 1,232; sh: 1,062; cpp: 688; yacc: 441; xml: 192; lex: 172; makefile: 40
file content (72 lines) | stat: -rw-r--r-- 1,338 bytes parent folder | download | duplicates (2)
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

#include <alloca.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <ini.h>

int main(int argc, char **argv)
{
	struct INI *ini;

	if (argc < 2) {
		printf("USAGE: test [INI_FILE]...\n");
		return EXIT_SUCCESS;
	}

	ini = ini_open(argv[1]);
	if (!ini)
		return EXIT_FAILURE;
	printf("INI file opened.\n");

	while (1) {
		const char *buf;
		char *name;
		size_t name_len;
		int res = ini_next_section(ini, &buf, &name_len);
		if (!res) {
			printf("End of file.\n");
			break;
		}
		if (res < 0) {
			printf("ERROR: code %i\n", res);
			goto error;
		}

		name = alloca(name_len + 1);
		name[name_len] = '\0';
		memcpy(name, buf, name_len);
		printf("Opening section: \'%s\'\n", name);

		while (1) {
			const char *buf2;
			char *key, *value;
			size_t key_len, value_len;
			res = ini_read_pair(ini, &buf, &key_len, &buf2, &value_len);
			if (!res) {
				printf("No more data.\n");
				break;
			}
			if (res < 0) {
				printf("ERROR: code %i\n", res);
				goto error;
			}

			key = alloca(key_len + 1);
			key[key_len] = '\0';
			memcpy(key, buf, key_len);
			value = alloca(value_len + 1);
			value[value_len] = '\0';
			memcpy(value, buf2, value_len);
			printf("Reading key: \'%s\' value: \'%s\'\n", key, value);
		}
	}

	ini_close(ini);
	return EXIT_SUCCESS;

error:
	ini_close(ini);
	return EXIT_FAILURE;
}