]> git.sur5r.net Git - cc65/blob - src/common/strbuf.h
More string buffer work
[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 "inline.h"
46
47
48
49 /*****************************************************************************/
50 /*                                   Data                                    */
51 /*****************************************************************************/
52
53
54
55 typedef struct StrBuf StrBuf;
56 struct StrBuf {
57     unsigned    Allocated;
58     unsigned    Len;
59     char*       Buf;
60 };
61
62 /* Initializer for static string bufs */
63 #define STATIC_STRBUF_INITIALIZER       { 0, 0, 0 }
64
65
66
67 /*****************************************************************************/
68 /*                                   Code                                    */
69 /*****************************************************************************/
70
71
72
73 StrBuf* InitStrBuf (StrBuf* B);
74 /* Initialize a string buffer */
75
76 void DoneStrBuf (StrBuf* B);
77 /* Free the data of a string buffer (but not the struct itself) */
78
79 StrBuf* NewStrBuf (void);
80 /* Allocate, initialize and return a new StrBuf */
81
82 void FreeStrBuf (StrBuf* B);
83 /* Free a string buffer */
84
85 void SB_Realloc (StrBuf* B, unsigned NewSize);
86 /* Reallocate the string buffer space, make sure at least NewSize bytes are
87  * available. THIS IS NOT A USER CALLABLE FUNCTION!
88  */
89
90 #if defined(HAVE_INLINE)
91 INLINE unsigned SB_GetLen (StrBuf* B)
92 /* Return the length of the buffer contents */
93 {
94     return B->Len;
95 }
96 #else
97 #  define SB_GetLen(B)  (B)->Len
98 #endif
99
100 #if defined(HAVE_INLINE)
101 INLINE const char* SB_GetConstBuf (const StrBuf* B)
102 /* Return a buffer pointer */
103 {
104     return B->Buf;
105 }
106 #else
107 #  define SB_GetConstBuf(B)     (B)->Buf
108 #endif
109
110 #if defined(HAVE_INLINE)
111 INLINE char* SB_GetBuf (StrBuf* B)
112 /* Return a buffer pointer */
113 {
114     return B->Buf;
115 }
116 #else
117 #  define SB_GetBuf(B)     (B)->Buf
118 #endif
119
120 #if defined(HAVE_INLINE)
121 INLINE void SB_Clear (StrBuf* B)
122 /* Clear the string buffer (make it empty) */
123 {
124     B->Len = 0;
125 }
126 #else
127 #  define SB_Clear(B)   ((B)->Len = 0)
128 #endif
129
130 void SB_Terminate (StrBuf* B);
131 /* Zero terminate the given string buffer. NOTE: The terminating zero is not
132  * accounted for in B->Len, if you want that, you have to use AppendChar!
133  */
134
135 void SB_AppendChar (StrBuf* B, char C);
136 /* Append a character to a string buffer */
137
138 void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size);
139 /* Append a character buffer to the end of the string buffer */
140
141 #if defined(HAVE_INLINE)
142 INLINE void SB_AppendStr (StrBuf* B, const char* S)
143 /* Append a string to the end of the string buffer */
144 {
145     SB_AppendBuf (B, S, strlen (S));
146 }
147 #else
148 #  define SB_AppendStr(B, S)    SB_AppendBuf (B, S, strlen (S))
149 #endif
150
151 void SB_Copy (StrBuf* Target, const StrBuf* Source);
152 /* Copy Source to Target, discarding the old contents of Target */
153
154 #if defined(HAVE_INLINE)
155 INLINE void SB_Append (StrBuf* Target, const StrBuf* Source)
156 /* Append the contents of Source to Target */
157 {
158     SB_AppendBuf (Target, Source->Buf, Source->Len);
159 }
160 #else
161 #  define SB_Append(Target, Source)     SB_AppendBuf (Target, (Source)->Buf, (Source)->Len)
162 #endif
163
164 #if defined(HAVE_INLINE)
165 INLINE void SB_Cut (StrBuf* B, unsigned Len)
166 /* Cut the contents of B at the given length. If the current length of the
167  * buffer is smaller than Len, nothing will happen.
168  */
169 {
170     if (Len < B->Len) {
171         B->Len = Len;
172     }
173 }
174 #else
175 #  define SB_Cut(B, L)        if ((L) < (B)->Len) { (B)->Len = (L); }
176 #endif
177
178 void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len);
179 /* Copy a slice from Source into Target. The current contents of Target are
180  * destroyed. If Start is greater than the length of Source, or if Len
181  * characters aren't available, the result will be a buffer with less than Len
182  * bytes.
183  */
184
185
186
187 /* End of strbuf.h */
188
189 #endif
190
191
192