project_files/frontlib/model/cfg.c
changeset 7227 1c859f572d72
parent 7224 5143861c83bd
child 7230 240620f46dd7
--- a/project_files/frontlib/model/cfg.c	Tue Jun 12 11:25:05 2012 +0200
+++ b/project_files/frontlib/model/cfg.c	Tue Jun 12 21:10:11 2012 +0200
@@ -1,17 +1,16 @@
 #include "cfg.h"
 
-#include "../iniparser/iniparser.h"
-#include "../iniparser/dictionary.h"
 #include "../util/inihelper.h"
 #include "../util/logging.h"
 #include "../util/util.h"
 
 #include <stdio.h>
+#include <stdlib.h>
 
-static flib_cfg_meta *flib_cfg_meta_from_ini_handleError(flib_cfg_meta *result, dictionary *settingfile, dictionary *modfile) {
+static flib_cfg_meta *flib_cfg_meta_from_ini_handleError(flib_cfg_meta *result, flib_ini *settingfile, flib_ini *modfile) {
 	flib_cfg_meta_destroy(result);
-	iniparser_freedict(settingfile);
-	iniparser_freedict(modfile);
+	flib_ini_destroy(settingfile);
+	flib_ini_destroy(modfile);
 	return NULL;
 }
 
@@ -21,15 +20,15 @@
 		return NULL;
 	}
 	flib_cfg_meta *result = flib_calloc(1, sizeof(flib_cfg_meta));
-	dictionary *settingfile = iniparser_load(settingpath);
-	dictionary *modfile = iniparser_load(modpath);
+	flib_ini *settingfile = flib_ini_load(settingpath);
+	flib_ini *modfile = flib_ini_load(modpath);
 
 	if(!result || !settingfile || !modfile) {
 		return flib_cfg_meta_from_ini_handleError(result, settingfile, modfile);
 	}
 
-	result->settingCount = iniparser_getnsec(settingfile);
-	result->modCount = iniparser_getnsec(modfile);
+	result->settingCount = flib_ini_get_sectioncount(settingfile);
+	result->modCount = flib_ini_get_sectioncount(modfile);
 	result->settings = flib_calloc(result->settingCount, sizeof(flib_cfg_setting_meta));
 	result->mods = flib_calloc(result->modCount, sizeof(flib_cfg_mod_meta));
 
