project_files/frontlib/util/util.c
changeset 7230 240620f46dd7
parent 7227 1c859f572d72
child 7269 5b0aeef8ba2a
--- a/project_files/frontlib/util/util.c	Tue Jun 12 21:10:11 2012 +0200
+++ b/project_files/frontlib/util/util.c	Fri Jun 15 19:57:25 2012 +0200
@@ -56,7 +56,7 @@
 
 void *flib_malloc(size_t size) {
 	void *result = malloc(size);
-	if(!result) {
+	if(!result && size>0) {
 		flib_log_e("Out of memory trying to malloc %zu bytes.", size);
 	}
 	return result;
@@ -64,17 +64,29 @@
 
 void *flib_calloc(size_t count, size_t elementsize) {
 	void *result = calloc(count, elementsize);
-	if(!result) {
+	if(!result && count>0 && elementsize>0) {
 		flib_log_e("Out of memory trying to calloc %zu objects of %zu bytes each.", count, elementsize);
 	}
 	return result;
 }
 
+void *flib_realloc(void *ptr, size_t size) {
+	void *result = realloc(ptr, size);
+	if(!result && size>0) {
+		flib_log_e("Out of memory trying to realloc %zu bytes.", size);
+	}
+	return result;
+}
+
 static bool isAsciiAlnum(char c) {
 	return (c>='0' && c<='9') || (c>='a' && c <='z') || (c>='A' && c <='Z');
 }
 
 char *flib_urlencode(const char *inbuf) {
+	return flib_urlencode_pred(inbuf, isAsciiAlnum);
+}
+
+char *flib_urlencode_pred(const char *inbuf, bool (*needsEscaping)(char c)) {
 	if(!inbuf) {
 		return NULL;
 	}
@@ -91,7 +103,7 @@
 
     size_t inpos = 0, outpos = 0;
     while(inbuf[inpos]) {
-        if(isAsciiAlnum(inbuf[inpos])) {
+        if(!needsEscaping(inbuf[inpos])) {
         	outbuf[outpos++] = inbuf[inpos++];
         } else {
             if(snprintf(outbuf+outpos, 4, "%%%02X", (unsigned)((uint8_t*)inbuf)[inpos])<0) {