1 /*****************************************************************************/
5 /* Variable sized string buffers */
9 /* (C) 2001 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;
63 /* An empty string buf */
64 extern const StrBuf EmptyStrBuf;
66 /* Initializer for static string bufs */
67 #define STATIC_STRBUF_INITIALIZER { 0, 0, 0 }
69 /* Initializer for auto string bufs */
70 #define AUTO_STRBUF_INITIALIZER EmptyStrBuf
74 /*****************************************************************************/
76 /*****************************************************************************/
80 StrBuf* InitStrBuf (StrBuf* B);
81 /* Initialize a string buffer */
83 void DoneStrBuf (StrBuf* B);
84 /* Free the data of a string buffer (but not the struct itself) */
86 StrBuf* NewStrBuf (void);
87 /* Allocate, initialize and return a new StrBuf */
89 void FreeStrBuf (StrBuf* B);
90 /* Free a string buffer */
92 void SB_Realloc (StrBuf* B, unsigned NewSize);
93 /* Reallocate the string buffer space, make sure at least NewSize bytes are
94 * available. THIS IS NOT A USER CALLABLE FUNCTION!
97 #if defined(HAVE_INLINE)
98 INLINE unsigned SB_GetLen (StrBuf* B)
99 /* Return the length of the buffer contents */
104 # define SB_GetLen(B) (B)->Len
107 #if defined(HAVE_INLINE)
108 INLINE const char* SB_GetConstBuf (const StrBuf* B)
109 /* Return a buffer pointer */
114 # define SB_GetConstBuf(B) (B)->Buf
117 #if defined(HAVE_INLINE)
118 INLINE char* SB_GetBuf (StrBuf* B)
119 /* Return a buffer pointer */
124 # define SB_GetBuf(B) (B)->Buf
127 #if defined(HAVE_INLINE)
128 INLINE char SB_At (const StrBuf* B, unsigned Index)
129 /* Get a character from the buffer */
131 PRECONDITION (Index < B->Len);
132 return B->Buf[Index];
135 # define SB_At(B, Index) \
136 (PRECONDITION ((Index) < (B)->Len), \
140 #if defined(HAVE_INLINE)
141 INLINE char SB_AtUnchecked (const StrBuf* B, unsigned Index)
142 /* Get a character from the buffer */
144 return B->Buf[Index];
147 # define SB_AtUnchecked(B, Index) ((B)->Buf[Index])
150 #if defined(HAVE_INLINE)
151 INLINE int SB_IsEmpty (const StrBuf* B)
152 /* Return true if the string buffer is empty */
154 return (B->Len == 0);
157 # define SB_IsEmpty(B) ((B)->Len == 0)
160 #if defined(HAVE_INLINE)
161 INLINE void SB_Clear (StrBuf* B)
162 /* Clear the string buffer (make it empty) */
167 # define SB_Clear(B) ((B)->Len = 0)
170 void SB_Terminate (StrBuf* B);
171 /* Zero terminate the given string buffer. NOTE: The terminating zero is not
172 * accounted for in B->Len, if you want that, you have to use AppendChar!
175 void SB_CopyBuf (StrBuf* Target, const char* Buf, unsigned Size);
176 /* Copy Buf to Target, discarding the old contents of Target */
178 #if defined(HAVE_INLINE)
179 INLINE void SB_CopyStr (StrBuf* Target, const char* S)
180 /* Copy S to Target, discarding the old contents of Target */
182 SB_CopyBuf (Target, S, strlen (S));
185 # define SB_CopyStr(Target, S) SB_CopyBuf (Target, S, strlen (S))
188 #if defined(HAVE_INLINE)
189 INLINE void SB_Copy (StrBuf* Target, const StrBuf* Source)
190 /* Copy Source to Target, discarding the old contents of Target */
192 SB_CopyBuf (Target, Source->Buf, Source->Len);
195 # define SB_Copy(Target, Source) SB_CopyBuf (Target, (Source)->Buf, (Source)->Len)
198 void SB_AppendChar (StrBuf* B, char C);
199 /* Append a character to a string buffer */
201 void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size);
202 /* Append a character buffer to the end of the string buffer */
204 #if defined(HAVE_INLINE)
205 INLINE void SB_AppendStr (StrBuf* B, const char* S)
206 /* Append a string to the end of the string buffer */
208 SB_AppendBuf (B, S, strlen (S));
211 # define SB_AppendStr(B, S) SB_AppendBuf (B, S, strlen (S))
214 #if defined(HAVE_INLINE)
215 INLINE void SB_Append (StrBuf* Target, const StrBuf* Source)
216 /* Append the contents of Source to Target */
218 SB_AppendBuf (Target, Source->Buf, Source->Len);
221 # define SB_Append(Target, Source) SB_AppendBuf (Target, (Source)->Buf, (Source)->Len)
224 #if defined(HAVE_INLINE)
225 INLINE void SB_Cut (StrBuf* B, unsigned Len)
226 /* Cut the contents of B at the given length. If the current length of the
227 * buffer is smaller than Len, nothing will happen.
235 # define SB_Cut(B, L) if ((L) < (B)->Len) { (B)->Len = (L); }
238 void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len);
239 /* Copy a slice from Source into Target. The current contents of Target are
240 * destroyed. If Start is greater than the length of Source, or if Len
241 * characters aren't available, the result will be a buffer with less than Len
247 /* End of strbuf.h */