home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Lib / io / rd_buf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  2.2 KB  |  124 lines

  1. /* rd_buf.c: low level line reading of Control and DR Txt Files */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Lib/io/RCS/rd_buf.c,v 6.0 1991/12/18 20:22:26 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Lib/io/RCS/rd_buf.c,v 6.0 1991/12/18 20:22:26 jpo Rel $
  9.  *
  10.  * $Log: rd_buf.c,v $
  11.  * Revision 6.0  1991/12/18  20:22:26  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include "head.h"
  19.  
  20. #define COMMENTCHAR     '#'
  21. int                     protocol_mode = 0;
  22.  
  23. static int glinread ();
  24.  
  25.  
  26. /* ---------------------  Begin  Routines  -------------------------------- */
  27.  
  28.  
  29.  
  30.  
  31. int txt2buf (linebuf, len, fp)
  32. FILE           *fp;
  33. int        len;
  34. char           *linebuf;
  35. {
  36.     int             n;
  37.  
  38.     PP_DBG (("Lib/io/txt2buf()"));
  39.  
  40.     if ((n = glinread (fp, linebuf, len)) < 0)
  41.         return (RP_EOF);
  42.     if (n == 0)             /* time for a resync */
  43.         return (RP_PARM);
  44.     if (n == 1)
  45.         return (RP_DONE);
  46.     if (linebuf[--n] != '\n')
  47.         return (RP_MECH);
  48.     linebuf[n] = '\0';
  49.     return (RP_OK);
  50. }
  51.  
  52.  
  53.  
  54. /* ---------------------  Static  Routines  ------------------------------- */
  55.  
  56.  
  57.  
  58. static int glinread (fp, buf, size)
  59. register FILE  *fp;
  60. char           *buf;
  61. int    size;
  62. {
  63.     register int    c;
  64.     register int    i;
  65.     extern  int protocol_mode;
  66.  
  67.     PP_DBG (("Lib/io/glinread()"));
  68.  
  69.     if (feof(fp))
  70.         return EOF;
  71.  
  72.     for (--size, i = 0; i < size; i++)
  73.         switch (c = getc (fp)) {
  74.         case EOF:
  75.             return (EOF);
  76.  
  77.         case '\0':
  78.             return (0);
  79.  
  80.         case '\n':
  81.             *buf++ = c;
  82.             if (protocol_mode == 0) {
  83.                 /* -- first char on next line -- */
  84.                 (void) ungetc (c = getc (fp), fp);
  85.                 if (c == COMMENTCHAR) {
  86.                     /*
  87.                     check for comments within folding.
  88.                     Ignore everything up to and
  89.                     including new line character which
  90.                     is not followed by COMMENTCHAR,
  91.                     this could probably be recursive and
  92.                     clever - but what the hell
  93.                     */
  94.                     while (42) {
  95.                         if ((c = getc (fp)) == '\n') {
  96.                             (void) ungetc (c =
  97.                                    getc (fp), fp);
  98.                             if (c != COMMENTCHAR)
  99.                                 break;
  100.                         }
  101.                     }
  102.                 }
  103.  
  104.                 if (c == ' ' || c == '\t') {
  105.                     /* continuation line */
  106.                     c = getc (fp);
  107.                     *(buf - 1) = ' ';
  108.                     break;
  109.                 }
  110.             }
  111.             *buf = 0;
  112.             return (++i);
  113.  
  114.         default:
  115.             *buf++ = c;
  116.         }
  117.  
  118.     /*
  119.      * if ever get here then line too long
  120.      */
  121.     *buf = 0;
  122.     return (i);
  123. }
  124.