]> git.sur5r.net Git - cc65/blob - src/common/strbuf.h
Adding functionality to StrBuf
[cc65] / src / common / strbuf.h
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 strbuf.h                                  */
4 /*                                                                           */
5 /*                       Variable sized string buffers                       */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001      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;
59     unsigned    Len;
60     char*       Buf;
61 };
62
63 /* An empty string buf */
64 extern const StrBuf EmptyStrBuf;
65
66 /* Initializer for static string bufs */
67 #define STATIC_STRBUF_INITIALIZER       { 0, 0, 0 }
68
69 /* Initializer for auto string bufs */
70 #define AUTO_STRBUF_INITIALIZER         EmptyStrBuf
71
72
73
74 /*****************************************************************************/
75 /*                                   Code                                    */
76 /*****************************************************************************/
77
78
79
80 StrBuf* InitStrBuf (StrBuf* B);
81 /* Initialize a string buffer */
82
83 void DoneStrBuf (StrBuf* B);
84 /* Free the data of a string buffer (but not the struct itself) */
85
86 StrBuf* NewStrBuf (void);
87 /* Allocate, initialize and return a new StrBuf */
88
89 void FreeStrBuf (StrBuf* B);
90 /* Free a string buffer */
91
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!
95  */
96
97 #if defined(HAVE_INLINE)
98 INLINE unsigned SB_GetLen (StrBuf* B)
99 /* Return the length of the buffer contents */
100 {
101     return B->Len;
102 }
103 #else
104 #  define SB_GetLen(B)  (B)->Len
105 #endif
106
107 #if defined(HAVE_INLINE)
108 INLINE const char* SB_GetConstBuf (const StrBuf* B)
109 /* Return a buffer pointer */
110 {
111     return B->Buf;
112 }
113 #else
114 #  define SB_GetConstBuf(B)     (B)->Buf
115 #endif
116
117 #if defined(HAVE_INLINE)
118 INLINE char* SB_GetBuf (StrBuf* B)
119 /* Return a buffer pointer */
120 {
121     return B->Buf;
122 }
123 #else
124 #  define SB_GetBuf(B)     (B)->Buf
125 #endif
126
127 #if defined(HAVE_INLINE)
128 INLINE char SB_At (const StrBuf* B, unsigned Index)
129 /* Get a character from the buffer */
130 {
131     PRECONDITION (Index < B->Len);
132     return B->Buf[Index];
133 }
134 #else
135 #  define SB_At(B, Index)                       \
136         (PRECONDITION ((Index) < (B)->Len),     \
137         (B)->Buf[Index])
138 #endif
139
140 #if defined(HAVE_INLINE)
141 INLINE char SB_AtUnchecked (const StrBuf* B, unsigned Index)
142 /* Get a character from the buffer */
143 {
144     return B->Buf[Index];
145 }
146 #else
147 #  define SB_AtUnchecked(B, Index)      ((B)->Buf[Index])
148 #endif
149
150 #if defined(HAVE_INLINE)
151 INLINE int SB_IsEmpty (const StrBuf* B)
152 /* Return true if the string buffer is empty */
153 {
154     return (B->Len == 0);
155 }
156 #else
157 #  define SB_IsEmpty(B) ((B)->Len == 0)
158 #endif
159
160 #if defined(HAVE_INLINE)
161 INLINE void SB_Clear (StrBuf* B)
162 /* Clear the string buffer (make it empty) */
163 {
164     B->Len = 0;
165 }
166 #else
167 #  define SB_Clear(B)   ((B)->Len = 0)
168 #endif
169
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!
173  */
174
175 void SB_CopyBuf (StrBuf* Target, const char* Buf, unsigned Size);
176 /* Copy Buf to Target, discarding the old contents of Target */
177
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 */
181 {
182     SB_CopyBuf (Target, S, strlen (S));
183 }
184 #else
185 #  define SB_CopyStr(Target, S) SB_CopyBuf (Target, S, strlen (S))
186 #endif
187
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 */
191 {
192     SB_CopyBuf (Target, Source->Buf, Source->Len);
193 }
194 #else
195 #  define SB_Copy(Target, Source)       SB_CopyBuf (Target, (Source)->Buf, (Source)->Len)
196 #endif
197
198 void SB_AppendChar (StrBuf* B, char C);
199 /* Append a character to a string buffer */
200
201 void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size);
202 /* Append a character buffer to the end of the string buffer */
203
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 */
207 {
208     SB_AppendBuf (B, S, strlen (S));
209 }
210 #else
211 #  define SB_AppendStr(B, S)    SB_AppendBuf (B, S, strlen (S))
212 #endif
213
214 #if defined(HAVE_INLINE)
215 INLINE void SB_Append (StrBuf* Target, const StrBuf* Source)
216 /* Append the contents of Source to Target */
217 {
218     SB_AppendBuf (Target, Source->Buf, Source->Len);
219 }
220 #else
221 #  define SB_Append(Target, Source)     SB_AppendBuf (Target, (Source)->Buf, (Source)->Len)
222 #endif
223
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.
228  */
229 {
230     if (Len < B->Len) {
231         B->Len = Len;
232     }
233 }
234 #else
235 #  define SB_Cut(B, L)        if ((L) < (B)->Len) { (B)->Len = (L); }
236 #endif
237
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
242  * bytes.
243  */
244
245
246
247 /* End of strbuf.h */
248
249 #endif
250
251
252