project_files/frontlib/model/schemelist.c
changeset 7497 7e1d72fc03c7
parent 7482 d70a5b0d1190
child 10017 de822cd3df3a
equal deleted inserted replaced
7494:e65adfc99f15 7497:7e1d72fc03c7
    20 #include "schemelist.h"
    20 #include "schemelist.h"
    21 
    21 
    22 #include "../util/inihelper.h"
    22 #include "../util/inihelper.h"
    23 #include "../util/logging.h"
    23 #include "../util/logging.h"
    24 #include "../util/util.h"
    24 #include "../util/util.h"
    25 #include "../util/refcounter.h"
       
    26 #include "../util/list.h"
    25 #include "../util/list.h"
    27 
    26 
    28 #include <stdio.h>
    27 #include <stdio.h>
    29 #include <stdlib.h>
    28 #include <stdlib.h>
    30 #include <limits.h>
    29 #include <limits.h>
    33 static char *makePrefixedName(int schemeIndex, const char *settingName) {
    32 static char *makePrefixedName(int schemeIndex, const char *settingName) {
    34 	return flib_asprintf("%i\\%s", schemeIndex, settingName);
    33 	return flib_asprintf("%i\\%s", schemeIndex, settingName);
    35 }
    34 }
    36 
    35 
    37 static int readSettingsFromIni(flib_ini *ini, flib_scheme *scheme, int index) {
    36 static int readSettingsFromIni(flib_ini *ini, flib_scheme *scheme, int index) {
    38 	flib_metascheme *meta = scheme->meta;
       
    39 	bool error = false;
    37 	bool error = false;
    40 	for(int i=0; i<meta->settingCount && !error; i++) {
    38 	for(int i=0; i<flib_meta.settingCount && !error; i++) {
    41 		char *key = makePrefixedName(index, meta->settings[i].name);
    39 		char *key = makePrefixedName(index, flib_meta.settings[i].name);
    42 		if(!key) {
    40 		if(!key) {
    43 			error = true;
    41 			error = true;
    44 		} else if(flib_ini_get_int_opt(ini, &scheme->settings[i], key, meta->settings[i].def)) {
    42 		} else if(flib_ini_get_int_opt(ini, &scheme->settings[i], key, flib_meta.settings[i].def)) {
    45 			flib_log_e("Error reading setting %s in schemes file.", key);
    43 			flib_log_e("Error reading setting %s in schemes file.", key);
    46 			error = true;
    44 			error = true;
    47 		}
    45 		}
    48 		free(key);
    46 		free(key);
    49 	}
    47 	}
    50 	return error;
    48 	return error;
    51 }
    49 }
    52 
    50 
    53 static int readModsFromIni(flib_ini *ini, flib_scheme *scheme, int index) {
    51 static int readModsFromIni(flib_ini *ini, flib_scheme *scheme, int index) {
    54 	flib_metascheme *meta = scheme->meta;
       
    55 	bool error = false;
    52 	bool error = false;
    56 	for(int i=0; i<meta->modCount && !error; i++) {
    53 	for(int i=0; i<flib_meta.modCount && !error; i++) {
    57 		char *key = makePrefixedName(index, meta->mods[i].name);
    54 		char *key = makePrefixedName(index, flib_meta.mods[i].name);
    58 		if(!key) {
    55 		if(!key) {
    59 			error = true;
    56 			error = true;
    60 		} else if(flib_ini_get_bool_opt(ini, &scheme->mods[i], key, false)) {
    57 		} else if(flib_ini_get_bool_opt(ini, &scheme->mods[i], key, false)) {
    61 			flib_log_e("Error reading mod %s in schemes file.", key);
    58 			flib_log_e("Error reading mod %s in schemes file.", key);
    62 			error = true;
    59 			error = true;
    64 		free(key);
    61 		free(key);
    65 	}
    62 	}
    66 	return error;
    63 	return error;
    67 }
    64 }
    68 
    65 
    69 static flib_scheme *readSchemeFromIni(flib_metascheme *meta, flib_ini *ini, int index) {
    66 static flib_scheme *readSchemeFromIni(flib_ini *ini, int index) {
    70 	flib_scheme *result = NULL;
    67 	flib_scheme *result = NULL;
    71 	char *schemeNameKey = makePrefixedName(index+1, "name");
    68 	char *schemeNameKey = makePrefixedName(index+1, "name");
    72 	if(schemeNameKey) {
    69 	if(schemeNameKey) {
    73 		char *schemeName = NULL;
    70 		char *schemeName = NULL;
    74 		if(!flib_ini_get_str_opt(ini, &schemeName, schemeNameKey, "Unnamed")) {
    71 		if(!flib_ini_get_str_opt(ini, &schemeName, schemeNameKey, "Unnamed")) {
    75 			flib_scheme *tmpScheme = flib_scheme_create(meta, schemeName);
    72 			flib_scheme *tmpScheme = flib_scheme_create(schemeName);
    76 			if(tmpScheme) {
    73 			if(tmpScheme) {
    77 				if(!readSettingsFromIni(ini, tmpScheme, index) && !readModsFromIni(ini, tmpScheme, index)) {
    74 				if(!readSettingsFromIni(ini, tmpScheme, index) && !readModsFromIni(ini, tmpScheme, index)) {
    78 					result = tmpScheme;
    75 					result = tmpScheme;
    79 					tmpScheme = NULL;
    76 					tmpScheme = NULL;
    80 				}
    77 				}
    91 	flib_ini_destroy(ini);
    88 	flib_ini_destroy(ini);
    92 	flib_schemelist_destroy(result);
    89 	flib_schemelist_destroy(result);
    93 	return NULL;
    90 	return NULL;
    94 }
    91 }
    95 
    92 
    96 flib_schemelist *flib_schemelist_from_ini(flib_metascheme *meta, const char *filename) {
    93 flib_schemelist *flib_schemelist_from_ini(const char *filename) {
    97 	if(log_badargs_if2(meta==NULL, filename==NULL)) {
    94 	if(log_badargs_if(filename==NULL)) {
    98 		return NULL;
    95 		return NULL;
    99 	}
    96 	}
   100 
    97 
   101 	flib_schemelist *list = NULL;
    98 	flib_schemelist *list = NULL;
   102 	flib_ini *ini = flib_ini_load(filename);
    99 	flib_ini *ini = flib_ini_load(filename);
   115 		flib_log_e("Missing or malformed scheme count in config file %s.", filename);
   112 		flib_log_e("Missing or malformed scheme count in config file %s.", filename);
   116 		return fromIniHandleError(list, ini);
   113 		return fromIniHandleError(list, ini);
   117 	}
   114 	}
   118 
   115 
   119 	for(int i=0; i<schemeCount; i++) {
   116 	for(int i=0; i<schemeCount; i++) {
   120 		flib_scheme *scheme = readSchemeFromIni(meta, ini, i);
   117 		flib_scheme *scheme = readSchemeFromIni(ini, i);
   121 		if(!scheme || flib_schemelist_insert(list, scheme, i)) {
   118 		if(!scheme || flib_schemelist_insert(list, scheme, i)) {
   122 			flib_scheme_destroy(scheme);
   119 			flib_scheme_destroy(scheme);
   123 			flib_log_e("Error reading scheme %i from config file %s.", i, filename);
   120 			flib_log_e("Error reading scheme %i from config file %s.", i, filename);
   124 			return fromIniHandleError(list, ini);
   121 			return fromIniHandleError(list, ini);
   125 		}
   122 		}
   129 	flib_ini_destroy(ini);
   126 	flib_ini_destroy(ini);
   130 	return list;
   127 	return list;
   131 }
   128 }
   132 
   129 
   133 static int writeSchemeToIni(const flib_scheme *scheme, flib_ini *ini, int index) {
   130 static int writeSchemeToIni(const flib_scheme *scheme, flib_ini *ini, int index) {
   134 	flib_metascheme *meta = scheme->meta;
       
   135 	bool error = false;
   131 	bool error = false;
   136 
   132 
   137 	char *key = makePrefixedName(index+1, "name");
   133 	char *key = makePrefixedName(index+1, "name");
   138 	error |= !key || flib_ini_set_str(ini, key, scheme->name);
   134 	error |= !key || flib_ini_set_str(ini, key, scheme->name);
   139 	free(key);
   135 	free(key);
   140 
   136 
   141 	for(int i=0; i<meta->modCount && !error; i++) {
   137 	for(int i=0; i<flib_meta.modCount && !error; i++) {
   142 		char *key = makePrefixedName(index+1, meta->mods[i].name);
   138 		char *key = makePrefixedName(index+1, flib_meta.mods[i].name);
   143 		error |= !key || flib_ini_set_bool(ini, key, scheme->mods[i]);
   139 		error |= !key || flib_ini_set_bool(ini, key, scheme->mods[i]);
   144 		free(key);
   140 		free(key);
   145 	}
   141 	}
   146 
   142 
   147 	for(int i=0; i<meta->settingCount && !error; i++) {
   143 	for(int i=0; i<flib_meta.settingCount && !error; i++) {
   148 		char *key = makePrefixedName(index+1, meta->settings[i].name);
   144 		char *key = makePrefixedName(index+1, flib_meta.settings[i].name);
   149 		error |= !key || flib_ini_set_int(ini, key, scheme->settings[i]);
   145 		error |= !key || flib_ini_set_int(ini, key, scheme->settings[i]);
   150 		free(key);
   146 		free(key);
   151 	}
   147 	}
   152 	return error;
   148 	return error;
   153 }
   149 }