]> git.sur5r.net Git - cc65/blob - src/common/strbuf.c
f20b4cdc6cf0f793e56e8eb3be07fd3bed84cca8
[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 /*                                   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->Buf       = 0;
67     return B;
68 }
69
70
71
72 void DoneStrBuf (StrBuf* B)
73 /* Free the data of a string buffer (but not the struct itself) */
74 {
75     xfree (B->Buf);
76 }
77
78
79
80 StrBuf* NewStrBuf (void)
81 /* Allocate, initialize and return a new StrBuf */
82 {
83     /* Allocate a new string buffer */
84     StrBuf* B = xmalloc (sizeof (StrBuf));
85
86     /* Initialize the struct... */
87     InitStrBuf (B);
88
89     /* ...and return it */
90     return B;
91 }
92
93
94
95 void FreeStrBuf (StrBuf* B)
96 /* Free a string buffer */
97 {
98     DoneStrBuf (B);
99     xfree (B);
100 }
101
102
103
104 void SB_Realloc (StrBuf* B, unsigned NewSize)
105 /* Reallocate the string buffer space, make sure at least NewSize bytes are
106  * available.
107  */
108 {
109     /* Get the current size, use a minimum of 8 bytes */
110     unsigned NewAllocated = B->Allocated;
111     if (NewAllocated == 0) {
112         NewAllocated = 8;
113     }
114
115     /* Round up to the next power of two */
116     while (NewAllocated < NewSize) {
117         NewAllocated *= 2;
118     }
119
120     /* Reallocate the buffer */
121     B->Buf       = xrealloc (B->Buf, NewAllocated);
122     B->Allocated = NewAllocated;
123 }
124
125
126
127 void SB_Terminate (StrBuf* B)
128 /* Zero terminate the given string buffer. NOTE: The terminating zero is not
129  * accounted for in B->Len, if you want that, you have to use AppendChar!
130  */
131 {
132     unsigned NewLen = B->Len + 1;
133     if (NewLen > B->Allocated) {
134         SB_Realloc (B, NewLen);
135     }
136     B->Buf[B->Len] = '\0';
137 }
138
139
140
141 void SB_CopyBuf (StrBuf* Target, const char* Buf, unsigned Size)
142 /* Copy Buf to Target, discarding the old contents of Target */
143 {
144     if (Target->Allocated < Size) {
145         SB_Realloc (Target, Size);
146     }
147     memcpy (Target->Buf, Buf, Size);
148     Target->Len = Size;
149 }
150
151
152
153 void SB_AppendChar (StrBuf* B, char C)
154 /* Append a character to a string buffer */
155 {
156     unsigned NewLen = B->Len + 1;
157     if (NewLen > B->Allocated) {
158         SB_Realloc (B, NewLen);
159     }
160     B->Buf[B->Len] = C;
161     B->Len = NewLen;
162 }
163
164
165
166 void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size)
167 /* Append a character buffer to the end of the string buffer */
168 {
169     unsigned NewLen = B->Len + Size;
170     if (NewLen > B->Allocated) {
171         SB_Realloc (B, NewLen);
172     }
173     memcpy (B->Buf + B->Len, S, Size);
174     B->Len = NewLen;
175 }
176
177
178
179 void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len)
180 /* Copy a slice from Source into Target. The current contents of Target are
181  * destroyed. If Start is greater than the length of Source, or if Len
182  * characters aren't available, the result will be a buffer with less than Len
183  * bytes.
184  */
185 {
186     /* Calculate the length of the resulting buffer */
187     if (Start >= Source->Len) {
188         /* Target will be empty */
189         SB_Clear (Target);
190         return;
191     } else if (Start + Len > Source->Len) {
192         Len = (Start + Len) - Source->Len;
193     }
194
195     /* Make sure we have enough room in the target string buffer */
196     if (Len > Target->Allocated) {
197         SB_Realloc (Target, Len);
198     }
199
200     /* Copy the slice */
201     memcpy (Target->Buf, Source->Buf + Start, Len);
202     Target->Len = Len;
203 }
204
205
206