/* CHARIO.C		Character-oriented file I/O

	Written 1980 by Scott W. Layson
	This code is in the public domain.

These routines deal with reading and writing large blocks of
characters, when they do not fit neatly in sectors.  At the moment,
only sequential output is supported; input is fully random.

NGT added support to allow random output. Position first using
1) cread - cwrite will write after record read.
2) cseek - cwrite will write at position seeked.

cflush is automatic on subsequent read, seek or close.


Integrity of isect is not yet tested, also RELATIVE cseek is not
yet tested. */

/* #define DEBUG */

#define TRUE (-1)
#define FALSE 0
#define NULL 0


/* i/o buffer */
struct iobuf {
	int fd;
	int isect;			/* currently buffered sector */
	int nextc;			/* index of next char in buffer */
	int lastop;			/* last operation was */
	char buff [128];
	};

/* seek opcodes */
#define ABSOLUTE	0
#define RELATIVE	1

#define INPUT		0
#define OUTPUT		1
#define UPDATE		2

#define WASWRITE	0
#define WASFLUSH	1
#define WASREAD		2
#define WASSEEK		3
#define WASOPEN		4

copen (buf, fname)			/* open a file for char input */
	struct iobuf *buf;
	char *fname;
{
	buf->fd = open (fname, UPDATE);
	if (buf->fd == -1) return (-1);
	read (buf->fd, &buf->buff, 1);
	buf->isect = 0;
	buf->nextc = 0;
	buf->lastop = WASOPEN;
	return (buf);
	}


ccreat (buf, fname)			/* create a file for char output */
	struct iobuf *buf;
	char *fname;
{
	buf->fd = creat (fname);
	if (buf->fd == -1) return (-1);
	buf->isect = 0;
	buf->nextc = 0;
	buf->lastop = WASOPEN;
	return (buf);
	}


cclose (buf)				/* close the file assoc. with buf */
	struct iobuf *buf;
{
	if (buf->lastop == WASWRITE) cflush(buf);    /* NGT */
	close (buf->fd);
	}


cseek (buf, nbytes, code)	/* seek to a character (input only!) */
	struct iobuf *buf;
	unsigned nbytes, code;
{
	int newsect;

#ifdef DEBUG
	printf("Cseek - address = %d, code = %d, isect = %d, tell = %d\n",
			nbytes,code,buf->isect,tell(buf->fd));
#endif

	if (buf < 0) return (-1);
	if (buf->lastop == WASWRITE) cflush(buf);    /* NGT */
	if (code == RELATIVE) nbytes += buf->isect * 128 + buf->nextc;
	newsect = nbytes / 128;
	if (newsect != buf->isect
	    && (seek (buf->fd, newsect, ABSOLUTE) == -1
		   || read (buf->fd, &buf->buff, 1) == -1)) return (-1);
	buf->nextc = nbytes % 128;
	buf->isect = newsect;
	buf->lastop = WASSEEK;
	return (nbytes);
	}

ctell (buf)		/* tell where we are in characters */
	struct iobuf *buf;
{
	unsigned nbytes;

	if (buf < 0) return (-1);
	nbytes = buf->isect * 128 + buf->nextc;
	return (nbytes);
	}


cread (buf, dest, nbytes)	/* read some bytes into dest */
	struct iobuf *buf;
	char *dest;
	unsigned nbytes;
{
	int navail, nsects, nleft, nread1, nread2;

	if (buf < 0) return (-1);
	if (buf->lastop == WASWRITE) cflush(buf);    /* NGT */
	navail = umin (nbytes, 128 - buf->nextc);
	movmem (&buf->buff[buf->nextc], dest, navail);
	nbytes -= navail;
	buf->lastop = WASREAD;
	buf->nextc += navail;
	dest += navail;
	nsects = nbytes / 128;
	if (nsects) {
		nread1 = read (buf->fd, dest, nsects);
		if (nread1 == -1) return (navail);
		buf->isect += nread1;
		if (nread1 < nsects) return (navail + nread1 * 128);
		dest += nread1 * 128;
		}
	else nread1 = 0;
	if (buf->nextc == 128) {
		nread2 = read (buf->fd, &buf->buff, 1);
		if (nread2 == -1) return (navail);
		++(buf->isect);
		buf->nextc = 0;
		if (nread2 < 1) return (navail + nread1 * 128);
		}
	nleft = nbytes % 128;
	movmem (&buf->buff, dest, nleft);
	buf->nextc += nleft;
	return (navail + nbytes);
	}


cwrite (buf, source, nbytes)	/* write some bytes from source */
	struct iobuf *buf;
	char *source;
	unsigned nbytes;
{
	unsigned nleft, nfill, nsects;
	int skpnt;
#ifdef DEBUG
	int rsects;
	printf("Bytes to write = %d\n",nbytes);
#endif
	if (buf < 0) return (-1);
	if (buf->lastop != WASWRITE) {
			skpnt = buf->isect;
			if (skpnt < 0) skpnt = 0;
#ifdef DEBUG
	printf("cwrite - skpnt = %d\n",skpnt);
#endif
			seek(buf->fd,skpnt,ABSOLUTE);
		}
	nfill = 0;
	if (buf->nextc) {
		nfill = umin (nbytes, 128 - buf->nextc);
		movmem (source, &buf->buff[buf->nextc], nfill);
		buf->nextc += nfill;
		nbytes -= nfill;
		source += nfill;
		}
	if (buf->nextc == 128) {
		++(buf->isect);
		buf->nextc = 0;
		if (write (buf->fd, &buf->buff, 1) < 1) return (-1);
		}
	nsects = nbytes / 128;
#ifdef DEBUG
	printf("Sectors = %d\n",nsects);
	if (nsects) {
		rsects = write (buf->fd, source, nsects);
		printf("Written = %d\n",rsects);
		if (nsects != rsects) {
		printf("Write error: %s \n",errmsg(errno()));
			return (-1);
		}
	}
#else
	if (nsects  &&  write (buf->fd, source, nsects) < nsects)
		return (-1);
#endif
	buf->isect += nsects;
	nbytes %= 128;
	if (nbytes) {
		if (read(buf->fd,&buf->buff,1) == 1)
			seek(buf->fd,-1,RELATIVE);
		movmem (source + nsects * 128, &buf->buff, nbytes);
		buf->nextc += nbytes;
		}
	buf->lastop = WASWRITE;
#ifdef DEBUG
	printf("Bytes written = %d\n",nsects * 128 + nbytes + nfill);
#endif
	return (nsects * 128 + nbytes + nfill);
	}


cflush (buf)				/* flush an output buffer */
	struct iobuf *buf;
{
	if (buf->nextc  &&  write (buf->fd, &buf->buff, 1) < 1)
		return (-1);

#ifdef DEBUG
	printf("cflush - nextc = %d\n",buf->nextc);
#endif

	buf->lastop = WASFLUSH;
	return (1);
	}


umin (a, b)				/* unsigned min */
	unsigned a, b;
{
	return ((a < b) ? a : b);
	}


/* End of CHARIO.C  --  Character oriented file I/O */
signed min */
	unsigned a, b;
{
	return ((a < b) ? a : b);
	}


/* End of CHARIO.C  --  Character oriented file I/O */