home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / cbw / part01 / char-io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-16  |  1.2 KB  |  76 lines

  1. /*
  2.  * Read and write slashified characters.
  3.  * Translated from Tim Shepard's CLU code. 
  4.  */
  5.  
  6. #include    <stdio.h>
  7.  
  8. #define    EOL    (-37)
  9.  
  10. write_char(out, c)
  11. FILE    *out;
  12. int        c;
  13. {
  14.    char    *s;
  15.    char    buf[6];
  16.  
  17.    if (c == '\\')
  18.       {s = "\\\\";}
  19.    else if (c == '\n')
  20.       {s = "\\n";}
  21.    else if (c == '\t')
  22.       {s = "\\t";}
  23.    else if (c == '\f')
  24.       {s = "\\p";}
  25.    else if (c == '\b')
  26.       {s = "\\b";}
  27.    else if (c == '\r')
  28.       {s = "\\r";}
  29.    else if (c < ' ' || '~' < c)  {
  30.       s = buf;
  31.       s[0] = '\\';
  32.       s[1] = '0' + (c/64);
  33.       s[2] = '0' + ((c%64)/8);
  34.       s[3] = '0' + (c%8);
  35.       s[4] = '\000';
  36.       }
  37.    else  {
  38.       s = buf;
  39.       s[0] = c;
  40.       s[1] = '\000';
  41.       }
  42.  
  43.    fprintf(out, "%s", s);
  44. }
  45.  
  46.  
  47. int    read_char(inp)
  48. FILE    *inp;
  49. {
  50.    int    c;
  51.  
  52.    c = getc(inp);
  53.    if (c == EOF) return(EOF);
  54.    if (c == '\n') return(EOL);
  55.    if (c == '\\')  {
  56.       c = getc(inp);
  57.       if (c == EOF) return(EOF);
  58.       if (c == 'n')
  59.          {c = '\n';}
  60.       else if (c == 't')
  61.          {c = '\t';}
  62.       else if (c == 'p')
  63.          {c = '\f';}
  64.       else if (c == 'b')
  65.          {c = '\b';}
  66.       else if (c == 'r')
  67.          {c = '\r';}
  68.       else if ('0' <= c  &&  c <= '7')  {
  69.          c = 64*(c - '0');
  70.          c = c + 8*(getc(inp)-'0');
  71.          c = c + 1*(getc(inp)-'0');
  72.          }
  73.    }
  74.    return(c);
  75. }
  76.