1996-07-09 02:22:35 -04:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
*
|
1999-02-13 18:22:53 -05:00
|
|
|
* stringinfo.h
|
1999-04-24 23:19:27 -04:00
|
|
|
* Declarations/definitions for "StringInfo" functions.
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
Make StringInfo available to frontend code.
There's plenty places in frontend code that could benefit from a
string buffer implementation. Some because it yields simpler and
faster code, and some others because of the desire to share code
between backend and frontend.
While there is a string buffer implementation available to frontend
code, libpq's PQExpBuffer, it is clunkier than stringinfo, it
introduces a libpq dependency, doesn't allow for sharing between
frontend and backend code, and has a higher API/ABI stability
requirement due to being exposed via libpq.
Therefore it seems best to just making StringInfo being usable by
frontend code. There's not much to do for that, except for rewriting
two subsequent elog/ereport calls into others types of error
reporting, and deciding on a maximum string length.
For the maximum string size I decided to privately define MaxAllocSize
to the same value as used in the backend. It seems likely that we'll
want to reconsider this for both backend and frontend code in the not
too far away future.
For now I've left stringinfo.h in lib/, rather than common/, to reduce
the likelihood of unnecessary breakage. We could alternatively decide
to provide a redirecting stringinfo.h in lib/, or just not provide
compatibility.
Author: Andres Freund
Reviewed-By: Kyotaro Horiguchi, Daniel Gustafsson
Discussion: https://postgr.es/m/20190920051857.2fhnvhvx4qdddviz@alap3.anarazel.de
2019-11-05 17:56:40 -05:00
|
|
|
* StringInfo provides an extensible string data type (currently limited to a
|
|
|
|
|
* length of 1GB). It can be used to buffer either ordinary C strings
|
|
|
|
|
* (null-terminated text) or arbitrary binary data. All storage is allocated
|
|
|
|
|
* with palloc() (falling back to malloc in frontend code).
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
2022-01-07 19:04:57 -05:00
|
|
|
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
|
2000-01-26 00:58:53 -05:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
2010-09-20 16:08:53 -04:00
|
|
|
* src/include/lib/stringinfo.h
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
#ifndef STRINGINFO_H
|
|
|
|
|
#define STRINGINFO_H
|
|
|
|
|
|
|
|
|
|
/*-------------------------
|
1999-04-24 23:19:27 -04:00
|
|
|
* StringInfoData holds information about an extensible string.
|
|
|
|
|
* data is the current buffer for the string (allocated with palloc).
|
|
|
|
|
* len is the current string length. There is guaranteed to be
|
|
|
|
|
* a terminating '\0' at data[len], although this is not very
|
|
|
|
|
* useful when the string holds binary data rather than text.
|
|
|
|
|
* maxlen is the allocated size in bytes of 'data', i.e. the maximum
|
|
|
|
|
* string size (including the terminating '\0' char) that we can
|
1996-07-09 02:22:35 -04:00
|
|
|
* currently store in 'data' without having to reallocate
|
1999-04-24 23:19:27 -04:00
|
|
|
* more space. We must always have maxlen > len.
|
2003-04-18 20:02:30 -04:00
|
|
|
* cursor is initialized to zero by makeStringInfo or initStringInfo,
|
|
|
|
|
* but is not otherwise touched by the stringinfo.c routines.
|
|
|
|
|
* Some routines use it to scan through a StringInfo.
|
1999-04-24 23:19:27 -04:00
|
|
|
*-------------------------
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
|
|
|
|
typedef struct StringInfoData
|
|
|
|
|
{
|
|
|
|
|
char *data;
|
|
|
|
|
int len;
|
1999-04-24 23:19:27 -04:00
|
|
|
int maxlen;
|
2003-04-18 20:02:30 -04:00
|
|
|
int cursor;
|
1996-07-09 02:22:35 -04:00
|
|
|
} StringInfoData;
|
|
|
|
|
|
|
|
|
|
typedef StringInfoData *StringInfo;
|
|
|
|
|
|
1999-04-24 23:19:27 -04:00
|
|
|
|
|
|
|
|
/*------------------------
|
|
|
|
|
* There are two ways to create a StringInfo object initially:
|
|
|
|
|
*
|
2017-05-10 17:41:27 -04:00
|
|
|
* StringInfo stringptr = makeStringInfo();
|
1999-04-24 23:19:27 -04:00
|
|
|
* Both the StringInfoData and the data buffer are palloc'd.
|
|
|
|
|
*
|
|
|
|
|
* StringInfoData string;
|
2017-05-10 17:41:27 -04:00
|
|
|
* initStringInfo(&string);
|
1999-04-24 23:19:27 -04:00
|
|
|
* The data buffer is palloc'd but the StringInfoData is just local.
|
|
|
|
|
* This is the easiest approach for a StringInfo object that will
|
|
|
|
|
* only live as long as the current routine.
|
|
|
|
|
*
|
|
|
|
|
* To destroy a StringInfo, pfree() the data buffer, and then pfree() the
|
|
|
|
|
* StringInfoData if it was palloc'd. There's no special support for this.
|
|
|
|
|
*
|
|
|
|
|
* NOTE: some routines build up a string using StringInfo, and then
|
|
|
|
|
* release the StringInfoData but return the data string itself to their
|
|
|
|
|
* caller. At that point the data string looks like a plain palloc'd
|
|
|
|
|
* string.
|
|
|
|
|
*-------------------------
|
|
|
|
|
*/
|
|
|
|
|
|
1996-07-09 02:22:35 -04:00
|
|
|
/*------------------------
|
|
|
|
|
* makeStringInfo
|
2017-05-10 17:41:27 -04:00
|
|
|
* Create an empty 'StringInfoData' & return a pointer to it.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
|
|
|
|
extern StringInfo makeStringInfo(void);
|
|
|
|
|
|
1999-04-24 23:19:27 -04:00
|
|
|
/*------------------------
|
|
|
|
|
* initStringInfo
|
|
|
|
|
* Initialize a StringInfoData struct (with previously undefined contents)
|
2017-05-10 17:41:27 -04:00
|
|
|
* to describe an empty string.
|
1999-04-24 23:19:27 -04:00
|
|
|
*/
|
|
|
|
|
extern void initStringInfo(StringInfo str);
|
|
|
|
|
|
2007-03-03 14:32:55 -05:00
|
|
|
/*------------------------
|
|
|
|
|
* resetStringInfo
|
|
|
|
|
* Clears the current content of the StringInfo, if any. The
|
2017-05-10 17:41:27 -04:00
|
|
|
* StringInfo remains valid.
|
2007-03-03 14:32:55 -05:00
|
|
|
*/
|
|
|
|
|
extern void resetStringInfo(StringInfo str);
|
|
|
|
|
|
1996-07-09 02:22:35 -04:00
|
|
|
/*------------------------
|
|
|
|
|
* appendStringInfo
|
2003-04-24 17:16:45 -04:00
|
|
|
* Format text data under the control of fmt (an sprintf-style format string)
|
1999-04-24 23:19:27 -04:00
|
|
|
* and append it to whatever is already in str. More space is allocated
|
|
|
|
|
* to str if necessary. This is sort of like a combination of sprintf and
|
|
|
|
|
* strcat.
|
|
|
|
|
*/
|
2003-04-24 17:16:45 -04:00
|
|
|
extern void appendStringInfo(StringInfo str, const char *fmt,...) pg_attribute_printf(2, 3);
|
1999-04-24 23:19:27 -04:00
|
|
|
|
2003-04-24 17:16:45 -04:00
|
|
|
/*------------------------
|
|
|
|
|
* appendStringInfoVA
|
|
|
|
|
* Attempt to format text data under the control of fmt (an sprintf-style
|
|
|
|
|
* format string) and append it to whatever is already in str. If successful
|
2013-10-24 21:43:57 -04:00
|
|
|
* return zero; if not (because there's not enough space), return an estimate
|
|
|
|
|
* of the space needed, without modifying str. Typically the caller should
|
|
|
|
|
* pass the return value to enlargeStringInfo() before trying again; see
|
|
|
|
|
* appendStringInfo for standard usage pattern.
|
2003-04-24 17:16:45 -04:00
|
|
|
*/
|
2011-09-10 16:12:46 -04:00
|
|
|
extern int appendStringInfoVA(StringInfo str, const char *fmt, va_list args) pg_attribute_printf(2, 0);
|
2003-04-24 17:16:45 -04:00
|
|
|
|
|
|
|
|
/*------------------------
|
|
|
|
|
* appendStringInfoString
|
|
|
|
|
* Append a null-terminated string to str.
|
|
|
|
|
* Like appendStringInfo(str, "%s", s) but faster.
|
|
|
|
|
*/
|
|
|
|
|
extern void appendStringInfoString(StringInfo str, const char *s);
|
|
|
|
|
|
1999-04-24 23:19:27 -04:00
|
|
|
/*------------------------
|
|
|
|
|
* appendStringInfoChar
|
|
|
|
|
* Append a single byte to str.
|
|
|
|
|
* Like appendStringInfo(str, "%c", ch) but much faster.
|
|
|
|
|
*/
|
|
|
|
|
extern void appendStringInfoChar(StringInfo str, char ch);
|
|
|
|
|
|
2000-01-21 22:52:04 -05:00
|
|
|
/*------------------------
|
|
|
|
|
* appendStringInfoCharMacro
|
|
|
|
|
* As above, but a macro for even more speed where it matters.
|
|
|
|
|
* Caution: str argument will be evaluated multiple times.
|
|
|
|
|
*/
|
|
|
|
|
#define appendStringInfoCharMacro(str,ch) \
|
|
|
|
|
(((str)->len + 1 >= (str)->maxlen) ? \
|
|
|
|
|
appendStringInfoChar(str, ch) : \
|
2000-02-13 08:21:11 -05:00
|
|
|
(void)((str)->data[(str)->len] = (ch), (str)->data[++(str)->len] = '\0'))
|
2000-01-21 22:52:04 -05:00
|
|
|
|
2009-07-24 17:08:42 -04:00
|
|
|
/*------------------------
|
|
|
|
|
* appendStringInfoSpaces
|
|
|
|
|
* Append a given number of spaces to str.
|
|
|
|
|
*/
|
|
|
|
|
extern void appendStringInfoSpaces(StringInfo str, int count);
|
|
|
|
|
|
1999-04-24 23:19:27 -04:00
|
|
|
/*------------------------
|
|
|
|
|
* appendBinaryStringInfo
|
|
|
|
|
* Append arbitrary binary data to a StringInfo, allocating more space
|
|
|
|
|
* if necessary.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
1999-04-24 23:19:27 -04:00
|
|
|
extern void appendBinaryStringInfo(StringInfo str,
|
2000-01-21 22:52:04 -05:00
|
|
|
const char *data, int datalen);
|
2001-10-28 01:26:15 -05:00
|
|
|
|
2017-10-11 19:01:52 -04:00
|
|
|
/*------------------------
|
|
|
|
|
* appendBinaryStringInfoNT
|
|
|
|
|
* Append arbitrary binary data to a StringInfo, allocating more space
|
|
|
|
|
* if necessary. Does not ensure a trailing null-byte exists.
|
|
|
|
|
*/
|
|
|
|
|
extern void appendBinaryStringInfoNT(StringInfo str,
|
|
|
|
|
const char *data, int datalen);
|
|
|
|
|
|
2003-04-18 20:02:30 -04:00
|
|
|
/*------------------------
|
|
|
|
|
* enlargeStringInfo
|
|
|
|
|
* Make sure a StringInfo's buffer can hold at least 'needed' more bytes.
|
|
|
|
|
*/
|
|
|
|
|
extern void enlargeStringInfo(StringInfo str, int needed);
|
|
|
|
|
|
1996-07-09 02:22:35 -04:00
|
|
|
#endif /* STRINGINFO_H */
|