summaryrefslogtreecommitdiff
path: root/bouncer/ini.cpp
diff options
context:
space:
mode:
authorTreeki <treeki@gmail.com>2014-01-28 00:08:33 +0100
committerTreeki <treeki@gmail.com>2014-01-28 00:08:33 +0100
commitb95ed984f8bd2fe413d53d4b8677fe3d04bc1ad9 (patch)
tree5da2f4d891215fb709f7165644fd4a0c92f1aefd /bouncer/ini.cpp
parent277c08cbc35f4cb2b72f1b00ab3e5f8efd2f8fb2 (diff)
downloadbounce4-b95ed984f8bd2fe413d53d4b8677fe3d04bc1ad9.tar.gz
bounce4-b95ed984f8bd2fe413d53d4b8677fe3d04bc1ad9.zip
implement server configuration loading/saving
Diffstat (limited to '')
-rw-r--r--bouncer/ini.cpp110
1 files changed, 110 insertions, 0 deletions
diff --git a/bouncer/ini.cpp b/bouncer/ini.cpp
new file mode 100644
index 0000000..49ca38c
--- /dev/null
+++ b/bouncer/ini.cpp
@@ -0,0 +1,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);
+}
+*/