www.pudn.com > ASM86_64.rar > iob.c


#include 

#include "iob.h"


/*  the backup buffer for readln_from_iob() */
static char backup_buf[80];
static int backup = 0;

void init_iob(int fd, struct iob *p)
{
	p->fd = fd;
	p->cnt = 0;
	p->buf_ptr = p->buf;
	p->tail_ptr = p->buf;
}


/*********************************************************
 paramater: 
            p: the io buffer pointer
            count: read into buffer bytes size
 return:
	    -1 if ERROR, 
	    read into buffer size;
**********************************************************/
int read_into_iob(struct iob *p, unsigned int count)
{
	int cnt = 0;

	if (p->buf_ptr == p->tail_ptr) { 
		/* buffer free or readed by user */
		if (count > IO_BUFSIZE)
			cnt = IO_BUFSIZE;
		else
			cnt = count;
		if (p->buf_ptr > p->buf)	/* already read by user */
			p->buf_ptr = p->tail_ptr = p->buf;
		p->cnt = read(p->fd, p->buf_ptr, cnt);

	} else if (p->buf_ptr < p->tail_ptr) {
		/* buffer stand by user read */
		if (count > IO_BUFSIZE - (p->tail_ptr - p->buf))
			cnt = IO_BUFSIZE - (p->tail_ptr - p->buf);
		else
			cnt = count;
		p->cnt = read(p->fd, p->tail_ptr, cnt);
	}

	if (p->cnt < 0)
		return -1;

	p->tail_ptr += p->cnt;

	return p->cnt;
}



int read_from_iob(struct iob *p, void *usrbuf, unsigned int count)
{
	int cnt = 0;

	if (p->tail_ptr > p->buf_ptr) {
		if (count > (p->tail_ptr - p->buf_ptr))
			cnt = p->tail_ptr - p->buf_ptr;
		else
			cnt = count;

		memcpy(usrbuf, p->buf_ptr, cnt);
		p->buf_ptr += cnt;
	}

	
	return cnt;
}


int readln_from_iob(struct iob *p, char *usrbuf)
{
	int cnt = 0;
	char c;
	int i = 0;

	if (backup) {
		memcpy(usrbuf, backup_buf, backup);
		i = backup;
		backup = 0;
	}

	while (cnt = read_from_iob(p, &c, 1)) {
		if (c == 0x0d)
			continue;
		if (c == '\n')
			break;	
		if (i < LINE_SIZE)
			usrbuf[i++] = c;
	}
	usrbuf[i] = 0;
	
	if (c != '\n') {	
		/* the iob_buffer is free but no match '\n' */
		memcpy(backup_buf, usrbuf, i);
		backup = i;
	}

	return cnt;
}




/*************************************************************************/

int write_into_iob(struct iob *p, void *usrbuf, unsigned int count)
{
	int cnt = 0;

	if (p->buf_ptr >= p->tail_ptr) {
		if (count > IO_BUFSIZE)
			cnt = IO_BUFSIZE;
		else
			cnt = count;
		if (p->buf_ptr > p->buf)
			p->buf_ptr = p->tail_ptr = p->buf;
		memcpy(p->buf_ptr, usrbuf, cnt);
	} else if (p->tail_ptr > p->buf_ptr) {
		if (count > IO_BUFSIZE - (p->tail_ptr - p->buf))
			cnt = IO_BUFSIZE - (p->tail_ptr - p->buf);
		else
			cnt = count;
		memcpy(p->tail_ptr, usrbuf, cnt);
	}
	
	p->cnt = cnt;
	p->tail_ptr += p->cnt;
	
	return p->cnt;
}




int write_from_iob(struct iob *p, unsigned int count)
{
	int cnt = 0;
	
	if (count > p->tail_ptr - p->buf_ptr)
		cnt = p->tail_ptr - p->buf_ptr;
	else
		cnt = count;

	p->cnt = write(p->fd, p->buf_ptr, cnt);
	p->buf_ptr += cnt;
	
	return p->cnt;
}

int flush_all_iob(struct iob *p)
{
	int cnt = write(p->fd, p->buf, p->tail_ptr - p->buf);
	p->buf_ptr += cnt;

	return cnt;
}

int flush_last_iob(struct iob *p)
{
	int cnt = 0;
	if (p->tail_ptr >= p->buf_ptr)
		cnt = write(p->fd, p->buf_ptr, p->tail_ptr - p->buf_ptr);
	
	p->buf_ptr += cnt;

	return cnt;
}

int is_free_iob(struct iob *p)
{
	return p->buf_ptr == p->buf && p->tail_ptr == p->buf;
}


int is_full_iob(struct iob *p)
{
	return p->tail_ptr == p->buf + IO_BUFSIZE 
		&& p->buf_ptr + IO_BUFSIZE;
}

int get_free_bytes(struct iob *p)
{
	return IO_BUFSIZE - (p->tail_ptr - p->buf);
}

#if 0

#include 

main()
{
	char msg[] = "hello";
	struct iob r_iob, w_iob;
	char str[80];
	char c;

	int r_fd = open("opcode.h", O_RDONLY);
	int w_fd = open("ww", O_CREAT);

	init_iob(r_fd, &r_iob);
	init_iob(0, &w_iob);
	
	write_into_iob(&w_iob, msg, sizeof(msg));
	write_into_iob(&w_iob, msg, sizeof(msg));
	printf("free: %d\n", get_free_bytes(&w_iob));
	//printf("---%s---",w_iob.buf);
	write_from_iob(&w_iob,100);
	printf("free: %d\n", get_free_bytes(&w_iob));
	puts("");
	flush_all_iob(&w_iob);
	flush_last_iob(&w_iob);
/*
	while (read_into_iob(&r_iob, sizeof(r_iob.buf))) {

		write_into_iob(&w_iob, r_iob.buf,sizeof(w_iob.buf));
		write_from_iob(&w_iob, IO_BUFSIZE);
	}
*/
	close(r_fd);
	close(w_fd);
}

#endif