more work on config

This commit is contained in:
Sebastian Krzyszkowiak 2012-02-26 00:47:41 +01:00
parent 537dac3cfb
commit a88099b9d5
10 changed files with 60 additions and 14 deletions

View file

@ -21,6 +21,8 @@
Keys CANNOT be repeated in the same section. Keys CANNOT be repeated in the same section.
Rewriting config file removes all comments from it. Rewriting config file removes all comments from it.
If config file is not following this guides, it may be handled incorrectly.
*/ */
struct ConfigOption { struct ConfigOption {
@ -32,6 +34,26 @@ struct ConfigOption {
struct ConfigOption *config; struct ConfigOption *config;
void AppendToConfig(char* section, char* name, char* value) {
struct ConfigOption *new = malloc(sizeof(struct ConfigOption));
new->next = NULL;
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 {
old->next = new;
}
}
void InitConfig() { void InitConfig() {
FILE *file = fopen("SuperDerpy.ini","r+"); FILE *file = fopen("SuperDerpy.ini","r+");
if (! file) { return; } if (! file) { return; }
@ -67,7 +89,19 @@ void InitConfig() {
fclose(file); fclose(file);
} }
char* ReadConfigOption(char* section, char* name) { 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; struct ConfigOption *old = config;
char *ret = malloc(sizeof(char)*255); char *ret = malloc(sizeof(char)*255);
while (old!=NULL) { while (old!=NULL) {
@ -81,11 +115,21 @@ char* ReadConfigOption(char* section, char* name) {
return NULL; return NULL;
} }
char* GetConfigOptionDefault(char* section, char* name, char* def) {
char* ret = GetConfigOption(section, name);
if (!ret) return def; else return ret;
}
void DeinitConfig() { void DeinitConfig() {
FILE *file = fopen("SuperDerpy.ini","w+"); FILE *file = fopen("SuperDerpy.ini","w");
//char* section[255] = {}; char section[255] = {};
struct ConfigOption *old = config; struct ConfigOption *old = config;
while (old!=NULL) { while (old!=NULL) {
if (strcmp(section, old->section)) {
strcpy(section, old->section);
fprintf(file, "%s\n", section);
}
fprintf(file, "%s=%s\n", old->name, old->value);
struct ConfigOption *prev = old; struct ConfigOption *prev = old;
old=old->next; old=old->next;
free(prev->name); free(prev->name);

View file

@ -1,3 +1,5 @@
void InitConfig(); void InitConfig();
char* ReadConfigOption(char* section, char* name); char* GetConfigOption(char* section, char* name);
char* GetConfigOptionDefault(char* section, char* name, char* def);
void SetConfigOption(char* section, char* name, char* value);
void DeinitConfig(); void DeinitConfig();

View file

@ -72,7 +72,7 @@ struct Game {
ALLEGRO_EVENT_QUEUE *event_queue; ALLEGRO_EVENT_QUEUE *event_queue;
ALLEGRO_TIMER *timer; ALLEGRO_TIMER *timer;
ALLEGRO_BITMAP *console; ALLEGRO_BITMAP *console;
bool showconsole; bool showconsole, fx, music, fullscreen;
struct Menu menu; struct Menu menu;
struct Loading loading; struct Loading loading;
struct Intro intro; struct Intro intro;