OSDN Git Service

Add resetStringInfo(), which clears the content of a StringInfo, and
authorNeil Conway <neilc@samurai.com>
Sat, 3 Mar 2007 19:32:55 +0000 (19:32 +0000)
committerNeil Conway <neilc@samurai.com>
Sat, 3 Mar 2007 19:32:55 +0000 (19:32 +0000)
fixup various places in the tree that were clearing a StringInfo by hand.
Making this function a part of the API simplifies client code slightly,
and avoids needlessly peeking inside the StringInfo interface.

contrib/tablefunc/tablefunc.c
src/backend/catalog/pg_shdepend.c
src/backend/commands/copy.c
src/backend/lib/stringinfo.c
src/backend/libpq/pqcomm.c
src/backend/storage/lmgr/deadlock.c
src/backend/tcop/fastpath.c
src/backend/tcop/postgres.c
src/backend/utils/adt/rowtypes.c
src/backend/utils/adt/xml.c
src/include/lib/stringinfo.h

index 4e68428..f3b1eb2 100644 (file)
@@ -1378,15 +1378,12 @@ build_tuplestore_recursively(char *key_fld,
                                                                   "incompatible.")));
                }
 
+               initStringInfo(&branchstr);
+               initStringInfo(&chk_branchstr);
+               initStringInfo(&chk_current_key);
+
                for (i = 0; i < proc; i++)
                {
-                       /* start a new branch */
-                       initStringInfo(&branchstr);
-
-                       /* need these to check for recursion */
-                       initStringInfo(&chk_branchstr);
-                       initStringInfo(&chk_current_key);
-
                        /* initialize branch for this pass */
                        appendStringInfo(&branchstr, "%s", branch);
                        appendStringInfo(&chk_branchstr, "%s%s%s", branch_delim, branch, branch_delim);
@@ -1459,10 +1456,14 @@ build_tuplestore_recursively(char *key_fld,
                                                                                                        tupstore);
 
                        /* reset branch for next pass */
-                       xpfree(branchstr.data);
-                       xpfree(chk_branchstr.data);
-                       xpfree(chk_current_key.data);
+                       resetStringInfo(&branchstr);
+                       resetStringInfo(&chk_branchstr);
+                       resetStringInfo(&chk_current_key);
                }
+
+               xpfree(branchstr.data);
+               xpfree(chk_branchstr.data);
+               xpfree(chk_current_key.data);
        }
 
        return tupstore;
index e1c8dff..9ad012d 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/catalog/pg_shdepend.c,v 1.16 2007/01/05 22:19:25 momjian Exp $
+ *       $PostgreSQL: pgsql/src/backend/catalog/pg_shdepend.c,v 1.17 2007/03/03 19:32:54 neilc Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -588,8 +588,7 @@ checkSharedDependencies(Oid classId, Oid objectId)
                 * Note: we don't ever suppress per-database totals, which should be
                 * OK as long as there aren't too many databases ...
                 */
-               descs.len = 0;                  /* reset to empty */
-               descs.data[0] = '\0';
+               resetStringInfo(&descs);
 
                if (numLocalDeps > 0)
                {
index 30118d5..17f0135 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.276 2007/02/20 17:32:13 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.277 2007/03/03 19:32:54 neilc Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -466,9 +466,7 @@ CopySendEndOfRow(CopyState cstate)
                        break;
        }
 
-       /* Reset fe_msgbuf to empty */
-       fe_msgbuf->len = 0;
-       fe_msgbuf->data[0] = '\0';
+       resetStringInfo(fe_msgbuf);
 }
 
 /*
@@ -2193,9 +2191,7 @@ CopyReadLine(CopyState cstate)
 {
        bool            result;
 
-       /* Reset line_buf to empty */
-       cstate->line_buf.len = 0;
-       cstate->line_buf.data[0] = '\0';
+       resetStringInfo(&cstate->line_buf);
 
        /* Mark that encoding conversion hasn't occurred yet */
        cstate->line_buf_converted = false;
@@ -2262,8 +2258,7 @@ CopyReadLine(CopyState cstate)
                if (cvt != cstate->line_buf.data)
                {
                        /* transfer converted data back to line_buf */
-                       cstate->line_buf.len = 0;
-                       cstate->line_buf.data[0] = '\0';
+                       resetStringInfo(&cstate->line_buf);
                        appendBinaryStringInfo(&cstate->line_buf, cvt, strlen(cvt));
                        pfree(cvt);
                }
