Theta/modules/config.c

57 lines
2.1 KiB
C

/*
Copyright (C) 2019-2020 Toldi Balázs Ádám
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
File created at: 2019. 10. 16.
*/
#include "config.h"
Config config_load() {
Config cfg;
map_init(&cfg);
FILE *f;
f = fopen("config.ini", "r");
if (f != NULL) {
char buf[256], key[65], value[65];
while (fgets(buf, sizeof(buf), f) != NULL) {
if (buf[0] == '#') continue;
if (sscanf(buf, "%[^=]=%s\n", key, value) == 2) {
map_addPair(&cfg, map_make_pair(key, value));
memset(key, 0, 65);
memset(value, 0, 65);
memset(buf, 0, 256);
}
}
} else {
f = fopen("config.ini", "w");
fprintf(f, "#Becenév\n"
"#nickname=Pelda\n"
"#A program által használt port (Alapértelmezett: %s)\n"
"port=%s\n"
"#Ezne a porton lesz elérhető a felhasználói felület (Alapértelmezett: %s)\n"
"interface-port=%s\n"
"#Ebben a mappában vannak tárolva a html fájlok a felhasználói felülethez (Alapértelmezett: %s)\n"
"interface-folder=%s\n"
"#A felhasználói felület csak ezen a gépen érhető elő (Alapértelmezett: true;Ajánlott)\n"
"interface-local=true", DEFAULT_PORT, DEFAULT_PORT, DEFAULT_INTERFACE_PORT, DEFAULT_INTERFACE_PORT,
DEFAULT_WWW_FOLDER, DEFAULT_WWW_FOLDER);
fclose(f);
}
return cfg;
}