about summary refs log tree commit diff stats
path: root/config.c
blob: 52e1b5e44e769c624a393cfe8b2322248ceaeb93 (plain) (blame)
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
#define _XOPEN_SOURCE 700

#include "config.h"

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

char config_key[KV_STRING_LIMIT] = {'\0'};
char config_value[KV_STRING_LIMIT] = {'\0'};

int config_get(size_t *llen, FILE *fp) {
	int i = 0;
	int k = 0;
	int v = 0;
	int len = 0;
	int in_key = 1;
	char *line = NULL;

	memset(config_key, '\0', KV_STRING_LIMIT);
	memset(config_value, '\0', KV_STRING_LIMIT);

	if (getline(&line, llen, fp) < 0) {
		free(line);
		return -1;
	}

	len = strlen(line);

	switch (line[0]) {
	case '=':
	case '#':
		free(line);
		return 0;

	default:
		if (isspace(line[0])) {
			free(line);
			return 0;
		}
	}

	for (i = 0; i < len; i++) {
		char c = line[i];

		switch (c) {
		case '=':
			if (in_key == 1) {
				in_key = 0;
				break;
			}

			/* fallthrough */
		default:
			if (c != '\n') {
				if (isspace(c)) {
					/* if previous char was '=' then skip */
					if (i > 0 && line[i - 1] == '=') {
						break;
					}

					/* if next char is '=' then skip */
					if (i + 1 < len && line[i + 1] == '=') {
						break;
					}
				}

				if (in_key) {
					config_key[k++] = c;
				} else {
					config_value[v++] = c;
				}
			}

			break;
		}
	}

	free(line);

	return 0;
}