1 /*****************************************************************************/
5 /* Variable sized string buffers */
9 /* (C) 2001-2004 Ullrich von Bassewitz */
10 /* Römerstrasse 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 /*****************************************************************************/
50 /*****************************************************************************/
52 /*****************************************************************************/
56 typedef struct StrBuf StrBuf;
58 unsigned Allocated; /* Size of allocated memory */
59 unsigned Len; /* Length of the string */
60 unsigned Index; /* Used for reading (Get and friends) */
61 char* Buf; /* Pointer to buffer */
64 /* An empty string buf */
65 extern const StrBuf EmptyStrBuf;
67 /* Initializer for static string bufs */
68 #define STATIC_STRBUF_INITIALIZER { 0, 0, 0, 0 }
70 /* Initializer for auto string bufs */
71 #define AUTO_STRBUF_INITIALIZER EmptyStrBuf
75 /*****************************************************************************/
77 /*****************************************************************************/
81 StrBuf* InitStrBuf (StrBuf* B);
82 /* Initialize a string buffer */
84 void DoneStrBuf (StrBuf* B);
85 /* Free the data of a string buffer (but not the struct itself) */
87 StrBuf* NewStrBuf (void);
88 /* Allocate, initialize and return a new StrBuf */
90 void FreeStrBuf (StrBuf* B);
91 /* Free a string buffer */
93 #if defined(HAVE_INLINE)
94 INLINE unsigned SB_GetLen (const StrBuf* B)
95 /* Return the length of the buffer contents */
100 # define SB_GetLen(B) (B)->Len
103 #if defined(HAVE_INLINE)
104 INLINE unsigned SB_GetIndex (const StrBuf* B)
105 /* Return the user index of the string buffer */
110 # define SB_GetIndex(B) (B)->Index
113 #if defined(HAVE_INLINE)
114 INLINE const char* SB_GetConstBuf (const StrBuf* B)
115 /* Return a buffer pointer */
120 # define SB_GetConstBuf(B) (B)->Buf
123 #if defined(HAVE_INLINE)
124 INLINE char* SB_GetBuf (StrBuf* B)
125 /* Return a buffer pointer */
130 # define SB_GetBuf(B) (B)->Buf
133 #if defined(HAVE_INLINE)
134 INLINE char SB_At (const StrBuf* B, unsigned Index)
135 /* Get a character from the buffer */
137 PRECONDITION (Index < B->Len);
138 return B->Buf[Index];
141 char SB_At (const StrBuf* B, unsigned Index);
142 /* Get a character from the buffer */
145 #if defined(HAVE_INLINE)
146 INLINE char SB_AtUnchecked (const StrBuf* B, unsigned Index)
147 /* Get a character from the buffer */
149 return B->Buf[Index];
152 # define SB_AtUnchecked(B, Index) ((B)->Buf[Index])
155 #if defined(HAVE_INLINE)
156 INLINE int SB_IsEmpty (const StrBuf* B)
157 /* Return true if the string buffer is empty */
159 return (B->Len == 0);
162 # define SB_IsEmpty(B) ((B)->Len == 0)
165 #if defined(HAVE_INLINE)
166 INLINE int SB_NotEmpty (const StrBuf* B)
167 /* Return true if the string buffer is not empty */
172 # define SB_NotEmpty(B) ((B)->Len > 0)
175 #if defined(HAVE_INLINE)
176 INLINE void SB_Clear (StrBuf* B)
177 /* Clear the string buffer (make it empty) */
179 B->Len = B->Index = 0;
182 # define SB_Clear(B) ((B)->Len = (B)->Index = 0)
185 #if defined(HAVE_INLINE)
186 INLINE void SB_Reset (StrBuf* B)
187 /* Reset the string buffer index to zero */
192 # define SB_Reset(B) ((B)->Index = 0)
195 #if defined(HAVE_INLINE)
196 INLINE char SB_Get (StrBuf* B)
197 /* Return the next character from the string incrementing Index. Returns NUL
198 * if the end of the string is reached.
201 return (B->Index < B->Len)? B->Buf[B->Index++] : '\0';
204 # define SB_Get(B) (((B)->Index < (B)->Len)? (B)->Buf[(B)->Index++] : '\0')
207 #if defined(HAVE_INLINE)
208 INLINE char SB_Peek (const StrBuf* B)
209 /* Look at the next character from the string without incrementing Index.
210 * Returns NUL if the end of the string is reached.
213 return (B->Index < B->Len)? B->Buf[B->Index] : '\0';
216 # define SB_Peek(B) (((B)->Index < (B)->Len)? (B)->Buf[(B)->Index] : '\0')
219 #if defined(HAVE_INLINE)
220 INLINE char SB_LookAt (const StrBuf* B, unsigned Index)
221 /* Look at a specific character from the string. Returns NUL if the given
222 * index is greater than the size of the string.
225 return (Index < B->Len)? B->Buf[Index] : '\0';
228 # define SB_LookAt(B,Index) (((Index) < (B)->Len)? (B)->Buf[(Index)] : '\0')
231 #if defined(HAVE_INLINE)
232 INLINE char SB_LookAtLast (const StrBuf* B)
233 /* Look at the last character from the string. Returns NUL if the string buffer
237 return (B->Len > 0)? B->Buf[B->Len-1] : '\0';
240 # define SB_LookAtLast(B) (((B)->Len > 0)? (B)->Buf[(B)->Len-1] : '\0')
243 #if defined(HAVE_INLINE)
244 INLINE void SB_Skip (StrBuf* B)
245 /* Skip the next character in the string buffer if this is possible. */
247 if (B->Index < B->Len) {
252 # define SB_Skip(B) do { if ((B)->Index < (B)->Len) ++(B)->Index; } while (0)
255 void SB_Drop (StrBuf* B, unsigned Count);
256 /* Drop characters from the end of the string. */
258 void SB_Terminate (StrBuf* B);
259 /* Zero terminate the given string buffer. NOTE: The terminating zero is not
260 * accounted for in B->Len, if you want that, you have to use AppendChar!
263 void SB_CopyBuf (StrBuf* Target, const char* Buf, unsigned Size);
264 /* Copy Buf to Target, discarding the old contents of Target */
266 #if defined(HAVE_INLINE)
267 INLINE void SB_CopyStr (StrBuf* Target, const char* S)
268 /* Copy S to Target, discarding the old contents of Target */
270 SB_CopyBuf (Target, S, strlen (S));
273 void SB_CopyStr (StrBuf* Target, const char* S);
274 /* Copy S to Target, discarding the old contents of Target */
277 #if defined(HAVE_INLINE)
278 INLINE void SB_Copy (StrBuf* Target, const StrBuf* Source)
279 /* Copy Source to Target, discarding the old contents of Target */
281 SB_CopyBuf (Target, Source->Buf, Source->Len);
282 Target->Index = Source->Index;
285 void SB_Copy (StrBuf* Target, const StrBuf* Source);
286 /* Copy Source to Target, discarding the old contents of Target */
289 void SB_AppendChar (StrBuf* B, int C);
290 /* Append a character to a string buffer */
292 void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size);
293 /* Append a character buffer to the end of the string buffer */
295 #if defined(HAVE_INLINE)
296 INLINE void SB_AppendStr (StrBuf* B, const char* S)
297 /* Append a string to the end of the string buffer */
299 SB_AppendBuf (B, S, strlen (S));
302 void SB_AppendStr (StrBuf* B, const char* S);
303 /* Append a string to the end of the string buffer */
306 #if defined(HAVE_INLINE)
307 INLINE void SB_Append (StrBuf* Target, const StrBuf* Source)
308 /* Append the contents of Source to Target */
310 SB_AppendBuf (Target, Source->Buf, Source->Len);
313 void SB_Append (StrBuf* Target, const StrBuf* Source);
314 /* Append the contents of Source to Target */
317 #if defined(HAVE_INLINE)
318 INLINE void SB_Cut (StrBuf* B, unsigned Len)
319 /* Cut the contents of B at the given length. If the current length of the
320 * buffer is smaller than Len, nothing will happen.
328 void SB_Cut (StrBuf* B, unsigned Len);
329 /* Cut the contents of B at the given length. If the current length of the
330 * buffer is smaller than Len, nothing will happen.
334 void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len);
335 /* Copy a slice from Source into Target. The current contents of Target are
336 * destroyed. If Start is greater than the length of Source, or if Len
337 * characters aren't available, the result will be a buffer with less than Len
341 void SB_Move (StrBuf* Target, StrBuf* Source);
342 /* Move the complete contents of Source to target. This will delete the old
343 * contents of Target, and Source will be empty after the call.
346 int SB_Compare (const StrBuf* S1, const StrBuf* S2);
347 /* Do a lexical compare of S1 and S2. See strcmp for result codes. */
351 /* End of strbuf.h */