From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/translat.c | 71 +++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/translat.c (limited to 'reference/C/CONTRIB/SNIP/translat.c') diff --git a/reference/C/CONTRIB/SNIP/translat.c b/reference/C/CONTRIB/SNIP/translat.c new file mode 100755 index 0000000..e9aa3a0 --- /dev/null +++ b/reference/C/CONTRIB/SNIP/translat.c @@ -0,0 +1,71 @@ +/* +** Public Domain by Jerry Coffin. +** +** Interpets a string in a manner similar to that the compiler +** does string literals in a program. All escape sequences are +** longer than their translated equivalant, so the string is +** translated in place and either remains the same length or +** becomes shorter. +*/ + +#include +#include + +char *translate(char *string) +{ + char *here=string; + size_t len=strlen(string); + int num; + int numlen; + + while (NULL!=(here=strchr(here,'\\'))) + { + numlen=1; + switch (here[1]) + { + case '\\': + break; + + case 'r': + *here = '\r'; + break; + + case 'n': + *here = '\n'; + break; + + case 't': + *here = '\t'; + break; + + case 'v': + *here = '\v'; + break; + + case 'a': + *here = '\a'; + break; + + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + numlen = sscanf(here,"%o",&num); + *here = (char)num; + break; + + case 'x': + numlen = sscanf(here,"%x",&num); + *here = (char) num; + break; + } + num = here - string + numlen; + here++; + memmove(here,here+numlen,len-num ); + } + return string; +} -- cgit v1.2.3-54-g00ecf