123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- #include <stddef.h>
- //FIXME out gets silently truncated if outsize is too small
- size_t escape(char* in, char* out, size_t outsize) {
- size_t l = 0;
- while(*in && l + 3 < outsize) {
- switch(*in) {
- case '\n':
- *out++ = '\\';
- l++;
- *out = 'n';
- break;
- case '\r':
- *out++ = '\\';
- l++;
- *out = 'r';
- break;
- case '\t':
- *out++ = '\\';
- l++;
- *out = 't';
- break;
- case '\\':
- *out++ = '\\';
- l++;
- *out = '\\';
- break;
- case '"':
- *out++ = '\\';
- l++;
- *out = '"';
- break;
- case '\v':
- *out++ = '\\';
- l++;
- *out = '\v';
- break;
- case '\?':
- *out++ = '\\';
- l++;
- *out = '\?';
- break;
- case '\f':
- *out++ = '\\';
- l++;
- *out = '\f';
- break;
- default:
- *out = *in;
- }
- in++;
- out++;
- l++;
- }
- *out = 0;
- return l;
- }
- #include <assert.h>
- #include <stdlib.h>
- size_t unescape(char* in, char *out, size_t outsize) {
- size_t l = 0;
- while(*in && l + 2 < outsize) {
- switch (*in) {
- case '\\':
- ++in;
- assert(*in);
- switch(*in) {
- case 'n':
- *out='\n';
- break;
- case 'r':
- *out='\r';
- break;
- case 't':
- *out='\t';
- break;
- case '\\':
- *out='\\';
- break;
- case '"':
- *out='"';
- break;
- case 'v':
- *out='\v';
- break;
- case '\?':
- *out = '\?';
- break;
- case 'f':
- *out = '\f';
- break;
- case '\'':
- *out = '\'';
- break;
- case 'b':
- *out = '\b';
- break;
- // FIXME add handling of hex and octal
- default:
- abort();
- }
- break;
- default:
- *out=*in;
- }
- in++;
- out++;
- l++;
- }
- *out = 0;
- return l;
- }
|