1 /*-------------------------------------------------------------------------
4 * Declarations/definitions for "PQExpBuffer" functions.
6 * PQExpBuffer provides an indefinitely-extensible string data type.
7 * It can be used to buffer either ordinary C strings (null-terminated text)
8 * or arbitrary binary data. All storage is allocated with malloc().
10 * This module is essentially the same as the backend's StringInfo data type,
11 * but it is intended for use in frontend libpq and client applications.
12 * Thus, it does not rely on palloc() nor elog().
14 * It does rely on vsnprintf(); if configure finds that libc doesn't provide
15 * a usable vsnprintf(), then a copy of our own implementation of it will
16 * be linked into libpq.
18 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
19 * Portions Copyright (c) 1994, Regents of the University of California
21 * $PostgreSQL: pgsql/src/interfaces/libpq/pqexpbuffer.h,v 1.21 2008/11/26 16:23:11 tgl Exp $
23 *-------------------------------------------------------------------------
28 /*-------------------------
29 * PQExpBufferData holds information about an extensible string.
30 * data is the current buffer for the string (allocated with malloc).
31 * len is the current string length. There is guaranteed to be
32 * a terminating '\0' at data[len], although this is not very
33 * useful when the string holds binary data rather than text.
34 * maxlen is the allocated size in bytes of 'data', i.e. the maximum
35 * string size (including the terminating '\0' char) that we can
36 * currently store in 'data' without having to reallocate
37 * more space. We must always have maxlen > len.
39 * An exception occurs if we failed to allocate enough memory for the string
40 * buffer. In that case data points to a statically allocated empty string,
41 * and len = maxlen = 0.
42 *-------------------------
44 typedef struct PQExpBufferData
51 typedef PQExpBufferData *PQExpBuffer;
53 /*------------------------
54 * Test for a broken (out of memory) PQExpBuffer.
55 * When a buffer is "broken", all operations except resetting or deleting it
57 *------------------------
59 #define PQExpBufferBroken(str) \
60 ((str) == NULL || (str)->maxlen == 0)
62 /*------------------------
63 * Initial size of the data buffer in a PQExpBuffer.
64 * NB: this must be large enough to hold error messages that might
65 * be returned by PQrequestCancel().
66 *------------------------
68 #define INITIAL_EXPBUFFER_SIZE 256
70 /*------------------------
71 * There are two ways to create a PQExpBuffer object initially:
73 * PQExpBuffer stringptr = createPQExpBuffer();
74 * Both the PQExpBufferData and the data buffer are malloc'd.
76 * PQExpBufferData string;
77 * initPQExpBuffer(&string);
78 * The data buffer is malloc'd but the PQExpBufferData is presupplied.
79 * This is appropriate if the PQExpBufferData is a field of another
81 *-------------------------
84 /*------------------------
86 * Create an empty 'PQExpBufferData' & return a pointer to it.
88 extern PQExpBuffer createPQExpBuffer(void);
90 /*------------------------
92 * Initialize a PQExpBufferData struct (with previously undefined contents)
93 * to describe an empty string.
95 extern void initPQExpBuffer(PQExpBuffer str);
97 /*------------------------
98 * To destroy a PQExpBuffer, use either:
100 * destroyPQExpBuffer(str);
101 * free()s both the data buffer and the PQExpBufferData.
102 * This is the inverse of createPQExpBuffer().
104 * termPQExpBuffer(str)
105 * free()s the data buffer but not the PQExpBufferData itself.
106 * This is the inverse of initPQExpBuffer().
108 * NOTE: some routines build up a string using PQExpBuffer, and then
109 * release the PQExpBufferData but return the data string itself to their
110 * caller. At that point the data string looks like a plain malloc'd
113 extern void destroyPQExpBuffer(PQExpBuffer str);
114 extern void termPQExpBuffer(PQExpBuffer str);
116 /*------------------------
118 * Reset a PQExpBuffer to empty
120 * Note: if possible, a "broken" PQExpBuffer is returned to normal.
122 extern void resetPQExpBuffer(PQExpBuffer str);
124 /*------------------------
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. (In the latter case
130 * the buffer is left in "broken" state.)
132 extern int enlargePQExpBuffer(PQExpBuffer str, size_t needed);
134 /*------------------------
136 * Format text data under the control of fmt (an sprintf-like format string)
137 * and insert it into str. More space is allocated to str if necessary.
138 * This is a convenience routine that does the same thing as
139 * resetPQExpBuffer() followed by appendPQExpBuffer().
142 printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
143 /* This extension allows gcc to check the format string */
144 __attribute__((format(printf, 2, 3)));
146 /*------------------------
148 * Format text data under the control of fmt (an sprintf-like format string)
149 * and append it to whatever is already in str. More space is allocated
150 * to str if necessary. This is sort of like a combination of sprintf and
154 appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
155 /* This extension allows gcc to check the format string */
156 __attribute__((format(printf, 2, 3)));
158 /*------------------------
159 * appendPQExpBufferStr
160 * Append the given string to a PQExpBuffer, allocating more space
163 extern void appendPQExpBufferStr(PQExpBuffer str, const char *data);
165 /*------------------------
166 * appendPQExpBufferChar
167 * Append a single byte to str.
168 * Like appendPQExpBuffer(str, "%c", ch) but much faster.
170 extern void appendPQExpBufferChar(PQExpBuffer str, char ch);
172 /*------------------------
173 * appendBinaryPQExpBuffer
174 * Append arbitrary binary data to a PQExpBuffer, allocating more space
177 extern void appendBinaryPQExpBuffer(PQExpBuffer str,
178 const char *data, size_t datalen);
180 #endif /* PQEXPBUFFER_H */