home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / lib / dlibssrc / ctlcnv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-06  |  2.0 KB  |  92 lines

  1. #include <stdio.h>
  2.  
  3. extern    char    _numstr[];        /* string of hexadecimal digits */
  4.  
  5. char *ctlcnv(string)
  6. char *string;
  7. /*
  8.  *    Convert \<char> notation in <string> to actual characters.  This
  9.  *    is useful for reading strings from a stream when you want to allow
  10.  *    insertion of control character or other characters that may have
  11.  *    special meaning otherwise, or may not otherwise be allowed.  The
  12.  *    following formats are supported:
  13.  *        \n        newline or linefeed
  14.  *        \r        carriage return
  15.  *        \0        null character (value 0)
  16.  *        \b        backspace
  17.  *        \t        horizontal tab
  18.  *        \v        vertical tab
  19.  *        \f        form feed
  20.  *        \a        alarm (bell)
  21.  *        \\        backslash
  22.  *        \'        single quote
  23.  *        \"        double quote
  24.  *        \NNN        octal constant
  25.  *        \xNN        hexadecimal constant
  26.  *        \<nl>        "folded" line (both characters removed)
  27.  */
  28. {
  29.     register char *p, *q, c;
  30.     register int i, n;
  31.  
  32.     p = q = string;
  33.     while(*q) {
  34.         if(*q == '\\') {
  35.             switch(*++q) {
  36.                 case 'n':    /* newline or linefeed */
  37.                     *p++ = '\n';
  38.                     break;
  39.                 case 'r':    /* carriage return */
  40.                     *p++ = '\r';
  41.                     break;
  42.                 case 'b':    /* backspace */
  43.                     *p++ = '\b';
  44.                     break;
  45.                 case 't':    /* horizontal tab */
  46.                     *p++ = '\t';
  47.                     break;
  48.                 case 'v':    /* vertical tab */
  49.                     *p++ = '\v';
  50.                     break;
  51.                 case 'f':    /* form feed */
  52.                     *p++ = '\f';
  53.                     break;
  54.                 case 'a':    /* alarm (bell) */
  55.                     *p++ = '\a';
  56.                     break;
  57.                 case '0':
  58.                 case '1':
  59.                 case '2':
  60.                 case '3':    /* octal constant */
  61.                     c = (*q - '0');
  62.                     i = 0;
  63.                     n = 3;
  64.                     do {
  65.                         i = (i<<3) + c;
  66.                         c = (*++q - '0');
  67.                     } while(--n & (c >= 0) && (c < 8));
  68.                     *p++ = i;
  69.                     --q;
  70.                     break;
  71.                 case 'x':    /* hexadecimal constant */
  72.                     i = 0;
  73.                     n = 2;
  74.                     while(n-- & ((c = strpos(_numstr,
  75.                         toupper(*++q))) >= 0))
  76.                         i = (i << 4) + c;
  77.                     --q;
  78.                     break;
  79.                 case '\n':    /* "folded" line */
  80.                     break;
  81.                 default:
  82.                     *p++ = *q;
  83.             }
  84.         }
  85.         else
  86.             *p++ = *q;
  87.         ++q;
  88.     }
  89.     *p = '\0';
  90.     return(string);
  91. }
  92.