@@ -2686,9 +2681,7 @@ CopyReadAttributesText(CopyState cstate, int maxfields, char **fieldvals)
                return 0;
        }
 
-       /* reset attribute_buf to empty */
-       cstate->attribute_buf.len = 0;
-       cstate->attribute_buf.data[0] = '\0';
+       resetStringInfo(&cstate->attribute_buf);
 
        /*
         * The de-escaped attributes will certainly not be longer than the input
@@ -2886,9 +2879,7 @@ CopyReadAttributesCSV(CopyState cstate, int maxfields, char **fieldvals)
                return 0;
        }
 
-       /* reset attribute_buf to empty */
-       cstate->attribute_buf.len = 0;
-       cstate->attribute_buf.data[0] = '\0';
+       resetStringInfo(&cstate->attribute_buf);
 
        /*
         * The de-escaped attributes will certainly not be longer than the input
@@ -3040,12 +3031,9 @@ CopyReadBinaryAttribute(CopyState cstate,
                                 errmsg("invalid field size")));
 
        /* reset attribute_buf to empty, and load raw data in it */
-       cstate->attribute_buf.len = 0;
-       cstate->attribute_buf.data[0] = '\0';
-       cstate->attribute_buf.cursor = 0;
+       resetStringInfo(&cstate->attribute_buf);
 
        enlargeStringInfo(&cstate->attribute_buf, fld_size);
-
        if (CopyGetData(cstate, cstate->attribute_buf.data,
                                        fld_size, fld_size) != fld_size)
                ereport(ERROR,
index 826212d..b0854dd 100644 (file)
@@ -9,7 +9,7 @@
  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- *       $PostgreSQL: pgsql/src/backend/lib/stringinfo.c,v 1.44 2007/01/05 22:19:29 momjian Exp $
+ *       $PostgreSQL: pgsql/src/backend/lib/stringinfo.c,v 1.45 2007/03/03 19:32:54 neilc Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -49,8 +49,20 @@ initStringInfo(StringInfo str)
 
        str->data = (char *) palloc(size);
        str->maxlen = size;
-       str->len = 0;
+       resetStringInfo(str);
+}
+
+/*
+ * resetStringInfo
+ *
+ * Reset the StringInfo: the data buffer remains valid, but its
+ * previous content, if any, is cleared.
+ */
+void
+resetStringInfo(StringInfo str)
+{
        str->data[0] = '\0';
+       str->len = 0;
        str->cursor = 0;
 }
 
index a8f4024..d9439e7 100644 (file)
@@ -30,7 +30,7 @@
  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- *     $PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.190 2007/02/13 19:18:53 tgl Exp $
+ *     $PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.191 2007/03/03 19:32:54 neilc Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -860,10 +860,7 @@ pq_getstring(StringInfo s)
 {
        int                     i;
 
-       /* Reset string to empty */
-       s->len = 0;
-       s->data[0] = '\0';
-       s->cursor = 0;
+       resetStringInfo(s);
 
        /* Read until we get the terminating '\0' */
        for (;;)
@@ -915,10 +912,7 @@ pq_getmessage(StringInfo s, int maxlen)
 {
        int32           len;
 
-       /* Reset message buffer to empty */
-       s->len = 0;
-       s->data[0] = '\0';
-       s->cursor = 0;
+       resetStringInfo(s);
 
        /* Read message length word */
        if (pq_getbytes((char *) &len, 4) == EOF)
index ddbadfa..f213008 100644 (file)
@@ -12,7 +12,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/storage/lmgr/deadlock.c,v 1.45 2007/03/03 18:46:40 momjian Exp $
+ *       $PostgreSQL: pgsql/src/backend/storage/lmgr/deadlock.c,v 1.46 2007/03/03 19:32:54 neilc Exp $
  *
  *     Interface:
  *
@@ -933,8 +933,7 @@ DeadLockReport(void)
                        appendStringInfoChar(&buf, '\n');
 
                /* reset buf2 to hold next object description */
-               buf2.len = 0;
-               buf2.data[0] = '\0';
+               resetStringInfo(&buf2);
 
                DescribeLockTag(&buf2, &info->locktag);
 
index fba983e..cd1911f 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/tcop/fastpath.c,v 1.95 2007/01/05 22:19:39 momjian Exp $
+ *       $PostgreSQL: pgsql/src/backend/tcop/fastpath.c,v 1.96 2007/03/03 19:32:54 neilc Exp $
  *
  * NOTES
  *       This cruft is the server side of PQfn.
@@ -480,10 +480,7 @@ parse_fcall_arguments(StringInfo msgBuf, struct fp_info * fip,
                                                 argsize)));
 
                        /* Reset abuf to empty, and insert raw data into it */
