]> git.sur5r.net Git - cc65/blob - src/common/strbuf.h
Add a user index to class StrBuf
[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 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
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 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!
96  */
97
98 #if defined(HAVE_INLINE)
99 INLINE unsigned SB_GetLen (StrBuf* B)
100 /* Return the length of the buffer contents */
101 {
102     return B->Len;
103 }
104 #else
105 #  define SB_GetLen(B)  (B)->Len
106 #endif
107
108 #if defined(HAVE_INLINE)
109 INLINE unsigned SB_GetIndex (StrBuf* B)
110 /* Return the user index of the string buffer */
111 {
112     return B->Index;
113 }
114 #else
115 #  define SB_GetIndex(B)  (B)->Index
116 #endif
117
118 #if defined(HAVE_INLINE)
119 INLINE const char* SB_GetConstBuf (const StrBuf* B)
120 /* Return a buffer pointer */
121 {
122     return B->Buf;
123 }
124 #else
125 #  define SB_GetConstBuf(B)     (B)->Buf
126 #endif
127
128 #if defined(HAVE_INLINE)
129 INLINE char* SB_GetBuf (StrBuf* B)
130 /* Return a buffer pointer */
131 {
132     return B->Buf;
133 }
134 #else
135 #  define SB_GetBuf(B)     (B)->Buf
136 #endif
137
138 #if defined(HAVE_INLINE)
139 INLINE char SB_At (const StrBuf* B, unsigned Index)
140 /* Get a character from the buffer */
141 {
142     PRECONDITION (Index < B->Len);
143     return B->Buf[Index];
144 }
145 #else
146 char SB_At (const StrBuf* B, unsigned Index);
147 /* Get a character from the buffer */
148 #endif
149
150 #if defined(HAVE_INLINE)
151 INLINE char SB_AtUnchecked (const StrBuf* B, unsigned Index)
152 /* Get a character from the buffer */
153 {
154     return B->Buf[Index];
155 }
156 #else
157 #  define SB_AtUnchecked(B, Index)      ((B)->Buf[Index])
158 #endif
159
160 #if defined(HAVE_INLINE)
161 INLINE int SB_IsEmpty (const StrBuf* B)
162 /* Return true if the string buffer is empty */
163 {
164     return (B->Len == 0);
165 }
166 #else
167 #  define SB_IsEmpty(B) ((B)->Len == 0)
168 #endif
169
170 #if defined(HAVE_INLINE)
171 INLINE void SB_Clear (StrBuf* B)
172 /* Clear the string buffer (make it empty) */
173 {
174     B->Len = 0;
175 }
176 #else
177 #  define SB_Clear(B)   ((B)->Len = 0)
178 #endif
179
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.
184  */
185 {
186     return (B->Index < B->Len)? B->Buf[B->Index++] : '\0';
187 }
188 #else
189 #  define SB_Get(B)     (((B)->Index < (B)->Len)? (B)->Buf[(B)->Index++] : '\0')
190 #endif
191
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.
196  */
197 {
198     return (B->Index < B->Len)? B->Buf[B->Index] : '\0';
199 }
200 #else
201 #  define SB_Peek(B)     (((B)->Index < (B)->Len)? (B)->Buf[(B)->Index] : '\0')
202 #endif
203
204 void SB_Terminate (StrBuf* B);
205 /* Zero terminate the given string buffer. NOTE: The terminating zero is not
206  * accounted for in B->Len, if you want that, you have to use AppendChar!
207  */
208
209 void SB_CopyBuf (StrBuf* Target, const char* Buf, unsigned Size);
210 /* Copy Buf to Target, discarding the old contents of Target */
211
212 #if defined(HAVE_INLINE)
213 INLINE void SB_CopyStr (StrBuf* Target, const char* S)
214 /* Copy S to Target, discarding the old contents of Target */
215 {
216     SB_CopyBuf (Target, S, strlen (S));
217 }
218 #else
219 void SB_CopyStr (StrBuf* Target, const char* S);
220 /* Copy S to Target, discarding the old contents of Target */
221 #endif
222
223 #if defined(HAVE_INLINE)
224 INLINE void SB_Copy (StrBuf* Target, const StrBuf* Source)
225 /* Copy Source to Target, discarding the old contents of Target */
226 {
227     SB_CopyBuf (Target, Source->Buf, Source->Len);
228 }
229 #else
230 void SB_Copy (StrBuf* Target, const StrBuf* Source);
231 /* Copy Source to Target, discarding the old contents of Target */
232 #endif
233
234 void SB_AppendChar (StrBuf* B, char C);
235 /* Append a character to a string buffer */
236
237 void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size);
238 /* Append a character buffer to the end of the string buffer */
239
240 #if defined(HAVE_INLINE)
241 INLINE void SB_AppendStr (StrBuf* B, const char* S)
242 /* Append a string to the end of the string buffer */
243 {
244     SB_AppendBuf (B, S, strlen (S));
245 }
246 #else
247 void SB_AppendStr (StrBuf* B, const char* S);
248 /* Append a string to the end of the string buffer */
249 #endif
250
251 #if defined(HAVE_INLINE)
252 INLINE void SB_Append (StrBuf* Target, const StrBuf* Source)
253 /* Append the contents of Source to Target */
254 {
255     SB_AppendBuf (Target, Source->Buf, Source->Len);
256 }
257 #else
258 void SB_Append (StrBuf* Target, const StrBuf* Source);
259 /* Append the contents of Source to Target */
260 #endif
261
262 #if defined(HAVE_INLINE)
263 INLINE void SB_Cut (StrBuf* B, unsigned Len)
264 /* Cut the contents of B at the given length. If the current length of the
265  * buffer is smaller than Len, nothing will happen.
266  */
267 {
268     if (Len < B->Len) {
269         B->Len = Len;
270     }
271 }
272 #else
273 void SB_Cut (StrBuf* B, unsigned Len);
274 /* Cut the contents of B at the given length. If the current length of the
275  * buffer is smaller than Len, nothing will happen.
276  */
277 #endif
278
279 void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len);
280 /* Copy a slice from Source into Target. The current contents of Target are
281  * destroyed. If Start is greater than the length of Source, or if Len
282  * characters aren't available, the result will be a buffer with less than Len
283  * bytes.
284  */
285
286
287
288 /* End of strbuf.h */
289
290 #endif
291
292
293