Header And Logo

PostgreSQL
| The world's most advanced open source database.

Functions

stringinfo.c File Reference

#include "postgres.h"
#include "lib/stringinfo.h"
#include "utils/memutils.h"
Include dependency graph for stringinfo.c:

Go to the source code of this file.

Functions

StringInfo makeStringInfo (void)
void initStringInfo (StringInfo str)
void resetStringInfo (StringInfo str)
void appendStringInfo (StringInfo str, const char *fmt,...)
bool appendStringInfoVA (StringInfo str, const char *fmt, va_list args)
void appendStringInfoString (StringInfo str, const char *s)
void appendStringInfoChar (StringInfo str, char ch)
void appendStringInfoSpaces (StringInfo str, int count)
void appendBinaryStringInfo (StringInfo str, const char *data, int datalen)
void enlargeStringInfo (StringInfo str, int needed)

Function Documentation

void appendBinaryStringInfo ( StringInfo  str,
const char *  data,
int  datalen 
)
void appendStringInfo ( StringInfo  str,
const char *  fmt,
  ... 
)

Definition at line 78 of file stringinfo.c.

References appendStringInfoVA(), enlargeStringInfo(), and StringInfoData::maxlen.

{
    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);
    }
}

void appendStringInfoChar ( StringInfo  str,
char  ch 
)

Definition at line 177 of file stringinfo.c.

References appendStringInfo(), StringInfoData::data, enlargeStringInfo(), StringInfoData::len, and StringInfoData::maxlen.

{
    /* Make more room if needed */
    if (str->len + 1 >= str->maxlen)
        enlargeStringInfo(str, 1);

    /* OK, append the character */
    str->data[str->len] = ch;
    str->len++;
    str->data[str->len] = '\0';
}

void appendStringInfoSpaces ( StringInfo  str,
int  count 
)

Definition at line 195 of file stringinfo.c.

References StringInfoData::data, enlargeStringInfo(), and StringInfoData::len.

Referenced by appendContextKeyword(), ExplainCloseGroup(), ExplainDummyGroup(), ExplainNode(), ExplainOpenGroup(), ExplainProperty(), ExplainPropertyList(), ExplainXMLTag(), ExplainYAMLLineStarting(), get_setop_query(), show_hash_info(), show_sort_info(), and text_format_append_string().

{
    if (count > 0)
    {
        /* Make more room if needed */
        enlargeStringInfo(str, count);

        /* OK, append the spaces */
        while (--count >= 0)
            str->data[str->len++] = ' ';
        str->data[str->len] = '\0';
    }
}

void appendStringInfoString ( StringInfo  str,
const char *  s 
)

Definition at line 165 of file stringinfo.c.

References appendBinaryStringInfo(), and appendStringInfo().

{
    appendBinaryStringInfo(str, s, strlen(s));
}

bool appendStringInfoVA ( StringInfo  str,
const char *  fmt,
va_list  args 
)

Definition at line 113 of file stringinfo.c.

References Assert, StringInfoData::data, StringInfoData::len, StringInfoData::maxlen, NULL, and vsnprintf().

Referenced by appendStringInfo(), and PLy_elog().

{
    int         avail,
                nprinted;

    Assert(str != NULL);

    /*
     * 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;

    /*
     * 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.
     */
#ifdef USE_ASSERT_CHECKING
    str->data[str->maxlen - 1] = '\0';
#endif

    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;
    }

    /* Restore the trailing null so that str is unmodified. */
    str->data[str->len] = '\0';
    return false;
}

void enlargeStringInfo ( StringInfo  str,
int  needed 
)

Definition at line 253 of file stringinfo.c.

References StringInfoData::data, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, StringInfoData::len, MaxAllocSize, StringInfoData::maxlen, and repalloc().

Referenced by appendBinaryStringInfo(), appendStringInfo(), appendStringInfoChar(), appendStringInfoSpaces(), CopyReadAttributesCSV(), CopyReadAttributesText(), CopyReadBinaryAttribute(), GetOldFunctionMessage(), PLy_elog(), pq_getmessage(), and XLogSend().

{
    int         newlen;

    /*
     * Guard against out-of-range "needed" values.  Without this, we can get
     * an overflow or infinite loop in the following.
     */
    if (needed < 0)             /* should not happen */
        elog(ERROR, "invalid string enlargement request size: %d", needed);
    if (((Size) needed) >= (MaxAllocSize - (Size) str->len))
        ereport(ERROR,
                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                 errmsg("out of memory"),
                 errdetail("Cannot enlarge string buffer containing %d bytes by %d more bytes.",
                           str->len, needed)));

    needed += str->len + 1;     /* total space required now */

    /* Because of the above test, we now have needed <= MaxAllocSize */

    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;

    /*
     * 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.
     */
    if (newlen > (int) MaxAllocSize)
        newlen = (int) MaxAllocSize;

    str->data = (char *) repalloc(str->data, newlen);

    str->maxlen = newlen;
}

