]> git.sur5r.net Git - cc65/blob - src/common/strbuf.c
675314dccc4a6da7c2386889297de00318bd24ed
[cc65] / src / common / strbuf.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 strbuf.c                                  */
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 #include <string.h>
37
38 /* common */
39 #include "xmalloc.h"
40 #include "strbuf.h"
41
42
43
44 /*****************************************************************************/
45 /*                                   Data                                    */
46 /*****************************************************************************/
47
48
49
50 /* An empty string buf */
51 const StrBuf EmptyStrBuf = STATIC_STRBUF_INITIALIZER;
52
53
54
55 /*****************************************************************************/
56 /*                                   Code                                    */
57 /*****************************************************************************/
58
59
60
61 StrBuf* InitStrBuf (StrBuf* B)
62 /* Initialize a string buffer */
63 {
64     B->Allocated = 0;
65     B->Len       = 0;
66     B->Index     = 0;
67     B->Buf       = 0;
68     return B;
69 }
70
71
72
73 void DoneStrBuf (StrBuf* B)
74 /* Free the data of a string buffer (but not the struct itself) */
75 {
76     xfree (B->Buf);
77 }
78
79
80
81 StrBuf* NewStrBuf (void)
82 /* Allocate, initialize and return a new StrBuf */
83 {
84     /* Allocate a new string buffer */
85     StrBuf* B = xmalloc (sizeof (StrBuf));
86
87     /* Initialize the struct... */
88     InitStrBuf (B);
89
90     /* ...and return it */
91     return B;
92 }
93
94
95
96 void FreeStrBuf (StrBuf* B)
97 /* Free a string buffer */
98 {
99     DoneStrBuf (B);
100     xfree (B);
101 }
102
103
104
105 void SB_Realloc (StrBuf* B, unsigned NewSize)
106 /* Reallocate the string buffer space, make sure at least NewSize bytes are
107  * available.
108  */
109 {
110     /* Get the current size, use a minimum of 8 bytes */
111     unsigned NewAllocated = B->Allocated;
112     if (NewAllocated == 0) {
113         NewAllocated = 8;
114     }
115
116     /* Round up to the next power of two */
117     while (NewAllocated < NewSize) {
118         NewAllocated *= 2;
119     }
120
121     /* Reallocate the buffer */
122     B->Buf       = xrealloc (B->Buf, NewAllocated);
123     B->Allocated = NewAllocated;
124 }
125
126
127
128 #if !defined(HAVE_INLINE)
129 char SB_At (const StrBuf* B, unsigned Index)
130 /* Get a character from the buffer */
131 {
132     PRECONDITION (Index < B->Len);
133     return B->Buf[Index];
134 }
135 #endif
136
137
138
139 void SB_Terminate (StrBuf* B)
140 /* Zero terminate the given string buffer. NOTE: The terminating zero is not
141  * accounted for in B->Len, if you want that, you have to use AppendChar!
142  */
143 {
144     unsigned NewLen = B->Len + 1;
145     if (NewLen > B->Allocated) {
146         SB_Realloc (B, NewLen);
147     }
148     B->Buf[B->Len] = '\0';
149 }
150
151
152
153 void SB_CopyBuf (StrBuf* Target, const char* Buf, unsigned Size)
154 /* Copy Buf to Target, discarding the old contents of Target */
155 {
156     if (Target->Allocated < Size) {
157         SB_Realloc (Target, Size);
158     }
159     memcpy (Target->Buf, Buf, Size);
160     Target->Len = Size;
161 }
162
163
164
165 #if !defined(HAVE_INLINE)
166 void SB_CopyStr (StrBuf* Target, const char* S)
167 /* Copy S to Target, discarding the old contents of Target */
168 {
169     SB_CopyBuf (Target, S, strlen (S));
170 }
171 #endif
172
173
174
175 #if !defined(HAVE_INLINE)
176 void SB_Copy (StrBuf* Target, const StrBuf* Source)
177 /* Copy Source to Target, discarding the old contents of Target */
178 {
179     SB_CopyBuf (Target, Source->Buf, Source->Len);
180     Target->Index = Source->Index;
181 }
182 #endif
183
184
185
186 void SB_AppendChar (StrBuf* B, char C)
187 /* Append a character to a string buffer */
188 {
189     unsigned NewLen = B->Len + 1;
190     if (NewLen > B->Allocated) {
191         SB_Realloc (B, NewLen);
192     }
193     B->Buf[B->Len] = C;
194     B->Len = NewLen;
195 }
196
197
198
199 void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size)
200 /* Append a character buffer to the end of the string buffer */
201 {
202     unsigned NewLen = B->Len + Size;
203     if (NewLen > B->Allocated) {
204         SB_Realloc (B, NewLen);
205     }
206     memcpy (B->Buf + B->Len, S, Size);
207     B->Len = NewLen;
208 }
209
210
211
212 #if !defined(HAVE_INLINE)
213 void SB_AppendStr (StrBuf* B, const char* S)
214 /* Append a string to the end of the string buffer */
215 {
216     SB_AppendBuf (B, S, strlen (S));
217 }
218 #endif
219
220
221
222 #if !defined(HAVE_INLINE)
223 void SB_Append (StrBuf* Target, const StrBuf* Source)
224 /* Append the contents of Source to Target */
225 {
226     SB_AppendBuf (Target, Source->Buf, Source->Len);
227 }
228 #endif
229
230
231
232 #if !defined(HAVE_INLINE)
233 void SB_Cut (StrBuf* B, unsigned Len)
234 /* Cut the contents of B at the given length. If the current length of the
235  * buffer is smaller than Len, nothing will happen.
236  */
237 {
238     if (Len < B->Len) {
239         B->Len = Len;
240     }
241 }
242 #endif
243
244
245
246 void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len)
247 /* Copy a slice from Source into Target. The current contents of Target are
248  * destroyed. If Start is greater than the length of Source, or if Len
249  * characters aren't available, the result will be a buffer with less than Len
250  * bytes.
251  */
252 {
253     /* Calculate the length of the resulting buffer */
254     if (Start >= Source->Len) {
255         /* Target will be empty */
256         SB_Clear (Target);
257         return;
258     } else if (Start + Len > Source->Len) {
259         Len = (Start + Len) - Source->Len;
260     }
261
262     /* Make sure we have enough room in the target string buffer */
263     if (Len > Target->Allocated) {
264         SB_Realloc (Target, Len);
265     }
266
267     /* Copy the slice */
268     memcpy (Target->Buf, Source->Buf + Start, Len);
269     Target->Len = Len;
270 }
271
272
273