home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNIP9404.ZIP / TRANSLAT.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  2KB  |  72 lines

  1. /*
  2. ** Public Domain by Jerry Coffin.
  3. **
  4. ** Interpets a string in a manner similar to that the compiler
  5. ** does string literals in a program.  All escape sequences are
  6. ** longer than their translated equivalant, so the string is
  7. ** translated in place and either remains the same length or
  8. ** becomes shorter.
  9. */
  10.  
  11. #include <string.h>
  12. #include <stdio.h>
  13.  
  14. char *translate(char *string)
  15. {
  16.       char *here=string;
  17.       size_t len=strlen(string);
  18.       int num;
  19.       int numlen;
  20.  
  21.       while (NULL!=(here=strchr(here,'\\')))
  22.       {
  23.             numlen=1;
  24.             switch (here[1])
  25.             {
  26.             case '\\':
  27.                   break;
  28.  
  29.             case 'r':
  30.                   *here = '\r';
  31.                   break;
  32.  
  33.             case 'n':
  34.                   *here = '\n';
  35.                   break;
  36.  
  37.             case 't':
  38.                   *here = '\t';
  39.                   break;
  40.  
  41.             case 'v':
  42.                   *here = '\v';
  43.                   break;
  44.  
  45.             case 'a':
  46.                   *here = '\a';
  47.                   break;
  48.  
  49.             case '0':
  50.             case '1':
  51.             case '2':
  52.             case '3':
  53.             case '4':
  54.             case '5':
  55.             case '6':
  56.             case '7':
  57.                   numlen = sscanf(here,"%o",&num);
  58.                   *here = (char)num;
  59.                   break;
  60.  
  61.             case 'x':
  62.                   numlen = sscanf(here,"%x",&num);
  63.                   *here = (char) num;
  64.                   break;
  65.             }
  66.             num = here - string + numlen;
  67.             here++;
  68.             memmove(here,here+numlen,len-num );
  69.       }
  70.       return string;
  71. }
  72.