1 /*****************************************************************************/
5 /* Variable sized string buffers */
9 /* (C) 2001-2008 Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
48 /*****************************************************************************/
50 /*****************************************************************************/
54 /* An empty string buf */
55 const StrBuf EmptyStrBuf = STATIC_STRBUF_INITIALIZER;
59 /*****************************************************************************/
61 /*****************************************************************************/
65 #if !defined(HAVE_INLINE)
66 StrBuf* SB_Init (StrBuf* B)
67 /* Initialize a string buffer */
76 StrBuf* SB_InitFromString (StrBuf* B, const char* S)
77 /* Initialize a string buffer from a literal string. Beware: The buffer won't
78 * store a copy but a pointer to the actual string.
90 void SB_Done (StrBuf* B)
91 /* Free the data of a string buffer (but not the struct itself) */
100 StrBuf* NewStrBuf (void)
101 /* Allocate, initialize and return a new StrBuf */
103 /* Allocate a new string buffer */
104 StrBuf* B = xmalloc (sizeof (StrBuf));
106 /* Initialize the struct... */
109 /* ...and return it */
115 void FreeStrBuf (StrBuf* B)
116 /* Free a string buffer */
124 void SB_Realloc (StrBuf* B, unsigned NewSize)
125 /* Reallocate the string buffer space, make sure at least NewSize bytes are
129 /* Get the current size, use a minimum of 8 bytes */
130 unsigned NewAllocated = B->Allocated;
131 if (NewAllocated == 0) {
135 /* Round up to the next power of two */
136 while (NewAllocated < NewSize) {
140 /* Reallocate the buffer. Beware: The allocated size may be zero while the
141 * length is not. This means that we have a buffer that wasn't allocated
145 /* Just reallocate the block */
146 B->Buf = xrealloc (B->Buf, NewAllocated);
148 /* Allocate a new block and copy */
149 B->Buf = memcpy (xmalloc (NewAllocated), B->Buf, B->Len);
152 /* Remember the new block size */
153 B->Allocated = NewAllocated;
158 static void SB_CheapRealloc (StrBuf* B, unsigned NewSize)
159 /* Reallocate the string buffer space, make sure at least NewSize bytes are
160 * available. This function won't copy the old buffer contents over to the new
161 * buffer and may be used if the old contents are overwritten later.
164 /* Get the current size, use a minimum of 8 bytes */
165 unsigned NewAllocated = B->Allocated;
166 if (NewAllocated == 0) {
170 /* Round up to the next power of two */
171 while (NewAllocated < NewSize) {
175 /* Free the old buffer if there is one */
180 /* Allocate a fresh block */
181 B->Buf = xmalloc (NewAllocated);
183 /* Remember the new block size */
184 B->Allocated = NewAllocated;
189 #if !defined(HAVE_INLINE)
190 char SB_At (const StrBuf* B, unsigned Index)
191 /* Get a character from the buffer */
193 PRECONDITION (Index < B->Len);
194 return B->Buf[Index];
200 void SB_Drop (StrBuf* B, unsigned Count)
201 /* Drop characters from the end of the string. */
203 PRECONDITION (Count <= B->Len);
205 if (B->Index > B->Len) {
212 void SB_Terminate (StrBuf* B)
213 /* Zero terminate the given string buffer. NOTE: The terminating zero is not
214 * accounted for in B->Len, if you want that, you have to use AppendChar!
217 unsigned NewLen = B->Len + 1;
218 if (NewLen > B->Allocated) {
219 SB_Realloc (B, NewLen);
221 B->Buf[B->Len] = '\0';
226 void SB_CopyBuf (StrBuf* Target, const char* Buf, unsigned Size)
227 /* Copy Buf to Target, discarding the old contents of Target */
230 if (Target->Allocated < Size) {
231 SB_CheapRealloc (Target, Size);
233 memcpy (Target->Buf, Buf, Size);
240 #if !defined(HAVE_INLINE)
241 void SB_CopyStr (StrBuf* Target, const char* S)
242 /* Copy S to Target, discarding the old contents of Target */
244 SB_CopyBuf (Target, S, strlen (S));
250 #if !defined(HAVE_INLINE)
251 void SB_Copy (StrBuf* Target, const StrBuf* Source)
252 /* Copy Source to Target, discarding the old contents of Target */
254 SB_CopyBuf (Target, Source->Buf, Source->Len);
255 Target->Index = Source->Index;
261 void SB_AppendChar (StrBuf* B, int C)
262 /* Append a character to a string buffer */
264 unsigned NewLen = B->Len + 1;
265 if (NewLen > B->Allocated) {
266 SB_Realloc (B, NewLen);
268 B->Buf[B->Len] = (char) C;
274 void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size)
275 /* Append a character buffer to the end of the string buffer */
277 unsigned NewLen = B->Len + Size;
278 if (NewLen > B->Allocated) {
279 SB_Realloc (B, NewLen);
281 memcpy (B->Buf + B->Len, S, Size);
287 #if !defined(HAVE_INLINE)
288 void SB_AppendStr (StrBuf* B, const char* S)
289 /* Append a string to the end of the string buffer */
291 SB_AppendBuf (B, S, strlen (S));
297 #if !defined(HAVE_INLINE)
298 void SB_Append (StrBuf* Target, const StrBuf* Source)
299 /* Append the contents of Source to Target */
301 SB_AppendBuf (Target, Source->Buf, Source->Len);
307 #if !defined(HAVE_INLINE)
308 void SB_Cut (StrBuf* B, unsigned Len)
309 /* Cut the contents of B at the given length. If the current length of the
310 * buffer is smaller than Len, nothing will happen.
321 void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len)
322 /* Copy a slice from Source into Target. The current contents of Target are
323 * destroyed. If Start is greater than the length of Source, or if Len
324 * characters aren't available, the result will be a buffer with less than Len
328 /* Calculate the length of the resulting buffer */
329 if (Start >= Source->Len) {
330 /* Target will be empty */
334 if (Start + Len > Source->Len) {
335 Len = Source->Len - Start;
338 /* Make sure we have enough room in the target string buffer */
339 if (Len > Target->Allocated) {
340 SB_Realloc (Target, Len);
344 memcpy (Target->Buf, Source->Buf + Start, Len);
350 void SB_Move (StrBuf* Target, StrBuf* Source)
351 /* Move the complete contents of Source to target. This will delete the old
352 * contents of Target, and Source will be empty after the call.
355 /* Free the target string */
356 if (Target->Allocated) {
360 /* Move all data from Source to Target */
369 void SB_ToLower (StrBuf* S)
370 /* Convert all characters in S to lower case */
374 for (I = 0; I < S->Len; ++I, ++B) {
383 void SB_ToUpper (StrBuf* S)
384 /* Convert all characters in S to upper case */
388 for (I = 0; I < S->Len; ++I, ++B) {
397 int SB_Compare (const StrBuf* S1, const StrBuf* S2)
398 /* Do a lexical compare of S1 and S2. See strcmp for result codes. */
401 if (S1->Len < S2->Len) {
402 Result = memcmp (S1->Buf, S2->Buf, S1->Len);
404 /* S1 considered lesser because it's shorter */
407 } else if (S1->Len > S2->Len) {
408 Result = memcmp (S1->Buf, S2->Buf, S2->Len);
410 /* S2 considered lesser because it's shorter */
414 Result = memcmp (S1->Buf, S2->Buf, S1->Len);
421 int SB_CompareStr (const StrBuf* S1, const char* S2)
422 /* Do a lexical compare of S1 and S2. See strcmp for result codes. */
425 unsigned S2Len = strlen (S2);
426 if (S1->Len < S2Len) {
427 Result = memcmp (S1->Buf, S2, S1->Len);
429 /* S1 considered lesser because it's shorter */
432 } else if (S1->Len > S2Len) {
433 Result = memcmp (S1->Buf, S2, S2Len);
435 /* S2 considered lesser because it's shorter */
439 Result = memcmp (S1->Buf, S2, S1->Len);
446 void SB_VPrintf (StrBuf* S, const char* Format, va_list ap)
447 /* printf function with S as target. The function is safe, which means that
448 * the current contents of S are discarded, and are allocated again with
449 * a matching size for the output. The function will call FAIL when problems
450 * are detected (anything that let xsnprintf return -1).
456 /* Since we must determine the space needed anyway, we will try with
457 * the currently allocated memory. If the call succeeds, we've saved
458 * an allocation. If not, we have to reallocate and try again.
461 SizeNeeded = xvsnprintf (S->Buf, S->Allocated, Format, tmp);
464 /* Check the result, the xvsnprintf function should not fail */
465 CHECK (SizeNeeded >= 0);
467 /* Check if we must reallocate */
468 if ((unsigned) SizeNeeded >= S->Allocated) {
469 /* Must retry. Use CheapRealloc to avoid copying */
470 SB_CheapRealloc (S, SizeNeeded + 1); /* Account for '\0' */
471 (void) xvsnprintf (S->Buf, S->Allocated, Format, ap);
474 /* Update string buffer variables */
481 void SB_Printf (StrBuf* S, const char* Format, ...)
482 /* vprintf function with S as target. The function is safe, which means that
483 * the current contents of S are discarded, and are allocated again with
484 * a matching size for the output. The function will call FAIL when problems
485 * are detected (anything that let xsnprintf return -1).
489 va_start (ap, Format);
490 SB_VPrintf (S, Format, ap);