summaryrefslogtreecommitdiffstats
path: root/src/config-ini.c
blob: 7cfa1ffa72930a052ce1bb04eac205dfaf84ed1a (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
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
#include "config-ini.h"
#include "types.h"

#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void free_field(ini_field_t *field) {
	free(field->key);
	free(field->value);
	free(field);
}

static void free_section(ini_section_t *section) {
	ini_field_t *field, *tmp;

	list_for_each_entry_safe(field, tmp, &section->fields, node)
		free_field(field);

	free(section->name);
	free(section);
}

void free_ini_file(ini_file_t *file) {
	ini_section_t *section, *tmp;

	list_for_each_entry_safe(section, tmp, &file->sections, node)
		free_section(section);

	free(file);
}

static bool add_field(ini_section_t *section, const char *key, const char *value) {
	ini_field_t *field = calloc(1, sizeof(*field));
	if (!field)
		return false;

	field->key = strdup(key);
	field->value = strdup(value);

	if (!field->key || !field->value) {
		free_field(field);
		return false;
	}

	list_add_tail(&field->node, &section->fields);

	return true;
}

static ini_section_t * add_section(ini_file_t *file, const char *name) {
	ini_section_t *section = calloc(1, sizeof(*section));
	if (!section)
		return NULL;
	
	section->name = strdup(name);
	if (!section->name) {
		free(section);
		return false;
	}

	INIT_LIST_HEAD(&section->fields);
	list_add_tail(&section->node, &file->sections);

	return section;
}

ini_file_t * read_ini_file(const char *filename) {
	FILE *f = fopen(filename, "r");
	if (!f)
		return NULL;
	
	ini_file_t *file = calloc(1, sizeof(*file));
	if (!file)
		goto error;

	INIT_LIST_HEAD(&file->sections);

	ini_section_t *section = NULL;
	int err = 0;

	char *line = NULL;
	size_t n = 0;

	while (getline(&line, &n, f) >= 0) {
		char *input = line;

		while (isspace(input[0]))
			input++;
		
		if (input[0] == '#' || input[0] == '\0')
			continue;

		size_t len = strlen(input);

		while (isspace(input[len-1]))
			len--;

		if (input[0] == '[') {
			if (input[len-1] != ']') {
				err = EINVAL;
				goto error;
			}

			input[len-1] = '\0';

			section = add_section(file, input+1);
			if (!section)
				goto error;
		} else {
			if (!section) {
				err = EINVAL;
				goto error;
			}

			input[len] = '\0';

			char *delim = strchr(input, '=');
			if (!delim) {
				err = EINVAL;
				goto error;
			}
			
			*delim = '\0';

			if (!add_field(section, input, delim+1))
				goto error;
		}
	}

	if (ferror(f))
		err = EIO;

error:
	free(line);
	fclose(f);

	if (err) {
		free_ini_file(file);
		errno = err;
		return NULL;
	}

	return file;
}