home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Boldly Go Collection
/
version40.iso
/
TS
/
17A
/
DRWIN101.ZIP
/
BUFUTIL.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-07-23
|
2KB
|
135 lines
#include <stdio.h>
#include <stdlib.h>
#include "bufutil.h"
int buferr=0;
int bufseterr(int e) { buferr=e; return (e==BUF_NONE); }
BUFF *bufopen(int size)
{
BUFF *b;
buferr=BUF_NONE;
b=(BUFF *)malloc(sizeof(BUFF));
if (b==NULL) { bufseterr(BUF_NOMEM); return NULL; }
b->siz=size;
b->buf=(BYTE*)malloc(size);
if (b->buf==NULL) {
free(b);
bufseterr(BUF_NOMEM);
return NULL;
}
b->hed=0;
b->tal=0;
b->cnt=0;
b->trm='\n';
return b;
} /*bufopen*/
int bufclose(BUFF *b)
{
buferr=BUF_NONE;
if (b==NULL) return bufseterr(BUF_BADBUFF);
free(b->buf);
free(b);
return bufseterr(BUF_NONE);
} /*bufclose*/
int bufputc(BUFF *b,int c)
{
buferr=BUF_NONE;
if (b==NULL) return bufseterr(BUF_BADBUFF);
if (b->cnt>=b->siz) return bufseterr(BUF_FULL);
b->cnt++;
b->buf[b->hed++]=c;
if (b->hed>=b->siz) b->hed=0;
return 1;
} /*bufputc*/
int bufgetc(BUFF *b)
{
int c;
buferr=BUF_NONE;
if (b==NULL) return bufseterr(BUF_BADBUFF);
if (b->cnt==0) return bufseterr(BUF_EMPTY);
b->cnt--;
c=b->buf[b->tal++];
if (b->tal>=b->siz) b->tal=0;
return c;
} /*bufgetc*/
int bufputs(BUFF *b,char *s)
{
buferr=BUF_NONE;
if (b==NULL) return bufseterr(BUF_BADBUFF);
while (*s && bufputc(b,*s)) s++;
if (*s) return 0;
return 1;
} /*bufputs*/
int bufgets(BUFF *b,char *s,int n)
{
buferr=BUF_NONE;
if (b==NULL) return bufseterr(BUF_BADBUFF);
while ((--n>0) && ((*s=bufgetc(b))!=b->trm) && (!buferr)) s++;
*s=0;
return buferr==BUF_NONE;
} /*bufgets*/
int bufputns(BUFF *b,char *s,int n)
{
buferr=BUF_NONE;
if (b==NULL) return bufseterr(BUF_BADBUFF);
while ((n-->0) && (!buferr)) bufputc(b,*s++);
return buferr==BUF_NONE;
} /*bufputns*/
int bufgetns(BUFF *b,char *s,int n)
{
buferr=BUF_NONE;
if (b==NULL) return bufseterr(BUF_BADBUFF);
while ((n-->0) && (!buferr)) *s++=bufgetc(b);
return buferr==BUF_NONE;
} /*bufgetns*/
int bufempty(BUFF *b)
{
buferr=BUF_NONE;
if (b==NULL) return bufseterr(BUF_BADBUFF);
return b->cnt==0;
} /*bufempty*/
int buffull(BUFF *b)
{
buferr=BUF_NONE;
if (b==NULL) return bufseterr(BUF_BADBUFF);
return (b->cnt==b->siz);
} /*buffull*/
int bufflush(BUFF *b)
{
buferr=BUF_NONE;
if (b==NULL) return bufseterr(BUF_BADBUFF);
b->cnt=0;
b->hed=0;
b->tal=0;
return 1;
} /*bufflush*/