diff -r 6171f0bad318 -r f7b49b2c5d84 project_files/frontlib/util/util.c --- a/project_files/frontlib/util/util.c Wed Jun 27 22:52:19 2012 +0200 +++ b/project_files/frontlib/util/util.c Thu Jul 05 00:33:24 2012 +0200 @@ -26,6 +26,7 @@ #include #include #include +#include char *flib_asprintf(const char *fmt, ...) { va_list argp; @@ -37,13 +38,9 @@ char *flib_vasprintf(const char *fmt, va_list args) { char *result = NULL; - if(!fmt) { - flib_log_e("null parameter in flib_vasprintf"); - } else { + if(!log_badargs_if(fmt==NULL)) { int requiredSize = vsnprintf(NULL, 0, fmt, args)+1; // Figure out how much memory we need, - if(requiredSize<0) { - flib_log_e("Error formatting string with template \"%s\" in flib_vasprintf", fmt); - } else { + if(!log_e_if(requiredSize<0, "Error formatting string with template \"%s\"", fmt)) { char *tmpbuf = flib_malloc(requiredSize); // allocate it if(tmpbuf && vsnprintf(tmpbuf, requiredSize, fmt, args)>=0) { // and then do the actual formatting. result = tmpbuf; @@ -56,41 +53,41 @@ } char *flib_join(char **parts, int partCount, const char *delimiter) { - size_t totalSize = 1; - size_t delimLen = strlen(delimiter); - for(int i=0; i0) { - strcpy(result+outpos, delimiter); - outpos += delimLen; + if(result) { + size_t outpos = 0; + for(int i=0; i0) { + strcpy(result+outpos, delimiter); + outpos += delimLen; + } + strcpy(result+outpos, parts[i]); + outpos += strlen(parts[i]); } - strcpy(result+outpos, parts[i]); - outpos += strlen(parts[i]); } } return result; } char *flib_strdupnull(const char *str) { - if(!str) { - return NULL; - } - return flib_asprintf("%s", str); + return str==NULL ? NULL : flib_asprintf("%s", str); } void *flib_bufdupnull(const void *buf, size_t size) { - if(!buf || size==0) { - return NULL; - } - void *result = flib_malloc(size); - if(result) { - memcpy(result, buf, size); + void *result = NULL; + if(!log_badargs_if(buf==NULL && size>0)) { + result = flib_malloc(size); + if(result) { + memcpy(result, buf, size); + } } return result; } @@ -127,41 +124,45 @@ return flib_urlencode_pred(inbuf, isAsciiAlnum); } +static size_t countCharsToEscape(const char *inbuf, bool (*needsEscaping)(char c)) { + size_t result = 0; + for(const char *c=inbuf; *c; c++) { + if(needsEscaping(*c)) { + result++; + } + } + return result; +} + char *flib_urlencode_pred(const char *inbuf, bool (*needsEscaping)(char c)) { + char *result = NULL; + if(inbuf && !log_badargs_if(needsEscaping == NULL)) { + size_t insize = strlen(inbuf); + if(!log_e_if(insize > SIZE_MAX/4, "String too long: %zu bytes.", insize)) { + size_t escapeCount = countCharsToEscape(inbuf, needsEscaping); + result = flib_malloc(insize + escapeCount*2 + 1); + } + if(result) { + char *out = result; + for(const char *in = inbuf; *in; in++) { + if(!needsEscaping(*in)) { + *out = *in; + out++; + } else { + snprintf(out, 4, "%%%02x", (unsigned)(*(uint8_t*)in)); + out += 3; + } + } + *out = 0; + } + } + return result; +} + +char *flib_urldecode(const char *inbuf) { if(!inbuf) { return NULL; } - size_t insize = strlen(inbuf); - if(insize > SIZE_MAX/4) { - flib_log_e("String too long in flib_urlencode: %zu bytes.", insize); - return NULL; - } - - char *outbuf = flib_malloc(insize*3+1); - if(!outbuf) { - return NULL; - } - - size_t inpos = 0, outpos = 0; - while(inbuf[inpos]) { - if(!needsEscaping(inbuf[inpos])) { - outbuf[outpos++] = inbuf[inpos++]; - } else { - if(snprintf(outbuf+outpos, 4, "%%%02X", (unsigned)((uint8_t*)inbuf)[inpos])<0) { - flib_log_e("printf error in flib_urlencode"); - free(outbuf); - return NULL; - } - inpos++; - outpos += 3; - } - } - outbuf[outpos] = 0; - char *shrunk = realloc(outbuf, outpos+1); - return shrunk ? shrunk : outbuf; -} - -char *flib_urldecode(const char *inbuf) { char *outbuf = flib_malloc(strlen(inbuf)+1); if(!outbuf) { return NULL; @@ -183,7 +184,7 @@ } bool flib_contains_dir_separator(const char *str) { - if(!log_badparams_if(!str)) { + if(!log_badargs_if(!str)) { for(;*str;str++) { if(*str=='\\' || *str=='/') { return true; @@ -193,6 +194,10 @@ return false; } +bool flib_strempty(const char *str) { + return !str || !*str; +} + int flib_gets(char *str, size_t strlen) { if(fgets(str, strlen, stdin)) { for(char *s=str; *s; s++) {