project_files/frontlib/ini/inihelper.c
changeset 7177 bf6cf4dd847a
parent 7175 038e3415100a
equal deleted inserted replaced
7175:038e3415100a 7177:bf6cf4dd847a
     1 #include "inihelper.h"
     1 #include "inihelper.h"
     2 #include "../logging.h"
     2 #include "../logging.h"
       
     3 #include "../util.h"
     3 
     4 
     4 #include <string.h>
     5 #include <string.h>
     5 #include <stdlib.h>
     6 #include <stdlib.h>
     6 #include <ctype.h>
     7 #include <ctype.h>
     7 #include <limits.h>
     8 #include <limits.h>
     8 #include <errno.h>
     9 #include <errno.h>
     9 #include <stdarg.h>
    10 #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 
    11 
    30 static bool keychar_needs_urlencoding(char c) {
    12 static bool keychar_needs_urlencoding(char c) {
    31 	return !isalnum(c);
    13 	return !isalnum(c);
    32 }
    14 }
    33 
    15 
    48     size_t inpos = 0, outpos = 0;
    30     size_t inpos = 0, outpos = 0;
    49     while(inbuf[inpos]) {
    31     while(inbuf[inpos]) {
    50         if(!keychar_needs_urlencoding(inbuf[inpos])) {
    32         if(!keychar_needs_urlencoding(inbuf[inpos])) {
    51         	outbuf[outpos++] = inbuf[inpos++];
    33         	outbuf[outpos++] = inbuf[inpos++];
    52         } else {
    34         } else {
    53             sprintf(outbuf+outpos, "%%%02X", inbuf[inpos]);
    35             if(snprintf(outbuf+outpos, 4, "%%%02X", (unsigned)((uint8_t*)inbuf)[inpos])<0) {
       
    36             	free(outbuf);
       
    37             	return NULL;
       
    38             }
    54             inpos++;
    39             inpos++;
    55             outpos += 3;
    40             outpos += 3;
    56         }
    41         }
    57     }
    42     }
    58     outbuf[outpos] = 0;
    43     outbuf[outpos] = 0;
   104 	}
    89 	}
   105 	return result;
    90 	return result;
   106 }
    91 }
   107 
    92 
   108 char *inihelper_getstringdup(dictionary *inifile, bool *error, const char *sectionName, const char *keyName) {
    93 char *inihelper_getstringdup(dictionary *inifile, bool *error, const char *sectionName, const char *keyName) {
   109 	return inihelper_strdupnull(inihelper_getstring(inifile, error, sectionName, keyName));
    94 	return flib_strdupnull(inihelper_getstring(inifile, error, sectionName, keyName));
   110 }
    95 }
   111 
    96 
   112 int inihelper_getint(dictionary *inifile, bool *error, const char *sectionName, const char *keyName) {
    97 int inihelper_getint(dictionary *inifile, bool *error, const char *sectionName, const char *keyName) {
   113 	char *value = inihelper_getstring(inifile, error, sectionName, keyName);
    98 	char *value = inihelper_getstring(inifile, error, sectionName, keyName);
   114 	if(!value) {
    99 	if(!value) {