[utils] add url_encode() function
This commit is contained in:
parent
f1631752c4
commit
396700797b
2 changed files with 31 additions and 0 deletions
|
@ -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); }
|
||||
|
||||
|
|
27
src/utils.c
27
src/utils.c
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue