project_files/frontlib/ini/inihelper.c
changeset 7179 f84805e6df03
parent 7177 bf6cf4dd847a
child 7182 076aba32abd3
equal deleted inserted replaced
7177:bf6cf4dd847a 7179:f84805e6df03
     1 #include "inihelper.h"
       
     2 #include "../logging.h"
       
     3 #include "../util.h"
       
     4 
       
     5 #include <string.h>
       
     6 #include <stdlib.h>
       
     7 #include <ctype.h>
       
     8 #include <limits.h>
       
     9 #include <errno.h>
       
    10 #include <stdarg.h>
       
    11 
       
    12 static bool keychar_needs_urlencoding(char c) {
       
    13 	return !isalnum(c);
       
    14 }
       
    15 
       
    16 char *inihelper_urlencode(const char *inbuf) {
       
    17 	if(!inbuf) {
       
    18 		return NULL;
       
    19 	}
       
    20 	size_t insize = strlen(inbuf);
       
    21 	if(insize > SIZE_MAX/4) {
       
    22 		return NULL;
       
    23 	}
       
    24 
       
    25 	char *outbuf = malloc(insize*3+1);
       
    26 	if(!outbuf) {
       
    27 		return NULL;
       
    28 	}
       
    29 
       
    30     size_t inpos = 0, outpos = 0;
       
    31     while(inbuf[inpos]) {
       
    32         if(!keychar_needs_urlencoding(inbuf[inpos])) {
       
    33         	outbuf[outpos++] = inbuf[inpos++];
       
    34         } else {
       
    35             if(snprintf(outbuf+outpos, 4, "%%%02X", (unsigned)((uint8_t*)inbuf)[inpos])<0) {
       
    36             	free(outbuf);
       
    37             	return NULL;
       
    38             }
       
    39             inpos++;
       
    40             outpos += 3;
       
    41         }
       
    42     }
       
    43     outbuf[outpos] = 0;
       
    44     return outbuf;
       
    45 }
       
    46 
       
    47 char *inihelper_urldecode(const char *inbuf) {
       
    48 	char *outbuf = malloc(strlen(inbuf)+1);
       
    49 	if(!outbuf) {
       
    50 		return NULL;
       
    51 	}
       
    52 
       
    53     size_t inpos = 0, outpos = 0;
       
    54     while(inbuf[inpos]) {
       
    55         if(inbuf[inpos] == '%' && isxdigit(inbuf[inpos+1]) && isxdigit(inbuf[inpos+2])) {
       
    56             char temp[3] = {inbuf[inpos+1],inbuf[inpos+2],0};
       
    57             outbuf[outpos++] = strtol(temp, NULL, 16);
       
    58             inpos += 3;
       
    59         } else {
       
    60         	outbuf[outpos++] = inbuf[inpos++];
       
    61         }
       
    62     }
       
    63     outbuf[outpos] = 0;
       
    64     return outbuf;
       
    65 }
       
    66 
       
    67 char *inihelper_createDictKey(const char *sectionName, const char *keyName) {
       
    68 	if(!sectionName || !keyName) {
       
    69 		return NULL;
       
    70 	}
       
    71 	return flib_asprintf("%s:%s", sectionName, keyName);
       
    72 }
       
    73 
       
    74 char *inihelper_getstring(dictionary *inifile, bool *error, const char *sectionName, const char *keyName) {
       
    75 	if(!inifile || !sectionName || !keyName) {
       
    76 		*error = true;
       
    77 		return NULL;
       
    78 	}
       
    79 	char *extendedkey = inihelper_createDictKey(sectionName, keyName);
       
    80 	if(!extendedkey) {
       
    81 		*error = true;
       
    82 		return NULL;
       
    83 	}
       
    84 	char *result = iniparser_getstring(inifile, extendedkey, NULL);
       
    85 	free(extendedkey);
       
    86 	if(!result) {
       
    87 		flib_log_i("Missing ini setting: %s/%s", sectionName, keyName);
       
    88 		*error = true;
       
    89 	}
       
    90 	return result;
       
    91 }
       
    92 
       
    93 char *inihelper_getstringdup(dictionary *inifile, bool *error, const char *sectionName, const char *keyName) {
       
    94 	return flib_strdupnull(inihelper_getstring(inifile, error, sectionName, keyName));
       
    95 }
       
    96 
       
    97 int inihelper_getint(dictionary *inifile, bool *error, const char *sectionName, const char *keyName) {
       
    98 	char *value = inihelper_getstring(inifile, error, sectionName, keyName);
       
    99 	if(!value) {
       
   100 		return 0;
       
   101 	} else {
       
   102 		errno = 0;
       
   103 		long val = strtol(value, NULL, 10);
       
   104 		if(errno!=0) {
       
   105 			*error = true;
       
   106 			return 0;
       
   107 		}
       
   108 		if(val<INT_MIN || val>INT_MAX) {
       
   109 			*error = true;
       
   110 			return 0;
       
   111 		}
       
   112 		return (int)val;
       
   113 	}
       
   114 }
       
   115 
       
   116 bool inihelper_getbool(dictionary *inifile, bool *error, const char *sectionName, const char *keyName) {
       
   117 	char *value = inihelper_getstring(inifile, error, sectionName, keyName);
       
   118 	if(!value) {
       
   119 		return false;
       
   120 	} else {
       
   121 		bool trueval = strchr("1tTyY", value[0]);
       
   122 		bool falseval = strchr("0fFnN", value[0]);
       
   123 		if(!trueval && !falseval) {
       
   124 			*error = true;
       
   125 			return false;
       
   126 		} else {
       
   127 			return trueval;
       
   128 		}
       
   129 	}
       
   130 }
       
   131 
       
   132 int inihelper_setstr(dictionary *dict, const char *sectionName, const char *keyName, const char *value) {
       
   133 	int result = -1;
       
   134 	if(!dict || !sectionName || !keyName || !value) {
       
   135 		flib_log_e("inihelper_setstr called with bad parameters");
       
   136 	} else {
       
   137 		char *extendedkey = inihelper_createDictKey(sectionName, keyName);
       
   138 		if(extendedkey) {
       
   139 			result = iniparser_set(dict, extendedkey, value);
       
   140 			free(extendedkey);
       
   141 		}
       
   142 	}
       
   143 	return result;
       
   144 }
       
   145 
       
   146 int inihelper_setint(dictionary *dict, const char *sectionName, const char *keyName, int value) {
       
   147 	int result = -1;
       
   148 	if(!dict || !sectionName || !keyName) {
       
   149 		flib_log_e("inihelper_setint called with bad parameters");
       
   150 	} else {
       
   151 		char *strvalue = flib_asprintf("%i", value);
       
   152 		if(strvalue) {
       
   153 			result = inihelper_setstr(dict, sectionName, keyName, strvalue);
       
   154 			free(strvalue);
       
   155 		}
       
   156 	}
       
   157 	return result;
       
   158 }
       
   159 
       
   160 int inihelper_setbool(dictionary *dict, const char *sectionName, const char *keyName, bool value) {
       
   161 	int result = -1;
       
   162 	if(!dict || !sectionName || !keyName) {
       
   163 		flib_log_e("inihelper_setint called with bad parameters");
       
   164 	} else {
       
   165 		result = inihelper_setstr(dict, sectionName, keyName, value ? "true" : "false");
       
   166 	}
       
   167 	return result;
       
   168 }