void initStringInfo ( StringInfo  str  ) 

Definition at line 46 of file stringinfo.c.

References StringInfoData::data, StringInfoData::maxlen, palloc(), and resetStringInfo().

Referenced by array_to_text_internal(), BeginCopyFrom(), bqarr_in(), build_tuplestore_recursively(), BuildIndexValueDescription(), checkSharedDependencies(), concat_internal(), config_enum_get_options(), ConvertTriggerToFK(), create_cursor(), cube_out(), cursor_to_xml(), dblink_close(), dblink_fdw_validator(), dblink_fetch(), dblink_open(), DeadLockReport(), deparse_expression_pretty(), deparseAnalyzeSizeSql(), do_pg_start_backup(), errdetail_params(), escape_xml(), estimate_path_cost_size(), exec_stmt_raise(), ExecBuildSlotValueDescription(), ExecEvalXml(), execute_extension_script(), expand_fmt_string(), ExportSnapshot(), file_fdw_validator(), flatten_set_variable_args(), format_node_dump(), format_operator_internal(), format_procedure_internal(), fsm_page_contents(), funcname_signature_string(), generate_operator_name(), get_from_clause(), get_sql_delete(), get_sql_insert(), get_sql_update(), get_target_list(), get_tuple_of_interest(), getObjectDescription(), getObjectIdentity(), getObjectTypeDescription(), incompatible_module_error(), initialize_worker_spi(), KnownAssignedXidsDisplay(), makeStringInfo(), map_multipart_sql_identifier_to_xml_name(), map_sql_catalog_to_xmlschema_types(), map_sql_identifier_to_xml_name(), map_sql_schema_to_xmlschema_types(), map_sql_table_to_xmlschema(), map_sql_type_to_xml_name(), map_sql_type_to_xmlschema_type(), map_sql_typecoll_to_xmlschema_types(), map_sql_value_to_xml_value(), map_xml_name_to_sql_identifier(), mxid_to_string(), NameListToQuotedString(), NameListToString(), nodeToString(), op_signature_string(), parse_fcall_arguments(), parse_fcall_arguments_20(), parseTypeString(), pg_extension_update_paths(), pg_get_constraintdef_worker(), pg_get_function_arguments(), pg_get_function_identity_arguments(), pg_get_function_result(), pg_get_functiondef(), pg_get_indexdef_worker(), pg_get_ruledef_worker(), pg_get_triggerdef_worker(), pg_get_viewdef_worker(), PLy_elog(), PLy_traceback(), postgres_fdw_validator(), postgresAcquireSampleRowsFunc(), postgresAnalyzeForeignTable(), postgresGetForeignPlan(), PostgresMain(), postgresPlanForeignModify(), postgresql_fdw_validator(), pq_beginmessage(), pq_begintypsend(), pretty_format_node_dump(), print_function_rettype(), process_pipe_input(), ProcSleep(), quote_object_name(), quote_qualified_identifier(), range_bound_escape(), range_deparse(), range_parse_bound(), range_recv(), record_in(), record_out(), recv_password_packet(), replace_text(), replace_text_regexp(), reportDependentObjects(), ri_Check_Pk_Match(), RI_FKey_cascade_del(), RI_FKey_cascade_upd(), RI_FKey_check(), RI_FKey_setdefault_del(), RI_FKey_setdefault_upd(), RI_FKey_setnull_del(), RI_FKey_setnull_upd(), RI_Initial_Check(), ri_ReportViolation(), ri_restrict_del(), ri_restrict_upd(), rm_redo_error_callback(), schema_get_xml_visible_tables(), send_message_to_frontend(), send_message_to_server_log(), sepgsql_attribute_post_create(), sepgsql_audit_log(), sepgsql_database_post_create(), sepgsql_proc_post_create(), sepgsql_relation_post_create(), sepgsql_schema_post_create(), serialize_deflist(), ShowTransactionStateRec(), ShowUsage(), StartupXLOG(), table_to_xml_internal(), text_format(), txid_snapshot_out(), TypeNameListToString(), TypeNameToString(), uuid_out(), WalReceiverMain(), WalSndLoop(), worker_spi_main(), write_csvlog(), XLogInsert(), xml_out_internal(), xmlcomment(), xmlconcat(), xmlpi(), xmlroot(), and xpath_table().

{
    int         size = 1024;    /* initial default buffer size */

    str->data = (char *) palloc(size);
    str->maxlen = size;
    resetStringInfo(str);
}

StringInfo makeStringInfo ( void   ) 
void resetStringInfo ( StringInfo  str  )