/* librock exec/struct/vsbop.c librock_CHISEL _summary Perform operations on a librock_VSB_s */ /* 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 #define librock_assert(a) #define librock_PTR #define librock_CONST const /**************************************************************/ #endif #ifndef librock_ISOLATED /**************************************************************/ #define librock_IMPLEMENT_vsbop #include #include #include #ifndef librock_assert #include #define librock_assert(a) assert(a) #endif /**************************************************************/ #endif #ifdef librock_IMPL_LIDESC #ifndef librock_NOIMPL_LIDESC_vsbop /**************************************************************/ #include /* librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ void *librock_LIDESC_vsbop[] = { "\n" __FILE__ librock_LIDESC_librock "\n", 0 }; /**************************************************************/ #endif #endif #ifndef librock_WRAP /**************************************************************/ #define librock_body_VSBDeleteAtPtr librock_VSBDeleteAtPtr #define librock_body_VSBInsertAtPtr librock_VSBInsertAtPtr #define librock_body_VSBtextrep librock_VSBtextrep /**************************************************************/ #endif #ifndef librock_NOIMPL_VSBDeleteAtPtr /**************************************************************/ char librock_PTR *librock_body_VSBDeleteAtPtr(struct librock_VSB_s librock_PTR *v,const char 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/ */ #ifdef librock_MANUAL_VSBDeleteAtPtr /* librock_VSBDeleteAtPtr - remove section of buffer starting at pointer librock_VSBInsertAtPtr - insert section of buffer starting at pointer */ /**/ #include char *librock_VSBDeleteAtPtr( struct librock_VSB_s *v, char *ptr, int len); char *librock_VSBInsertAtPtr( struct librock_VSB_s *v, char *ptr, const char *src,int len); /* These routines allow removing or inserting characters in a librock_VSB with the location given by a pointer. Since VSBInsert and VSBDelete will cause pointers to become stale, this provides a useful alternative. Returns pointer at same position in the possibly moved buffer. Typical use is */ #ifdef librock_TYPICAL_USE_VSBInsertAtPtr #include struct librock_VSB_s *v = librock_VSBalloc(); char *ptr; librock_VSBAppend(v,"This is a test.",15); ptr = memchr(v->buf,'.',v->inbuf); if (ptr) { ptr = librock_VSBDeleteAtPtr(v,ptr,1); ptr = librock_VSBInsertAtPtr(v,ptr,"!!",2); } printf("%s\n",librock_VSBsz(v)); librock_VSBfree(v); #endif /* librock_assert() librock_CHISEL _usesrc "exec/struct/vsb.c" librock_VSBDelete() librock_VSBInsert() */ /* 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 */ /*-------------------------------------------*/ int off; int preserve = 0; if (!ptr) { librock_assert(ptr); return 0; } if ((ptr < v->buf)|| (ptr >= v->buf+v->inbuf)) { librock_assert(0); /* Out of range */ return 0; } off = ptr - v->buf; if (v->inbuf < v->sizebuf) { /* We preserve a terminator, if it exists */ v->inbuf++; preserve = 1; } librock_VSBDelete(v,off,len); v->inbuf -= preserve; return v->buf + off; } /* VSBDeleteAtPtr */ /**************************************************************/ #endif /* NOIMP */ #ifndef librock_NOIMPL_VSBInsertAtPtr /**************************************************************/ char librock_PTR *librock_body_VSBInsertAtPtr(struct librock_VSB_s librock_PTR *v,const char librock_PTR *ptr, const char librock_PTR *src,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/ */ int off; int preserve = 0; if (!ptr) { librock_assert(ptr); return 0; } if ((ptr < v->buf)|| (ptr > v->buf+v->inbuf)) { librock_assert(0); /* Out of range */ return 0; } if (v->inbuf < v->sizebuf) { /* We preserve a terminator, if it exists */ v->inbuf++; preserve = 1; } off = ptr - v->buf; librock_VSBInsert(v,off,src,len); v->inbuf -= preserve; return v->buf + off; } /* VSBInsertAtPtr */ /**************************************************************/ #endif /* NOIMP */ #ifndef librock_NOIMPL_VSBtextrep /**************************************************************/ void librock_body_VSBtextrep(struct librock_VSB_s *vsb,const char *pszSearch,const char *pszReplace) { /*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_VSBtextrep /* librock_VSBtextrep - replace substrings within a librock_VSB */ /**/ #include void librock_VSBtextrep( struct librock_VSB_s *vsb, const char *pszSearch, const char *pszReplace ); /* Working from the beginning, find substrings in a librock_VSB, replacing them. Typical use is */ #ifdef librock_TYPICAL_USE_VSBtextrep struct librock_VSB_s *v = librock_VSBalloc(); librock_VSBAppend(v,"is ",3); librock_VSBInsert(v,0,"this ",5); librock_VSBtextrep(v,"this","This"); printf("%s\n",librock_VSBsz(v)); librock_VSBfree(v); #endif /* memchr() memcmp() strlen() librock_CHISEL _usesrc "exec/struct/vsb.c" librock_VSBDelete() librock_VSBInsert() */ /* 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 */ /*-------------------------------------------*/ const char *ptr; const char *end; int ind; int cbSearch; int cbReplace; ptr = vsb->buf; end = ptr + vsb->inbuf; ptr = (char *) memchr(ptr,*pszSearch,end-ptr); if (ptr) { cbSearch = strlen(pszSearch); cbReplace = strlen(pszReplace); while(ptr) { if (!memcmp(ptr,pszSearch,cbSearch)) { /* Replace */ ind = ptr - vsb->buf; librock_VSBDelete(vsb,ind,cbSearch); if (cbReplace) { librock_VSBInsert(vsb,ind,pszReplace,cbReplace); ind += cbReplace; } ptr = vsb->buf+ind; end = vsb->buf + vsb->inbuf; } else { ptr++; } ptr = (char *) memchr(ptr,*pszSearch,end-ptr); } } } /* VSBtextrep */ #endif /* NOIMP */ /* $Log: vsbop.c,v $ Revision 1.5 2002/08/02 03:27:32 forrest@mibsoftware.com rights=#1 Allow VSBInsertAtPtr at end of buffer. 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 */