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 /*****************************************************************************/
49 /*****************************************************************************/
51 /*****************************************************************************/
55 typedef struct StrBuf StrBuf;
62 /* Initializer for static string bufs */
63 #define STATIC_STRBUF_INITIALIZER { 0, 0, 0 }
67 /*****************************************************************************/
69 /*****************************************************************************/
73 StrBuf* InitStrBuf (StrBuf* B);
74 /* Initialize a string buffer */
76 void DoneStrBuf (StrBuf* B);
77 /* Free the data of a string buffer (but not the struct itself) */
79 StrBuf* NewStrBuf (void);
80 /* Allocate, initialize and return a new StrBuf */
82 void FreeStrBuf (StrBuf* B);
83 /* Free a string buffer */
85 void SB_Realloc (StrBuf* B, unsigned NewSize);
86 /* Reallocate the string buffer space, make sure at least NewSize bytes are
87 * available. THIS IS NOT A USER CALLABLE FUNCTION!
90 #if defined(HAVE_INLINE)
91 INLINE unsigned SB_GetLen (StrBuf* B)
92 /* Return the length of the buffer contents */
97 # define SB_GetLen(B) (B)->Len
100 #if defined(HAVE_INLINE)
101 INLINE const char* SB_GetConstBuf (const StrBuf* B)
102 /* Return a buffer pointer */
107 # define SB_GetConstBuf(B) (B)->Buf
110 #if defined(HAVE_INLINE)
111 INLINE char* SB_GetBuf (StrBuf* B)
112 /* Return a buffer pointer */
117 # define SB_GetBuf(B) (B)->Buf
120 #if defined(HAVE_INLINE)
121 INLINE void SB_Clear (StrBuf* B)
122 /* Clear the string buffer (make it empty) */
127 # define SB_Clear(B) ((B)->Len = 0)
130 void SB_Terminate (StrBuf* B);
131 /* Zero terminate the given string buffer. NOTE: The terminating zero is not
132 * accounted for in B->Len, if you want that, you have to use AppendChar!
135 void SB_AppendChar (StrBuf* B, char C);
136 /* Append a character to a string buffer */
138 void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size);
139 /* Append a character buffer to the end of the string buffer */
141 #if defined(HAVE_INLINE)
142 INLINE void SB_AppendStr (StrBuf* B, const char* S)
143 /* Append a string to the end of the string buffer */
145 SB_AppendBuf (B, S, strlen (S));
148 # define SB_AppendStr(B, S) SB_AppendBuf (B, S, strlen (S))
151 void SB_Copy (StrBuf* Target, const StrBuf* Source);
152 /* Copy Source to Target, discarding the old contents of Target */
154 #if defined(HAVE_INLINE)
155 INLINE void SB_Append (StrBuf* Target, const StrBuf* Source)
156 /* Append the contents of Source to Target */
158 SB_AppendBuf (Target, Source->Buf, Source->Len);
161 # define SB_Append(Target, Source) SB_AppendBuf (Target, (Source)->Buf, (Source)->Len)
164 #if defined(HAVE_INLINE)
165 INLINE void SB_Cut (StrBuf* B, unsigned Len)
166 /* Cut the contents of B at the given length. If the current length of the
167 * buffer is smaller than Len, nothing will happen.
175 # define SB_Cut(B, L) if ((L) < (B)->Len) { (B)->Len = (L); }
178 void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len);
179 /* Copy a slice from Source into Target. The current contents of Target are
180 * destroyed. If Start is greater than the length of Source, or if Len
181 * characters aren't available, the result will be a buffer with less than Len
187 /* End of strbuf.h */