summaryrefslogtreecommitdiff
path: root/bouncer/ini.cpp
blob: 49ca38cf00f87cf505d6d169ce8306fb52d412fe (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
#include "ini.h"
#include <stdio.h>
#include <string.h>


std::list<INI::Section> INI::load(const char *path) {
	std::list<Section> sections;

	FILE *f = fopen(path, "r");
	if (!f)
		return sections;

	Section *section = 0;

	for (;;) {
		char line[4096];
		if (!fgets(line, sizeof(line), f))
			break;

		int length = strlen(line);

		// Get rid of the newline
		if ((length > 0) && (line[length - 1] == '\n')) {
			line[length - 1] = 0;
			--length;
		}

		// Ignore empty lines, comments
		if (!length)
			continue;
		if (line[0] == ';')
			continue;
		if (line[0] == '#')
			continue;

		// New section?
		if ((length > 2) && (line[0] == '[') && (line[length - 1] == ']')) {
			line[length - 1] = 0;

			sections.push_back(Section());
			section = &sections.back();
			section->title = &line[1];

		} else if (section) {
			// We can only add values to a section if we have one :p

			char *eq = strchr(line, '=');
			if (eq) {
				*eq = 0;
				char *value = eq + 1;

				section->data[line] = value;
			}
		}
	}

	fclose(f);

	return sections;
}


bool INI::save(const char *path, const std::list<INI::Section> &sections) {
	FILE *f = fopen(path, "w");
	if (!f)
		return false;


	for (auto &section : sections) {
		fputc('[', f);
		fputs(section.title.c_str(), f);
		fputs("]\n\n", f);

		for (auto &i : section.data) {
			fputs(i.first.c_str(), f);
			fputc('=', f);
			fputs(i.second.c_str(), f);
			fputc('\n', f);
		}

		fputc('\n', f);
	}


	fclose(f);

	return true;
}



/*
int main(int argc, char **argv) {
	INI::Section a, b;

	a.title = "boop";
	a.data["a"] = "b";
	a.data["c"] = "d";

	b.title = "boop2";
	b.data["a"] = "b";
	b.data["c"] = "d";

	std::list<INI::Section> sections = { a, b };
	INI::save("tryme.ini", sections);

	std::list<INI::Section> munged = INI::load("tryme.ini");
	INI::save("tryme2.ini", munged);
}
*/