project_files/frontlib/util/inihelper.c
changeset 7224 5143861c83bd
parent 7179 f84805e6df03
child 7227 1c859f572d72
equal deleted inserted replaced
7221:8d04e85ca204 7224:5143861c83bd
    20 	size_t insize = strlen(inbuf);
    20 	size_t insize = strlen(inbuf);
    21 	if(insize > SIZE_MAX/4) {
    21 	if(insize > SIZE_MAX/4) {
    22 		return NULL;
    22 		return NULL;
    23 	}
    23 	}
    24 
    24 
    25 	char *outbuf = malloc(insize*3+1);
    25 	char *outbuf = flib_malloc(insize*3+1);
    26 	if(!outbuf) {
    26 	if(!outbuf) {
    27 		return NULL;
    27 		return NULL;
    28 	}
    28 	}
    29 
    29 
    30     size_t inpos = 0, outpos = 0;
    30     size_t inpos = 0, outpos = 0;
    39             inpos++;
    39             inpos++;
    40             outpos += 3;
    40             outpos += 3;
    41         }
    41         }
    42     }
    42     }
    43     outbuf[outpos] = 0;
    43     outbuf[outpos] = 0;
    44     return outbuf;
    44     char *shrunk = realloc(outbuf, outpos+1);
       
    45     return shrunk ? shrunk : outbuf;
    45 }
    46 }
    46 
    47 
    47 char *inihelper_urldecode(const char *inbuf) {
    48 char *inihelper_urldecode(const char *inbuf) {
    48 	char *outbuf = malloc(strlen(inbuf)+1);
    49 	char *outbuf = flib_malloc(strlen(inbuf)+1);
    49 	if(!outbuf) {
    50 	if(!outbuf) {
    50 		return NULL;
    51 		return NULL;
    51 	}
    52 	}
    52 
    53 
    53     size_t inpos = 0, outpos = 0;
    54     size_t inpos = 0, outpos = 0;
    59         } else {
    60         } else {
    60         	outbuf[outpos++] = inbuf[inpos++];
    61         	outbuf[outpos++] = inbuf[inpos++];
    61         }
    62         }
    62     }
    63     }
    63     outbuf[outpos] = 0;
    64     outbuf[outpos] = 0;
    64     return outbuf;
    65     char *shrunk = realloc(outbuf, outpos+1);
       
    66     return shrunk ? shrunk : outbuf;
    65 }
    67 }
    66 
    68 
    67 char *inihelper_createDictKey(const char *sectionName, const char *keyName) {
    69 char *inihelper_createDictKey(const char *sectionName, const char *keyName) {
    68 	if(!sectionName || !keyName) {
    70 	if(!sectionName || !keyName) {
       
    71 		flib_log_e("null parameter in inihelper_createDictKey");
    69 		return NULL;
    72 		return NULL;
    70 	}
    73 	}
    71 	return flib_asprintf("%s:%s", sectionName, keyName);
    74 	return flib_asprintf("%s:%s", sectionName, keyName);
    72 }
    75 }
    73 
    76 
    74 char *inihelper_getstring(dictionary *inifile, bool *error, const char *sectionName, const char *keyName) {
    77 char *inihelper_getstring(dictionary *inifile, bool *error, const char *sectionName, const char *keyName) {
    75 	if(!inifile || !sectionName || !keyName) {
    78 	if(!inifile || !sectionName || !keyName) {
       
    79 		flib_log_e("null parameter in inihelper_getstring");
    76 		*error = true;
    80 		*error = true;
    77 		return NULL;
    81 		return NULL;
    78 	}
    82 	}
    79 	char *extendedkey = inihelper_createDictKey(sectionName, keyName);
    83 	char *extendedkey = inihelper_createDictKey(sectionName, keyName);
    80 	if(!extendedkey) {
    84 	if(!extendedkey) {
    82 		return NULL;
    86 		return NULL;
    83 	}
    87 	}
    84 	char *result = iniparser_getstring(inifile, extendedkey, NULL);
    88 	char *result = iniparser_getstring(inifile, extendedkey, NULL);
    85 	free(extendedkey);
    89 	free(extendedkey);
    86 	if(!result) {
    90 	if(!result) {
    87 		flib_log_i("Missing ini setting: %s/%s", sectionName, keyName);
    91 		flib_log_d("Missing ini setting: %s/%s", sectionName, keyName);
    88 		*error = true;
    92 		*error = true;
    89 	}
    93 	}
    90 	return result;
    94 	return result;
    91 }
    95 }
    92 
    96 
   100 		return 0;
   104 		return 0;
   101 	} else {
   105 	} else {
   102 		errno = 0;
   106 		errno = 0;
   103 		long val = strtol(value, NULL, 10);
   107 		long val = strtol(value, NULL, 10);
   104 		if(errno!=0) {
   108 		if(errno!=0) {
       
   109 			flib_log_w("Cannot parse ini setting %s/%s = \"%s\" as integer.", sectionName, keyName, value);
   105 			*error = true;
   110 			*error = true;
   106 			return 0;
   111 			return 0;
   107 		}
   112 		}
   108 		if(val<INT_MIN || val>INT_MAX) {
   113 		if(val<INT_MIN || val>INT_MAX) {
       
   114 			flib_log_w("ini setting %s/%s = \"%s\" is too large or too small.", sectionName, keyName, value);
   109 			*error = true;
   115 			*error = true;
   110 			return 0;
   116 			return 0;
   111 		}
   117 		}
   112 		return (int)val;
   118 		return (int)val;
   113 	}
   119 	}
   119 		return false;
   125 		return false;
   120 	} else {
   126 	} else {
   121 		bool trueval = strchr("1tTyY", value[0]);
   127 		bool trueval = strchr("1tTyY", value[0]);
   122 		bool falseval = strchr("0fFnN", value[0]);
   128 		bool falseval = strchr("0fFnN", value[0]);
   123 		if(!trueval && !falseval) {
   129 		if(!trueval && !falseval) {
       
   130 			flib_log_w("ini setting %s/%s = \"%s\" is not a recognized truth value.", sectionName, keyName, value);
   124 			*error = true;
   131 			*error = true;
   125 			return false;
   132 			return false;
   126 		} else {
   133 		} else {
   127 			return trueval;
   134 			return trueval;
   128 		}
   135 		}
   130 }
   137 }
   131 
   138 
   132 int inihelper_setstr(dictionary *dict, const char *sectionName, const char *keyName, const char *value) {
   139 int inihelper_setstr(dictionary *dict, const char *sectionName, const char *keyName, const char *value) {
   133 	int result = -1;
   140 	int result = -1;
   134 	if(!dict || !sectionName || !keyName || !value) {
   141 	if(!dict || !sectionName || !keyName || !value) {
   135 		flib_log_e("inihelper_setstr called with bad parameters");
   142 		flib_log_e("null parameter in inihelper_setstr");
   136 	} else {
   143 	} else {
   137 		char *extendedkey = inihelper_createDictKey(sectionName, keyName);
   144 		char *extendedkey = inihelper_createDictKey(sectionName, keyName);
   138 		if(extendedkey) {
   145 		if(extendedkey) {
   139 			result = iniparser_set(dict, extendedkey, value);
   146 			result = iniparser_set(dict, extendedkey, value);
   140 			free(extendedkey);
       
   141 		}
   147 		}
       
   148 		free(extendedkey);
   142 	}
   149 	}
   143 	return result;
   150 	return result;
   144 }
   151 }
   145 
   152 
   146 int inihelper_setint(dictionary *dict, const char *sectionName, const char *keyName, int value) {
   153 int inihelper_setint(dictionary *dict, const char *sectionName, const char *keyName, int value) {
   147 	int result = -1;
   154 	int result = -1;
   148 	if(!dict || !sectionName || !keyName) {
   155 	if(!dict || !sectionName || !keyName) {
   149 		flib_log_e("inihelper_setint called with bad parameters");
   156 		flib_log_e("null parameter in inihelper_setint");
   150 	} else {
   157 	} else {
   151 		char *strvalue = flib_asprintf("%i", value);
   158 		char *strvalue = flib_asprintf("%i", value);
   152 		if(strvalue) {
   159 		if(strvalue) {
   153 			result = inihelper_setstr(dict, sectionName, keyName, strvalue);
   160 			result = inihelper_setstr(dict, sectionName, keyName, strvalue);
   154 			free(strvalue);
   161 			free(strvalue);
   158 }
   165 }
   159 
   166 
   160 int inihelper_setbool(dictionary *dict, const char *sectionName, const char *keyName, bool value) {
   167 int inihelper_setbool(dictionary *dict, const char *sectionName, const char *keyName, bool value) {
   161 	int result = -1;
   168 	int result = -1;
   162 	if(!dict || !sectionName || !keyName) {
   169 	if(!dict || !sectionName || !keyName) {
   163 		flib_log_e("inihelper_setint called with bad parameters");
   170 		flib_log_e("null parameter in inihelper_setbool");
   164 	} else {
   171 	} else {
   165 		result = inihelper_setstr(dict, sectionName, keyName, value ? "true" : "false");
   172 		result = inihelper_setstr(dict, sectionName, keyName, value ? "true" : "false");
   166 	}
   173 	}
   167 	return result;
   174 	return result;
   168 }
   175 }