@@ -38,44 +37,45 @@
 	}
 
 	for(int i=0; i<result->settingCount; i++) {
-		char *sectionName = iniparser_getsecname(settingfile, i);
-		if(!sectionName) {
+		result->settings[i].iniName = flib_ini_get_sectionname(settingfile, i);
+		if(!result->settings[i].iniName) {
 			return flib_cfg_meta_from_ini_handleError(result, settingfile, modfile);
 		}
 
 		bool error = false;
-		result->settings[i].iniName = flib_strdupnull(sectionName);
-		result->settings[i].title = inihelper_getstringdup(settingfile, &error, sectionName, "title");
-		result->settings[i].engineCommand = inihelper_getstringdup(settingfile, &error, sectionName, "command");
-		result->settings[i].image = inihelper_getstringdup(settingfile, &error, sectionName, "image");
-		result->settings[i].checkOverMax = inihelper_getbool(settingfile, &error, sectionName, "checkOverMax");
-		result->settings[i].times1000 = inihelper_getbool(settingfile, &error, sectionName, "times1000");
-		result->settings[i].min = inihelper_getint(settingfile, &error, sectionName, "min");
-		result->settings[i].max = inihelper_getint(settingfile, &error, sectionName, "max");
-		result->settings[i].def = inihelper_getint(settingfile, &error, sectionName, "default");
+		error |= flib_ini_enter_section(settingfile, result->settings[i].iniName);
+		error |= flib_ini_get_str(settingfile, &result->settings[i].title, "title");
+		error |= flib_ini_get_str(settingfile, &result->settings[i].engineCommand, "command");
+		error |= flib_ini_get_str(settingfile, &result->settings[i].image, "image");
+		error |= flib_ini_get_bool(settingfile, &result->settings[i].checkOverMax, "checkOverMax");
+		error |= flib_ini_get_bool(settingfile, &result->settings[i].times1000, "times1000");
+		error |= flib_ini_get_int(settingfile, &result->settings[i].min, "min");
+		error |= flib_ini_get_int(settingfile, &result->settings[i].max, "max");
+		error |= flib_ini_get_int(settingfile, &result->settings[i].def, "default");
+
 		if(error) {
-			flib_log_e("Missing or malformed ini parameter in file %s, section %s", settingpath, sectionName);
+			flib_log_e("Missing or malformed ini parameter in file %s, section %s", settingpath, result->settings[i].iniName);
 			return flib_cfg_meta_from_ini_handleError(result, settingfile, modfile);
 		}
 	}
 
 	for(int i=0; i<result->modCount; i++) {
-		char *sectionName = iniparser_getsecname(modfile, i);
-		if(!sectionName) {
+		result->mods[i].iniName = flib_ini_get_sectionname(modfile, i);
+		if(!result->mods[i].iniName) {
 			return flib_cfg_meta_from_ini_handleError(result, settingfile, modfile);
 		}
 
 		bool error = false;
-		result->mods[i].iniName = flib_strdupnull(sectionName);
-		result->mods[i].bitmaskIndex = inihelper_getint(modfile, &error, sectionName, "bitmaskIndex");
+		error |= flib_ini_enter_section(modfile, result->mods[i].iniName);
+		error |= flib_ini_get_int(modfile, &result->mods[i].bitmaskIndex, "bitmaskIndex");
 		if(error) {
-			flib_log_e("Missing or malformed ini parameter in file %s, section %s", modpath, sectionName);
+			flib_log_e("Missing or malformed ini parameter in file %s, section %s", modpath, result->mods[i].iniName);
 			return flib_cfg_meta_from_ini_handleError(result, settingfile, modfile);
 		}
 	}
 
-	iniparser_freedict(settingfile);
-	iniparser_freedict(modfile);
+	flib_ini_destroy(settingfile);
+	flib_ini_destroy(modfile);
 	return result;
 }
 
@@ -124,8 +124,8 @@
 	return result;
 }
 
-flib_cfg *flib_cfg_from_ini_handleError(flib_cfg *result, dictionary *settingfile) {
-	iniparser_freedict(settingfile);
+flib_cfg *flib_cfg_from_ini_handleError(flib_cfg *result, flib_ini *settingfile) {
+	flib_ini_destroy(settingfile);
 	flib_cfg_destroy(result);
 	return NULL;
 }
@@ -135,36 +135,45 @@
 		flib_log_e("null parameter in flib_cfg_from_ini");
 		return NULL;
 	}
-	dictionary *settingfile = iniparser_load(filename);
+	flib_ini *settingfile = flib_ini_load(filename);
 	if(!settingfile) {
 		return NULL;
 	}
 
