]> granicus.if.org Git - postgresql/blob - src/interfaces/libpq/pqexpbuffer.c
Update copyright for 2006. Update scripts.
[postgresql] / src / interfaces / libpq / pqexpbuffer.c
1 /*-------------------------------------------------------------------------
2  *
3  * pqexpbuffer.c
4  *
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().
8  *
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().
12  *
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.
16  *
17  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
18  * Portions Copyright (c) 1994, Regents of the University of California
19  *
20  * $PostgreSQL: pgsql/src/interfaces/libpq/pqexpbuffer.c,v 1.22 2006/03/05 15:59:10 momjian Exp $
21  *
22  *-------------------------------------------------------------------------
23  */
24
25 #include "postgres_fe.h"
26
27 #include <limits.h>
28
29 #include "pqexpbuffer.h"
30
31 #ifdef WIN32
32 #include "win32.h"
33 #endif
34
35 /*
36  * createPQExpBuffer
37  *
38  * Create an empty 'PQExpBufferData' & return a pointer to it.
39  */
40 PQExpBuffer
41 createPQExpBuffer(void)
42 {
43         PQExpBuffer res;
44
45         res = (PQExpBuffer) malloc(sizeof(PQExpBufferData));
46         if (res != NULL)
47                 initPQExpBuffer(res);
48
49         return res;
50 }
51
52 /*
53  * initPQExpBuffer
54  *
55  * Initialize a PQExpBufferData struct (with previously undefined contents)
56  * to describe an empty string.
57  */
58 void
59 initPQExpBuffer(PQExpBuffer str)
60 {
61         str->data = (char *) malloc(INITIAL_EXPBUFFER_SIZE);
62         if (str->data == NULL)
63         {
64                 str->maxlen = 0;
65                 str->len = 0;
66         }
67         else
68         {
69                 str->maxlen = INITIAL_EXPBUFFER_SIZE;
70                 str->len = 0;
71                 str->data[0] = '\0';
72         }
73 }
74
75 /*
76  * destroyPQExpBuffer(str);
77  *
78  *              free()s both the data buffer and the PQExpBufferData.
79  *              This is the inverse of createPQExpBuffer().
80  */
81 void
82 destroyPQExpBuffer(PQExpBuffer str)
83 {
84         if (str)
85         {
86                 termPQExpBuffer(str);
87                 free(str);
88         }
89 }
90
91 /*
92  * termPQExpBuffer(str)
93  *              free()s the data buffer but not the PQExpBufferData itself.
94  *              This is the inverse of initPQExpBuffer().
95  */
96 void
97 termPQExpBuffer(PQExpBuffer str)
98 {
99         if (str->data)
100         {
101                 free(str->data);
102                 str->data = NULL;
103         }
104         /* just for luck, make the buffer validly empty. */
105         str->maxlen = 0;
106         str->len = 0;
107 }
108
109 /*
110  * resetPQExpBuffer
111  *              Reset a PQExpBuffer to empty
112  */
113 void
114 resetPQExpBuffer(PQExpBuffer str)
115 {
116         if (str)
117         {
118                 str->len = 0;
119                 if (str->data)
120                         str->data[0] = '\0';
121         }
122 }
123
124 /*
125  * enlargePQExpBuffer
126  * Make sure there is enough space for 'needed' more bytes in the buffer
127  * ('needed' does not include the terminating null).
128  *
129  * Returns 1 if OK, 0 if failed to enlarge buffer.
130  */
131 int
132 enlargePQExpBuffer(PQExpBuffer str, size_t needed)
133 {
134         size_t          newlen;
135         char       *newdata;
136
137         /*
138          * Guard against ridiculous "needed" values, which can occur if we're fed
139          * bogus data.  Without this, we can get an overflow or infinite loop in
140          * the following.
141          */
142         if (needed >= ((size_t) INT_MAX - str->len))
143                 return 0;
144
145         needed += str->len + 1;         /* total space required now */
146
147         /* Because of the above test, we now have needed <= INT_MAX */
148
149         if (needed <= str->maxlen)
150                 return 1;                               /* got enough space already */
151
152         /*
153          * We don't want to allocate just a little more space with each append;
154          * for efficiency, double the buffer size each time it overflows.
155          * Actually, we might need to more than double it if 'needed' is big...
156          */
157         newlen = (str->maxlen > 0) ? (2 * str->maxlen) : 64;
158         while (needed > newlen)
159                 newlen = 2 * newlen;
160
161         /*
162          * Clamp to INT_MAX in case we went past it.  Note we are assuming here
163          * that INT_MAX <= UINT_MAX/2, else the above loop could overflow.      We
164          * will still have newlen >= needed.
165          */
166         if (newlen > (size_t) INT_MAX)
167                 newlen = (size_t) INT_MAX;
168
169         newdata = (char *) realloc(str->data, newlen);
170         if (newdata != NULL)
171         {
172                 str->data = newdata;
173                 str->maxlen = newlen;
174                 return 1;
175         }
176         return 0;
177 }
178
179 /*
180  * printfPQExpBuffer
181  * Format text data under the control of fmt (an sprintf-like format string)
182  * and insert it into str.      More space is allocated to str if necessary.
183  * This is a convenience routine that does the same thing as
184  * resetPQExpBuffer() followed by appendPQExpBuffer().
185  */
186 void
187 printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
188 {
189         va_list         args;
190         size_t          avail;
191         int                     nprinted;
192
193         resetPQExpBuffer(str);
194
195         for (;;)
196         {
197                 /*
198                  * Try to format the given string into the available space; but if
199                  * there's hardly any space, don't bother trying, just fall through to
200                  * enlarge the buffer first.
201                  */
202                 if (str->maxlen > str->len + 16)
203                 {
204                         avail = str->maxlen - str->len - 1;
205                         va_start(args, fmt);
206                         nprinted = vsnprintf(str->data + str->len, avail,
207                                                                  fmt, args);
208                         va_end(args);
209
210                         /*
211                          * Note: some versions of vsnprintf return the number of chars
212                          * actually stored, but at least one returns -1 on failure. Be
213                          * conservative about believing whether the print worked.
214                          */
215                         if (nprinted >= 0 && nprinted < (int) avail - 1)
216                         {
217                                 /* Success.  Note nprinted does not include trailing null. */
218                                 str->len += nprinted;
219                                 break;
220                         }
221                 }
222                 /* Double the buffer size and try again. */
223                 if (!enlargePQExpBuffer(str, str->maxlen))
224                         return;                         /* oops, out of memory */
225         }
226 }
227
228 /*
229  * appendPQExpBuffer
230  *
231  * Format text data under the control of fmt (an sprintf-like format string)
232  * and append it to whatever is already in str.  More space is allocated
233  * to str if necessary.  This is sort of like a combination of sprintf and
234  * strcat.
235  */
236 void
237 appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
238 {
239         va_list         args;
240         size_t          avail;
241         int                     nprinted;
242
243         for (;;)
244         {
245                 /*
246                  * Try to format the given string into the available space; but if
247                  * there's hardly any space, don't bother trying, just fall through to
248                  * enlarge the buffer first.
249                  */
250                 if (str->maxlen > str->len + 16)
251                 {
252                         avail = str->maxlen - str->len - 1;
253                         va_start(args, fmt);
254                         nprinted = vsnprintf(str->data + str->len, avail,
255                                                                  fmt, args);
256                         va_end(args);
257
258                         /*
259                          * Note: some versions of vsnprintf return the number of chars
260                          * actually stored, but at least one returns -1 on failure. Be
261                          * conservative about believing whether the print worked.
262                          */
263                         if (nprinted >= 0 && nprinted < (int) avail - 1)
264                         {
265                                 /* Success.  Note nprinted does not include trailing null. */
266                                 str->len += nprinted;
267                                 break;
268                         }
269                 }
270                 /* Double the buffer size and try again. */
271                 if (!enlargePQExpBuffer(str, str->maxlen))
272                         return;                         /* oops, out of memory */
273         }
274 }
275
276 /*
277  * appendPQExpBufferStr
278  * Append the given string to a PQExpBuffer, allocating more space
279  * if necessary.
280  */
281 void
282 appendPQExpBufferStr(PQExpBuffer str, const char *data)
283 {
284         appendBinaryPQExpBuffer(str, data, strlen(data));
285 }
286
287 /*
288  * appendPQExpBufferChar
289  * Append a single byte to str.
290  * Like appendPQExpBuffer(str, "%c", ch) but much faster.
291  */
292 void
293 appendPQExpBufferChar(PQExpBuffer str, char ch)
294 {
295         /* Make more room if needed */
296         if (!enlargePQExpBuffer(str, 1))
297                 return;
298
299         /* OK, append the character */
300         str->data[str->len] = ch;
301         str->len++;
302         str->data[str->len] = '\0';
303 }
304
305 /*
306  * appendBinaryPQExpBuffer
307  *
308  * Append arbitrary binary data to a PQExpBuffer, allocating more space
309  * if necessary.
310  */
311 void
312 appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
313 {
314         /* Make more room if needed */
315         if (!enlargePQExpBuffer(str, datalen))
316                 return;
317
318         /* OK, append the data */
319         memcpy(str->data + str->len, data, datalen);
320         str->len += datalen;
321
322         /*
323          * Keep a trailing null in place, even though it's probably useless for
324          * binary data...
325          */
326         str->data[str->len] = '\0';
327 }