pqexpbuffer.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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-2016, PostgreSQL Global Development Group
  19. * Portions Copyright (c) 1994, Regents of the University of California
  20. *
  21. * src/interfaces/libpq/pqexpbuffer.h
  22. *
  23. *-------------------------------------------------------------------------
  24. */
  25. #ifndef PQEXPBUFFER_H
  26. #define PQEXPBUFFER_H
  27. /*-------------------------
  28. * PQExpBufferData holds information about an extensible string.
  29. * data is the current buffer for the string (allocated with malloc).
  30. * len is the current string length. There is guaranteed to be
  31. * a terminating '\0' at data[len], although this is not very
  32. * useful when the string holds binary data rather than text.
  33. * maxlen is the allocated size in bytes of 'data', i.e. the maximum
  34. * string size (including the terminating '\0' char) that we can
  35. * currently store in 'data' without having to reallocate
  36. * more space. We must always have maxlen > len.
  37. *
  38. * An exception occurs if we failed to allocate enough memory for the string
  39. * buffer. In that case data points to a statically allocated empty string,
  40. * and len = maxlen = 0.
  41. *-------------------------
  42. */
  43. typedef struct PQExpBufferData
  44. {
  45. char *data;
  46. size_t len;
  47. size_t maxlen;
  48. } PQExpBufferData;
  49. typedef PQExpBufferData *PQExpBuffer;
  50. /*------------------------
  51. * Test for a broken (out of memory) PQExpBuffer.
  52. * When a buffer is "broken", all operations except resetting or deleting it
  53. * are no-ops.
  54. *------------------------
  55. */
  56. #define PQExpBufferBroken(str) \
  57. ((str) == NULL || (str)->maxlen == 0)
  58. /*------------------------
  59. * Same, but for use when using a static or local PQExpBufferData struct.
  60. * For that, a null-pointer test is useless and may draw compiler warnings.
  61. *------------------------
  62. */
  63. #define PQExpBufferDataBroken(buf) \
  64. ((buf).maxlen == 0)
  65. /*------------------------
  66. * Initial size of the data buffer in a PQExpBuffer.
  67. * NB: this must be large enough to hold error messages that might
  68. * be returned by PQrequestCancel().
  69. *------------------------
  70. */
  71. #define INITIAL_EXPBUFFER_SIZE 256
  72. /*------------------------
  73. * There are two ways to create a PQExpBuffer object initially:
  74. *
  75. * PQExpBuffer stringptr = createPQExpBuffer();
  76. * Both the PQExpBufferData and the data buffer are malloc'd.
  77. *
  78. * PQExpBufferData string;
  79. * initPQExpBuffer(&string);
  80. * The data buffer is malloc'd but the PQExpBufferData is presupplied.
  81. * This is appropriate if the PQExpBufferData is a field of another
  82. * struct.
  83. *-------------------------
  84. */
  85. /*------------------------
  86. * createPQExpBuffer
  87. * Create an empty 'PQExpBufferData' & return a pointer to it.
  88. */
  89. extern PQExpBuffer createPQExpBuffer(void);
  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. * To destroy a PQExpBuffer, use either:
  98. *
  99. * destroyPQExpBuffer(str);
  100. * free()s both the data buffer and the PQExpBufferData.
  101. * This is the inverse of createPQExpBuffer().
  102. *
  103. * termPQExpBuffer(str)
  104. * free()s the data buffer but not the PQExpBufferData itself.
  105. * This is the inverse of initPQExpBuffer().
  106. *
  107. * NOTE: some routines build up a string using PQExpBuffer, and then
  108. * release the PQExpBufferData but return the data string itself to their
  109. * caller. At that point the data string looks like a plain malloc'd
  110. * string.
  111. */
  112. extern void destroyPQExpBuffer(PQExpBuffer str);
  113. extern void termPQExpBuffer(PQExpBuffer str);
  114. /*------------------------
  115. * resetPQExpBuffer
  116. * Reset a PQExpBuffer to empty
  117. *
  118. * Note: if possible, a "broken" PQExpBuffer is returned to normal.
  119. */
  120. extern void resetPQExpBuffer(PQExpBuffer str);
  121. /*------------------------
  122. * enlargePQExpBuffer
  123. * Make sure there is enough space for 'needed' more bytes in the buffer
  124. * ('needed' does not include the terminating null).
  125. *
  126. * Returns 1 if OK, 0 if failed to enlarge buffer. (In the latter case
  127. * the buffer is left in "broken" state.)
  128. */
  129. extern int enlargePQExpBuffer(PQExpBuffer str, size_t needed);
  130. /*------------------------
  131. * printfPQExpBuffer
  132. * Format text data under the control of fmt (an sprintf-like format string)
  133. * and insert it into str. More space is allocated to str if necessary.
  134. * This is a convenience routine that does the same thing as
  135. * resetPQExpBuffer() followed by appendPQExpBuffer().
  136. */
  137. extern void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...) pg_attribute_printf(2, 3);
  138. /*------------------------
  139. * appendPQExpBuffer
  140. * Format text data under the control of fmt (an sprintf-like format string)
  141. * and append it to whatever is already in str. More space is allocated
  142. * to str if necessary. This is sort of like a combination of sprintf and
  143. * strcat.
  144. */
  145. extern void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...) pg_attribute_printf(2, 3);
  146. /*------------------------
  147. * appendPQExpBufferStr
  148. * Append the given string to a PQExpBuffer, allocating more space
  149. * if necessary.
  150. */
  151. extern void appendPQExpBufferStr(PQExpBuffer str, const char *data);
  152. /*------------------------
  153. * appendPQExpBufferChar
  154. * Append a single byte to str.
  155. * Like appendPQExpBuffer(str, "%c", ch) but much faster.
  156. */
  157. extern void appendPQExpBufferChar(PQExpBuffer str, char ch);
  158. /*------------------------
  159. * appendBinaryPQExpBuffer
  160. * Append arbitrary binary data to a PQExpBuffer, allocating more space
  161. * if necessary.
  162. */
  163. extern void appendBinaryPQExpBuffer(PQExpBuffer str,
  164. const char *data, size_t datalen);
  165. #endif /* PQEXPBUFFER_H */