/* * iobuf.c * * An I/O buffer library based on sbuf. * * History: * 12/03/2014 Creation. * 14/03/2014 Compilation fixes. * * Author: Dario Rodriguez dario@softhome.net * This library is licensed on the terms of the GNU LGPL v2+ */ #include <stdlib.h> #include <string.h> #include "iobuf.h" /* exported funcions */ siobuf * iobuf_init(int insize,int outsize) { siobuf *iobuf; if((iobuf=malloc(sizeof(siobuf)))==NULL) return(NULL); memset(iobuf,0,sizeof(siobuf)); if((iobuf->in=sbuf_init(insize))==NULL) { free(iobuf),iobuf=NULL; return(NULL); } if((iobuf->out=sbuf_init(outsize))==NULL) { sbuf_free(iobuf->in),iobuf->in=NULL; free(iobuf),iobuf=NULL; return(NULL); } return(iobuf); } siobuf * iobuf_staticinit(siobuf *uninitiobuf, sbuf *uninitinbuf, sbuf *uninitoutbuf,int insize, int outsize, char *inbuf, char *outbuf) { memset(uninitiobuf,0,sizeof(siobuf)); if((uninitiobuf->in=sbuf_staticinit(uninitinbuf,insize,inbuf))==NULL) return(NULL); if((uninitiobuf->out=sbuf_staticinit(uninitoutbuf,outsize,outbuf))==NULL) return(NULL); return(uninitiobuf); } void iobuf_free(siobuf *iobuf) { if(iobuf==NULL) return; if(iobuf->in!=NULL) sbuf_free(iobuf->in),iobuf->in=NULL; if(iobuf->out!=NULL) sbuf_free(iobuf->out),iobuf->out=NULL; free(iobuf); } void iobuf_staticfree(siobuf *iobuf) { if(iobuf==NULL) return; sbuf_staticfree(iobuf->in),iobuf->in=NULL; sbuf_staticfree(iobuf->out),iobuf->out=NULL; return; }