home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / TRANSLAT.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  83 lines

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