]> git.sur5r.net Git - cc65/blob - src/common/strbuf.c
c763dbba14d1e21a6b68a41b2d90bdb1b99d1685
[cc65] / src / common / strbuf.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 strbuf.c                                  */
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 #include <string.h>
37
38 /* common */
39 #include "xmalloc.h"
40 #include "strbuf.h"
41
42
43
44 /*****************************************************************************/
45 /*                                  Helpers                                  */
46 /*****************************************************************************/
47
48
49
50 static void SB_Realloc (StrBuf* B, unsigned NewSize)
51 /* Reallocate the string buffer space, make sure at least NewSize bytes are
52  * available.
53  */
54 {
55     /* Get the current size, use a minimum of 8 bytes */
56     unsigned NewAllocated = B->Allocated;
57     if (NewAllocated == 0) {
58         NewAllocated = 8;
59     }
60
61     /* Round up to the next power of two */
62     while (NewAllocated < NewSize) {
63         NewAllocated *= 2;
64     }
65
66     /* Reallocate the buffer */
67     B->Buf       = xrealloc (B->Buf, NewAllocated);
68     B->Allocated = NewAllocated;
69 }
70
71
72
73 /*****************************************************************************/
74 /*                                   Code                                    */
75 /*****************************************************************************/
76
77
78
79 StrBuf* InitStrBuf (StrBuf* B)
80 /* Initialize a string buffer */
81 {
82     B->Allocated = 0;
83     B->Len       = 0;
84     B->Buf       = 0;
85     return B;
86 }
87
88
89
90 void DoneStrBuf (StrBuf* B)
91 /* Free the data of a string buffer (but not the struct itself) */
92 {
93     xfree (B->Buf);
94     B->Allocated = 0;
95     B->Len       = 0;
96     B->Buf       = 0;
97 }
98
99
100
101 StrBuf* NewStrBuf (void)
102 /* Allocate, initialize and return a new StrBuf */
103 {
104     /* Allocate a new string buffer */
105     StrBuf* B = xmalloc (sizeof (StrBuf));
106
107     /* Initialize the struct... */
108     InitStrBuf (B);
109
110     /* ...and return it */
111     return B;
112 }
113
114
115
116 void FreeStrBuf (StrBuf* B)
117 /* Free a string buffer */
118 {
119     /* Don't use Done here, since we won't use the struct later */
120     xfree (B->Buf);
121     xfree (B);
122 }
123
124
125
126 void SB_Terminate (StrBuf* B)
127 /* Zero terminate the given string buffer. NOTE: The terminating zero is not
128  * accounted for in B->Len, if you want that, you have to use AppendChar!
129  */
130 {
131     unsigned NewLen = B->Len + 1;
132     if (NewLen > B->Allocated) {
133         SB_Realloc (B, NewLen);
134     }
135     B->Buf[B->Len] = '\0';
136 }
137
138
139
140 void SB_AppendChar (StrBuf* B, char C)
141 /* Append a character to a string buffer */
142 {
143     unsigned NewLen = B->Len + 1;
144     if (NewLen > B->Allocated) {
145         SB_Realloc (B, NewLen);
146     }
147     B->Buf[B->Len] = C;
148     B->Len = NewLen;
149 }
150
151
152
153 void SB_AppendStr (StrBuf* B, const char* S)
154 /* Append a string to the end of the string buffer */
155 {
156     SB_AppendBuf (B, S, strlen (S));
157 }
158
159
160
161 void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size)
162 /* Append a character buffer to the end of the string buffer */
163 {
164     unsigned NewLen = B->Len + Size;
165     if (NewLen > B->Allocated) {
166         SB_Realloc (B, NewLen);
167     }
168     memcpy (B->Buf + B->Len, S, Size);
169     B->Len = NewLen;
170 }
171
172
173
174 void SB_Copy (StrBuf* Target, const StrBuf* Source)
175 /* Copy Source to Target, discarding the old contents of Target */
176 {
177     if (Target->Allocated < Source->Allocated) {
178         SB_Realloc (Target, Source->Allocated);
179     }
180     memcpy (Target->Buf, Source->Buf, Source->Len);
181     Target->Len = Source->Len;
182 }
183
184
185
186 void SB_Append (StrBuf* Target, const StrBuf* Source)
187 /* Append the contents of Source to Target */
188 {
189     SB_AppendBuf (Target, Source->Buf, Source->Len);
190 }
191
192
193