-	bool error = false;
-	char *schemename = inihelper_getstring(settingfile, &error, "Scheme", "name");
-	if(!schemename) {
+	char *schemename = NULL;
+	if(flib_ini_enter_section(settingfile, "Scheme")) {
+		flib_log_e("Missing section \"Scheme\" in config file %s.", filename);
+		return flib_cfg_from_ini_handleError(NULL, settingfile);
+	}
+	if(flib_ini_get_str(settingfile, &schemename, "name")) {
+		flib_log_e("Missing scheme name in config file %s.", filename);
 		return flib_cfg_from_ini_handleError(NULL, settingfile);
 	}
 
 	flib_cfg *result = flib_cfg_create(meta, schemename);
 
-	for(int i=0; i<meta->settingCount; i++) {
-		char *key = inihelper_createDictKey("BasicSettings", meta->settings[i].iniName);
-		if(!key) {
-			return flib_cfg_from_ini_handleError(result, settingfile);
+	if(flib_ini_enter_section(settingfile, "BasicSettings")) {
+		flib_log_w("Missing section \"BasicSettings\" in config file %s, using defaults.", filename);
+	} else {
+		for(int i=0; i<meta->settingCount; i++) {
+			if(flib_ini_get_int_opt(settingfile, &result->settings[i], meta->settings[i].iniName, meta->settings[i].def)) {
+				flib_log_e("Error reading BasicSetting %s in config file %s.", meta->settings[i].iniName, filename);
+				return flib_cfg_from_ini_handleError(result, settingfile);
+			}
 		}
-		result->settings[i] = iniparser_getint(settingfile, key, meta->settings[i].def);
-		free(key);
 	}
-	for(int i=0; i<meta->modCount; i++) {
-		char *key = inihelper_createDictKey("GameMods", meta->mods[i].iniName);
-		if(!key) {
-			return flib_cfg_from_ini_handleError(result, settingfile);
+
+	if(flib_ini_enter_section(settingfile, "GameMods")) {
+		flib_log_w("Missing section \"GameMods\" in config file %s, using defaults.", filename);
+	} else {
+		for(int i=0; i<meta->modCount; i++) {
+			if(flib_ini_get_bool_opt(settingfile, &result->mods[i], meta->mods[i].iniName, false)) {
+				flib_log_e("Error reading GameMod %s in config file %s.", meta->mods[i].iniName, filename);
+				return flib_cfg_from_ini_handleError(result, settingfile);
+			}
 		}
-		result->mods[i] = iniparser_getboolean(settingfile, key, false);
-		free(key);
 	}
-	iniparser_freedict(settingfile);
+	flib_ini_destroy(settingfile);
 	return result;
 }
 
@@ -173,35 +182,36 @@
 	if(!meta || !filename || !config || config->modCount!=meta->modCount || config->settingCount!=meta->settingCount) {
 		flib_log_e("Invalid parameter in flib_cfg_to_ini");
 	} else {
-		dictionary *dict = iniparser_load(filename);
-		if(!dict) {
-			dict = dictionary_new(0);
-		}
-		if(dict) {
+		flib_ini *ini = flib_ini_create(filename);
+		if(ini) {
 			bool error = false;
-			// Add the sections
-			error |= iniparser_set(dict, "Scheme", NULL);
-			error |= iniparser_set(dict, "BasicSettings", NULL);
-			error |= iniparser_set(dict, "GameMods", NULL);
 
 			// Add the values
-			error |= inihelper_setstr(dict, "Scheme", "name", config->schemeName);
-			for(int i=0; i<config->settingCount; i++) {
-				error |= inihelper_setint(dict, "BasicSettings", meta->settings[i].iniName, config->settings[i]);
-			}
-			for(int i=0; i<config->modCount; i++) {
-				error |= inihelper_setbool(dict, "GameMods", meta->mods[i].iniName, config->mods[i]);
+			error |= flib_ini_create_section(ini, "Scheme");
+			if(!error) {
+				error |= flib_ini_set_str(ini, "name", config->schemeName);
 			}
+
+
+			error |= flib_ini_create_section(ini, "BasicSettings");
 			if(!error) {
-				FILE *inifile = fopen(filename, "wb");
-				if(inifile) {
-					iniparser_dump_ini(dict, inifile);
-					fclose(inifile);
-					result = 0;
+				for(int i=0; i<config->settingCount; i++) {
+					error |= flib_ini_set_int(ini, meta->settings[i].iniName, config->settings[i]);
 				}
 			}
-			dictionary_del(dict);
+
+			error |= flib_ini_create_section(ini, "GameMods");
+			if(!error) {
+				for(int i=0; i<config->modCount; i++) {
+					error |= flib_ini_set_bool(ini, meta->mods[i].iniName, config->mods[i]);
+				}
+			}
+
+			if(!error) {
+				result = flib_ini_save(ini, filename);
+			}
 		}
+		flib_ini_destroy(ini);
 	}
 	return result;
 }