-                       abuf.len = 0;
-                       abuf.data[0] = '\0';
-                       abuf.cursor = 0;
-
+                       resetStringInfo(&abuf);
                        appendBinaryStringInfo(&abuf,
                                                                   pq_getmsgbytes(msgBuf, argsize),
                                                                   argsize);
@@ -613,10 +610,7 @@ parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info * fip,
                                                 argsize)));
 
                /* Reset abuf to empty, and insert raw data into it */
-               abuf.len = 0;
-               abuf.data[0] = '\0';
-               abuf.cursor = 0;
-
+               resetStringInfo(&abuf);
                appendBinaryStringInfo(&abuf,
                                                           pq_getmsgbytes(msgBuf, argsize),
                                                           argsize);
index afb6b4d..cfb6731 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.526 2007/03/02 23:37:22 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.527 2007/03/03 19:32:54 neilc Exp $
  *
  * NOTES
  *       this is the "main" module of the postgres backend and
@@ -205,10 +205,7 @@ InteractiveBackend(StringInfo inBuf)
        printf("backend> ");
        fflush(stdout);
 
-       /* Reset inBuf to empty */
-       inBuf->len = 0;
-       inBuf->data[0] = '\0';
-       inBuf->cursor = 0;
+       resetStringInfo(inBuf);
 
        for (;;)
        {
index d7ea553..c856acb 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/utils/adt/rowtypes.c,v 1.18 2007/01/05 22:19:42 momjian Exp $
+ *       $PostgreSQL: pgsql/src/backend/utils/adt/rowtypes.c,v 1.19 2007/03/03 19:32:55 neilc Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -168,8 +168,7 @@ record_in(PG_FUNCTION_ARGS)
                        /* Extract string for this column */
                        bool            inquote = false;
 
-                       buf.len = 0;
-                       buf.data[0] = '\0';
+                       resetStringInfo(&buf);
                        while (inquote || !(*ptr == ',' || *ptr == ')'))
                        {
                                char            ch = *ptr++;
index 547d98d..921fe1d 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.33 2007/03/01 14:52:04 petere Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.34 2007/03/03 19:32:55 neilc Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -835,8 +835,7 @@ xml_init(void)
        else
        {
                /* Reset pre-existing buffer to empty */
-               xml_err_buf->data[0] = '\0';
-               xml_err_buf->len = 0;
+               resetStringInfo(xml_err_buf);
        }
        /* Now that xml_err_buf exists, safe to call xml_errorHandler */
        xmlSetGenericErrorFunc(NULL, xml_errorHandler);
@@ -1197,8 +1196,7 @@ xml_ereport(int level, int sqlcode,
        if (xml_err_buf->len > 0)
        {
                detail = pstrdup(xml_err_buf->data);
-               xml_err_buf->data[0] = '\0';
-               xml_err_buf->len = 0;
+               resetStringInfo(xml_err_buf);
        }
        else
                detail = NULL;
index 8658101..b5a7eba 100644 (file)
@@ -10,7 +10,7 @@
  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/lib/stringinfo.h,v 1.33 2007/01/05 22:19:55 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/lib/stringinfo.h,v 1.34 2007/03/03 19:32:55 neilc Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -79,6 +79,13 @@ extern StringInfo makeStringInfo(void);
 extern void initStringInfo(StringInfo str);
 
 /*------------------------
+ * resetStringInfo
+ * Clears the current content of the StringInfo, if any. The
+ * StringInfo remains valid.
+ */
+extern void resetStringInfo(StringInfo str);
+
+/*------------------------
  * appendStringInfo
  * Format text data under the control of fmt (an sprintf-style format string)
  * and append it to whatever is already in str.  More space is allocated