1 /*-------------------------------------------------------------------------
5 * PQExpBuffer provides an indefinitely-extensible string data type.
6 * It can be used to buffer either ordinary C strings (null-terminated text)
7 * or arbitrary binary data. All storage is allocated with malloc().
9 * This module is essentially the same as the backend's StringInfo data type,
10 * but it is intended for use in frontend libpq and client applications.
11 * Thus, it does not rely on palloc() nor elog().
13 * It does rely on vsnprintf(); if configure finds that libc doesn't provide
14 * a usable vsnprintf(), then a copy of our own implementation of it will
15 * be linked into libpq.
17 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
18 * Portions Copyright (c) 1994, Regents of the University of California
20 * $PostgreSQL: pgsql/src/interfaces/libpq/pqexpbuffer.c,v 1.20 2004/12/31 22:03:50 pgsql Exp $
22 *-------------------------------------------------------------------------
25 #include "postgres_fe.h"
29 #include "pqexpbuffer.h"
38 * Create an empty 'PQExpBufferData' & return a pointer to it.
41 createPQExpBuffer(void)
45 res = (PQExpBuffer) malloc(sizeof(PQExpBufferData));
55 * Initialize a PQExpBufferData struct (with previously undefined contents)
56 * to describe an empty string.
59 initPQExpBuffer(PQExpBuffer str)
61 str->data = (char *) malloc(INITIAL_EXPBUFFER_SIZE);
62 if (str->data == NULL)
69 str->maxlen = INITIAL_EXPBUFFER_SIZE;
76 * destroyPQExpBuffer(str);
78 * free()s both the data buffer and the PQExpBufferData.
79 * This is the inverse of createPQExpBuffer().
82 destroyPQExpBuffer(PQExpBuffer str)
92 * termPQExpBuffer(str)
93 * free()s the data buffer but not the PQExpBufferData itself.
94 * This is the inverse of initPQExpBuffer().
97 termPQExpBuffer(PQExpBuffer str)
104 /* just for luck, make the buffer validly empty. */
111 * Reset a PQExpBuffer to empty
114 resetPQExpBuffer(PQExpBuffer str)
126 * Make sure there is enough space for 'needed' more bytes in the buffer
127 * ('needed' does not include the terminating null).
129 * Returns 1 if OK, 0 if failed to enlarge buffer.
132 enlargePQExpBuffer(PQExpBuffer str, size_t needed)
138 * Guard against ridiculous "needed" values, which can occur if we're
139 * fed bogus data. Without this, we can get an overflow or infinite
140 * loop in the following.
142 if (needed >= ((size_t) INT_MAX - str->len))
145 needed += str->len + 1; /* total space required now */
147 /* Because of the above test, we now have needed <= INT_MAX */
149 if (needed <= str->maxlen)
150 return 1; /* got enough space already */
153 * We don't want to allocate just a little more space with each
154 * append; for efficiency, double the buffer size each time it
155 * overflows. Actually, we might need to more than double it if
158 newlen = (str->maxlen > 0) ? (2 * str->maxlen) : 64;
159 while (needed > newlen)
163 * Clamp to INT_MAX in case we went past it. Note we are assuming
164 * here that INT_MAX <= UINT_MAX/2, else the above loop could
165 * overflow. We will still have newlen >= needed.
167 if (newlen > (size_t) INT_MAX)
168 newlen = (size_t) INT_MAX;
170 newdata = (char *) realloc(str->data, newlen);
174 str->maxlen = newlen;
182 * Format text data under the control of fmt (an sprintf-like format string)
183 * and insert it into str. More space is allocated to str if necessary.
184 * This is a convenience routine that does the same thing as
185 * resetPQExpBuffer() followed by appendPQExpBuffer().
188 printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
194 resetPQExpBuffer(str);
199 * Try to format the given string into the available space; but if
200 * there's hardly any space, don't bother trying, just fall
201 * through to enlarge the buffer first.
203 if (str->maxlen > str->len + 16)
205 avail = str->maxlen - str->len - 1;
207 nprinted = vsnprintf(str->data + str->len, avail,
212 * Note: some versions of vsnprintf return the number of chars
213 * actually stored, but at least one returns -1 on failure. Be
214 * conservative about believing whether the print worked.
216 if (nprinted >= 0 && nprinted < (int) avail - 1)
218 /* Success. Note nprinted does not include trailing null. */
219 str->len += nprinted;
223 /* Double the buffer size and try again. */
224 if (!enlargePQExpBuffer(str, str->maxlen))
225 return; /* oops, out of memory */
232 * Format text data under the control of fmt (an sprintf-like format string)
233 * and append it to whatever is already in str. More space is allocated
234 * to str if necessary. This is sort of like a combination of sprintf and
238 appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
247 * Try to format the given string into the available space; but if
248 * there's hardly any space, don't bother trying, just fall
249 * through to enlarge the buffer first.
251 if (str->maxlen > str->len + 16)
253 avail = str->maxlen - str->len - 1;
255 nprinted = vsnprintf(str->data + str->len, avail,
260 * Note: some versions of vsnprintf return the number of chars
261 * actually stored, but at least one returns -1 on failure. Be
262 * conservative about believing whether the print worked.
264 if (nprinted >= 0 && nprinted < (int) avail - 1)
266 /* Success. Note nprinted does not include trailing null. */
267 str->len += nprinted;
271 /* Double the buffer size and try again. */
272 if (!enlargePQExpBuffer(str, str->maxlen))
273 return; /* oops, out of memory */
278 * appendPQExpBufferStr
279 * Append the given string to a PQExpBuffer, allocating more space
283 appendPQExpBufferStr(PQExpBuffer str, const char *data)
285 appendBinaryPQExpBuffer(str, data, strlen(data));
289 * appendPQExpBufferChar
290 * Append a single byte to str.
291 * Like appendPQExpBuffer(str, "%c", ch) but much faster.
294 appendPQExpBufferChar(PQExpBuffer str, char ch)
296 /* Make more room if needed */
297 if (!enlargePQExpBuffer(str, 1))
300 /* OK, append the character */
301 str->data[str->len] = ch;
303 str->data[str->len] = '\0';
307 * appendBinaryPQExpBuffer
309 * Append arbitrary binary data to a PQExpBuffer, allocating more space
313 appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
315 /* Make more room if needed */
316 if (!enlargePQExpBuffer(str, datalen))
319 /* OK, append the data */
320 memcpy(str->data + str->len, data, datalen);
324 * Keep a trailing null in place, even though it's probably useless
327 str->data[str->len] = '\0';