home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 2001 June
/
VPR0106A.BIN
/
OLS
/
BZ2L003
/
bz2l003.lzh
/
BZ2LIB
/
STRINGQ.C
< prev
next >
Wrap
C/C++ Source or Header
|
1998-06-02
|
2KB
|
120 lines
/*
stringq
process char buffer as queue like pipe
by Yoshioka Tsuneo(QWF00133@niftyserve.or.jp)
This File is Copy,Edit,Re-Distribute,etc.. FREE!
Welcome any e-mail!!
*/
#include "stringq.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
STRINGQ *STRINGQ_open(int size)
{
STRINGQ *b;
b = malloc(sizeof(STRINGQ));
if(!b)return NULL;
memset(b,0,sizeof(STRINGQ));
b->buf = malloc(size);
if(! b->buf){free(b);return NULL;}
memset(b->buf ,0,size);
b->bufsize = size;
return b;
}
int STRINGQ_getc(STRINGQ *b)
{
int c;
if(b->len==0){
if(b->exist_eof){return EOF;}else{return -2;}
}
c = b->buf[b->start_ptr];
b->start_ptr++;
if(b->start_ptr == b->bufsize){b->start_ptr = 0;}
b->len--;
return c;
}
int STRINGQ_ungetc(int c,STRINGQ *b)
{
b->start_ptr--;
if(b->start_ptr <0){b->start_ptr = b->bufsize -1;}
b->buf[b->start_ptr] = c;
b->len++;
return c;
}
int STRINGQ_putc(int c,STRINGQ *b)
{
if(b->len==b->bufsize){return -2;}
b->buf[b->end_ptr] = c;
b->end_ptr ++;
if(b->end_ptr == b->bufsize){b->end_ptr = 0;}
b->len++;
return c;
}
void STRINGQ_puteof(STRINGQ *b)
{
b->exist_eof = 1;
}
void STRINGQ_close(STRINGQ *b)
{
free(b->buf);
free(b);
}
int STRINGQ_write(STRINGQ *b,char *buff,int len)
{
int len2;
int save_len = len;
if(len+b->len > b->bufsize){return -2;}
len2=MIN(len,b->bufsize - b->start_ptr);
memcpy(b->buf + b->end_ptr,buff,len2);
len -= len2;
b->len += len2;
b->end_ptr += len2;
if(b->end_ptr == b->bufsize){b->end_ptr = 0;}
if(len>0){
memcpy(b->buf,buff+len2,len);
b->len += len;
b->end_ptr += len;
}
return save_len;
}
int STRINGQ_read(STRINGQ *b,char *buff,int len)
{
int readlen;
int len2;
if(len > b->len){
if(b->exist_eof){
readlen = b->len;
}else{
return -2;
}
}else{
readlen = len;
}
/*len = readlen;*/
len2 = MIN(len,b->bufsize - b->start_ptr);
memcpy(buff,b->buf + b->start_ptr,len2);
len -= len2;
b->len -= len2;
b->start_ptr += len2;
if(b->start_ptr == b->bufsize){b->start_ptr =0;}
if(len>0){
/* b->end_ptr == 0*/
memcpy(buff+len2,b->buf,len);
b->len -=len;
b->start_ptr +=len;
}
return readlen;
}