diff options
Diffstat (limited to 'util.h')
-rw-r--r-- | util.h | 25 |
1 files changed, 25 insertions, 0 deletions
@@ -30,6 +30,7 @@ #ifndef __UTIL_H #define __UTIL_H #include <stdlib.h> +#include <stdio.h> /* Abort on out of memory */ static void redisOOM(void) { @@ -37,4 +38,28 @@ static void redisOOM(void) { exit(1); } +static size_t chrtos(char *buf, size_t size, char byte) { + size_t len = 0; + + switch(byte) { + case '\\': + case '"': + len = snprintf(buf,size,"\"\\%c\"",byte); + break; + case '\n': len = snprintf(buf,size,"\"\\n\""); break; + case '\r': len = snprintf(buf,size,"\"\\r\""); break; + case '\t': len = snprintf(buf,size,"\"\\t\""); break; + case '\a': len = snprintf(buf,size,"\"\\a\""); break; + case '\b': len = snprintf(buf,size,"\"\\b\""); break; + default: + if (isprint(byte)) + len = snprintf(buf,size,"\"%c\"",byte); + else + len = snprintf(buf,size,"\"\\x%02x\"",(unsigned char)byte); + break; + } + + return len; +} + #endif |