Created
September 8, 2013 01:32
-
-
Save brimston3/6481123 to your computer and use it in GitHub Desktop.
Wrote this for amusement; it is not fully tested. A friend wanted a way to avoid freeing return results from a function call that he called repeatedly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* strbuf.c | |
* Andrew Domaszek | |
* CodeLibrary/dynamic_string | |
* | |
* Copyright September 6, 2013, Andrew Domaszek | |
* All rights reserved. | |
* | |
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 <organization> 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 <COPYRIGHT HOLDER> 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. | |
*/ | |
// This define will make operations slightly faster at the expense of leaving the | |
// data (that will be shortly overwritten) unmodified when allocated. | |
//#define STRBUF_NOINITIALIZE | |
#include <stdlib.h> | |
typedef struct { | |
char * string; | |
size_t length; | |
size_t cap; | |
size_t endpos; | |
} strbuf; | |
strbuf * strbufAlloc(size_t startsize); | |
void strbufFree(strbuf * sb); | |
strbuf * strbufGrow(strbuf * sb, size_t growby); | |
strbuf * strbufCat(strbuf * sb,char * str); | |
strbuf * strbufNCat(strbuf * sb,char * str, size_t copylen); | |
strbuf * strbufClear(strbuf * sb) | |
strbuf * strbufAlloc(size_t startsize) | |
{ | |
strbuf * sb = malloc(sizeof(strbuf)); | |
if (!sb) return null; | |
memset(sb,0,sizeof(strbuf)); | |
if (startsize > 0) | |
{ | |
sb->string = malloc(startsize+1); | |
sb->length = startsize; | |
sb->cap = startsize; | |
} | |
return sb; | |
} | |
void strbufFree(strbuf * sb) | |
{ | |
if (!sb) return; | |
if (sb->string) free(sb->string); | |
free(sb); | |
} | |
strbuf * strbufGrow(strbuf * sb, size_t growby) | |
{ | |
if (!sb) return null; | |
size_t newlen = sb->length + growby; | |
size_t newcap = sb->cap; | |
if (newlen > sb->cap) | |
{ | |
if (newlen > sb->cap * 2) | |
{ | |
newcap = newlen; | |
} | |
else | |
{ | |
newcap = sb->cap * 2; | |
} | |
char * strdata = realloc(sb->string,newcap); | |
if (!strdata) | |
return 0; | |
sb->string = strdata; | |
// we got good data back, reassign it to our pointer. | |
// Then initialize new segment to null. | |
#ifndef STRBUF_NOINITIALIZE | |
memset(sb->string+sb->cap,0,newcap - sb->cap); | |
#endif | |
sb->cap = newcap; | |
} | |
sb->length = newlen; | |
return sb; | |
} | |
strbuf * strbufCat(strbuf * sb,char * str) | |
{ | |
if (!sb || !str) | |
return null; | |
size_t copybytes = strlen(str); | |
return strbufNCat(sb,str,copybytes); | |
} | |
// Do not do clever things and try to copy sb->string back into the strbuf | |
// That will probably break in horrific ways | |
strbuf * strbufNCat(strbuf * sb,char * str, size_t copylen) | |
{ | |
if (!sb || !str) | |
return null; | |
// add an extra byte for the null byte. | |
size_t needlen = sb->endpos+copylen+1; | |
if (needlen > sb->length) | |
{ | |
sb = strbufGrow(sb,needlen-sb->length); | |
if (!sb) | |
return null; | |
} | |
memcpy(sb->string+sb->endpos, str, copylen); | |
// null terminate data for safety. | |
sb->string[sb->endpos+copylen] = 0; | |
sb->endpos += copylen; | |
return sb; | |
} | |
strbuf * strbufClear(strbuf * sb) | |
{ | |
if (!sb) return null; | |
#ifndef STRBUF_NOINITIALIZE | |
if (sb->string) | |
memset(sb->string,0,sb->cap); | |
#endif | |
sb->len = 0; | |
sb->endpos = 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment