2
0

stringinfo.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*-------------------------------------------------------------------------
  2. *
  3. * stringinfo.h
  4. * Declarations/definitions for "StringInfo" functions.
  5. *
  6. * StringInfo provides an extensible string data type (currently limited to a
  7. * length of 1GB). It can be used to buffer either ordinary C strings
  8. * (null-terminated text) or arbitrary binary data. All storage is allocated
  9. * with palloc() (falling back to malloc in frontend code).
  10. *
  11. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  12. * Portions Copyright (c) 1994, Regents of the University of California
  13. *
  14. * src/include/lib/stringinfo.h
  15. *
  16. *-------------------------------------------------------------------------
  17. */
  18. #ifndef STRINGINFO_H
  19. #define STRINGINFO_H
  20. /*-------------------------
  21. * StringInfoData holds information about an extensible string.
  22. * data is the current buffer for the string (allocated with palloc).
  23. * len is the current string length. There is guaranteed to be
  24. * a terminating '\0' at data[len], although this is not very
  25. * useful when the string holds binary data rather than text.
  26. * maxlen is the allocated size in bytes of 'data', i.e. the maximum
  27. * string size (including the terminating '\0' char) that we can
  28. * currently store in 'data' without having to reallocate
  29. * more space. We must always have maxlen > len.
  30. * cursor is initialized to zero by makeStringInfo or initStringInfo,
  31. * but is not otherwise touched by the stringinfo.c routines.
  32. * Some routines use it to scan through a StringInfo.
  33. *-------------------------
  34. */
  35. typedef struct StringInfoData
  36. {
  37. char *data;
  38. int len;
  39. int maxlen;
  40. int cursor;
  41. } StringInfoData;
  42. typedef StringInfoData *StringInfo;
  43. /*------------------------
  44. * There are two ways to create a StringInfo object initially:
  45. *
  46. * StringInfo stringptr = makeStringInfo();
  47. * Both the StringInfoData and the data buffer are palloc'd.
  48. *
  49. * StringInfoData string;
  50. * initStringInfo(&string);
  51. * The data buffer is palloc'd but the StringInfoData is just local.
  52. * This is the easiest approach for a StringInfo object that will
  53. * only live as long as the current routine.
  54. *
  55. * To destroy a StringInfo, pfree() the data buffer, and then pfree() the
  56. * StringInfoData if it was palloc'd. There's no special support for this.
  57. *
  58. * NOTE: some routines build up a string using StringInfo, and then
  59. * release the StringInfoData but return the data string itself to their
  60. * caller. At that point the data string looks like a plain palloc'd
  61. * string.
  62. *-------------------------
  63. */
  64. /*------------------------
  65. * makeStringInfo
  66. * Create an empty 'StringInfoData' & return a pointer to it.
  67. */
  68. extern StringInfo makeStringInfo(void);
  69. /*------------------------
  70. * initStringInfo
  71. * Initialize a StringInfoData struct (with previously undefined contents)
  72. * to describe an empty string.
  73. */
  74. extern void initStringInfo(StringInfo str);
  75. /*------------------------
  76. * resetStringInfo
  77. * Clears the current content of the StringInfo, if any. The
  78. * StringInfo remains valid.
  79. */
  80. extern void resetStringInfo(StringInfo str);
  81. /*------------------------
  82. * appendStringInfo
  83. * Format text data under the control of fmt (an sprintf-style format string)
  84. * and append it to whatever is already in str. More space is allocated
  85. * to str if necessary. This is sort of like a combination of sprintf and
  86. * strcat.
  87. */
  88. extern void appendStringInfo(StringInfo str, const char *fmt,...) pg_attribute_printf(2, 3);
  89. /*------------------------
  90. * appendStringInfoVA
  91. * Attempt to format text data under the control of fmt (an sprintf-style
  92. * format string) and append it to whatever is already in str. If successful
  93. * return zero; if not (because there's not enough space), return an estimate
  94. * of the space needed, without modifying str. Typically the caller should
  95. * pass the return value to enlargeStringInfo() before trying again; see
  96. * appendStringInfo for standard usage pattern.
  97. */
  98. extern int appendStringInfoVA(StringInfo str, const char *fmt, va_list args) pg_attribute_printf(2, 0);
  99. /*------------------------
  100. * appendStringInfoString
  101. * Append a null-terminated string to str.
  102. * Like appendStringInfo(str, "%s", s) but faster.
  103. */
  104. extern void appendStringInfoString(StringInfo str, const char *s);
  105. /*------------------------
  106. * appendStringInfoChar
  107. * Append a single byte to str.
  108. * Like appendStringInfo(str, "%c", ch) but much faster.
  109. */
  110. extern void appendStringInfoChar(StringInfo str, char ch);
  111. /*------------------------
  112. * appendStringInfoCharMacro
  113. * As above, but a macro for even more speed where it matters.
  114. * Caution: str argument will be evaluated multiple times.
  115. */
  116. #define appendStringInfoCharMacro(str,ch) \
  117. (((str)->len + 1 >= (str)->maxlen) ? \
  118. appendStringInfoChar(str, ch) : \
  119. (void)((str)->data[(str)->len] = (ch), (str)->data[++(str)->len] = '\0'))
  120. /*------------------------
  121. * appendStringInfoSpaces
  122. * Append a given number of spaces to str.
  123. */
  124. extern void appendStringInfoSpaces(StringInfo str, int count);
  125. /*------------------------
  126. * appendBinaryStringInfo
  127. * Append arbitrary binary data to a StringInfo, allocating more space
  128. * if necessary.
  129. */
  130. extern void appendBinaryStringInfo(StringInfo str,
  131. const char *data, int datalen);
  132. /*------------------------
  133. * appendBinaryStringInfoNT
  134. * Append arbitrary binary data to a StringInfo, allocating more space
  135. * if necessary. Does not ensure a trailing null-byte exists.
  136. */
  137. extern void appendBinaryStringInfoNT(StringInfo str,
  138. const char *data, int datalen);
  139. /*------------------------
  140. * enlargeStringInfo
  141. * Make sure a StringInfo's buffer can hold at least 'needed' more bytes.
  142. */
  143. extern void enlargeStringInfo(StringInfo str, int needed);
  144. #endif /* STRINGINFO_H */