robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
ad22d0ace9
master
1/* strbuf - String buffer routines 2* 3* Copyright (c) 2010-2012 Mark Pulford <mark@kyne.com.au> 4* 5* Permission is hereby granted, free of charge, to any person obtaining 6* a copy of this software and associated documentation files (the 7* "Software"), to deal in the Software without restriction, including 8* without limitation the rights to use, copy, modify, merge, publish, 9* distribute, sublicense, and/or sell copies of the Software, and to 10* permit persons to whom the Software is furnished to do so, subject to 11* the following conditions: 12* 13* The above copyright notice and this permission notice shall be 14* included in all copies or substantial portions of the Software. 15* 16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23*/ 24 25#include <stdio.h> 26#include <stdlib.h> 27#include <stdarg.h> 28#include <string.h> 29#include <stdint.h> 30 31#include "strbuf.h" 32 33static void die (const char * fmt , ...) 34{ 35va_list arg ; 36 37va_start (arg ,fmt ); 38vfprintf (stderr ,fmt ,arg ); 39va_end (arg ); 40fprintf (stderr ,"\n" ); 41 42abort (); 43} 44 45void strbuf_init (strbuf_t * s ,size_t len ) 46{ 47size_t size ; 48 49if (!len ) 50size = STRBUF_DEFAULT_SIZE ; 51else 52size = len + 1 ;/* \0 terminator */ 53if (size < len ) 54die ("Overflow, len: %zu" ,len ); 55s -> buf = NULL ; 56s -> size = size ; 57s -> length = 0 ; 58s -> dynamic = 0 ; 59s -> reallocs = 0 ; 60s -> debug = 0 ; 61 62s -> buf = (char * )malloc (size ); 63if (!s -> buf ) 64die ("Out of memory" ); 65 66strbuf_ensure_null (s ); 67} 68 69strbuf_t * strbuf_new (size_t len ) 70{ 71strbuf_t * s ; 72 73s = (strbuf_t * )malloc (sizeof (strbuf_t )); 74if (!s ) 75die ("Out of memory" ); 76 77strbuf_init (s ,len ); 78 79/* Dynamic strbuf allocation / deallocation */ 80s -> dynamic = 1 ; 81 82return s ; 83} 84 85static inline void debug_stats (strbuf_t * s ) 86{ 87if (s -> debug ) { 88fprintf (stderr ,"strbuf(%lx) reallocs: %d, length: %zd, size: %zd\n" , 89 (long )s ,s -> reallocs ,s -> length ,s -> size ); 90 } 91} 92 93/* If strbuf_t has not been dynamically allocated, strbuf_free() can 94* be called any number of times strbuf_init() */ 95void strbuf_free (strbuf_t * s ) 96{ 97debug_stats (s ); 98 99if (s -> buf ) { 100free (s -> buf ); 101s -> buf = NULL ; 102 } 103if (s -> dynamic ) 104free (s ); 105} 106 107char * strbuf_free_to_string (strbuf_t * s ,size_t * len ) 108{ 109char * buf ; 110 111debug_stats (s ); 112 113strbuf_ensure_null (s ); 114 115buf = s -> buf ; 116if (len ) 117* len = s -> length ; 118 119if (s -> dynamic ) 120free (s ); 121 122return buf ; 123} 124 125static size_t calculate_new_size (strbuf_t * s ,size_t len ) 126{ 127size_t reqsize ,newsize ; 128 129if (len <=0 ) 130die ("BUG: Invalid strbuf length requested" ); 131 132/* Ensure there is room for optional NULL termination */ 133reqsize = len + 1 ; 134if (reqsize < len ) 135die ("Overflow, len: %zu" ,len ); 136 137/* If the user has requested to shrink the buffer, do it exactly */ 138if (s -> size > reqsize ) 139return reqsize ; 140 141newsize = s -> size ; 142if (reqsize >=SIZE_MAX /2 ) { 143newsize = reqsize ; 144 }else { 145/* Exponential sizing */ 146while (newsize < reqsize ) 147newsize *=2 ; 148 } 149 150if (newsize < reqsize ) 151die ("BUG: strbuf length would overflow, len: %zu" ,len ); 152 153 154return newsize ; 155} 156 157 158/* Ensure strbuf can handle a string length bytes long (ignoring NULL 159* optional termination). */ 160void strbuf_resize (strbuf_t * s ,size_t len ) 161{ 162size_t newsize ; 163 164newsize = calculate_new_size (s ,len ); 165 166if (s -> debug > 1 ) { 167fprintf (stderr ,"strbuf(%lx) resize: %zd => %zd\n" , 168 (long )s ,s -> size ,newsize ); 169 } 170 171s -> size = newsize ; 172s -> buf = (char * )realloc (s -> buf ,s -> size ); 173if (!s -> buf ) 174die ("Out of memory, len: %zu" ,len ); 175s -> reallocs ++ ; 176} 177 178void strbuf_append_string (strbuf_t * s ,const char * str ) 179{ 180int i ; 181size_t space ; 182 183space = strbuf_empty_length (s ); 184 185for (i = 0 ;str [i ];i ++ ) { 186if (space < 1 ) { 187strbuf_resize (s ,s -> length + 1 ); 188space = strbuf_empty_length (s ); 189 } 190 191s -> buf [s -> length ]= str [i ]; 192s -> length ++ ; 193space -- ; 194 } 195} 196 197 198/* vi:ai et sw=4 ts=4: 199*/