~ecs/ini

ini/frontend.c -rw-r--r-- 3.4 KiB
7edc48e0 — Eyal Sawady ini.1: better indentation in EXAMPLES 10 months ago
                                                                                
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* Copyright (c) 2019 Eyal Sawady
 *
 * Frontend for Ben Hoyt's inih library.
 * inih is licensed under the 3-clause BSD license, and this frontend
 * is licensed under the MIT license. See LICENSE and LICENSE.inih for
 * details.
 *
 * Interface:
 * 	0 opts:  Help message
 * 	1 opt:   List all sections in file
 * 	2 opts:  List all keys in a section
 * 	3 opts:  Value of key in section
 * 	>3 opts: Help message
 */

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "inih/ini.h"

/* Invariants:
 *   size <= capacity
 *   sections is an array of *unique* strings.
 */
struct array {
	int size;
	int capacity;
	char **sections;
};

static void deinit_array(struct array arr)
{
	int i;
	for (i = 0; i < arr.size; i++) {
		free(arr.sections[i]);
	}
	free(arr.sections);
}

static int section_list_handler(void *user, const char *section,
		const char *key, const char *value)
{
	struct array *sections = (struct array *)user;
	bool found_match = false;

	int i;
	for (i = 0; i < sections->size; i++) {
		if (strcmp(sections->sections[i], section) == 0) {
			found_match = true;
			break;
		}
	}

	if (!found_match) {
		printf("%s\n", section);
		if (sections->size + 1 > sections->capacity) {
			size_t oldbcap =
				(size_t)sections->capacity * sizeof(char *);
			sections->capacity *= 2;
			sections->sections =
				realloc(sections->sections, oldbcap * 2);
		}
		{
			size_t section_len = strlen(section);
			sections->sections[sections->size] =
				malloc(section_len + 1);
			strncpy(sections->sections[sections->size], section,
					section_len + 1);
			sections->size++;
		}
	}

	(void)key, (void)value; /* unused */
	return 1;
}

struct key_array {
	char *section;
	struct array *keys;
};

static int key_list_handler(void *user, const char *section,
		const char *key, const char *value)
{
	char *section_to_match = (char *)user;

	if (strcmp(section_to_match, section) != 0) {
		return 1;
	}

	printf("%s\n", key);

	(void)value; /* unused */
	return 1;
}

struct ini_value {
	char *section;
	char *key;
};

static int value_handler(void *user, const char *section,
		const char *key, const char *value)
{
	struct ini_value *ini_value = (struct ini_value *)user;

	if (strcmp(ini_value->section, section) != 0) {
		return 1;
	}
	if (strcmp(ini_value->key, key) != 0) {
		return 1;
	}

	printf("%s\n", value);
	return 1;
}

/* Exit status:
 *   0: Success
 *   1: Improper usage
 *   2: Could not load INI file
 *
 * In order to avoid complexity, if there's no match, the exit status
 * will be 0 and nothing will be printed.
 */
int main(int argc, char **argv)
{
	FILE *file;
	if (argc >= 2) {
		if (!strcmp(argv[1], "-")) {
			file = stdin;
		} else {
			file = fopen(argv[1], "r");
		}
	}
	switch (argc) {
	case 2:
	{
		struct array sections = {0, 8, NULL};
		sections.sections = malloc(8 * sizeof(char *));

		if (ini_parse_file(file, section_list_handler, &sections) < 0) {
			deinit_array(sections);
			goto fail;
		}
		deinit_array(sections);
		break;
	}
	case 3:
		if (ini_parse_file(file, key_list_handler, argv[2]) < 0) {
			goto fail;
		}
		break;
	case 4:
	{
		struct ini_value ini_value;
		ini_value.section = argv[2];
		ini_value.key = argv[3];

		if (ini_parse_file(file, value_handler, &ini_value) < 0) {
			goto fail;
		}
		break;
	}
	default:
		printf("Usage: %s <file> [<section> [<key>]]\n", argv[0]);
		if (argc >= 2) {
			fclose(file);
		}
		return 1;
	}
	fclose(file);
	return 0;
fail:
	fclose(file);
	printf("Can't load '%s'\n", argv[1]);
	return 2;
}