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 /*****************************************************************************/
51 /*****************************************************************************/
53 /*****************************************************************************/
57 typedef struct StrBuf StrBuf;
59 unsigned Allocated; /* Size of allocated memory */
60 unsigned Len; /* Length of the string */
61 unsigned Index; /* Used for reading (Get and friends) */
62 char* Buf; /* Pointer to buffer */
65 /* An empty string buf */
66 extern const StrBuf EmptyStrBuf;
68 /* Initializer for static string bufs */
69 #define STATIC_STRBUF_INITIALIZER { 0, 0, 0, 0 }
71 /* Initializer for auto string bufs */
72 #define AUTO_STRBUF_INITIALIZER EmptyStrBuf
76 /*****************************************************************************/
78 /*****************************************************************************/
82 StrBuf* InitStrBuf (StrBuf* B);
83 /* Initialize a string buffer */
85 void DoneStrBuf (StrBuf* B);
86 /* Free the data of a string buffer (but not the struct itself) */
88 StrBuf* NewStrBuf (void);
89 /* Allocate, initialize and return a new StrBuf */
91 void FreeStrBuf (StrBuf* B);
92 /* Free a string buffer */
94 void SB_Realloc (StrBuf* B, unsigned NewSize);
95 /* Reallocate the string buffer space, make sure at least NewSize bytes are
99 #if defined(HAVE_INLINE)
100 INLINE unsigned SB_GetLen (const StrBuf* B)
101 /* Return the length of the buffer contents */
106 # define SB_GetLen(B) (B)->Len
109 #if defined(HAVE_INLINE)
110 INLINE unsigned SB_GetIndex (const StrBuf* B)
111 /* Return the user index of the string buffer */
116 # define SB_GetIndex(B) (B)->Index
119 #if defined(HAVE_INLINE)
120 INLINE const char* SB_GetConstBuf (const StrBuf* B)
121 /* Return a buffer pointer */
126 # define SB_GetConstBuf(B) (B)->Buf
129 #if defined(HAVE_INLINE)
130 INLINE char* SB_GetBuf (StrBuf* B)
131 /* Return a buffer pointer */
136 # define SB_GetBuf(B) (B)->Buf
139 #if defined(HAVE_INLINE)
140 INLINE char SB_At (const StrBuf* B, unsigned Index)
141 /* Get a character from the buffer */
143 PRECONDITION (Index < B->Len);
144 return B->Buf[Index];
147 char SB_At (const StrBuf* B, unsigned Index);
148 /* Get a character from the buffer */
151 #if defined(HAVE_INLINE)
152 INLINE char SB_AtUnchecked (const StrBuf* B, unsigned Index)
153 /* Get a character from the buffer */
155 return B->Buf[Index];
158 # define SB_AtUnchecked(B, Index) ((B)->Buf[Index])
161 #if defined(HAVE_INLINE)
162 INLINE int SB_IsEmpty (const StrBuf* B)
163 /* Return true if the string buffer is empty */
165 return (B->Len == 0);
168 # define SB_IsEmpty(B) ((B)->Len == 0)
171 #if defined(HAVE_INLINE)
172 INLINE int SB_NotEmpty (const StrBuf* B)
173 /* Return true if the string buffer is not empty */
178 # define SB_NotEmpty(B) ((B)->Len > 0)
181 #if defined(HAVE_INLINE)
182 INLINE void SB_Clear (StrBuf* B)
183 /* Clear the string buffer (make it empty) */
185 B->Len = B->Index = 0;
188 # define SB_Clear(B) ((B)->Len = (B)->Index = 0)
191 #if defined(HAVE_INLINE)
192 INLINE void SB_Reset (StrBuf* B)
193 /* Reset the string buffer index to zero */
198 # define SB_Reset(B) ((B)->Index = 0)
201 #if defined(HAVE_INLINE)
202 INLINE char SB_Get (StrBuf* B)
203 /* Return the next character from the string incrementing Index. Returns NUL
204 * if the end of the string is reached.
207 return (B->Index < B->Len)? B->Buf[B->Index++] : '\0';
210 # define SB_Get(B) (((B)->Index < (B)->Len)? (B)->Buf[(B)->Index++] : '\0')
213 #if defined(HAVE_INLINE)
214 INLINE char SB_Peek (const StrBuf* B)
215 /* Look at the next character from the string without incrementing Index.
216 * Returns NUL if the end of the string is reached.
219 return (B->Index < B->Len)? B->Buf[B->Index] : '\0';
222 # define SB_Peek(B) (((B)->Index < (B)->Len)? (B)->Buf[(B)->Index] : '\0')
225 #if defined(HAVE_INLINE)
226 INLINE char SB_LookAt (const StrBuf* B, unsigned Index)
227 /* Look at a specific character from the string. Returns NUL if the given
228 * index is greater than the size of the string.
231 return (Index < B->Len)? B->Buf[Index] : '\0';
234 # define SB_LookAt(B,Index) (((Index) < (B)->Len)? (B)->Buf[(Index)] : '\0')
237 #if defined(HAVE_INLINE)
238 INLINE char SB_LookAtLast (const StrBuf* B)
239 /* Look at the last character from the string. Returns NUL if the string buffer
243 return (B->Len > 0)? B->Buf[B->Len-1] : '\0';
246 # define SB_LookAtLast(B) (((B)->Len > 0)? (B)->Buf[(B)->Len-1] : '\0')
249 #if defined(HAVE_INLINE)
250 INLINE void SB_Skip (StrBuf* B)
251 /* Skip the next character in the string buffer if this is possible. */
253 if (B->Index < B->Len) {
258 # define SB_Skip(B) do { if ((B)->Index < (B)->Len) ++(B)->Index; } while (0)
261 void SB_Drop (StrBuf* B, unsigned Count);
262 /* Drop characters from the end of the string. */
264 void SB_Terminate (StrBuf* B);
265 /* Zero terminate the given string buffer. NOTE: The terminating zero is not
266 * accounted for in B->Len, if you want that, you have to use AppendChar!
269 void SB_CopyBuf (StrBuf* Target, const char* Buf, unsigned Size);
270 /* Copy Buf to Target, discarding the old contents of Target */
272 #if defined(HAVE_INLINE)
273 INLINE void SB_CopyStr (StrBuf* Target, const char* S)
274 /* Copy S to Target, discarding the old contents of Target */
276 SB_CopyBuf (Target, S, strlen (S));
279 void SB_CopyStr (StrBuf* Target, const char* S);
280 /* Copy S to Target, discarding the old contents of Target */
283 #if defined(HAVE_INLINE)
284 INLINE void SB_Copy (StrBuf* Target, const StrBuf* Source)
285 /* Copy Source to Target, discarding the old contents of Target */
287 SB_CopyBuf (Target, Source->Buf, Source->Len);
288 Target->Index = Source->Index;
291 void SB_Copy (StrBuf* Target, const StrBuf* Source);
292 /* Copy Source to Target, discarding the old contents of Target */
295 void SB_AppendChar (StrBuf* B, int C);
296 /* Append a character to a string buffer */
298 void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size);
299 /* Append a character buffer to the end of the string buffer */
301 #if defined(HAVE_INLINE)
302 INLINE void SB_AppendStr (StrBuf* B, const char* S)
303 /* Append a string to the end of the string buffer */
305 SB_AppendBuf (B, S, strlen (S));
308 void SB_AppendStr (StrBuf* B, const char* S);
309 /* Append a string to the end of the string buffer */
312 #if defined(HAVE_INLINE)
313 INLINE void SB_Append (StrBuf* Target, const StrBuf* Source)
314 /* Append the contents of Source to Target */
316 SB_AppendBuf (Target, Source->Buf, Source->Len);
319 void SB_Append (StrBuf* Target, const StrBuf* Source);
320 /* Append the contents of Source to Target */
323 #if defined(HAVE_INLINE)
324 INLINE void SB_Cut (StrBuf* B, unsigned Len)
325 /* Cut the contents of B at the given length. If the current length of the
326 * buffer is smaller than Len, nothing will happen.
334 void SB_Cut (StrBuf* B, unsigned Len);
335 /* Cut the contents of B at the given length. If the current length of the
336 * buffer is smaller than Len, nothing will happen.
340 void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len);
341 /* Copy a slice from Source into Target. The current contents of Target are
342 * destroyed. If Start is greater than the length of Source, or if Len
343 * characters aren't available, the result will be a buffer with less than Len
347 void SB_Move (StrBuf* Target, StrBuf* Source);
348 /* Move the complete contents of Source to target. This will delete the old
349 * contents of Target, and Source will be empty after the call.
352 void SB_ToLower (StrBuf* S);
353 /* Convert all characters in S to lower case */
355 void SB_ToUpper (StrBuf* S);
356 /* Convert all characters in S to upper case */
358 int SB_Compare (const StrBuf* S1, const StrBuf* S2);
359 /* Do a lexical compare of S1 and S2. See strcmp for result codes. */
361 void SB_VPrintf (StrBuf* S, const char* Format, va_list ap);
362 /* printf function with S as target. The function is safe, which means that
363 * the current contents of S are discarded, and are allocated again with
364 * a matching size for the output. The function will call FAIL when problems
365 * are detected (anything that let xsnprintf return -1).
368 void SB_Printf (StrBuf* S, const char* Format, ...);
369 /* vprintf function with S as target. The function is safe, which means that
370 * the current contents of S are discarded, and are allocated again with
371 * a matching size for the output. The function will call FAIL when problems
372 * are detected (anything that let xsnprintf return -1).
377 /* End of strbuf.h */