]> granicus.if.org Git - postgresql/blob - src/interfaces/libpq/pqexpbuffer.h
Tweak new PQExpBufferBroken macro to suppress warnings from pickier
[postgresql] / src / interfaces / libpq / pqexpbuffer.h
1 /*-------------------------------------------------------------------------
2  *
3  * pqexpbuffer.h
4  *        Declarations/definitions for "PQExpBuffer" functions.
5  *
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().
9  *
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().
13  *
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.
17  *
18  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
19  * Portions Copyright (c) 1994, Regents of the University of California
20  *
21  * $PostgreSQL: pgsql/src/interfaces/libpq/pqexpbuffer.h,v 1.21 2008/11/26 16:23:11 tgl Exp $
22  *
23  *-------------------------------------------------------------------------
24  */
25 #ifndef PQEXPBUFFER_H
26 #define PQEXPBUFFER_H
27
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.
38  *
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  *-------------------------
43  */
44 typedef struct PQExpBufferData
45 {
46         char       *data;
47         size_t          len;
48         size_t          maxlen;
49 } PQExpBufferData;
50
51 typedef PQExpBufferData *PQExpBuffer;
52
53 /*------------------------
54  * Test for a broken (out of memory) PQExpBuffer.
55  * When a buffer is "broken", all operations except resetting or deleting it
56  * are no-ops.
57  *------------------------
58  */
59 #define PQExpBufferBroken(str)  \
60         ((str) == NULL || (str)->maxlen == 0)
61
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  *------------------------
67  */
68 #define INITIAL_EXPBUFFER_SIZE  256
69
70 /*------------------------
71  * There are two ways to create a PQExpBuffer object initially:
72  *
73  * PQExpBuffer stringptr = createPQExpBuffer();
74  *              Both the PQExpBufferData and the data buffer are malloc'd.
75  *
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
80  *              struct.
81  *-------------------------
82  */
83
84 /*------------------------
85  * createPQExpBuffer
86  * Create an empty 'PQExpBufferData' & return a pointer to it.
87  */
88 extern PQExpBuffer createPQExpBuffer(void);
89
90 /*------------------------
91  * initPQExpBuffer
92  * Initialize a PQExpBufferData struct (with previously undefined contents)
93  * to describe an empty string.
94  */
95 extern void initPQExpBuffer(PQExpBuffer str);
96
97 /*------------------------
98  * To destroy a PQExpBuffer, use either:
99  *
100  * destroyPQExpBuffer(str);
101  *              free()s both the data buffer and the PQExpBufferData.
102  *              This is the inverse of createPQExpBuffer().
103  *
104  * termPQExpBuffer(str)
105  *              free()s the data buffer but not the PQExpBufferData itself.
106  *              This is the inverse of initPQExpBuffer().
107  *
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
111  * string.
112  */
113 extern void destroyPQExpBuffer(PQExpBuffer str);
114 extern void termPQExpBuffer(PQExpBuffer str);
115
116 /*------------------------
117  * resetPQExpBuffer
118  *              Reset a PQExpBuffer to empty
119  *
120  * Note: if possible, a "broken" PQExpBuffer is returned to normal.
121  */
122 extern void resetPQExpBuffer(PQExpBuffer str);
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.  (In the latter case
130  * the buffer is left in "broken" state.)
131  */
132 extern int      enlargePQExpBuffer(PQExpBuffer str, size_t needed);
133
134 /*------------------------
135  * printfPQExpBuffer
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().
140  */
141 extern void
142 printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
143 /* This extension allows gcc to check the format string */
144 __attribute__((format(printf, 2, 3)));
145
146 /*------------------------
147  * appendPQExpBuffer
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
151  * strcat.
152  */
153 extern void
154 appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
155 /* This extension allows gcc to check the format string */
156 __attribute__((format(printf, 2, 3)));
157
158 /*------------------------
159  * appendPQExpBufferStr
160  * Append the given string to a PQExpBuffer, allocating more space
161  * if necessary.
162  */
163 extern void appendPQExpBufferStr(PQExpBuffer str, const char *data);
164
165 /*------------------------
166  * appendPQExpBufferChar
167  * Append a single byte to str.
168  * Like appendPQExpBuffer(str, "%c", ch) but much faster.
169  */
170 extern void appendPQExpBufferChar(PQExpBuffer str, char ch);
171
172 /*------------------------
173  * appendBinaryPQExpBuffer
174  * Append arbitrary binary data to a PQExpBuffer, allocating more space
175  * if necessary.
176  */
177 extern void appendBinaryPQExpBuffer(PQExpBuffer str,
178                                                 const char *data, size_t datalen);
179
180 #endif   /* PQEXPBUFFER_H */