1999-04-25 05:19:27 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* stringinfo.c
|
1999-04-25 05:19:27 +02:00
|
|
|
*
|
|
|
|
* StringInfo provides an indefinitely-extensible string data type.
|
|
|
|
* It can be used to buffer either ordinary C strings (null-terminated text)
|
|
|
|
* or arbitrary binary data. All storage is allocated with palloc().
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2004-12-31 23:04:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2004-12-31 23:04:05 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/lib/stringinfo.c,v 1.41 2004/12/31 21:59:48 pgsql Exp $
|
1999-04-25 05:19:27 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1999-04-25 05:19:27 +02:00
|
|
|
#include "postgres.h"
|
2003-04-19 02:02:30 +02:00
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
#include "lib/stringinfo.h"
|
2004-05-11 22:07:26 +02:00
|
|
|
#include "utils/memutils.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
|
1998-12-14 09:11:17 +01:00
|
|
|
/*
|
1996-07-09 08:22:35 +02:00
|
|
|
* makeStringInfo
|
|
|
|
*
|
1999-04-25 05:19:27 +02:00
|
|
|
* Create an empty 'StringInfoData' & return a pointer to it.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
StringInfo
|
1999-04-25 05:19:27 +02:00
|
|
|
makeStringInfo(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
StringInfo res;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
res = (StringInfo) palloc(sizeof(StringInfoData));
|
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
initStringInfo(res);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return res;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1998-12-14 09:11:17 +01:00
|
|
|
/*
|
1999-04-25 05:19:27 +02:00
|
|
|
* initStringInfo
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1999-04-25 05:19:27 +02:00
|
|
|
* Initialize a StringInfoData struct (with previously undefined contents)
|
|
|
|
* to describe an empty string.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
initStringInfo(StringInfo str)
|
|
|
|
{
|
|
|
|
int size = 256; /* initial default buffer size */
|
|
|
|
|
1999-08-31 03:28:37 +02:00
|
|
|
str->data = (char *) palloc(size);
|
1999-04-25 05:19:27 +02:00
|
|
|
str->maxlen = size;
|
|
|
|
str->len = 0;
|
|
|
|
str->data[0] = '\0';
|
2003-04-19 02:02:30 +02:00
|
|
|
str->cursor = 0;
|
1999-04-25 05:19:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* appendStringInfo
|
|
|
|
*
|
2003-04-24 23:16:45 +02:00
|
|
|
* Format text data under the control of fmt (an sprintf-style format string)
|
1999-04-25 05:19:27 +02: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.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
void
|
2003-08-04 02:43:34 +02:00
|
|
|
appendStringInfo(StringInfo str, const char *fmt,...)
|
2003-04-24 23:16:45 +02:00
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
bool success;
|
|
|
|
|
|
|
|
/* Try to format the data. */
|
|
|
|
va_start(args, fmt);
|
|
|
|
success = appendStringInfoVA(str, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Double the buffer size and try again. */
|
|
|
|
enlargeStringInfo(str, str->maxlen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* appendStringInfoVA
|
|
|
|
*
|
|
|
|
* Attempt to format text data under the control of fmt (an sprintf-style
|
2003-08-04 02:43:34 +02:00
|
|
|
* format string) and append it to whatever is already in str. If successful
|
2003-04-24 23:16:45 +02:00
|
|
|
* return true; if not (because there's not enough space), return false
|
|
|
|
* without modifying str. Typically the caller would enlarge str and retry
|
|
|
|
* on false return --- see appendStringInfo for standard usage pattern.
|
|
|
|
*
|
|
|
|
* XXX This API is ugly, but there seems no alternative given the C spec's
|
|
|
|
* restrictions on what can portably be done with va_list arguments: you have
|
|
|
|
* to redo va_start before you can rescan the argument list, and we can't do
|
|
|
|
* that from here.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
appendStringInfoVA(StringInfo str, const char *fmt, va_list args)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-08-31 03:28:37 +02:00
|
|
|
int avail,
|
|
|
|
nprinted;
|
1999-04-25 05:19:27 +02:00
|
|
|
|
|
|
|
Assert(str != NULL);
|
1998-12-14 09:11:17 +01:00
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
/*
|
|
|
|
* If there's hardly any space, don't bother trying, just fail to make
|
|
|
|
* the caller enlarge the buffer first.
|
|
|
|
*/
|
|
|
|
avail = str->maxlen - str->len - 1;
|
|
|
|
if (avail < 16)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Assert check here is to catch buggy vsnprintf that overruns the
|
|
|
|
* specified buffer length. Solaris 7 in 64-bit mode is an example of
|
|
|
|
* a platform with such a bug.
|
2003-04-24 23:16:45 +02:00
|
|
|
*/
|
2002-03-04 19:34:02 +01:00
|
|
|
#ifdef USE_ASSERT_CHECKING
|
2003-04-24 23:16:45 +02:00
|
|
|
str->data[str->maxlen - 1] = '\0';
|
2002-03-04 19:34:02 +01:00
|
|
|
#endif
|
|
|
|
|
2003-04-24 23:16:45 +02:00
|
|
|
nprinted = vsnprintf(str->data + str->len, avail, fmt, args);
|
|
|
|
|
|
|
|
Assert(str->data[str->maxlen - 1] == '\0');
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: some versions of vsnprintf return the number of chars
|
|
|
|
* actually stored, but at least one returns -1 on failure. Be
|
|
|
|
* conservative about believing whether the print worked.
|
|
|
|
*/
|
|
|
|
if (nprinted >= 0 && nprinted < avail - 1)
|
|
|
|
{
|
|
|
|
/* Success. Note nprinted does not include trailing null. */
|
|
|
|
str->len += nprinted;
|
|
|
|
return true;
|
1999-08-31 03:28:37 +02:00
|
|
|
}
|
2003-04-24 23:16:45 +02:00
|
|
|
|
|
|
|
/* Restore the trailing null so that str is unmodified. */
|
|
|
|
str->data[str->len] = '\0';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* appendStringInfoString
|
|
|
|
*
|
|
|
|
* Append a null-terminated string to str.
|
|
|
|
* Like appendStringInfo(str, "%s", s) but faster.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
appendStringInfoString(StringInfo str, const char *s)
|
|
|
|
{
|
|
|
|
appendBinaryStringInfo(str, s, strlen(s));
|
1999-04-25 05:19:27 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
/*
|
1999-04-25 05:19:27 +02:00
|
|
|
* appendStringInfoChar
|
2003-04-19 02:02:30 +02:00
|
|
|
*
|
1999-04-25 05:19:27 +02:00
|
|
|
* Append a single byte to str.
|
|
|
|
* Like appendStringInfo(str, "%c", ch) but much faster.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
appendStringInfoChar(StringInfo str, char ch)
|
|
|
|
{
|
|
|
|
/* Make more room if needed */
|
1999-11-01 06:10:32 +01:00
|
|
|
if (str->len + 1 >= str->maxlen)
|
|
|
|
enlargeStringInfo(str, 1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
/* OK, append the character */
|
|
|
|
str->data[str->len] = ch;
|
|
|
|
str->len++;
|
|
|
|
str->data[str->len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* appendBinaryStringInfo
|
|
|
|
*
|
|
|
|
* Append arbitrary binary data to a StringInfo, allocating more space
|
|
|
|
* if necessary.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
|
|
|
|
{
|
|
|
|
Assert(str != NULL);
|
|
|
|
|
|
|
|
/* Make more room if needed */
|
|
|
|
enlargeStringInfo(str, datalen);
|
|
|
|
|
|
|
|
/* OK, append the data */
|
|
|
|
memcpy(str->data + str->len, data, datalen);
|
|
|
|
str->len += datalen;
|
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
|
|
|
* Keep a trailing null in place, even though it's probably useless
|
1999-04-25 05:19:27 +02:00
|
|
|
* for binary data...
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1999-04-25 05:19:27 +02:00
|
|
|
str->data[str->len] = '\0';
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2003-04-19 02:02:30 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* enlargeStringInfo
|
|
|
|
*
|
|
|
|
* Make sure there is enough space for 'needed' more bytes
|
|
|
|
* ('needed' does not include the terminating null).
|
|
|
|
*
|
2003-04-24 23:16:45 +02:00
|
|
|
* External callers usually need not concern themselves with this, since
|
|
|
|
* all stringinfo.c routines do it automatically. However, if a caller
|
2003-04-19 02:02:30 +02:00
|
|
|
* knows that a StringInfo will eventually become X bytes large, it
|
|
|
|
* can save some palloc overhead by enlarging the buffer before starting
|
|
|
|
* to store data in it.
|
|
|
|
*
|
|
|
|
* NB: because we use repalloc() to enlarge the buffer, the string buffer
|
|
|
|
* will remain allocated in the same memory context that was current when
|
|
|
|
* initStringInfo was called, even if another context is now current.
|
|
|
|
* This is the desired and indeed critical behavior!
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
enlargeStringInfo(StringInfo str, int needed)
|
|
|
|
{
|
|
|
|
int newlen;
|
|
|
|
|
2004-05-11 22:07:26 +02:00
|
|
|
/*
|
|
|
|
* Guard against ridiculous "needed" values, which can occur if we're
|
2004-08-29 07:07:03 +02:00
|
|
|
* fed bogus data. Without this, we can get an overflow or infinite
|
2004-05-11 22:07:26 +02:00
|
|
|
* loop in the following.
|
|
|
|
*/
|
|
|
|
if (needed < 0 ||
|
|
|
|
((Size) needed) >= (MaxAllocSize - (Size) str->len))
|
|
|
|
elog(ERROR, "invalid string enlargement request size %d",
|
|
|
|
needed);
|
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
needed += str->len + 1; /* total space required now */
|
2004-05-11 22:07:26 +02:00
|
|
|
|
|
|
|
/* Because of the above test, we now have needed <= MaxAllocSize */
|
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
if (needed <= str->maxlen)
|
|
|
|
return; /* got enough space already */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't want to allocate just a little more space with each
|
|
|
|
* append; for efficiency, double the buffer size each time it
|
|
|
|
* overflows. Actually, we might need to more than double it if
|
|
|
|
* 'needed' is big...
|
|
|
|
*/
|
|
|
|
newlen = 2 * str->maxlen;
|
|
|
|
while (needed > newlen)
|
|
|
|
newlen = 2 * newlen;
|
|
|
|
|
2004-05-11 22:07:26 +02:00
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* Clamp to MaxAllocSize in case we went past it. Note we are
|
|
|
|
* assuming here that MaxAllocSize <= INT_MAX/2, else the above loop
|
|
|
|
* could overflow. We will still have newlen >= needed.
|
2004-05-11 22:07:26 +02:00
|
|
|
*/
|
|
|
|
if (newlen > (int) MaxAllocSize)
|
|
|
|
newlen = (int) MaxAllocSize;
|
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
str->data = (char *) repalloc(str->data, newlen);
|
|
|
|
|
|
|
|
str->maxlen = newlen;
|
|
|
|
}
|