]> git.sur5r.net Git - cc65/blob - src/common/strbuf.h
Beafed up the string buffer module
[cc65] / src / common / strbuf.h
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 strbuf.h                                  */
4 /*                                                                           */
5 /*                       Variable sized string buffers                       */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001-2002 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #ifndef STRBUF_H
37 #define STRBUF_H
38
39
40
41 #include <string.h>
42
43 /* common */
44 #include "attrib.h"
45 #include "check.h"
46 #include "inline.h"
47
48
49
50 /*****************************************************************************/
51 /*                                   Data                                    */
52 /*****************************************************************************/
53
54
55
56 typedef struct StrBuf StrBuf;
57 struct 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 */
62 };
63
64 /* An empty string buf */
65 extern const StrBuf EmptyStrBuf;
66
67 /* Initializer for static string bufs */
68 #define STATIC_STRBUF_INITIALIZER       { 0, 0, 0, 0 }
69
70 /* Initializer for auto string bufs */
71 #define AUTO_STRBUF_INITIALIZER         EmptyStrBuf
72
73
74
75 /*****************************************************************************/
76 /*                                   Code                                    */
77 /*****************************************************************************/
78
79
80
81 StrBuf* InitStrBuf (StrBuf* B);
82 /* Initialize a string buffer */
83
84 void DoneStrBuf (StrBuf* B);
85 /* Free the data of a string buffer (but not the struct itself) */
86
87 StrBuf* NewStrBuf (void);
88 /* Allocate, initialize and return a new StrBuf */
89
90 void FreeStrBuf (StrBuf* B);
91 /* Free a string buffer */
92
93 #if defined(HAVE_INLINE)
94 INLINE unsigned SB_GetLen (StrBuf* B)
95 /* Return the length of the buffer contents */
96 {
97     return B->Len;
98 }
99 #else
100 #  define SB_GetLen(B)  (B)->Len
101 #endif
102
103 #if defined(HAVE_INLINE)
104 INLINE unsigned SB_GetIndex (StrBuf* B)
105 /* Return the user index of the string buffer */
106 {
107     return B->Index;
108 }
109 #else
110 #  define SB_GetIndex(B)  (B)->Index
111 #endif
112
113 #if defined(HAVE_INLINE)
114 INLINE const char* SB_GetConstBuf (const StrBuf* B)
115 /* Return a buffer pointer */
116 {
117     return B->Buf;
118 }
119 #else
120 #  define SB_GetConstBuf(B)     (B)->Buf
121 #endif
122
123 #if defined(HAVE_INLINE)
124 INLINE char* SB_GetBuf (StrBuf* B)
125 /* Return a buffer pointer */
126 {
127     return B->Buf;
128 }
129 #else
130 #  define SB_GetBuf(B)     (B)->Buf
131 #endif
132
133 #if defined(HAVE_INLINE)
134 INLINE char SB_At (const StrBuf* B, unsigned Index)
135 /* Get a character from the buffer */
136 {
137     PRECONDITION (Index < B->Len);
138     return B->Buf[Index];
139 }
140 #else
141 char SB_At (const StrBuf* B, unsigned Index);
142 /* Get a character from the buffer */
143 #endif
144
145 #if defined(HAVE_INLINE)
146 INLINE char SB_AtUnchecked (const StrBuf* B, unsigned Index)
147 /* Get a character from the buffer */
148 {
149     return B->Buf[Index];
150 }
151 #else
152 #  define SB_AtUnchecked(B, Index)      ((B)->Buf[Index])
153 #endif
154
155 #if defined(HAVE_INLINE)
156 INLINE int SB_IsEmpty (const StrBuf* B)
157 /* Return true if the string buffer is empty */
158 {
159     return (B->Len == 0);
160 }
161 #else
162 #  define SB_IsEmpty(B) ((B)->Len == 0)
163 #endif
164
165 #if defined(HAVE_INLINE)
166 INLINE int SB_NotEmpty (const StrBuf* B)
167 /* Return true if the string buffer is not empty */
168 {
169     return (B->Len > 0);
170 }
171 #else
172 #  define SB_NotEmpty(B) ((B)->Len > 0)
173 #endif
174
175 #if defined(HAVE_INLINE)
176 INLINE void SB_Clear (StrBuf* B)
177 /* Clear the string buffer (make it empty) */
178 {
179     B->Len = B->Index = 0;
180 }
181 #else
182 #  define SB_Clear(B)   ((B)->Len = (B)->Index = 0)
183 #endif
184
185 #if defined(HAVE_INLINE)
186 INLINE void SB_Reset (StrBuf* B)
187 /* Reset the string buffer index to zero */
188 {
189     B->Index = 0;
190 }
191 #else
192 #  define SB_Reset(B)   ((B)->Index = 0)
193 #endif
194
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.
199  */
200 {
201     return (B->Index < B->Len)? B->Buf[B->Index++] : '\0';
202 }
203 #else
204 #  define SB_Get(B)     (((B)->Index < (B)->Len)? (B)->Buf[(B)->Index++] : '\0')
205 #endif
206
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.
211  */
212 {
213     return (B->Index < B->Len)? B->Buf[B->Index] : '\0';
214 }
215 #else
216 #  define SB_Peek(B)     (((B)->Index < (B)->Len)? (B)->Buf[(B)->Index] : '\0')
217 #endif
218
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.
223  */
224 {
225     return (Index < B->Len)? B->Buf[Index] : '\0';
226 }
227 #else
228 #  define SB_Peek(B,Index)     (((Index) < (B)->Len)? (B)->Buf[(Index)] : '\0')
229 #endif
230
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
234  * is empty.
235  */
236 {
237     return (B->Len > 0)? B->Buf[B->Len-1] : '\0';
238 }
239 #else
240 #  define SB_LookAtLast(B)      (((B)->Len > 0)? (B)->Buf[(B)->Len-1] : '\0')
241 #endif
242
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. */
246 {
247     if (B->Index < B->Len) {
248         ++B->Index;
249     }
250 }
251 #else
252 #  define SB_Skip(B)     do { if ((B)->Index < (B)->Len) ++(B)->Index; } while (0)
253 #endif
254
255 void SB_Drop (StrBuf* B, unsigned Count);
256 /* Drop characters from the end of the string. */
257
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!
261  */
262
263 void SB_CopyBuf (StrBuf* Target, const char* Buf, unsigned Size);
264 /* Copy Buf to Target, discarding the old contents of Target */
265
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 */
269 {
270     SB_CopyBuf (Target, S, strlen (S));
271 }
272 #else
273 void SB_CopyStr (StrBuf* Target, const char* S);
274 /* Copy S to Target, discarding the old contents of Target */
275 #endif
276
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 */
280 {
281     SB_CopyBuf (Target, Source->Buf, Source->Len);
282     Target->Index = Source->Index;
283 }
284 #else
285 void SB_Copy (StrBuf* Target, const StrBuf* Source);
286 /* Copy Source to Target, discarding the old contents of Target */
287 #endif
288
289 void SB_AppendChar (StrBuf* B, char C);
290 /* Append a character to a string buffer */
291
292 void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size);
293 /* Append a character buffer to the end of the string buffer */
294
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 */
298 {
299     SB_AppendBuf (B, S, strlen (S));
300 }
301 #else
302 void SB_AppendStr (StrBuf* B, const char* S);
303 /* Append a string to the end of the string buffer */
304 #endif
305
306 #if defined(HAVE_INLINE)
307 INLINE void SB_Append (StrBuf* Target, const StrBuf* Source)
308 /* Append the contents of Source to Target */
309 {
310     SB_AppendBuf (Target, Source->Buf, Source->Len);
311 }
312 #else
313 void SB_Append (StrBuf* Target, const StrBuf* Source);
314 /* Append the contents of Source to Target */
315 #endif
316
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.
321  */
322 {
323     if (Len < B->Len) {
324         B->Len = Len;
325     }
326 }
327 #else
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.
331  */
332 #endif
333
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
338  * bytes.
339  */
340
341
342
343 /* End of strbuf.h */
344
345 #endif
346
347
348