]> git.sur5r.net Git - cc65/blob - src/common/strbuf.h
Add #pragma charmap()
[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 char SB_At (const StrBuf* B, unsigned Index);
136 /* Get a character from the buffer */
137 #endif
138
139 #if defined(HAVE_INLINE)
140 INLINE char SB_AtUnchecked (const StrBuf* B, unsigned Index)
141 /* Get a character from the buffer */
142 {
143     return B->Buf[Index];
144 }
145 #else
146 #  define SB_AtUnchecked(B, Index)      ((B)->Buf[Index])
147 #endif
148
149 #if defined(HAVE_INLINE)
150 INLINE int SB_IsEmpty (const StrBuf* B)
151 /* Return true if the string buffer is empty */
152 {
153     return (B->Len == 0);
154 }
155 #else
156 #  define SB_IsEmpty(B) ((B)->Len == 0)
157 #endif
158
159 #if defined(HAVE_INLINE)
160 INLINE void SB_Clear (StrBuf* B)
161 /* Clear the string buffer (make it empty) */
162 {
163     B->Len = 0;
164 }
165 #else
166 #  define SB_Clear(B)   ((B)->Len = 0)
167 #endif
168
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!
172  */
173
174 void SB_CopyBuf (StrBuf* Target, const char* Buf, unsigned Size);
175 /* Copy Buf to Target, discarding the old contents of Target */
176
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 */
180 {
181     SB_CopyBuf (Target, S, strlen (S));
182 }
183 #else
184 void SB_CopyStr (StrBuf* Target, const char* S);
185 /* Copy S to Target, discarding the old contents of Target */
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 void SB_Copy (StrBuf* Target, const StrBuf* Source);
196 /* Copy Source to Target, discarding the old contents of Target */
197 #endif
198
199 void SB_AppendChar (StrBuf* B, char C);
200 /* Append a character to a string buffer */
201
202 void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size);
203 /* Append a character buffer to the end of the string buffer */
204
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 */
208 {
209     SB_AppendBuf (B, S, strlen (S));
210 }
211 #else
212 void SB_AppendStr (StrBuf* B, const char* S);
213 /* Append a string to the end of the string buffer */
214 #endif
215
216 #if defined(HAVE_INLINE)
217 INLINE void SB_Append (StrBuf* Target, const StrBuf* Source)
218 /* Append the contents of Source to Target */
219 {
220     SB_AppendBuf (Target, Source->Buf, Source->Len);
221 }
222 #else
223 void SB_Append (StrBuf* Target, const StrBuf* Source);
224 /* Append the contents of Source to Target */
225 #endif
226
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.
231  */
232 {
233     if (Len < B->Len) {
234         B->Len = Len;
235     }
236 }
237 #else
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.
241  */
242 #endif
243
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
248  * bytes.
249  */
250
251
252
253 /* End of strbuf.h */
254
255 #endif
256
257
258