libsuperderpy/src/config.c

170 lines
5 KiB
C
Raw Normal View History

2012-02-28 13:09:12 +01:00
/*! \file config.c
* \brief Configuration manager code.
*
* Please keep in mind that config file has to be well-formed in order to function properly.
*
* Lines starting with '[' contain section name. No additional whitespace
* between braces and name is allowed.
*
* Section CANNOT be declared multiple times.
*
* Lines starting with '#' are ignored.
2012-02-28 23:16:55 +01:00
*
2012-02-28 13:09:12 +01:00
* All other lines have to look like this one:
2012-02-28 23:16:55 +01:00
*
2012-02-28 13:09:12 +01:00
* key=value
*
* All whitespace at beginning, end or around '=' char will belong to key or value.
* If multiple '=' are present, the first one is used to split line into key and value.
*
* Keys CANNOT be repeated in the same section.
*
* Rewriting config file removes all comments from it.
*
* If config file is not following this guides, it may be handled incorrectly.
*/
2012-03-04 13:32:42 +01:00
/*
* Copyright (c) Sebastian Krzyszkowiak <dos@dosowisko.net>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
2012-02-24 13:03:30 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
2012-02-29 12:16:11 +01:00
#include "config.h"
2012-02-24 13:03:30 +01:00
2012-02-28 23:16:55 +01:00
/*! \brief One config option in list of options. */
2012-02-24 13:03:30 +01:00
struct ConfigOption {
2012-02-28 23:16:55 +01:00
char* name; /*!< Name of the config entry. */
char* value; /*!< Value of the config entry. */
char* section; /*!< Section of the config entry (including braces). */
struct ConfigOption *next; /*!< Pointer to next option in list, or NULL if there is no more. */
2012-02-24 13:03:30 +01:00
};
2012-02-28 23:16:55 +01:00
struct ConfigOption *config; /*!< Pointer to first config entry in list. */
2012-02-24 13:03:30 +01:00
2012-02-28 23:16:55 +01:00
/*! \brief Adds new config entry. */
2012-02-26 00:47:41 +01:00
void AppendToConfig(char* section, char* name, char* value) {
struct ConfigOption *new = malloc(sizeof(struct ConfigOption));
new->next = NULL;
new->section = malloc(sizeof(char)*255);
new->name = malloc(sizeof(char)*255);
new->value = malloc(sizeof(char)*255);
2012-02-26 00:47:41 +01:00
strcpy(new->section, section);
strcpy(new->name, name);
strcpy(new->value, value);
if (config==NULL) config = new;
struct ConfigOption *old = config;
while (old->next != NULL) {
if (!strcmp(old->section, section)) break;
old=old->next;
}
if (old->next) {
new->next = old->next;
old->next = new;
} else {
2012-03-01 22:46:54 +01:00
if (old!=new) old->next = new;
2012-02-26 00:47:41 +01:00
}
}
2012-02-24 13:03:30 +01:00
void InitConfig() {
FILE *file = fopen("SuperDerpy.ini","r+");
2012-02-25 23:22:47 +01:00
if (! file) { return; }
2012-02-24 13:03:30 +01:00
char string[255];
char section[255] = "[SuperDerpy]";
2012-02-24 13:03:30 +01:00
struct ConfigOption *old = NULL;
while ( fgets (string , 255 , file) != NULL ) {
if ((string[0]=='#') || (string[0]=='\n')) { continue; }
2012-02-24 13:03:30 +01:00
if (string[strlen(string)-1]=='\n') string[strlen(string)-1]='\0';
if (string[0]=='[') { strcpy(section, string); continue; }
2012-02-24 13:03:30 +01:00
bool before=true;
struct ConfigOption *new = malloc(sizeof(struct ConfigOption));
if (old==NULL) {
config = new;
2012-02-24 13:03:30 +01:00
old = new;
} else { old->next = new; old = new; }
new->section = malloc(sizeof(char)*255);
strcpy(new->section, section);
new->name = malloc(sizeof(char)*255);
new->name[0] = '\0';
new->value = malloc(sizeof(char)*255);
new->value[0] = '\0';
new->next = NULL;
2012-02-29 12:16:11 +01:00
int i;
for (i=0; i<strlen(string); i++) {
2012-02-24 13:03:30 +01:00
if (string[i]=='=') { before=false; continue; }
char temp[2];
temp[0]=string[i];
temp[1]='\0';
if (before) strcat(new->name, temp);
else strcat(new->value, temp);
//printf("%s", temp);
}
}
fclose(file);
2012-02-24 13:03:30 +01:00
}
2012-02-26 00:47:41 +01:00
void SetConfigOption(char* section, char* name, char* value) {
struct ConfigOption *old = config;
while (old!=NULL) {
if (!strcmp(section, old->section) && !strcmp(name, old->name)) {
strcpy(old->value, value);
return;
}
old=old->next;
}
AppendToConfig(section, name, value);
}
char* GetConfigOption(char* section, char* name) {
struct ConfigOption *old = config;
2012-02-24 13:03:30 +01:00
char *ret = malloc(sizeof(char)*255);
while (old!=NULL) {
if (!strcmp(section, old->section) && !strcmp(name, old->name)) {
strcpy(ret, old->value);
return ret;
}
old=old->next;
}
free(ret);
return NULL;
}
2012-02-26 00:47:41 +01:00
char* GetConfigOptionDefault(char* section, char* name, char* def) {
char* ret = GetConfigOption(section, name);
if (!ret) return def; else return ret;
}
2012-02-24 13:03:30 +01:00
void DeinitConfig() {
2012-02-26 00:47:41 +01:00
FILE *file = fopen("SuperDerpy.ini","w");
char section[255] = {};
struct ConfigOption *old = config;
2012-02-24 13:03:30 +01:00
while (old!=NULL) {
2012-02-26 00:47:41 +01:00
if (strcmp(section, old->section)) {
strcpy(section, old->section);
fprintf(file, "%s\n", section);
}
fprintf(file, "%s=%s\n", old->name, old->value);
2012-02-24 13:03:30 +01:00
struct ConfigOption *prev = old;
old=old->next;
free(prev->name);
free(prev->value);
free(prev->section);
free(prev);
}
2012-02-25 23:22:47 +01:00
fclose(file);
2012-02-26 00:47:41 +01:00
}