buf.c revision 1.51 1 1.51 rillig /* $NetBSD: buf.c,v 1.51 2021/01/30 21:18:14 rillig Exp $ */
2 1.6 christos
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 1.14 agc * All rights reserved.
6 1.14 agc *
7 1.14 agc * This code is derived from software contributed to Berkeley by
8 1.14 agc * Adam de Boor.
9 1.14 agc *
10 1.14 agc * Redistribution and use in source and binary forms, with or without
11 1.14 agc * modification, are permitted provided that the following conditions
12 1.14 agc * are met:
13 1.14 agc * 1. Redistributions of source code must retain the above copyright
14 1.14 agc * notice, this list of conditions and the following disclaimer.
15 1.14 agc * 2. Redistributions in binary form must reproduce the above copyright
16 1.14 agc * notice, this list of conditions and the following disclaimer in the
17 1.14 agc * documentation and/or other materials provided with the distribution.
18 1.14 agc * 3. Neither the name of the University nor the names of its contributors
19 1.14 agc * may be used to endorse or promote products derived from this software
20 1.14 agc * without specific prior written permission.
21 1.14 agc *
22 1.14 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.14 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.14 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.14 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.14 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.14 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.14 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.14 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.14 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.14 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.14 agc * SUCH DAMAGE.
33 1.14 agc */
34 1.14 agc
35 1.14 agc /*
36 1.1 cgd * Copyright (c) 1988, 1989 by Adam de Boor
37 1.1 cgd * Copyright (c) 1989 by Berkeley Softworks
38 1.1 cgd * All rights reserved.
39 1.1 cgd *
40 1.1 cgd * This code is derived from software contributed to Berkeley by
41 1.1 cgd * Adam de Boor.
42 1.1 cgd *
43 1.1 cgd * Redistribution and use in source and binary forms, with or without
44 1.1 cgd * modification, are permitted provided that the following conditions
45 1.1 cgd * are met:
46 1.1 cgd * 1. Redistributions of source code must retain the above copyright
47 1.1 cgd * notice, this list of conditions and the following disclaimer.
48 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
49 1.1 cgd * notice, this list of conditions and the following disclaimer in the
50 1.1 cgd * documentation and/or other materials provided with the distribution.
51 1.1 cgd * 3. All advertising materials mentioning features or use of this software
52 1.1 cgd * must display the following acknowledgement:
53 1.1 cgd * This product includes software developed by the University of
54 1.1 cgd * California, Berkeley and its contributors.
55 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
56 1.1 cgd * may be used to endorse or promote products derived from this software
57 1.1 cgd * without specific prior written permission.
58 1.1 cgd *
59 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 1.1 cgd * SUCH DAMAGE.
70 1.1 cgd */
71 1.1 cgd
72 1.46 rillig /* Automatically-expanding null-terminated character buffers. */
73 1.1 cgd
74 1.27 rillig #include <limits.h>
75 1.27 rillig #include "make.h"
76 1.1 cgd
77 1.38 rillig /* "@(#)buf.c 8.1 (Berkeley) 6/6/93" */
78 1.51 rillig MAKE_RCSID("$NetBSD: buf.c,v 1.51 2021/01/30 21:18:14 rillig Exp $");
79 1.38 rillig
80 1.46 rillig /* Make space in the buffer for adding at least 16 more bytes. */
81 1.1 cgd void
82 1.46 rillig Buf_Expand(Buffer *buf)
83 1.1 cgd {
84 1.45 rillig buf->cap += buf->cap > 16 ? buf->cap : 16;
85 1.45 rillig buf->data = bmake_realloc(buf->data, buf->cap);
86 1.1 cgd }
87 1.24 dsl
88 1.41 rillig /* Add the bytes to the buffer. */
89 1.1 cgd void
90 1.40 rillig Buf_AddBytes(Buffer *buf, const char *bytes, size_t bytes_len)
91 1.1 cgd {
92 1.45 rillig size_t old_len = buf->len;
93 1.45 rillig char *end;
94 1.1 cgd
95 1.51 rillig if (old_len + bytes_len >= buf->cap) {
96 1.45 rillig size_t minIncr = bytes_len + 16;
97 1.45 rillig buf->cap += buf->cap > minIncr ? buf->cap : minIncr;
98 1.45 rillig buf->data = bmake_realloc(buf->data, buf->cap);
99 1.45 rillig }
100 1.45 rillig
101 1.45 rillig end = buf->data + old_len;
102 1.45 rillig buf->len = old_len + bytes_len;
103 1.45 rillig memcpy(end, bytes, bytes_len);
104 1.45 rillig end[bytes_len] = '\0';
105 1.1 cgd }
106 1.24 dsl
107 1.29 rillig /* Add the bytes between start and end to the buffer. */
108 1.28 rillig void
109 1.40 rillig Buf_AddBytesBetween(Buffer *buf, const char *start, const char *end)
110 1.28 rillig {
111 1.45 rillig Buf_AddBytes(buf, start, (size_t)(end - start));
112 1.28 rillig }
113 1.28 rillig
114 1.41 rillig /* Add the string to the buffer. */
115 1.28 rillig void
116 1.40 rillig Buf_AddStr(Buffer *buf, const char *str)
117 1.28 rillig {
118 1.45 rillig Buf_AddBytes(buf, str, strlen(str));
119 1.28 rillig }
120 1.28 rillig
121 1.41 rillig /* Add the number to the buffer. */
122 1.27 rillig void
123 1.40 rillig Buf_AddInt(Buffer *buf, int n)
124 1.27 rillig {
125 1.45 rillig enum {
126 1.45 rillig bits = sizeof(int) * CHAR_BIT,
127 1.45 rillig max_octal_digits = (bits + 2) / 3,
128 1.45 rillig max_decimal_digits = /* at most */ max_octal_digits,
129 1.45 rillig max_sign_chars = 1,
130 1.45 rillig str_size = max_sign_chars + max_decimal_digits + 1
131 1.45 rillig };
132 1.45 rillig char str[str_size];
133 1.27 rillig
134 1.45 rillig size_t len = (size_t)snprintf(str, sizeof str, "%d", n);
135 1.45 rillig Buf_AddBytes(buf, str, len);
136 1.27 rillig }
137 1.27 rillig
138 1.29 rillig /* Mark the buffer as empty, so it can be filled with data again. */
139 1.1 cgd void
140 1.40 rillig Buf_Empty(Buffer *buf)
141 1.1 cgd {
142 1.45 rillig buf->len = 0;
143 1.45 rillig buf->data[0] = '\0';
144 1.1 cgd }
145 1.24 dsl
146 1.44 rillig /* Initialize a buffer. */
147 1.24 dsl void
148 1.44 rillig Buf_InitSize(Buffer *buf, size_t cap)
149 1.1 cgd {
150 1.45 rillig buf->cap = cap;
151 1.45 rillig buf->len = 0;
152 1.45 rillig buf->data = bmake_malloc(cap);
153 1.45 rillig buf->data[0] = '\0';
154 1.24 dsl }
155 1.1 cgd
156 1.44 rillig void
157 1.44 rillig Buf_Init(Buffer *buf)
158 1.44 rillig {
159 1.45 rillig Buf_InitSize(buf, 256);
160 1.44 rillig }
161 1.44 rillig
162 1.47 rillig /*
163 1.48 rillig * Free the data from the buffer.
164 1.49 rillig * Leave the buffer itself in an indeterminate state.
165 1.48 rillig */
166 1.48 rillig void
167 1.48 rillig Buf_Done(Buffer *buf)
168 1.48 rillig {
169 1.48 rillig free(buf->data);
170 1.48 rillig
171 1.49 rillig #ifdef CLEANUP
172 1.48 rillig buf->cap = 0;
173 1.48 rillig buf->len = 0;
174 1.48 rillig buf->data = NULL;
175 1.49 rillig #endif
176 1.48 rillig }
177 1.48 rillig
178 1.48 rillig /*
179 1.48 rillig * Return the data from the buffer.
180 1.49 rillig * Leave the buffer itself in an indeterminate state.
181 1.47 rillig */
182 1.35 rillig char *
183 1.48 rillig Buf_DoneData(Buffer *buf)
184 1.1 cgd {
185 1.45 rillig char *data = buf->data;
186 1.45 rillig
187 1.49 rillig #ifdef CLEANUP
188 1.45 rillig buf->cap = 0;
189 1.45 rillig buf->len = 0;
190 1.45 rillig buf->data = NULL;
191 1.49 rillig #endif
192 1.24 dsl
193 1.45 rillig return data;
194 1.1 cgd }
195 1.25 sjg
196 1.25 sjg #ifndef BUF_COMPACT_LIMIT
197 1.45 rillig # define BUF_COMPACT_LIMIT 128 /* worthwhile saving */
198 1.25 sjg #endif
199 1.25 sjg
200 1.47 rillig /*
201 1.49 rillig * Return the data from the buffer.
202 1.49 rillig * Leave the buffer itself in an indeterminate state.
203 1.29 rillig *
204 1.29 rillig * If the buffer size is much greater than its content,
205 1.47 rillig * a new buffer will be allocated and the old one freed.
206 1.47 rillig */
207 1.35 rillig char *
208 1.48 rillig Buf_DoneDataCompact(Buffer *buf)
209 1.25 sjg {
210 1.25 sjg #if BUF_COMPACT_LIMIT > 0
211 1.45 rillig if (buf->cap - buf->len >= BUF_COMPACT_LIMIT) {
212 1.45 rillig /* We trust realloc to be smart */
213 1.45 rillig char *data = bmake_realloc(buf->data, buf->len + 1);
214 1.45 rillig data[buf->len] = '\0'; /* XXX: unnecessary */
215 1.48 rillig Buf_DoneData(buf);
216 1.45 rillig return data;
217 1.45 rillig }
218 1.25 sjg #endif
219 1.48 rillig return Buf_DoneData(buf);
220 1.25 sjg }
221