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