/* librock/exec/struct/vsb.c librock_CHISEL _summary Variable size memory block/buffer implementation Copyright (c) 2001-2002, Forrest J. Cavalier III, doing business as Mib Software, Saylorsburg Pennsylvania USA Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************/ #ifdef librock_NOTLIBROCK /* ABOUT THIS FILE: GUIDE TO QUICK REUSE WITHOUT REWORK This file uses many preprocessor conditional blocks and features to publish http://www.mibsoftware.com/librock/ You can disable those features with little or no editing, and reuse this file: 1. At compile time, enable this conditional block by defining the preprocessor symbol librock_NOTLIBROCK, either with a compiler command line parameter, or as a #define in a source file which then #includes this source file. 2. Define any preprocessor symbols in this block (if any) appropriately for your machine and compilation environment. 3. Copy and use the declarations from this block in your source files as appropriate. This file is originally from the librock library, which is Free (libre), free (no cost), rock-stable API, and works on gcc/MSVC/Windows/Linux/BSD/more. Get originals, updates, license certificates, more, at http://www.mibsoftware.com/librock/ (Change log appears at end of this file.) */ #define librock_ISOLATED #include #include #include #include #define librock_PTR #define librock_CONST const #define librock_PRIVATE static #ifndef librock_assert #define librock_assert(a) #endif struct librock_VSB_s { char * const buf; /* 5-23-96 Pointer moved or assigned ONLY in VSB module */ int inbuf; int sizebuf; }; /* 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); #endif /* ifdef librock_NOTLIBROCK */ /**************************************************************/ #ifndef librock_ISOLATED /**************************************************************/ #define librock_IMPLEMENT_vsb #include #include #ifndef librock_assert #define librock_assert assert #endif #include #include #include #include /**************************************************************/ #endif #ifdef librock_IMPL_LIDESC #ifndef librock_NOIMPL_LIDESC_vsb /**************************************************************/ #include /* librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ void *librock_LIDESC_vsb[] = { "\n" __FILE__ librock_LIDESC_librock "\n", 0 }; /**************************************************************/ #endif #endif #ifndef librock_WRAP /**************************************************************/ #define librock_body_VSBrealloc librock_VSBrealloc #define librock_body_VSBalloc librock_VSBalloc #define librock_body_VSBallocXA librock_VSBallocXA #define librock_body_VSBalloccpy librock_VSBalloccpy #define librock_body_VSBfree librock_VSBfree #define librock_body_VSBsz librock_VSBsz #define librock_body_VSBWriteDisable librock_VSBWriteDisable #define librock_body_VSBWriteEnable librock_VSBWriteEnable #define librock_body_VSBSetXA librock_VSBSetXA #define librock_body_VSBInsert librock_VSBInsert #define librock_body_VSBAppend librock_VSBAppend #define librock_body_VSBGrow librock_VSBGrow #define librock_body_VSBShrink librock_VSBShrink #define librock_body_VSBDelete librock_VSBDelete #define librock_body_VSBClear librock_VSBClear #define librock_body_VSBSetLength librock_VSBSetLength /**************************************************************/ #endif #ifndef librock_fmalloc #define librock_frealloc realloc #define librock_ffree free #define librock_fmalloc(a,b) malloc(b) #endif #ifndef librock_memcpy #define librock_memcpy memcpy #define librock_memset memset #define librock_memmove memmove #endif #ifndef librock_VSBchunk #define librock_VSBchunk 64 #endif /* Improved on 12/12/95 to support Read Only VSBs */ /* Protocol: Two types of Read only VSBs: sizebuf == -1 if v->buf was not allocated. sizebuf == 0 if v->buf was allocated, but is read only Only difference in handling after creation is: Allocated buffers are freed on VSBfree. Assertions added to prevent modifying read only VSBs */ /**************************************************************/ int librock_VSBGrow(struct librock_VSB_s librock_PTR *v,int appsize); /* Used internally */ void librock_VSBShrink(struct librock_VSB_s librock_PTR *v); /* Used internally */ #ifndef librock_NOIMPL_VSBrealloc char librock_PTR *librock_body_VSBrealloc(struct librock_VSB_s librock_PTR *v,int size) { /* Written 5-23-96 for MIB_VSB_1 */ /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ char librock_PTR *ptr; if (!v) { /* 5-27-96 */ assert(0); return 0; } if (size <= 0) { assert(0); /* That is not the purpose of this function. Set VSBSetReadOnly() or VSBSetLength */ size = librock_VSBchunk; } ptr = (char *) librock_frealloc(v->buf,size); if (!ptr) { return 0; /* Show request was denied */ } *((char librock_PTR * librock_PTR *) &(v->buf)) = ptr; /* Get around const */ v->sizebuf = size; return ptr; }; /**************************************************************/ #endif /* NOIMPL */ #ifndef librock_NOIMPL_VSBalloc /**************************************************************/ struct librock_VSB_s librock_PTR *librock_body_VSBalloc() { /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_VSBalloc /* librock_VSBalloc - create librock variable-sized contiguous buffer (librock_VSB) librock_VSBallocXA - create librock_VSB for externally allocated buffer librock_VSBalloccpy - create librock_VSB and preload librock_VSBrealloc - decrease or expand a librock_VSB librock_VSBsz - Ensure '\0' follows the buffer, and return pointer to buffer. librock_VSBfree - deallocate storage associated with a librock_VSB */ /**/ #include struct librock_VSB_s * librock_VSBalloc(); struct librock_VSB_s * librock_VSBallocXA( const void *p, int len ); struct librock_VSB_s * librock_VSBalloccpy( const void *ptr, int len ); void librock_VSBfree( struct librock_VSB_s *v ); char * librock_VSBsz( struct librock_VSB_s *v ); /* librock_VSBalloc returns a pointer to a freshly malloc'ed librock_VSB_s structure and malloc's a starting buffer. The librock_VSB_s describes a contiguous block of memory which can expand and contract as needed. (Some "slack" overallocation is performed to reduce buffer size changes.) Memory is obtained from malloc(), but callers must use the librock_VSB routines to manage the VSB. The allocated buffer size can be up to INT_MAX. Callers can access the structure members "buf" (pointer) and "inbuf" (count of bytes) but should never change them. Use librock_VSBallocXA() to create a librock_VSB structure which describes a buffer already externally allocated. It will be "WriteDisabled." Use librock_VSBalloccpy() to create a librock_VSB structure and copy in data initially. Use librock_VSBrealloc() to resize a buffer. The VSB.inbuf member is not changed. Returns 0 if memory realloc fails. Typical use is to create a workspace for data, especially non-string data. librock_VSB_s is useful as a primitive in the implementation of other basic data structures, such as lists of pointers, or structures. */ #ifdef librock_TYPICAL_USE_VSBalloc #include struct librock_VSB_s *v = librock_VSBalloc(); librock_VSBAppend(v,"is ",3); librock_VSBInsert(v,0,"this ",5); printf("%s\n",librock_VSBsz(v)); librock_VSBfree(v); #endif /* For C++ uses, see the librock_CVsb wrapper class. See also the librock_VSIL_s routines. */ /* malloc() // #ifndef librock_fmalloc realloc() // #ifndef librock_fmalloc free() // #ifndef librock_fmalloc memcpy() // #ifndef librock_memcpy memmove() // #ifndef librock_memcpy memset() // #ifndef librock_memcpy // Uses macros for memory operations librock_fmalloc // librock_ffree librock_frealloc librock_memcpy librock_memmove // and librock_memset. If those macros are not defined at // compile time, they are set to use malloc(), realloc(), // free(), memcpy(), memmove(), and memset() */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ #endif /* MANUAL */ /*-------------------------------------------*/ struct librock_VSB_s librock_PTR *v; v = (struct librock_VSB_s librock_PTR *) librock_fmalloc(0,sizeof(*v)); if (!v) { return v; } librock_memset(v,0,sizeof(*v)); /* Initial pointer is NULL */ v->inbuf = 0; if (!librock_VSBrealloc(v,librock_VSBchunk)) { librock_ffree(v); /* Request denied. */ return 0; } return v; } /* VSBalloc */ /**************************************************************/ #endif /* NOIMPL */ #ifndef librock_NOIMPL_VSBallocXA /**************************************************************/ struct librock_VSB_s librock_PTR *librock_body_VSBallocXA(const void librock_PTR *p,int len) { /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ struct librock_VSB_s librock_PTR *v; v = (struct librock_VSB_s librock_PTR *) librock_fmalloc(0,sizeof(*v)); if (!v) { return v; } *((char librock_PTR * librock_PTR *) &(v->buf)) = (char librock_PTR *) p; v->sizebuf = -1; /* Not allocated */ v->inbuf = len; return v; } /* VSBallocXA */ /**************************************************************/ #endif /* NOIMPL */ #ifndef librock_NOIMPL_VSBalloccpy /**************************************************************/ struct librock_VSB_s librock_PTR *librock_body_VSBalloccpy(const void librock_PTR *ptr,int len) { /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ struct librock_VSB_s librock_PTR *v; v = (struct librock_VSB_s librock_PTR *) librock_fmalloc(0,sizeof(*v)); if (!v) { return v; } librock_memset(v,0,sizeof(*v)); /* Initial pointer is NULL */ if (!librock_VSBrealloc(v,len)) { librock_VSBfree(v); return 0; } librock_memcpy(v->buf,ptr,len); v->inbuf = len; return v; } /* VSBalloccpy */ /**************************************************************/ #endif /* NOIMPL */ #ifndef librock_NOIMPL_VSBfree /**************************************************************/ void librock_body_VSBfree(struct librock_VSB_s librock_PTR *v) { /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ void librock_PTR *buf = 0; if (!v) { assert(0); /* 9/27/96 */ return; } if (v->sizebuf >= 0) { /* Allocated */ buf = v->buf; } /* Free in order 6-3-96*/ librock_ffree(v); if (buf) { librock_ffree(buf); } } /* VSBfree */ /**************************************************************/ #endif /* NOIMPL */ #ifndef librock_NOIMPL_VSBsz /**************************************************************/ char librock_PTR *librock_body_VSBsz(struct librock_VSB_s librock_PTR *v) { /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ if (!v) { /* 5-27-96 */ assert(0&&"librock_VSBAppend first arg 0"); return 0; } if (v->sizebuf <= 0) { assert(0&&"librock_VSBAppend read only"); /* Read only */ return 0; } if (!v->buf) { assert(0); return 0; } if (v->inbuf + 1 <= v->sizebuf) { /* There is room:the fast version */ v->buf[v->inbuf] = '\0'; } else { if (librock_VSBAppend(v,"",1) < 1) return 0; v->inbuf -= 1; } return v->buf; } /**************************************************************/ #endif /* NOIMPL */ #ifndef librock_NOIMPL_VSBGrow /**************************************************************/ int librock_body_VSBGrow(struct librock_VSB_s librock_PTR *v,int appsize) { /* returns error code. */ /* Used internally */ /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ if (!v->buf) { v->inbuf = 0; } if ((long) v->inbuf > INT_MAX - appsize - librock_VSBchunk ) { /* test added 10/14/96, 10/15/97 INT_MAX */ return -1; /* Can't do it */ } if (!librock_VSBrealloc(v,v->inbuf + appsize + librock_VSBchunk)) { return -1; /* error */ } return 0; } /* VSBGrow */ /**************************************************************/ #endif /* NOIMPL */ #ifndef librock_NOIMPL_VSBWriteDisable /**************************************************************/ void librock_body_VSBWriteDisable(struct librock_VSB_s librock_PTR *v) { /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_VSBWriteDisable /* librock_VSBWriteDisable - Mark a librock_VSB as read-only librock_VSBWriteEnable - Ensure a librock_VSB is writable librock_VSBSetXA - Use an externally allocated buffer */ /**/ #include void librock_VSBWriteDisable( struct librock_VSB_s *v ); void * librock_VSBWriteEnable( struct librock_VSB_s *v ); void librock_VSBSetXA( struct librock_VSB_s *v, const void *p,int len ); /* librock_VSBWriteDisable marks a librock_VSB as read-only. Future librock_VSB calls which attempt to modify the buffer will fail. librock_VSBWriteEnable reallocates the librock_VSB buffer so that librock_VSB calls can modify the buffer contents. Returns non-zero if successful. Use librock_VSBSetXA() to replace the buffer with one that is already externally allocated. It will be "WriteDisabled." */ #ifdef librock_TYPICAL_USE_VSBSetXA #include struct librock_VSB_s *v = librock_VSBalloc(); librock_VSBSetXA(v,"External Allocation.",10); /* Error to call VSBAppend, VSBInsert, or even VSBsz when buffer is WriteDisabled. librock_VSBAppend(v,"is ",3); librock_VSBInsert(v,0,"this ",5); printf("%s\n",librock_VSBsz(v)); */ printf("%.*s\n",v->inbuf,v->buf); librock_VSBfree(v); #endif /* librock_assert() librock_VSBrealloc() free() // #ifndef librock_fmalloc memcpy() // #ifndef librock_memcpy //librock_VSBSetXA uses macros for memory operations //librock_memcpy and librock_ffree. If not defined //at compile time, they are set to memcpy() and free() */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ #endif /* MANUAL */ /*-------------------------------------------*/ if (!v) { /* 5-27-96 */ librock_assert(0&&"librock_VSBSetLength NULL first argument"); return; } /* Make read only */ if (v->sizebuf <= 0) { return; /* Already read only */ } v->sizebuf = 0; /* Read only, but allocated */ return; } /* librock_VSBWriteDisable */ /**************************************************************/ #endif /* NOIMPL */ #ifndef librock_NOIMPL_VSBWriteEnable /**************************************************************/ void librock_PTR *librock_body_VSBWriteEnable(struct librock_VSB_s librock_PTR *v) { /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ const char librock_PTR *src; int size; if (!v) { /* 5-27-96 */ librock_assert(0&&"librock_VSBSetLength NULL first argument"); return 0; } if (v->sizebuf > 0) { return v->buf; /* Already writable */ } /* Here to create a writable buffer from a read only one */ if (v->sizebuf == 0) { /* Read Only. Allocated by us */ size = v->inbuf; if (size <= 0) { size = 1; } librock_VSBrealloc(v,size); /* If allocation fails, will not change sizebuf, which means v will still remain read only */ if (v->sizebuf > 0) { return v->buf; } return 0; } /* Here if we did not allocate v->buf. We allocate a copy */ size = v->inbuf; if (size <= 0) { size = librock_VSBchunk; } src = v->buf; *((char librock_PTR * librock_PTR *) &(v->buf)) = 0; if (!librock_VSBrealloc(v,size)) { return 0; /* Failed! */ } if (v->inbuf > 0) { librock_memcpy(v->buf,src,v->inbuf); } return v->buf; } /* librock_VSBWriteEnable */ /**************************************************************/ #endif /* NOIMPL */ #ifndef librock_NOIMPL_librock_VSBSetXA /**************************************************************/ void librock_body_VSBSetXA(struct librock_VSB_s librock_PTR *v,const void librock_PTR *p,int len) { /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ if (!v) { assert(0&&"VSBsetXA first argument NULL"); return; } if (v->sizebuf >= 0) { librock_ffree(v->buf); } *((char librock_PTR * librock_PTR *) &(v->buf)) = (char librock_PTR *) p; v->sizebuf = -1; /* Not allocated */ v->inbuf = len; return; } /* VSBsetXA */ /**************************************************************/ #endif /* NOIMPL */ #ifndef librock_NOIMPL_VSBInsert /**************************************************************/ int librock_body_VSBInsert(struct librock_VSB_s librock_PTR *v,int loc,const char librock_PTR *buf,int len) { /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_VSBInsert /* librock_VSBInsert - make room and copy a block into a librock_VSB buffer librock_VSBAppend - append a block onto the end of a librock_VSB buffer */ /**/ #include int librock_VSBInsert( struct librock_VSB_s *v, int loc, const char librock_PTR *buf,int len ); int librock_VSBAppend( struct librock_VSB_s *v, const char librock_PTR *buf,int len ); /* Moves existing data, reallocating the buffer if necessary, and copies in data. Returns number of characters added to the buffer, or 0 or -1 if could not. The librock_VSB must not be WriteDisable'd. Since a realloc is often required, any externally held pointers into the buffer are stale after these calls. This is a very common programmer error. If there is only one externally-held pointer, see librock_VSBDeleteAtPtr and librock_VSBInsertAtPtr. Typical use is */ #ifdef librock_TYPICAL_USE_VSBAppend struct librock_VSB_s *v = librock_VSBalloc(); librock_VSBAppend(v,"is ",3); librock_VSBInsert(v,0,"this ",5); printf("%s\n",librock_VSBsz(v)); librock_VSBfree(v); #endif /* librock_assert() memcpy() // #ifndef librock_memcpy memmove() // #ifndef librock_memcpy //Uses macros for memory operations librock_memmove and librock_memcpy. //If not defined at compile time, set to memmove() and memcpy() */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ #endif /* MANUAL */ /*-------------------------------------------*/ if (!v) { /* 5-27-96 */ librock_assert(0&&"librock_VSBInsert first arg 0"); return -1; /* 7-10-97, was 0 */ } if ((loc < 0)||(loc > v->inbuf)) { /* check added 3/16/96 */ librock_assert(0&&"VSBInsert: bad argument loc. Did you use a stale pointer?"); return -1; } if (len < 0) { /* check added 3/16/96 */ librock_assert(0&&"VSBInsert: bad argument len"); /* Check argument setup! */ return -1; } if (v->sizebuf <= 0) { librock_assert(0&&"VSBInsert: VSB is read only"); /* Read only */ return -1; } if (v->inbuf + len > v->sizebuf) { if (librock_VSBGrow(v,len)) { return 0; /* Won't fit */ } } if (v->inbuf > loc) { librock_memmove(v->buf+loc+len,v->buf+loc,v->inbuf - loc); } if (len > 0) { librock_memcpy(v->buf+loc,buf,len); } v->inbuf += len; return len; } /* VSBInsert */ /**************************************************************/ #endif /* NOIMPL */ #ifndef librock_NOIMPL_VSBAppend /**************************************************************/ int librock_body_VSBAppend(struct librock_VSB_s librock_PTR *v,const char librock_PTR *buf,int len) { /* returns number of characters inserted */ /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ if (!v) { /* 5-27-96 */ assert(0&&"librock_VSBAppend first arg 0"); return 0; } if (v->sizebuf <= 0) { assert(0&&"librock_VSBAppend read only"); /* Read only */ return -1; } if (!buf) { assert(0); return 0; } if (len < 0) { assert(0&&"librock_VSBAppend len < 0"); /* Check argument setup! */ return -1; } if (v->inbuf + len <= v->sizebuf) { /* There is room:the fast version */ librock_memcpy(v->buf+v->inbuf,buf,len); v->inbuf += len; return len; } else { return librock_VSBInsert(v,v->inbuf,buf,len); } } /* VSBAppend */ #endif /* NOIMPL */ #ifndef librock_NOIMPL_VSBShrink /**************************************************************/ void librock_body_VSBShrink(struct librock_VSB_s librock_PTR *v) { /* Called after inbuf was reduced. This frees up memory */ /* Used internally */ /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ int size; if (v->sizebuf > v->inbuf + librock_VSBchunk*4) { /* Should shrink it */ size = v->inbuf + librock_VSBchunk; if (size <= librock_VSBchunk) { size = librock_VSBchunk; } librock_VSBrealloc(v,size); } } /* _VSBShrink */ /**************************************************************/ #endif /* NOIMPL */ #ifndef librock_NOIMPL_VSBDelete /**************************************************************/ void librock_body_VSBDelete(struct librock_VSB_s librock_PTR *v,int loc,int len) { /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_VSBDelete /* librock_VSBDelete - remove a section from a librock_VSB buffer librock_VSBClear - empty a librock_VSB buffer librock_VSBSetLength - sets the count of bytes in the librock_VSB buffer */ /**/ #include void librock_VSBDelete( struct librock_VSB_s *v, int loc, /* index into buffer */ int len ); void librock_VSBClear( struct librock_VSB_s *v ); void librock_VSBSetLength( struct librock_VSB_s *v, int appsize ); /* librock_VSBDelete moves the remainder of a buffer down to the specified location and shortens the buffer. librock_VSBClear sets the count of bytes in the buffer to 0. librock_VSBSetLength sets the count of bytes in the buffer to appsize. If lengthened, the contents of the additional section is undefined. The librock_VSB must not be WriteDisable'd. Since a realloc can be performed (to give back excess space), any externally held pointers into the buffer are stale after these calls. This is a very common programmer error. Typical use is */ #ifdef librock_TYPICAL_USE_VSBDelete struct librock_VSB_s *v = librock_VSBalloc(); librock_VSBAppend(v,"AAAThis is ",11); librock_VSBDelete(v,0,3); printf("%s\n",librock_VSBsz(v)); librock_VSBfree(v); #endif /* librock_VSBrealloc() librock_assert() memmove() // #ifndef librock_memcpy //Uses macros for memory operations librock_memmove. //If not defined at compile time, set to memmove(). */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ #endif /* MANUAL */ /*-------------------------------------------*/ if (!v) { /* 5-27-96 */ librock_assert(0); return; } if (loc < 0) { /* 7-10-97 */ librock_assert(0); return; } if (len < 0) { /* 7-10-97 */ librock_assert(0); return; } if (v->sizebuf <= 0) { librock_assert(0); /* Read only */ return; } if (v->inbuf > loc + len) { librock_memmove(v->buf+loc,v->buf+loc+len,v->inbuf-(loc+len)); v->inbuf -= len; } else if ((v->inbuf > loc)&&(loc >= 0)) { v->inbuf = loc; } librock_VSBShrink(v); } /* VSBDelete */ /**************************************************************/ #endif /* NOIMPL */ #ifndef librock_NOIMPL_VSBClear /**************************************************************/ void librock_body_VSBClear(struct librock_VSB_s librock_PTR *v) { /* If sizebuf is large, this will also deallocate and reallocate so that memory is defragmented */ /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ if (!v) { /* 5-27-96 */ librock_assert(0); return; } if (v->sizebuf <= 0) { librock_assert(0); /* Read only */ return; } v->inbuf = 0; librock_VSBShrink(v); } /* VSBClear */ /**************************************************************/ #endif /* NOIMPL */ #ifndef librock_NOIMPL_VSBSetLength /**************************************************************/ void librock_body_VSBSetLength(struct librock_VSB_s librock_PTR *v,int appsize) { /*Copyright 1998-2000, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ if (!v) { /* 5-27-96 */ librock_assert(0&&"librock_VSBSetLength NULL first argument"); return; } if (v->sizebuf <= 0) { librock_assert(0&&"librock_VSBSetLength on read only buffer"); /* Read only */ return; } /* NOTE: v->sizebuf may be irrelevant when this is called from VSBSetReadOnly. */ if (!v->buf) { v->inbuf = 0; } /* Always REALLOC: gives some chance at best fit memory, */ librock_VSBrealloc(v,appsize ? appsize : librock_VSBchunk); if (v->inbuf > appsize) { v->inbuf = appsize; } return; } /* VSBSetLength */ /**************************************************************/ #endif /* NOIMPL */ /* $Log: vsb.c,v $ Revision 1.5 2002/08/02 03:41:16 forrest@mibsoftware.com rights=#1 Updated TYPICAL_USE section. Added NOTLIBROCK section. Moved CVS log to end. Changed LIDESC MD5 to HC. Revision 1.4 2002/04/09 03:33:28 forrest@mibsoftware.com rights=#1 Updated LICENSE in manual. Revision 1.3 2002/02/06 14:09:36 forrest@mibsoftware.com rights=#1 Standardized chg log Revision 1.2 2002/01/30 16:07:55 forrest@mibsoftware.com rights=#1 Renamed some .h files Revision 1.1 2002/01/29 14:24:38 forrest@mibsoftware.com rights=#1 initial rights#1 Copyright (c) Forrest J Cavalier III d-b-a Mib Software rights#1 License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */