]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
[utils] add url_encode() function
authorSam Stenvall <neggelandia@gmail.com>
Thu, 8 Jan 2015 12:22:12 +0000 (14:22 +0200)
committerJaroslav Kysela <perex@perex.cz>
Fri, 9 Jan 2015 12:34:03 +0000 (13:34 +0100)
src/tvheadend.h
src/utils.c

index 70eb54377a47ce7aebe7c63fd59fca4f191d0521..792400990adbeb0722df5c7c4ebabd37c27ef5aa 100644 (file)
@@ -711,6 +711,10 @@ int rmtree ( const char *path );
 
 char *regexp_escape ( const char *str );
 
+/* URL decoding */
+char to_hex(char code);
+char *url_encode(char *str);
+
 static inline int32_t deltaI32(int32_t a, int32_t b) { return (a > b) ? (a - b) : (b - a); }
 static inline uint32_t deltaU32(uint32_t a, uint32_t b) { return (a > b) ? (a - b) : (b - a); }
   
index 67448c455d2d525736977f41557d5efefa30ff0f..92d309fc58d25f0d7f67f6afedb9eedc9e15b096 100644 (file)
@@ -25,6 +25,7 @@
 #include <sys/types.h>
 #include <dirent.h>
 #include <unistd.h>
+#include <ctype.h>
 #include "tvheadend.h"
 
 #if defined(PLATFORM_DARWIN)
@@ -588,3 +589,29 @@ regexp_escape(const char* str)
   *b = 0;
   return tmp;
 }
+
+/* Converts an integer value to its hex character
+   http://www.geekhideout.com/urlcode.shtml */
+char to_hex(char code) {
+  static char hex[] = "0123456789abcdef";
+  return hex[code & 15];
+}
+
+/* Returns a url-encoded version of str
+   IMPORTANT: be sure to free() the returned string after use
+   http://www.geekhideout.com/urlcode.shtml */
+char *url_encode(char *str) {
+  char *pstr = str, *buf = malloc(strlen(str) * 3 + 1), *pbuf = buf;
+  while (*pstr) {
+    if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') 
+      *pbuf++ = *pstr;
+    /*else if (*pstr == ' ') 
+      *pbuf++ = '+';*/
+    else 
+      *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
+    pstr++;
+  }
+  *pbuf = '\0';
+  return buf;
+}
+