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 char SB_At (const StrBuf* B, unsigned Index);
136 /* Get a character from the buffer */
139 #if defined(HAVE_INLINE)
140 INLINE char SB_AtUnchecked (const StrBuf* B, unsigned Index)
141 /* Get a character from the buffer */
143 return B->Buf[Index];
146 # define SB_AtUnchecked(B, Index) ((B)->Buf[Index])
149 #if defined(HAVE_INLINE)
150 INLINE int SB_IsEmpty (const StrBuf* B)
151 /* Return true if the string buffer is empty */
153 return (B->Len == 0);
156 # define SB_IsEmpty(B) ((B)->Len == 0)
159 #if defined(HAVE_INLINE)
160 INLINE void SB_Clear (StrBuf* B)
161 /* Clear the string buffer (make it empty) */
166 # define SB_Clear(B) ((B)->Len = 0)
169 void SB_Terminate (StrBuf* B);
170 /* Zero terminate the given string buffer. NOTE: The terminating zero is not
171 * accounted for in B->Len, if you want that, you have to use AppendChar!
174 void SB_CopyBuf (StrBuf* Target, const char* Buf, unsigned Size);
175 /* Copy Buf to Target, discarding the old contents of Target */
177 #if defined(HAVE_INLINE)
178 INLINE void SB_CopyStr (StrBuf* Target, const char* S)
179 /* Copy S to Target, discarding the old contents of Target */
181 SB_CopyBuf (Target, S, strlen (S));
184 void SB_CopyStr (StrBuf* Target, const char* S);
185 /* Copy S to Target, discarding the old contents of Target */
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 void SB_Copy (StrBuf* Target, const StrBuf* Source);
196 /* Copy Source to Target, discarding the old contents of Target */
199 void SB_AppendChar (StrBuf* B, char C);
200 /* Append a character to a string buffer */
202 void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size);
203 /* Append a character buffer to the end of the string buffer */
205 #if defined(HAVE_INLINE)
206 INLINE void SB_AppendStr (StrBuf* B, const char* S)
207 /* Append a string to the end of the string buffer */
209 SB_AppendBuf (B, S, strlen (S));
212 void SB_AppendStr (StrBuf* B, const char* S);
213 /* Append a string to the end of the string buffer */
216 #if defined(HAVE_INLINE)
217 INLINE void SB_Append (StrBuf* Target, const StrBuf* Source)
218 /* Append the contents of Source to Target */
220 SB_AppendBuf (Target, Source->Buf, Source->Len);
223 void SB_Append (StrBuf* Target, const StrBuf* Source);
224 /* Append the contents of Source to Target */
227 #if defined(HAVE_INLINE)
228 INLINE void SB_Cut (StrBuf* B, unsigned Len)
229 /* Cut the contents of B at the given length. If the current length of the
230 * buffer is smaller than Len, nothing will happen.
238 void SB_Cut (StrBuf* B, unsigned Len);
239 /* Cut the contents of B at the given length. If the current length of the
240 * buffer is smaller than Len, nothing will happen.
244 void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len);
245 /* Copy a slice from Source into Target. The current contents of Target are
246 * destroyed. If Start is greater than the length of Source, or if Len
247 * characters aren't available, the result will be a buffer with less than Len
253 /* End of strbuf.h */