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 / ENTERPRS / CPM / UTILS / S / SMC21LIB.LZH / AUXBUF.C < prev    next >
Text File  |  2000-06-30  |  2KB  |  76 lines

  1.  
  2. #define NOCCARG
  3. #include stdio.h
  4. #include clib.def
  5. extern int *_auxsz, *_auxef, _auxrd, _auxwt, _auxfl, _status[];
  6. /*
  7. ** Loaded only if auxbuf() is called.  Longer explanation in DDJ.
  8. */
  9. int
  10.   _xsize[MAXFILES],  /*size of buffer*/
  11.   _xaddr[MAXFILES],   /*aux buffer address*/
  12.   _xnext[MAXFILES],   /*address of next byte in buffer*/
  13.   _xend[MAXFILES],    /*address of end-of-data in buffer*/
  14.   _xeof[MAXFILES];    /*true if current buffer ends file*/
  15. /*
  16. ** auxbuf(fd, size) -- allocate an auxiliary buffer of size size for
  17. ** fd fd.  Longer explanation in DDJ.
  18. */
  19. auxbuf(fd, size) int fd; char *size; {  /*fake unsigned*/
  20.   if(!_mode(fd) || size || avail(NO) < size || _xsize[fd])
  21.     return (ERR);
  22.   _xaddr[fd] = _xnext[fd] = _xend[fd] = malloc(size);
  23.   _auxef = _xeof;  /*pass locations to i/o routines*/
  24.   _auxrd =  _xread;
  25.   _auxwt = _xwrite;
  26.   _auxsz = _xsize;
  27.   _auxfl = _xflush;
  28.   _xsize[fd] = size;
  29.   return (NULL);
  30.   }
  31.  
  32. /*
  33. ** Fill buffer if necessary, and return next byte.
  34. */
  35. _xread(fd) int fd; {
  36.   char *ptr;
  37.   while(YES) {
  38.     ptr = _xnext[fd];
  39.     if(ptr < _xend[fd]) {++_xnext[fd]; return (*ptr);}
  40.     if(_xeof[fd]) {_seteof(fd); return (EOF);}
  41.     _auxsz = NULL;    /*avoid recursive loop*/
  42.     _xend[fd] = _xaddr[fd] 
  43.                 + read(fd, _xnext[fd]=_xaddr[fd], _xsize[fd]);
  44.     _auxsz = _xsize;  /*restore _auxsz*/
  45.     if(feof(fd)) {_xeof[fd] = YES; _clreof(fd);}
  46.     }
  47.   }
  48.  
  49. /*
  50. ** Empty buffer if necessary, and store ch in buffer.
  51. */
  52. _xwrite(ch, fd) int ch, fd; {
  53.   char *ptr;
  54.   while(YES) {
  55.     ptr = _xnext[fd];
  56.     if(ptr < (_xaddr[fd] + _xsize[fd]))
  57.       {*ptr = ch; ++_xnext[fd]; return (ch);}
  58.     if(_xflush(fd)) return (EOF);
  59.     }
  60.   }
  61.  
  62. /*
  63. ** Flush aux buffer to file.
  64. */
  65. _xflush(fd) int fd; {
  66.   int i, j;
  67.   i = _xnext[fd] - _xaddr[fd];
  68.   _auxsz = NULL;  /*avoid recursive loop*/
  69.   j = write(fd, _xnext[fd]=_xaddr[fd], i);
  70.   _auxsz = _xsize; /*restore _auxsz*/
  71.   if(i != j) return (EOF);
  72.   return (NULL);
  73.   }
  74.  
  75.  
  76.