project_files/frontlib/util/inihelper.c
changeset 7316 f7b49b2c5d84
parent 7314 6171f0bad318
child 7467 a9ab8067a2d1
equal deleted inserted replaced
7314:6171f0bad318 7316:f7b49b2c5d84
    71 	return result;
    71 	return result;
    72 }
    72 }
    73 
    73 
    74 flib_ini *flib_ini_load(const char *filename) {
    74 flib_ini *flib_ini_load(const char *filename) {
    75 	flib_ini *result = NULL;
    75 	flib_ini *result = NULL;
    76 	if(!filename) {
    76 	if(!log_badargs_if(filename==NULL)) {
    77 		flib_log_e("null parameter in flib_ini_load");
       
    78 	} else {
       
    79 		flib_ini *tmpIni = flib_calloc(1, sizeof(flib_ini));
    77 		flib_ini *tmpIni = flib_calloc(1, sizeof(flib_ini));
    80 		if(tmpIni) {
    78 		if(tmpIni) {
    81 			tmpIni->inidict = iniparser_load(filename);
    79 			tmpIni->inidict = iniparser_load(filename);
    82 			if(tmpIni->inidict) {
    80 			if(tmpIni->inidict) {
    83 				result = tmpIni;
    81 				result = tmpIni;
    89 	return result;
    87 	return result;
    90 }
    88 }
    91 
    89 
    92 int flib_ini_save(flib_ini *ini, const char *filename) {
    90 int flib_ini_save(flib_ini *ini, const char *filename) {
    93 	int result = INI_ERROR_OTHER;
    91 	int result = INI_ERROR_OTHER;
    94 	if(!ini || !filename) {
    92 	if(!log_badargs_if2(ini==NULL, filename==NULL)) {
    95 		flib_log_e("null parameter in flib_ini_save");
       
    96 	} else {
       
    97 		FILE *file = fopen(filename, "wb");
    93 		FILE *file = fopen(filename, "wb");
    98 		if(!file) {
    94 		if(!file) {
    99 			flib_log_e("Error opening file \"%s\" for writing.", filename);
    95 			flib_log_e("Error opening file \"%s\" for writing.", filename);
   100 		} else {
    96 		} else {
   101 			iniparser_dump_ini(ini->inidict, file);
    97 			iniparser_dump_ini(ini->inidict, file);
   123 	int result = INI_ERROR_OTHER;
   119 	int result = INI_ERROR_OTHER;
   124 	if(ini) {
   120 	if(ini) {
   125 		free(ini->currentSection);
   121 		free(ini->currentSection);
   126 		ini->currentSection = NULL;
   122 		ini->currentSection = NULL;
   127 	}
   123 	}
   128 	if(!ini || !section) {
   124 	if(!log_badargs_if2(ini==NULL, section==NULL)) {
   129 		flib_log_e("null parameter in flib_ini_enter_section");
       
   130 	} else {
       
   131 		if(!iniparser_find_entry(ini->inidict, section)) {
   125 		if(!iniparser_find_entry(ini->inidict, section)) {
   132 			result = INI_ERROR_NOTFOUND;
   126 			result = INI_ERROR_NOTFOUND;
   133 		} else {
   127 		} else {
   134 			ini->currentSection = flib_strdupnull(section);
   128 			ini->currentSection = flib_strdupnull(section);
   135 			if(ini->currentSection) {
   129 			if(ini->currentSection) {
   143 	return result;
   137 	return result;
   144 }
   138 }
   145 
   139 
   146 int flib_ini_create_section(flib_ini *ini, const char *section) {
   140 int flib_ini_create_section(flib_ini *ini, const char *section) {
   147 	int result = INI_ERROR_OTHER;
   141 	int result = INI_ERROR_OTHER;
   148 	if(!ini || !section) {
   142 	if(!log_badargs_if2(ini==NULL, section==NULL)) {
   149 		flib_log_e("null parameter in flib_ini_create_section");
       
   150 	} else {
       
   151 		result = flib_ini_enter_section(ini, section);
   143 		result = flib_ini_enter_section(ini, section);
   152 		if(result == INI_ERROR_NOTFOUND) {
   144 		if(result == INI_ERROR_NOTFOUND) {
   153 			if(iniparser_set(ini->inidict, section, NULL)) {
   145 			if(iniparser_set(ini->inidict, section, NULL)) {
   154 				result = INI_ERROR_OTHER;
   146 				result = INI_ERROR_OTHER;
   155 			} else {
   147 			} else {
   188 	return result;
   180 	return result;
   189 }
   181 }
   190 
   182 
   191 int flib_ini_get_str_opt(flib_ini *ini, char **outVar, const char *key, const char *def) {
   183 int flib_ini_get_str_opt(flib_ini *ini, char **outVar, const char *key, const char *def) {
   192 	int result = INI_ERROR_OTHER;
   184 	int result = INI_ERROR_OTHER;
   193 	if(!ini || !outVar || !key || !ini->currentSection) {
   185 	if(!log_badargs_if4(ini==NULL, ini->currentSection==NULL, outVar==NULL, key==NULL)) {
   194 		flib_log_e("null parameter or no current section in flib_ini_get_str_opt");
       
   195 	} else {
       
   196 		const char *value = findValue(ini->inidict, ini->currentSection, key);
   186 		const char *value = findValue(ini->inidict, ini->currentSection, key);
   197 		if(!value) {
   187 		if(!value) {
   198 			value = def;
   188 			value = def;
   199 		}
   189 		}
   200 		char *valueDup = flib_strdupnull(value);
   190 		char *valueDup = flib_strdupnull(value);
   264 	return result;
   254 	return result;
   265 }
   255 }
   266 
   256 
   267 int flib_ini_set_str(flib_ini *ini, const char *key, const char *value) {
   257 int flib_ini_set_str(flib_ini *ini, const char *key, const char *value) {
   268 	int result = INI_ERROR_OTHER;
   258 	int result = INI_ERROR_OTHER;
   269 	if(!ini || !key || !value || !ini->currentSection) {
   259 	if(log_badargs_if4(ini==NULL, ini->currentSection==NULL, key==NULL, value==NULL)) {
   270 		flib_log_e("null parameter or no current section in flib_ini_set_str");
       
   271 	} else {
       
   272 		char *dictKey = createDictKey(ini->currentSection, key);
   260 		char *dictKey = createDictKey(ini->currentSection, key);
   273 		if(dictKey) {
   261 		if(dictKey) {
   274 			result = iniparser_set(ini->inidict, dictKey, value);
   262 			result = iniparser_set(ini->inidict, dictKey, value);
   275 		}
   263 		}
   276 		free(dictKey);
   264 		free(dictKey);
   291 int flib_ini_set_bool(flib_ini *ini, const char *key, bool value) {
   279 int flib_ini_set_bool(flib_ini *ini, const char *key, bool value) {
   292 	return flib_ini_set_str(ini, key, value ? "true" : "false");
   280 	return flib_ini_set_str(ini, key, value ? "true" : "false");
   293 }
   281 }
   294 
   282 
   295 int flib_ini_get_sectioncount(flib_ini *ini) {
   283 int flib_ini_get_sectioncount(flib_ini *ini) {
   296 	int result = INI_ERROR_OTHER;
   284 	if(!log_badargs_if(ini==NULL)) {
   297 	if(!ini) {
   285 		return iniparser_getnsec(ini->inidict);
   298 		flib_log_e("null parameter in flib_ini_get_sectioncount");
   286 	}
   299 	} else {
   287 	return INI_ERROR_OTHER;
   300 		result = iniparser_getnsec(ini->inidict);
       
   301 	}
       
   302 	return result;
       
   303 }
   288 }
   304 
   289 
   305 char *flib_ini_get_sectionname(flib_ini *ini, int number) {
   290 char *flib_ini_get_sectionname(flib_ini *ini, int number) {
   306 	char *result = NULL;
   291 	if(!log_badargs_if2(ini==NULL, number<0)) {
   307 	if(!ini || number<0) {
   292 		return flib_strdupnull(iniparser_getsecname(ini->inidict, number));
   308 		flib_log_e("bad parameter in flib_ini_get_sectionname");
   293 	}
   309 	} else {
   294 	return NULL;
   310 		result = flib_strdupnull(iniparser_getsecname(ini->inidict, number));
       
   311 	}
       
   312 	return result;
       
   313 }
   295 }
   314 
   296 
   315 int flib_ini_get_keycount(flib_ini *ini) {
   297 int flib_ini_get_keycount(flib_ini *ini) {
   316 	int result = INI_ERROR_OTHER;
   298 	if(!log_badargs_if2(ini==NULL, ini->currentSection==NULL)) {
   317 	if(!ini || !ini->currentSection) {
   299 		return iniparser_getsecnkeys(ini->inidict, ini->currentSection);
   318 		flib_log_e("null parameter or no current section in flib_ini_get_keycount");
   300 	}
   319 	} else {
   301 	return INI_ERROR_OTHER;
   320 		result = iniparser_getsecnkeys(ini->inidict, ini->currentSection);
       
   321 	}
       
   322 	return result;
       
   323 }
   302 }
   324 
   303 
   325 char *flib_ini_get_keyname(flib_ini *ini, int number) {
   304 char *flib_ini_get_keyname(flib_ini *ini, int number) {
   326 	char *result = NULL;
   305 	char *result = NULL;
   327 	if(!ini || number<0 || !ini->currentSection) {
   306 	if(!log_badargs_if3(ini==NULL, ini->currentSection==NULL, number<0)) {
   328 		flib_log_e("bad parameter or no current section in flib_ini_get_keyname");
       
   329 	} else {
       
   330 		int keyCount = iniparser_getsecnkeys(ini->inidict, ini->currentSection);
   307 		int keyCount = iniparser_getsecnkeys(ini->inidict, ini->currentSection);
   331 		char **keys = iniparser_getseckeys(ini->inidict, ini->currentSection);
   308 		char **keys = iniparser_getseckeys(ini->inidict, ini->currentSection);
   332 		if(keys && keyCount>number) {
   309 		if(keys && keyCount>number) {
   333 			// The keys are in the format section:key, so we have to skip the section and colon.
   310 			// The keys are in the format section:key, so we have to skip the section and colon.
   334 			result = flib_strdupnull(keys[number]+strlen(ini->currentSection)+1);
   311 			result = flib_strdupnull(keys[number]+strlen(ini->currentSection)+1);