project_files/frontlib/model/cfg.c
author Medo <smaxein@googlemail.com>
Sat, 09 Jun 2012 03:28:38 +0200
changeset 7179 f84805e6df03
parent 7177 bf6cf4dd847a
child 7224 5143861c83bd
permissions -rw-r--r--
Implemented game launching API for the frontlib. It is still buggy though, and not all game settings can be conveniently created/modified yet.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
#include "cfg.h"
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
#include "../iniparser/iniparser.h"
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
#include "../iniparser/dictionary.h"
7179
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
     5
#include "../util/inihelper.h"
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
     6
#include "../util/logging.h"
f84805e6df03 Implemented game launching API for the frontlib.
Medo <smaxein@googlemail.com>
parents: 7177
diff changeset
     7
#include "../util/util.h"
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
#include <stdio.h>
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
static void freeCfgMeta(flib_cfg_meta *cfg) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
	if(cfg) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
		if(cfg->settings) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
			for(int i=0; i<cfg->settingCount; i++) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
				free(cfg->settings[i].iniName);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
				free(cfg->settings[i].title);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
				free(cfg->settings[i].engineCommand);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
				free(cfg->settings[i].image);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
			}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
			free(cfg->settings);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
		}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
		if(cfg->mods) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
			for(int i=0; i<cfg->modCount; i++) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
				free(cfg->mods[i].iniName);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
			}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
			free(cfg->mods);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
		}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
		free(cfg);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
flib_cfg_meta *flib_cfg_meta_from_ini(const char *settingpath, const char *modpath) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
	if(!settingpath || !modpath) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
		return NULL;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
	flib_cfg_meta *result = calloc(1, sizeof(flib_cfg_meta));
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
	dictionary *settingfile = iniparser_load(settingpath);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
	dictionary *modfile = iniparser_load(modpath);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
	if(!result || !settingfile || !modfile) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
		goto handleError;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
	result->settingCount = iniparser_getnsec(settingfile);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
	result->modCount = iniparser_getnsec(modfile);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
	result->settings = calloc(result->settingCount, sizeof(flib_cfg_setting_meta));
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
	result->mods = calloc(result->modCount, sizeof(flib_cfg_mod_meta));
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
	if(!result->settings || !result->mods) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
		goto handleError;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
	for(int i=0; i<result->settingCount; i++) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
		char *sectionName = iniparser_getsecname(settingfile, i);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
		if(!sectionName) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
			goto handleError;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
		}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
		bool error = false;
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents: 7175
diff changeset
    60
		result->settings[i].iniName = flib_strdupnull(sectionName);
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
		result->settings[i].title = inihelper_getstringdup(settingfile, &error, sectionName, "title");
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
		result->settings[i].engineCommand = inihelper_getstringdup(settingfile, &error, sectionName, "command");
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
		result->settings[i].image = inihelper_getstringdup(settingfile, &error, sectionName, "image");
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
		result->settings[i].checkOverMax = inihelper_getbool(settingfile, &error, sectionName, "checkOverMax");
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
		result->settings[i].times1000 = inihelper_getbool(settingfile, &error, sectionName, "times1000");
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
		result->settings[i].min = inihelper_getint(settingfile, &error, sectionName, "min");
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
		result->settings[i].max = inihelper_getint(settingfile, &error, sectionName, "max");
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
		result->settings[i].def = inihelper_getint(settingfile, &error, sectionName, "default");
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
		if(error) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
			flib_log_e("Missing or malformed ini parameter in file %s, section %s", settingpath, sectionName);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
			goto handleError;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
		}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
	for(int i=0; i<result->modCount; i++) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
		char *sectionName = iniparser_getsecname(modfile, i);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
		if(!sectionName) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
			goto handleError;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
		}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
		bool error = false;
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents: 7175
diff changeset
    82
		result->mods[i].iniName = flib_strdupnull(sectionName);
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
		result->mods[i].bitmaskIndex = inihelper_getint(modfile, &error, sectionName, "bitmaskIndex");
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
		if(error) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
			flib_log_e("Missing or malformed ini parameter in file %s, section %s", modpath, sectionName);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    86
			goto handleError;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    87
		}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    88
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    89
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    90
	iniparser_freedict(settingfile);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    91
	iniparser_freedict(modfile);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    92
	return result;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    93
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
	handleError:
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    95
	freeCfgMeta(result);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    96
	iniparser_freedict(settingfile);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    97
	iniparser_freedict(modfile);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    98
	return NULL;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
    99
}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   100
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   101
void flib_cfg_meta_destroy(flib_cfg_meta *metainfo) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   102
	freeCfgMeta(metainfo);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   103
}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   104
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   105
flib_cfg *flib_cfg_create(const flib_cfg_meta *meta, const char *schemeName) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   106
	flib_cfg *result = calloc(1, sizeof(flib_cfg));
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   107
	if(!meta || !result || !schemeName) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   108
		return NULL;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   109
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   110
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   111
	result->modCount = meta->modCount;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   112
	result->settingCount = meta->settingCount;
