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-2003, 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.16 2003/11/29 19:52:12 pgsql Exp $
22 *-------------------------------------------------------------------------
25 #include "postgres_fe.h"
27 #include "pqexpbuffer.h"
36 * Create an empty 'PQExpBufferData' & return a pointer to it.
39 createPQExpBuffer(void)
43 res = (PQExpBuffer) malloc(sizeof(PQExpBufferData));
53 * Initialize a PQExpBufferData struct (with previously undefined contents)
54 * to describe an empty string.
57 initPQExpBuffer(PQExpBuffer str)
59 str->data = (char *) malloc(INITIAL_EXPBUFFER_SIZE);
60 if (str->data == NULL)
67 str->maxlen = INITIAL_EXPBUFFER_SIZE;
74 * destroyPQExpBuffer(str);
76 * free()s both the data buffer and the PQExpBufferData.
77 * This is the inverse of createPQExpBuffer().
80 destroyPQExpBuffer(PQExpBuffer str)
90 * termPQExpBuffer(str)
91 * free()s the data buffer but not the PQExpBufferData itself.
92 * This is the inverse of initPQExpBuffer().
95 termPQExpBuffer(PQExpBuffer str)
102 /* just for luck, make the buffer validly empty. */
109 * Reset a PQExpBuffer to empty
112 resetPQExpBuffer(PQExpBuffer str)
124 * Make sure there is enough space for 'needed' more bytes in the buffer
125 * ('needed' does not include the terminating null).
127 * Returns 1 if OK, 0 if failed to enlarge buffer.
130 enlargePQExpBuffer(PQExpBuffer str, size_t needed)
135 needed += str->len + 1; /* total space required now */
136 if (needed <= str->maxlen)
137 return 1; /* got enough space already */
140 * We don't want to allocate just a little more space with each
141 * append; for efficiency, double the buffer size each time it
142 * overflows. Actually, we might need to more than double it if
145 newlen = (str->maxlen > 0) ? (2 * str->maxlen) : 64;
146 while (needed > newlen)
149 newdata = (char *) realloc(str->data, newlen);
153 str->maxlen = newlen;
161 * Format text data under the control of fmt (an sprintf-like format string)
162 * and insert it into str. More space is allocated to str if necessary.
163 * This is a convenience routine that does the same thing as
164 * resetPQExpBuffer() followed by appendPQExpBuffer().
167 printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
173 resetPQExpBuffer(str);
178 * Try to format the given string into the available space; but if
179 * there's hardly any space, don't bother trying, just fall
180 * through to enlarge the buffer first.
182 if (str->maxlen > str->len + 16)
184 avail = str->maxlen - str->len - 1;
186 nprinted = vsnprintf(str->data + str->len, avail,
191 * Note: some versions of vsnprintf return the number of chars
192 * actually stored, but at least one returns -1 on failure. Be
193 * conservative about believing whether the print worked.
195 if (nprinted >= 0 && nprinted < (int) avail - 1)
197 /* Success. Note nprinted does not include trailing null. */
198 str->len += nprinted;
202 /* Double the buffer size and try again. */
203 if (!enlargePQExpBuffer(str, str->maxlen))
204 return; /* oops, out of memory */
211 * Format text data under the control of fmt (an sprintf-like format string)
212 * and append it to whatever is already in str. More space is allocated
213 * to str if necessary. This is sort of like a combination of sprintf and
217 appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
226 * Try to format the given string into the available space; but if
227 * there's hardly any space, don't bother trying, just fall
228 * through to enlarge the buffer first.
230 if (str->maxlen > str->len + 16)
232 avail = str->maxlen - str->len - 1;
234 nprinted = vsnprintf(str->data + str->len, avail,
239 * Note: some versions of vsnprintf return the number of chars
240 * actually stored, but at least one returns -1 on failure. Be
241 * conservative about believing whether the print worked.
243 if (nprinted >= 0 && nprinted < (int) avail - 1)
245 /* Success. Note nprinted does not include trailing null. */
246 str->len += nprinted;
250 /* Double the buffer size and try again. */
251 if (!enlargePQExpBuffer(str, str->maxlen))
252 return; /* oops, out of memory */
257 * appendPQExpBufferStr
258 * Append the given string to a PQExpBuffer, allocating more space
262 appendPQExpBufferStr(PQExpBuffer str, const char *data)
264 appendBinaryPQExpBuffer(str, data, strlen(data));
268 * appendPQExpBufferChar
269 * Append a single byte to str.
270 * Like appendPQExpBuffer(str, "%c", ch) but much faster.
273 appendPQExpBufferChar(PQExpBuffer str, char ch)
275 /* Make more room if needed */
276 if (!enlargePQExpBuffer(str, 1))
279 /* OK, append the character */
280 str->data[str->len] = ch;
282 str->data[str->len] = '\0';
286 * appendBinaryPQExpBuffer
288 * Append arbitrary binary data to a PQExpBuffer, allocating more space
292 appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
294 /* Make more room if needed */
295 if (!enlargePQExpBuffer(str, datalen))
298 /* OK, append the data */
299 memcpy(str->data + str->len, data, datalen);
303 * Keep a trailing null in place, even though it's probably useless
306 str->data[str->len] = '\0';