6 #include "../util/logging.h" |
6 #include "../util/logging.h" |
7 #include "../util/util.h" |
7 #include "../util/util.h" |
8 |
8 |
9 #include <stdio.h> |
9 #include <stdio.h> |
10 |
10 |
11 static void freeCfgMeta(flib_cfg_meta *cfg) { |
11 static flib_cfg_meta *flib_cfg_meta_from_ini_handleError(flib_cfg_meta *result, dictionary *settingfile, dictionary *modfile) { |
12 if(cfg) { |
12 flib_cfg_meta_destroy(result); |
13 if(cfg->settings) { |
13 iniparser_freedict(settingfile); |
14 for(int i=0; i<cfg->settingCount; i++) { |
14 iniparser_freedict(modfile); |
15 free(cfg->settings[i].iniName); |
15 return NULL; |
16 free(cfg->settings[i].title); |
|
17 free(cfg->settings[i].engineCommand); |
|
18 free(cfg->settings[i].image); |
|
19 } |
|
20 free(cfg->settings); |
|
21 } |
|
22 if(cfg->mods) { |
|
23 for(int i=0; i<cfg->modCount; i++) { |
|
24 free(cfg->mods[i].iniName); |
|
25 } |
|
26 free(cfg->mods); |
|
27 } |
|
28 free(cfg); |
|
29 } |
|
30 } |
16 } |
31 |
17 |
32 flib_cfg_meta *flib_cfg_meta_from_ini(const char *settingpath, const char *modpath) { |
18 flib_cfg_meta *flib_cfg_meta_from_ini(const char *settingpath, const char *modpath) { |
33 if(!settingpath || !modpath) { |
19 if(!settingpath || !modpath) { |
34 return NULL; |
20 flib_log_e("null parameter in flib_cfg_meta_from_ini"); |
35 } |
21 return NULL; |
36 flib_cfg_meta *result = calloc(1, sizeof(flib_cfg_meta)); |
22 } |
|
23 flib_cfg_meta *result = flib_calloc(1, sizeof(flib_cfg_meta)); |
37 dictionary *settingfile = iniparser_load(settingpath); |
24 dictionary *settingfile = iniparser_load(settingpath); |
38 dictionary *modfile = iniparser_load(modpath); |
25 dictionary *modfile = iniparser_load(modpath); |
39 |
26 |
40 if(!result || !settingfile || !modfile) { |
27 if(!result || !settingfile || !modfile) { |
41 goto handleError; |
28 return flib_cfg_meta_from_ini_handleError(result, settingfile, modfile); |
42 } |
29 } |
43 |
30 |
44 result->settingCount = iniparser_getnsec(settingfile); |
31 result->settingCount = iniparser_getnsec(settingfile); |
45 result->modCount = iniparser_getnsec(modfile); |
32 result->modCount = iniparser_getnsec(modfile); |
46 result->settings = calloc(result->settingCount, sizeof(flib_cfg_setting_meta)); |
33 result->settings = flib_calloc(result->settingCount, sizeof(flib_cfg_setting_meta)); |
47 result->mods = calloc(result->modCount, sizeof(flib_cfg_mod_meta)); |
34 result->mods = flib_calloc(result->modCount, sizeof(flib_cfg_mod_meta)); |
48 |
35 |
49 if(!result->settings || !result->mods) { |
36 if(!result->settings || !result->mods) { |
50 goto handleError; |
37 return flib_cfg_meta_from_ini_handleError(result, settingfile, modfile); |
51 } |
38 } |
52 |
39 |
53 for(int i=0; i<result->settingCount; i++) { |
40 for(int i=0; i<result->settingCount; i++) { |
54 char *sectionName = iniparser_getsecname(settingfile, i); |
41 char *sectionName = iniparser_getsecname(settingfile, i); |
55 if(!sectionName) { |
42 if(!sectionName) { |
56 goto handleError; |
43 return flib_cfg_meta_from_ini_handleError(result, settingfile, modfile); |
57 } |
44 } |
58 |
45 |
59 bool error = false; |
46 bool error = false; |
60 result->settings[i].iniName = flib_strdupnull(sectionName); |
47 result->settings[i].iniName = flib_strdupnull(sectionName); |
61 result->settings[i].title = inihelper_getstringdup(settingfile, &error, sectionName, "title"); |
48 result->settings[i].title = inihelper_getstringdup(settingfile, &error, sectionName, "title"); |
66 result->settings[i].min = inihelper_getint(settingfile, &error, sectionName, "min"); |
53 result->settings[i].min = inihelper_getint(settingfile, &error, sectionName, "min"); |
67 result->settings[i].max = inihelper_getint(settingfile, &error, sectionName, "max"); |
54 result->settings[i].max = inihelper_getint(settingfile, &error, sectionName, "max"); |
68 result->settings[i].def = inihelper_getint(settingfile, &error, sectionName, "default"); |
55 result->settings[i].def = inihelper_getint(settingfile, &error, sectionName, "default"); |
69 if(error) { |
56 if(error) { |
70 flib_log_e("Missing or malformed ini parameter in file %s, section %s", settingpath, sectionName); |
57 flib_log_e("Missing or malformed ini parameter in file %s, section %s", settingpath, sectionName); |
71 goto handleError; |
58 return flib_cfg_meta_from_ini_handleError(result, settingfile, modfile); |
72 } |
59 } |
73 } |
60 } |
74 |
61 |
75 for(int i=0; i<result->modCount; i++) { |
62 for(int i=0; i<result->modCount; i++) { |
76 char *sectionName = iniparser_getsecname(modfile, i); |
63 char *sectionName = iniparser_getsecname(modfile, i); |
77 if(!sectionName) { |
64 if(!sectionName) { |
78 goto handleError; |
65 return flib_cfg_meta_from_ini_handleError(result, settingfile, modfile); |
79 } |
66 } |
80 |
67 |
81 bool error = false; |
68 bool error = false; |
82 result->mods[i].iniName = flib_strdupnull(sectionName); |
69 result->mods[i].iniName = flib_strdupnull(sectionName); |
83 result->mods[i].bitmaskIndex = inihelper_getint(modfile, &error, sectionName, "bitmaskIndex"); |
70 result->mods[i].bitmaskIndex = inihelper_getint(modfile, &error, sectionName, "bitmaskIndex"); |
84 if(error) { |
71 if(error) { |
85 flib_log_e("Missing or malformed ini parameter in file %s, section %s", modpath, sectionName); |
72 flib_log_e("Missing or malformed ini parameter in file %s, section %s", modpath, sectionName); |
86 goto handleError; |
73 return flib_cfg_meta_from_ini_handleError(result, settingfile, modfile); |
87 } |
74 } |
88 } |
75 } |
89 |
76 |
90 iniparser_freedict(settingfile); |
77 iniparser_freedict(settingfile); |
91 iniparser_freedict(modfile); |
78 iniparser_freedict(modfile); |
92 return result; |
79 return result; |
93 |
80 } |
94 handleError: |
81 |
95 freeCfgMeta(result); |
82 void flib_cfg_meta_destroy(flib_cfg_meta *cfg) { |
96 iniparser_freedict(settingfile); |
83 if(cfg) { |
97 iniparser_freedict(modfile); |
84 if(cfg->settings) { |
98 return NULL; |
85 for(int i=0; i<cfg->settingCount; i++) { |
99 } |
86 free(cfg->settings[i].iniName); |
100 |
87 free(cfg->settings[i].title); |
101 void flib_cfg_meta_destroy(flib_cfg_meta *metainfo) { |
88 free(cfg->settings[i].engineCommand); |
102 freeCfgMeta(metainfo); |
89 free(cfg->settings[i].image); |
|
90 } |
|
91 free(cfg->settings); |
|
92 } |
|
93 if(cfg->mods) { |
|
94 for(int i=0; i<cfg->modCount; i++) { |
|
95 free(cfg->mods[i].iniName); |
|
96 } |
|
97 free(cfg->mods); |
|
98 } |
|
99 free(cfg); |
|
100 } |
103 } |
101 } |
104 |
102 |
105 flib_cfg *flib_cfg_create(const flib_cfg_meta *meta, const char *schemeName) { |
103 flib_cfg *flib_cfg_create(const flib_cfg_meta *meta, const char *schemeName) { |
106 flib_cfg *result = calloc(1, sizeof(flib_cfg)); |
104 flib_cfg *result = flib_calloc(1, sizeof(flib_cfg)); |
107 if(!meta || !result || !schemeName) { |
105 if(!meta || !result || !schemeName) { |
|
106 flib_log_e("null parameter in flib_cfg_create"); |
108 return NULL; |
107 return NULL; |
109 } |
108 } |
110 |
109 |
111 result->modCount = meta->modCount; |
110 result->modCount = meta->modCount; |
112 result->settingCount = meta->settingCount; |
111 result->settingCount = meta->settingCount; |
113 result->schemeName = flib_strdupnull(schemeName); |
112 result->schemeName = flib_strdupnull(schemeName); |
114 result->mods = calloc(meta->modCount, sizeof(*result->mods)); |
113 result->mods = flib_calloc(meta->modCount, sizeof(*result->mods)); |
115 result->settings = calloc(meta->settingCount, sizeof(*result->settings)); |
114 result->settings = flib_calloc(meta->settingCount, sizeof(*result->settings)); |
116 |
115 |
117 if(!result->mods || !result->settings || !result->schemeName) { |
116 if(!result->mods || !result->settings || !result->schemeName) { |
118 flib_cfg_destroy(result); |
117 flib_cfg_destroy(result); |
119 return NULL; |
118 return NULL; |
120 } |
119 } |
168 return result; |
168 return result; |
169 } |
169 } |
170 |
170 |
171 int flib_cfg_to_ini(const flib_cfg_meta *meta, const char *filename, const flib_cfg *config) { |
171 int flib_cfg_to_ini(const flib_cfg_meta *meta, const char *filename, const flib_cfg *config) { |
172 int result = -1; |
172 int result = -1; |
173 if(meta && filename && config && config->modCount==meta->modCount && config->settingCount==meta->settingCount) { |
173 if(!meta || !filename || !config || config->modCount!=meta->modCount || config->settingCount!=meta->settingCount) { |
174 dictionary *dict = dictionary_new(0); |
174 flib_log_e("Invalid parameter in flib_cfg_to_ini"); |
|
175 } else { |
|
176 dictionary *dict = iniparser_load(filename); |
|
177 if(!dict) { |
|
178 dict = dictionary_new(0); |
|
179 } |
175 if(dict) { |
180 if(dict) { |
176 bool error = false; |
181 bool error = false; |
177 // Add the sections |
182 // Add the sections |
178 error |= iniparser_set(dict, "Scheme", NULL); |
183 error |= iniparser_set(dict, "Scheme", NULL); |
179 error |= iniparser_set(dict, "BasicSettings", NULL); |
184 error |= iniparser_set(dict, "BasicSettings", NULL); |