1 /*****************************************************************************/
5 /* Variable sized string buffers */
9 /* (C) 2001-2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 void SB_Realloc (StrBuf* B, unsigned NewSize);
94 /* Reallocate the string buffer space, make sure at least NewSize bytes are
95 * available. THIS IS NOT A USER CALLABLE FUNCTION!
98 #if defined(HAVE_INLINE)
99 INLINE unsigned SB_GetLen (StrBuf* B)
100 /* Return the length of the buffer contents */
105 # define SB_GetLen(B) (B)->Len
108 #if defined(HAVE_INLINE)
109 INLINE unsigned SB_GetIndex (StrBuf* B)
110 /* Return the user index of the string buffer */
115 # define SB_GetIndex(B) (B)->Index
118 #if defined(HAVE_INLINE)
119 INLINE const char* SB_GetConstBuf (const StrBuf* B)
120 /* Return a buffer pointer */
125 # define SB_GetConstBuf(B) (B)->Buf
128 #if defined(HAVE_INLINE)
129 INLINE char* SB_GetBuf (StrBuf* B)
130 /* Return a buffer pointer */
135 # define SB_GetBuf(B) (B)->Buf
138 #if defined(HAVE_INLINE)
139 INLINE char SB_At (const StrBuf* B, unsigned Index)
140 /* Get a character from the buffer */
142 PRECONDITION (Index < B->Len);
143 return B->Buf[Index];
146 char SB_At (const StrBuf* B, unsigned Index);
147 /* Get a character from the buffer */
150 #if defined(HAVE_INLINE)
151 INLINE char SB_AtUnchecked (const StrBuf* B, unsigned Index)
152 /* Get a character from the buffer */
154 return B->Buf[Index];
157 # define SB_AtUnchecked(B, Index) ((B)->Buf[Index])
160 #if defined(HAVE_INLINE)
161 INLINE int SB_IsEmpty (const StrBuf* B)
162 /* Return true if the string buffer is empty */
164 return (B->Len == 0);
167 # define SB_IsEmpty(B) ((B)->Len == 0)
170 #if defined(HAVE_INLINE)
171 INLINE void SB_Clear (StrBuf* B)
172 /* Clear the string buffer (make it empty) */
177 # define SB_Clear(B) ((B)->Len = 0)
180 #if defined(HAVE_INLINE)
181 INLINE char SB_Get (StrBuf* B)
182 /* Return the next character from the string incrementing Index. Returns NUL
183 * if the end of the string is reached.
186 return (B->Index < B->Len)? B->Buf[B->Index++] : '\0';
189 # define SB_Get(B) (((B)->Index < (B)->Len)? (B)->Buf[(B)->Index++] : '\0')
192 #if defined(HAVE_INLINE)
193 INLINE char SB_Peek (StrBuf* B)
194 /* Look at the next character from the string without incrementing Index.
195 * Returns NUL if the end of the string is reached.
198 return (B->Index < B->Len)? B->Buf[B->Index] : '\0';
201 # define SB_Peek(B) (((B)->Index < (B)->Len)? (B)->Buf[(B)->Index] : '\0')
204 #if defined(HAVE_INLINE)
205 INLINE void SB_Skip (StrBuf* B)
206 /* Skip the next character in the string buffer if this is possible. */
208 if (B->Index < B->Len) {
213 # define SB_Skip(B) do { if ((B)->Index < (B)->Len) ++(B)->Index; } while (0)
216 void SB_Terminate (StrBuf* B);
217 /* Zero terminate the given string buffer. NOTE: The terminating zero is not
218 * accounted for in B->Len, if you want that, you have to use AppendChar!
221 void SB_CopyBuf (StrBuf* Target, const char* Buf, unsigned Size);
222 /* Copy Buf to Target, discarding the old contents of Target */
224 #if defined(HAVE_INLINE)
225 INLINE void SB_CopyStr (StrBuf* Target, const char* S)
226 /* Copy S to Target, discarding the old contents of Target */
228 SB_CopyBuf (Target, S, strlen (S));
231 void SB_CopyStr (StrBuf* Target, const char* S);
232 /* Copy S to Target, discarding the old contents of Target */
235 #if defined(HAVE_INLINE)
236 INLINE void SB_Copy (StrBuf* Target, const StrBuf* Source)
237 /* Copy Source to Target, discarding the old contents of Target */
239 SB_CopyBuf (Target, Source->Buf, Source->Len);
242 void SB_Copy (StrBuf* Target, const StrBuf* Source);
243 /* Copy Source to Target, discarding the old contents of Target */
246 void SB_AppendChar (StrBuf* B, char C);
247 /* Append a character to a string buffer */
249 void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size);
250 /* Append a character buffer to the end of the string buffer */
252 #if defined(HAVE_INLINE)
253 INLINE void SB_AppendStr (StrBuf* B, const char* S)
254 /* Append a string to the end of the string buffer */
256 SB_AppendBuf (B, S, strlen (S));
259 void SB_AppendStr (StrBuf* B, const char* S);
260 /* Append a string to the end of the string buffer */
263 #if defined(HAVE_INLINE)
264 INLINE void SB_Append (StrBuf* Target, const StrBuf* Source)
265 /* Append the contents of Source to Target */
267 SB_AppendBuf (Target, Source->Buf, Source->Len);
270 void SB_Append (StrBuf* Target, const StrBuf* Source);
271 /* Append the contents of Source to Target */
274 #if defined(HAVE_INLINE)
275 INLINE void SB_Cut (StrBuf* B, unsigned Len)
276 /* Cut the contents of B at the given length. If the current length of the
277 * buffer is smaller than Len, nothing will happen.
285 void SB_Cut (StrBuf* B, unsigned Len);
286 /* Cut the contents of B at the given length. If the current length of the
287 * buffer is smaller than Len, nothing will happen.
291 void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len);
292 /* Copy a slice from Source into Target. The current contents of Target are
293 * destroyed. If Start is greater than the length of Source, or if Len
294 * characters aren't available, the result will be a buffer with less than Len
300 /* End of strbuf.h */