home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / BDSC / BDSC-2 / CHARIO.C < prev    next >
Text File  |  2000-06-30  |  4KB  |  166 lines

  1. /* CHARIO.C        Character-oriented file I/O
  2.  
  3.     Written 1980 by Scott W. Layson
  4.     This code is in the public domain.
  5.  
  6. These routines deal with reading and writing large blocks of
  7. characters, when they do not fit neatly in sectors.  At the moment,
  8. only sequential output is supported; input is fully random. */
  9.  
  10.  
  11. #define TRUE (-1)
  12. #define FALSE 0
  13. #define NULL 0
  14.  
  15.  
  16. /* i/o buffer */
  17. struct iobuf {
  18.     int fd;
  19.     int isect;            /* currently buffered sector */
  20.     int nextc;            /* index of next char in buffer */
  21.     char buff [128];
  22.     };
  23.  
  24. /* seek opcodes */
  25. #define ABSOLUTE    0
  26. #define RELATIVE    1
  27.  
  28. #define INPUT        0
  29. #define OUTPUT        1
  30. #define UPDATE        2
  31.  
  32.  
  33. copen (buf, fname)            /* open a file for char input */
  34.     struct iobuf *buf;
  35.     char *fname;
  36. {
  37.     buf->fd = open (fname, INPUT);
  38.     if (buf->fd == -1) return (-1);
  39.     read (buf->fd, &buf->buff, 1);
  40.     buf->isect = 0;
  41.     buf->nextc = 0;
  42.     return (buf);
  43.     }
  44.  
  45.  
  46. ccreat (buf, fname)            /* create a file for char output */
  47.     struct iobuf *buf;
  48.     char *fname;
  49. {
  50.     buf->fd = creat (fname);
  51.     if (buf->fd == -1) return (-1);
  52.     buf->isect = 0;
  53.     buf->nextc = 0;
  54.     return (buf);
  55.     }
  56.  
  57.  
  58. cclose (buf)                /* close the file assoc. with buf */
  59.     struct iobuf *buf;
  60. {
  61.     close (buf->fd);
  62.     }
  63.  
  64.  
  65. cseek (buf, nbytes, code)    /* seek to a character (input only!) */
  66.     struct iobuf *buf;
  67.     unsigned nbytes, code;
  68. {
  69.     int newsect;
  70.  
  71.     if (buf < 0) return (-1);
  72.     if (code == RELATIVE) nbytes += buf->isect * 128 + buf->nextc;
  73.     newsect = nbytes / 128;
  74.     if (newsect != buf->isect
  75.         && (seek (buf->fd, newsect, ABSOLUTE) == -1
  76.            || read (buf->fd, &buf->buff, 1) == -1)) return (-1);
  77.     buf->nextc = nbytes % 128;
  78.     buf->isect = newsect;
  79.     return (nbytes);
  80.     }
  81.  
  82.  
  83. cread (buf, dest, nbytes)    /* read some bytes into dest */
  84.     struct iobuf *buf;
  85.     char *dest;
  86.     unsigned nbytes;
  87. {
  88.     int navail, nsects, nleft, nread1, nread2;
  89.  
  90.     if (buf < 0) return (-1);
  91.     navail = umin (nbytes, 128 - buf->nextc);
  92.     movmem (&buf->buff[buf->nextc], dest, navail);
  93.     nbytes -= navail;
  94.     buf->nextc += navail;
  95.     dest += navail;
  96.     nsects = nbytes / 128;
  97.     if (nsects) {
  98.         nread1 = read (buf->fd, dest, nsects);
  99.         if (nread1 == -1) return (navail);
  100.         buf->isect += nread1;
  101.         if (nread1 < nsects) return (navail + nread1 * 128);
  102.         dest += nread1 * 128;
  103.         }
  104.     else nread1 = 0;
  105.     if (buf->nextc == 128) {
  106.         nread2 = read (buf->fd, &buf->buff, 1);
  107.         if (nread2 == -1) return (navail);
  108.         ++(buf->isect);
  109.         buf->nextc = 0;
  110.         if (nread2 < 1) return (navail + nread1 * 128);
  111.         }
  112.     nleft = nbytes % 128;
  113.     movmem (&buf->buff, dest, nleft);
  114.     buf->nextc += nleft;
  115.     return (navail + nbytes);
  116.     }
  117.  
  118.  
  119. cwrite (buf, source, nbytes)    /* write some bytes from source */
  120.     struct iobuf *buf;
  121.     char *source;
  122.     unsigned nbytes;
  123. {
  124.     unsigned nleft, nfill, nsects;
  125.     
  126.     if (buf < 0) return (-1);
  127.     if (buf->nextc) {
  128.         nfill = umin (nbytes, 128 - buf->nextc);
  129.         movmem (source, &buf->buff[buf->nextc], nfill);
  130.         buf->nextc += nfill;
  131.         nbytes -= nfill;
  132.         source += nfill;
  133.         }
  134.     if (buf->nextc == 128) {
  135.         ++(buf->isect);
  136.         buf->nextc = 0;
  137.         if (write (buf->fd, &buf->buff, 1) < 1) return (-1);
  138.         }
  139.     nsects = nbytes / 128;
  140.     if (nsects  &&  write (buf->fd, source, nsects) < nsects)
  141.         return (-1);
  142.     nbytes %= 128;
  143.     movmem (source + nsects * 128, &buf->buff, nbytes);
  144.     buf->nextc += nbytes;
  145.     return (nsects * 128 + nbytes);
  146.     }
  147.  
  148.  
  149. cflush (buf)                /* flush an output buffer */
  150.     struct iobuf *buf;
  151. {
  152.     if (buf->nextc  &&  write (buf->fd, &buf->buff, 1) < 1)
  153.         return (-1);
  154.     return (1);
  155.     }
  156.  
  157.  
  158. umin (a, b)                /* unsigned min */
  159.     unsigned a, b;
  160. {
  161.     return ((a < b) ? a : b);
  162.     }
  163.  
  164.  
  165. /* End of CHARIO.C  --  Character oriented file I/O */
  166.