7177
bf6cf4dd847a Implemented public API for letting the engine render maps
Medo <smaxein@googlemail.com>
parents: 7175
diff changeset
   113
	result->schemeName = flib_strdupnull(schemeName);
7175
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   114
	result->mods = calloc(meta->modCount, sizeof(*result->mods));
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   115
	result->settings = calloc(meta->settingCount, sizeof(*result->settings));
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   116
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   117
	if(!result->mods || !result->settings || !result->schemeName) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   118
		flib_cfg_destroy(result);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   119
		return NULL;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   120
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   121
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   122
	for(int i=0; i<meta->settingCount; i++) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   123
		result->settings[i] = meta->settings[i].def;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   124
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   125
	return result;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   126
}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   127
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   128
flib_cfg *flib_cfg_from_ini_handleError(flib_cfg *result, dictionary *settingfile) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   129
	iniparser_freedict(settingfile);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   130
	flib_cfg_destroy(result);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   131
	return NULL;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   132
}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   133
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   134
flib_cfg *flib_cfg_from_ini(const flib_cfg_meta *meta, const char *filename) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   135
	if(!meta || !filename) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   136
		return NULL;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   137
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   138
	dictionary *settingfile = iniparser_load(filename);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   139
	if(!settingfile) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   140
		return NULL;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   141
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   142
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   143
	bool error = false;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   144
	char *schemename = inihelper_getstring(settingfile, &error, "Scheme", "name");
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   145
	if(!schemename) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   146
		return flib_cfg_from_ini_handleError(NULL, settingfile);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   147
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   148
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   149
	flib_cfg *result = flib_cfg_create(meta, schemename);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   150
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   151
	for(int i=0; i<meta->settingCount; i++) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   152
		char *key = inihelper_createDictKey("BasicSettings", meta->settings[i].iniName);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   153
		if(!key) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   154
			return flib_cfg_from_ini_handleError(result, settingfile);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   155
		}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   156
		result->settings[i] = iniparser_getint(settingfile, key, meta->settings[i].def);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   157
		free(key);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   158
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   159
	for(int i=0; i<meta->modCount; i++) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   160
		char *key = inihelper_createDictKey("GameMods", meta->mods[i].iniName);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   161
		if(!key) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   162
			return flib_cfg_from_ini_handleError(result, settingfile);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   163
		}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   164
		result->mods[i] = iniparser_getboolean(settingfile, key, false);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   165
		free(key);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   166
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   167
	iniparser_freedict(settingfile);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   168
	return result;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   169
}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   170
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   171
int flib_cfg_to_ini(const flib_cfg_meta *meta, const char *filename, const flib_cfg *config) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   172
	int result = -1;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   173
	if(meta && filename && config && config->modCount==meta->modCount && config->settingCount==meta->settingCount) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   174
		dictionary *dict = dictionary_new(0);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   175
		if(dict) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   176
			bool error = false;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   177
			// Add the sections
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   178
			error |= iniparser_set(dict, "Scheme", NULL);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   179
			error |= iniparser_set(dict, "BasicSettings", NULL);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   180
			error |= iniparser_set(dict, "GameMods", NULL);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   181
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   182
			// Add the values
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   183
			error |= inihelper_setstr(dict, "Scheme", "name", config->schemeName);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   184
			for(int i=0; i<config->settingCount; i++) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   185
				error |= inihelper_setint(dict, "BasicSettings", meta->settings[i].iniName, config->settings[i]);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   186
			}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   187
			for(int i=0; i<config->modCount; i++) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   188
				error |= inihelper_setbool(dict, "GameMods", meta->mods[i].iniName, config->mods[i]);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   189
			}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   190
			if(!error) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   191
				FILE *inifile = fopen(filename, "wb");
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   192
				if(inifile) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   193
					iniparser_dump_ini(dict, inifile);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   194
					fclose(inifile);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   195
					result = 0;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   196
				}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   197
			}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   198
			dictionary_del(dict);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   199
		}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   200
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   201
	return result;
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   202
}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   203
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   204
void flib_cfg_destroy(flib_cfg* cfg) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   205
	if(cfg) {
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   206
		free(cfg->mods);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   207
		free(cfg->settings);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   208
		free(cfg->schemeName);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   209
		free(cfg);
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   210
	}
038e3415100a Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff changeset
   211
}