|
嗯……这个东西主要就是能分配一片内存,如果要往里面写什么,尽管调用函数,而不必关心缓存是否充足什么的~很简单啦~学过点数据结构都知道怎么写,之所以放上来,是因为这东西经常用到,我把这东西放上来,也免得各位以后用的时候再自己写了~
顺便说一下:这东西其实就是只用一片连续的内存,而没有用到链表什么的,之所以不用,是因为我当时没有这个需求。
代码里面的注释没多少,各位还是自己看代码啦~
另外,哪位要是觉得我提供的函数不够多不够爽,可以自己写~欢迎各位往里面加东西
要是引用的话麻烦写名作者就可以了~
PS:里面好像还有写测试用的注释……反正不影响运行……各位就凑合了~
先是头文件(bufctl.h):
[code:1]
/*
Author: Monnand
*/
#ifndef MONNAND_BUFCTL_H
#define MONNAND_BUFCTL_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MONNAND_BUFCTL_STEP ab->step
#define MONNAND_MAXBUF 100
#define MONNAND_MAXLINE 256
#define MONNAND_DEFAULT_STEP 100
#define MONNAND_DEFAULT_SIZE 256
#define ARRBUF_USELEN(ab) (((ab)->use_tail)-((ab)->buf))
#define ARRBUF_MAXLEN(ab) (((ab)->tail)-((ab)->buf))
#ifndef MONNAND_STATUS
#define MONNAND_STATUS
#define MONNAND_EOF 2
#define MONNAND_ERROR -1
#define MONNAND_SUCC 0
#define MONNAND_BUFMORE -2
#endif
typedef struct tag_arrbuf
{
void *use_tail;
void *tail;
int step;
void *buf;
}arrbuf;
/***************************************/
arrbuf *arrbuf_init(int, int); /*tested*/
int arrbuf_destroy(arrbuf *);/*tested*/
int arrbuf_set(arrbuf *, int, size_t);/*tested*/
int arrbuf_clean(arrbuf *);/*tested*/
char *arrbuf_bufcpy(arrbuf *);/*tested*/
/***************************************/
int arrbuf_write(arrbuf *, void *, int);/*tested*/
int arrbuf_strin(arrbuf *, char *);/*tested*/
int arrbuf_rewrite(arrbuf *, void *, int);/*tested*/
int arrbuf_writefd(arrbuf *, int);/*tested*/
int arrbuf_readfd(arrbuf *, int);/*tested*/
int arrbuf_freadline(arrbuf *, FILE *);
/***************************************/
int arrbuf_setstep(arrbuf *, int);/*tested*/
int arrbuf_travel(arrbuf *, int, travel_handler, void *);/*tested*/
int arrbuf_minsize(arrbuf *,int);/*tested*/
#ifndef MONNAND_MYIO
#define MONNAND_MYIO
int my_write(int, const void *, size_t);/*tested*/
#endif
#endif
[/code:1]
函数定义都在bufctl.c里面:
bufctl.c
[code:1]
#include "bufctl.h"
int my_write(int fd,const void *buf,size_t c)
{
int i;
for(i = write(fd,buf,c);i < c;i = write(fd,buf,c))
{
if(i<0)
return i;
buf += i;
}
return c;
}
arrbuf *arrbuf_init(int init_len,int init_step)
{
arrbuf *ret;
ret = (arrbuf *) malloc(sizeof(arrbuf));
if(NULL==ret)
return NULL;
if(init_len<=0)
init_len = MONNAND_DEFAULT_SIZE;
if(init_step<=0)
init_step = MONNAND_DEFAULT_STEP;
ret->buf=(void *)malloc(init_len);
if(NULL==(ret->buf))
{
free(ret);
return NULL;
}
memset(ret->buf,0,init_len);
ret->tail = ret->buf + init_len;
ret->use_tail = ret->buf;
ret->step = init_step;
return ret;
}
int arrbuf_destroy(arrbuf *ab)
{
if(NULL==ab)
return 0;
if(ab->buf!=ab->tail&&
ab->buf!=NULL)
free(ab->buf);
free(ab);
return 0;
}
int arrbuf_set(arrbuf *ab,int c,size_t len)
{
register int i;
i = ARRBUF_USELEN(ab);
if(NULL==ab||NULL==ab->buf)
return -1;
if(len>ARRBUF_MAXLEN(ab))
{
ab->buf = realloc(ab->buf,len+ab->step);
if(NULL == ab->buf)
return -1;
ab->tail = ab->buf + ab->step + len;
ab->use_tail = ab->buf + i;
memset(ab->buf,0,len+ab->step);
}
memset(ab->buf,c,len);
return 0;
}
int arrbuf_clean(arrbuf *ab)
{
if(NULL==ab)
return -1;
ab->use_tail = ab->buf;
if(NULL==ab->buf)
return 0;
else
memset(ab->buf,0,ARRBUF_MAXLEN(ab));
return 0;
}
char *arrbuf_bufcpy(arrbuf *ab)
{
register void *ret;
if(NULL==ab)
return NULL;
if(ab->tail==ab->buf||
ab->use_tail==ab->buf)
return NULL;
if(NULL==ab->buf)
return NULL;
ret = malloc(ARRBUF_USELEN(ab));
if(NULL==ret)
return NULL;
memcpy(ret, ab->buf, ARRBUF_USELEN(ab));
return ret;
}
int arrbuf_write(arrbuf *ab,void *buf,int len)
{
register int i;
register int j;
if(ab->use_tail>=ab->tail ||
(ab->use_tail + len)>=ab->tail)
{
i = ARRBUF_USELEN(ab)+len+ab->step;
j = ARRBUF_USELEN(ab);
ab->buf = realloc(ab->buf,i);
if(NULL == ab->buf)
return -1;
ab->tail = ab->buf + i;
ab->use_tail = ab->buf + j;
memset(ab->use_tail,0,i-j);
}
memcpy(ab->use_tail,buf,len);
ab->use_tail += len;
return 0;
}
int arrbuf_strin(arrbuf *ab,char *str)
{
if(NULL==ab||NULL==str)
return -1;
return arrbuf_write(ab,str,strlen(str)+1);
}
int arrbuf_rewrite(arrbuf *ab,void *buf,int len)
{
if(arrbuf_clean(ab)<0)
return -1;
return arrbuf_write(ab,buf,len);
}
int arrbuf_writefd(arrbuf *ab,int fd)
{
if(NULL==ab||fd<0)
return -1;
if(NULL==ab->buf)
return 0;
if(0==ARRBUF_USELEN(ab))
return 0;
return my_write(fd,ab->buf,ARRBUF_USELEN(ab));
}
int arrbuf_readfd(arrbuf *ab,int fd)
{
char buf[MONNAND_MAXBUF];
int i;
memset(buf,0,MONNAND_MAXBUF);
if(NULL==ab||fd<0)
return -1;
for(i = read(fd,buf,MONNAND_MAXBUF);i>0;i = read(fd,buf,MONNAND_MAXBUF))
arrbuf_write(ab,buf,i);
if(i<0)
return -1;
return 0;
}
int arrbuf_writefd(arrbuf *ab,int fd)
{
if(NULL==ab||fd<0)
return -1;
if(NULL==ab->buf)
return 0;
if(0==ARRBUF_USELEN(ab))
return 0;
return my_write(fd,ab->buf,ARRBUF_USELEN(ab));
}
int arrbuf_readfd(arrbuf *ab,int fd)
{
char buf[MONNAND_MAXBUF];
int i;
memset(buf,0,MONNAND_MAXBUF);
if(NULL==ab||fd<0)
return -1;
for(i = read(fd,buf,MONNAND_MAXBUF);i>0;i = read(fd,buf,MONNAND_MAXBUF))
arrbuf_write(ab,buf,i);
if(i<0)
return -1;
return 0;
}
int arrbuf_setstep(arrbuf *ab,int s)
{
if(s>0)
{
ab->step = s;
return MONNAND_SUCC;
}
else
return MONNAND_ERROR;
}
int arrbuf_travel(arrbuf *ab,int size,travel_handler p,void *arg)
{
register void *cur, *t;
register int i;
t = ab->buf+ARRBUF_USELEN(ab);
for( cur = ab->buf;
( (cur<t) && ((cur+size)<=t) );
cur += size)
{
i = p(cur,arg);
if(i<0)
return i;
}
return 0;
}
int arrbuf_minsize(arrbuf *ab,int size)
{
register size_t i;
register int j;
if(NULL==ab)
return -1;
i = ARRBUF_USELEN(ab)+ ((size>0)?size:0);
j = ARRBUF_USELEN(ab);
ab->buf = (void *)realloc(ab->buf,i);
if(NULL==ab->buf)
return -1;
ab->tail = ab->buf + i;
ab->use_tail = ab->buf + j;
return 0;
}
[/code:1]
凑合看啦~不明白再说~ |
|