home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / quickplot / part2 / iobuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.7 KB  |  172 lines

  1. /* :set tabstops=4                                                    */
  2. static char *RCSid = "$Header: iobuf.c,v 1.1 86/04/20 16:16:43 sysad Exp $";
  3.  
  4. /*
  5.  * $Log:    iobuf.c,v $
  6.  * Revision 1.1  86/04/20  16:16:43  sysad
  7.  * Initial distribution version
  8.  * 
  9.  * 
  10.  */
  11.  
  12. /* An utterly simple buffering scheme; it's all we need.            */
  13.  
  14. /* It is the intent of the author that this software may be distributed
  15.  * and used freely, without restriction.  If you make improvements or
  16.  * enhancements, I would appreciate a copy.
  17.  *
  18.  * Duane H. Hesser    Teltone Corporation
  19.  * ....uw-beaver!tikal!sysad
  20.  * ....uw-beaver!tikal!dhh
  21.  */
  22.  
  23. #include    "io.h"
  24.  
  25. bufopen(file,io)
  26. char *file;
  27. struct inbuf *io;
  28. {
  29.  
  30.     if((io->fd = open(file,0)) < 0)
  31.         return(-1);
  32.     io->nleft = 0;
  33.     io->eof = 0;
  34.     return(io->fd);
  35. }
  36.  
  37. bufclose(io)
  38. struct inbuf *io;
  39. {
  40.     if(io->fd >= 0) close(io->fd);
  41.     io->eof = 1;
  42.     io->nleft = 0;
  43.     io->fd = -1;
  44. }
  45.  
  46. char
  47. getbyte(io)
  48. struct inbuf *io;
  49. {
  50.     char next,ebuf[32];
  51.  
  52.     if(io->nleft <= 0)
  53.     {
  54.         if(io->fd < 0)
  55.         {
  56.             sprintf(ebuf,"%s: unopen file error in getbyte()\n");
  57.             write(2,ebuf,strlen(ebuf));
  58.             exit(69);
  59.         }
  60.         io->nleft = read(io->fd,io->buff,INBUFSIZE);
  61.         if(io->nleft < 0)
  62.         {
  63.             sprintf(ebuf,"%s: read error in getbyte()\n");
  64.             write(2,ebuf,strlen(ebuf));
  65.             exit(69);
  66.         }
  67.         io->nextp = io->buff;
  68.     }
  69.     if(io->nleft-- <= 0)
  70.     {
  71.         io->eof++;
  72.         return('\0');
  73.     }
  74.     next = *(io->nextp++) & '\377';
  75.     return(next);
  76. }
  77.  
  78.  
  79.  
  80. getstr(io,buf)
  81. struct inbuf *io;
  82. char *buf;
  83. {
  84.     char *ptr = buf;
  85.     char getbyte();
  86.  
  87.     while( *ptr = getbyte(io) )
  88.     {
  89.         if(*ptr++ == '\n')
  90.         {
  91.             *--ptr = '\0';
  92.             break;
  93.         }
  94.     }
  95.     return(strlen(buf));
  96. }
  97.  
  98.  
  99. #ifdef BIGENDIAN
  100. union {
  101.     short sh_word;
  102.     struct {
  103.         char hi_byte;
  104.         char lo_byte;
  105.     } sh_bytes;
  106. } sh;
  107. #else
  108. union {
  109.     short sh_word;
  110.     struct {
  111.         char lo_byte;
  112.         char hi_byte;
  113.     } sh_bytes;
  114. } sh;
  115. #endif
  116. short
  117. getshort(io)
  118. struct inbuf *io;
  119. {
  120.     char c,ebuf[32];
  121.  
  122.     c = getbyte(io);
  123.     if(eof(io))
  124.     {
  125.         sprintf(ebuf,"unexpected EOF in getshort()\n");
  126.         write(2,ebuf,strlen(ebuf));
  127.         exit(69);
  128.     }
  129.     else sh.sh_bytes.lo_byte = c;
  130.  
  131.     c = getbyte(io);
  132.     if(eof(io))
  133.     {
  134.         sprintf(ebuf,"unexpected EOF in getshort()\n");
  135.         write(2,ebuf,strlen(ebuf));
  136.         exit(69);
  137.     }
  138.     else sh.sh_bytes.hi_byte = c;
  139.     return(sh.sh_word);
  140. }
  141.  
  142. eof(source)
  143. struct inbuf *source;
  144. {
  145.     return(source->eof);
  146. }
  147.  
  148.  
  149. /* Note that we don't even bother to "open" for output; just use
  150.  * file descriptor 1 (normally stdout)
  151.  */
  152. bufwrite(from,len,outbuf)
  153. char *from;
  154. int len;
  155. struct outbuf *outbuf;
  156. {
  157.     if(len <= 0) return;
  158.     if((len + outbuf->datalen) > OUTBUFSIZE) buf_flush(outbuf);
  159.     bcopy(from,outbuf->nextp,len);
  160.     outbuf->datalen += len;
  161.     outbuf->nextp += len;
  162.     if((len + outbuf->datalen) >= OUTBUFSIZE) buf_flush(outbuf);
  163. }
  164.  
  165. buf_flush(buf)
  166. struct outbuf *buf;
  167. {
  168.     if(buf->datalen > 0) write(1,buf->buffer,buf->datalen);
  169.     buf->nextp = buf->buffer;
  170.     buf->datalen = 0;
  171. }
  172.