#ifndef librock_INC_VSB_H #define librock_INC_VSB_H /* librock/vsb.h NOTE: This file is intended to be included from librock/mstruct.h If it is not, then #include FIRST, and protect from multiple inclusion with #ifndef/#define librock_INC_VSB_H See http://www.mibsoftware.com/librock/ for documentation and original copies of this software. This software shall be distributed with the implementation source code according to the license terms of the source code. License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ /* This is one of the general purpose librock memory structures. It is used as the basis of other memory structures and classes. For an overview of all of them see librock/mstruct.h. VSB - implements a variable sized block of memory. A struct librock_VSB_s points to a block of memory which can move so that it can be resized. Functions are provided which can insert and delete data in the block of memory, which can be as large as the maximum value of an int. The VSB functions "overallocate" memory (take more than is needed to satisfy the request) to accomodate some growth without having to move the buffer. For this reason, the size of the block and the size of the allocation are also members of the struct librock_VSB_s. Typical uses of Vsb's: working buffers for string editing, "building" variable length memory structures, holding variable length objects, implementing variable length arrays or memory blocks where all memory must be contigous. Adding a NUL terminator to the string is easily using the NULTerminate() call. (But also see the librock/astring.h functions for handling allocated NUL terminated strings.) C++ wrapper: CVsb C++ derived classes: CVsbLongList a contiguous block of long values CVsbShortList a contiguous block short values CVsbPtrList a contiguous block of void * values */ /* C++ usage: C usage: Declare a struct librock_VSB_s *, and initialize with a return from VSBalloc(). When complete, call VSBfree(). For special purpose uses, it is also possible to declare a struct librock_VSB_s, as long as all members are initialised to 0. This should be done only with an awareness of the underlying implementation. All VSB functions take a pointer to a struct librock_VSB_s as the first argument. Access the member items READ ONLY. Do not modify them. */ #ifdef __cplusplus extern "C" { #endif /**************************************************************/ #define librock_VSB_1 struct librock_VSB_s { /* Variable size buffer: holds up to 32K bytes */ char * const buf; /* 5-23-96 Pointer moved or assigned ONLY in VSB module */ int inbuf; int sizebuf; #ifdef __cplusplus /* C++ constructor added 5-23-96 */ librock_VSB_s():buf(0) { /* Made _far 6/3/96 */ *((const char * *) &buf) = 0; inbuf = 0; sizebuf = -1; /* Not allocated */ }; #endif }; /* Variable Size Buffer:vsb.c */ struct librock_VSB_s *librock_VSBalloc(); void librock_VSBfree(struct librock_VSB_s *v); char *librock_VSBrealloc(struct librock_VSB_s *v,int size); struct librock_VSB_s *librock_VSBalloccpy(const void *ptr,int len); struct librock_VSB_s *librock_VSBallocXA(const void *p,int len); void librock_VSBSetXA(struct librock_VSB_s *v,const void *p,int len); void librock_VSBWriteDisable(struct librock_VSB_s *v); void *librock_VSBWriteEnable(struct librock_VSB_s *v); int librock_VSBInsert(struct librock_VSB_s *v,int loc,const char *buf,int len); int librock_VSBAppend(struct librock_VSB_s *v,const char *buf,int len); void librock_VSBDelete(struct librock_VSB_s *v,int loc,int len); void librock_VSBClear(struct librock_VSB_s *v); void librock_VSBSetLength(struct librock_VSB_s *v,int appsize); char *librock_VSBsz(struct librock_VSB_s *v); /* VSB operations */ char *librock_VSBInsertAtPtr(struct librock_VSB_s *v,const char *ptr, const char *src,int len); char *librock_VSBDeleteAtPtr(struct librock_VSB_s *v,const char *ptr, int len); void librock_VSBtextrep(struct librock_VSB_s *vsb,const char *pszSearch,const char *pszReplace); int librock_VSBcmp(struct librock_VSB_s *v,const char *str); unsigned librock_VSBstripbl(struct librock_VSB_s *v,int ind); /* Types of VSBs */ void librock_PropertyVSB_Insert(struct librock_VSB_s *v,int offset,const char *tag,const char *value); void librock_PropertyVSB_Remove(struct librock_VSB_s *v,int offset,const char *tag); const char *librock_PropertyVSB_Lookup(struct librock_VSB_s *v,int offset,const char *tag); #ifdef __cplusplus }; #endif #ifdef __cplusplus class librock_CVsb { /* Wrapper for struct librock_VSB_s */ protected: struct librock_VSB_s m_vsb; public: librock_CVsb() { ::librock_VSBrealloc(&m_vsb,16); }; ~librock_CVsb() {::librock_VSBSetXA(&m_vsb,0,0);}; int VSBInsert(int loc,const char *buf,int len) {return ::librock_VSBInsert(&m_vsb,loc,buf,len);}; int VSBInsertsz(int loc,const char *lpsz); /* 6-28-96 */ void VSBDelete(int loc,int len){::librock_VSBDelete(&m_vsb,loc,len);}; void VSBClear(){::librock_VSBClear(&m_vsb);}; void VSBSetLength(int size){::librock_VSBSetLength(&m_vsb,size);}; void VSBSetXA(const char *p,int size){::librock_VSBSetXA(&m_vsb,p,size);}; int VSBAppend(const char *buf,int len){return ::librock_VSBAppend(&m_vsb,buf,len);}; int VSBAppendsz(const char *buf); struct librock_VSB_s *vsb() {return &m_vsb;}; int inbuf() {return m_vsb.inbuf;}; char *buf() {return m_vsb.buf;}; char *VSBsz() { return ::librock_VSBsz(&m_vsb);}; }; class librock_CVsbLongList { protected: struct librock_VSB_s *m_pvsb; public: librock_CVsbLongList() { m_pvsb = librock_VSBalloc(); } ~librock_CVsbLongList() {if (m_pvsb) {librock_VSBfree(m_pvsb);}}; int ItemCount() {return m_pvsb->inbuf/sizeof(long);}; void Clear() {librock_VSBClear(m_pvsb);}; void InsertBefore(int ind,long l) {::librock_VSBInsert(m_pvsb,ind*sizeof(long),(char *) &l,sizeof(long));}; void Append(long l) {::librock_VSBAppend(m_pvsb,(char *) &l,sizeof(long));}; void Remove(int ind) {::librock_VSBDelete(m_pvsb,ind * sizeof(long),sizeof(long));}; long & lvalue(int ind); }; class librock_CVsbShortList { protected: struct librock_VSB_s *m_pvsb; public: librock_CVsbShortList() { m_pvsb = librock_VSBalloc(); } ~librock_CVsbShortList() {if (m_pvsb) {librock_VSBfree(m_pvsb);}}; int ItemCount() {return m_pvsb->inbuf/sizeof(short);}; void InsertBefore(int ind,short l) {::librock_VSBInsert(m_pvsb,ind*sizeof(short),(char *) &l,sizeof(short));}; void Append(short l) {::librock_VSBAppend(m_pvsb,(char *) &l,sizeof(short));}; void Remove(int ind) {::librock_VSBDelete(m_pvsb,ind * sizeof(short),sizeof(short));}; void Clear() {librock_VSBClear(m_pvsb);}; short &lvalue(int ind) { short *pRet; pRet = (short *)m_pvsb->buf + ind;return *pRet; }; }; class librock_CVsbPtrList { protected: struct librock_VSB_s *m_pvsb; public: librock_CVsbPtrList() { m_pvsb = librock_VSBalloc(); } ~librock_CVsbPtrList() {if (m_pvsb) {librock_VSBfree(m_pvsb);}}; int ItemCount() {return m_pvsb->inbuf/sizeof(void *);}; void Clear() {librock_VSBClear(m_pvsb);}; void InsertBefore(int ind,void *l) {::librock_VSBInsert(m_pvsb,ind*sizeof(void *),(char *) &l,sizeof(void *));}; void Append(void *l) {::librock_VSBAppend(m_pvsb,(char *) &l,sizeof(void *));}; void Remove(int ind) {::librock_VSBDelete(m_pvsb,ind * sizeof(void*),sizeof(void *));}; void *GetAt(int ind) { /* 4/21/99 */ if (ind >= ItemCount()) { return 0; } return ((void * *)m_pvsb->buf)[ind]; } void *PutAt(int ind,void *p) { /* 4/21/99 */ if (ind >= ItemCount()) { return 0; } ((void * *)m_pvsb->buf)[ind] = p; return ((void * *)m_pvsb->buf)[ind]; } void *ppAt(int ind) { /* 6/9/99 */ return m_pvsb->buf + ind; } void * & lvalue(int ind); }; #endif /* __cplusplus declarations */ #endif /* librock_INC_VSB_H */ /* $Log: vsb.h,v $ Revision 1.3 2002/02/10 03:20:40 forrest@mibsoftware.com rights=#1 Standardized chg log Revision 1.2 2002/01/29 04:40:03 forrest@mibsoftware.com rights=#1 Prep for publish. API clean up, TAB, space at eol removal Revision 1.1 2001/01/06 19:36:17 forrest@mibsoftware.com rights=#1 Initial import to CVS rights#1 Copyright (c) Forrest J Cavalier III d-b-a Mib Software rights#1 License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */