home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / lib / C / stringinfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  3.0 KB  |  116 lines

  1. /*======================================================================
  2.  *
  3.  * FILE:
  4.  * stringinfo.c
  5.  *
  6.  * $Header: /private/postgres/src/lib/C/RCS/stringinfo.c,v 1.2 1991/02/20 10:33:38 cimarron Exp $
  7.  *
  8.  * These are routines that can be used to write informations to a string,
  9.  * without having to worry about string lengths, space allocation etc.
  10.  *
  11.  * Ideally the interface should look like the file i/o interface,
  12.  *======================================================================
  13.  */
  14. #include <strings.h>
  15. #include "nodes/pg_lisp.h"
  16. #include "tmp/stringinfo.h"
  17. #include "utils/log.h"
  18.  
  19. /*---------------------------------------------------------------------
  20.  * makeStringInfo
  21.  *
  22.  * Create a StringInfoData & return a pointer to it.
  23.  *
  24.  *---------------------------------------------------------------------
  25.  */
  26. StringInfo
  27. makeStringInfo()
  28. {
  29.     StringInfo res;
  30.     long size;
  31.  
  32.     res = (StringInfo) palloc(sizeof(StringInfoData));
  33.     if (res == NULL) {
  34.     elog(WARN, "makeStringInfo: Out of memory!");
  35.     }
  36.  
  37.     size = 100;
  38.     res->data = (String) palloc(size);
  39.     if (res->data == NULL) {
  40.     elog(WARN,
  41.         "makeStringInfo: Out of memory! (%ld bytes requested)", size);
  42.     }
  43.     res->maxlen = size;
  44.     res->len = 0;
  45.     /*
  46.      * NOTE: we must initialize `res->data' to the empty string because
  47.      * we use 'strcat' in 'appendStringInfo', which of course it always
  48.      * expects a null terminated string.
  49.      */
  50.     res->data[0] = '\0';
  51.  
  52.     return(res);
  53. }
  54.  
  55. /*---------------------------------------------------------------------
  56.  * appendStringInfo
  57.  *
  58.  * append to the current 'StringInfo' a new string.
  59.  * If there is not enough space in the current 'data', then reallocate
  60.  * some more...
  61.  *
  62.  * NOTE: if we reallocate space, we pfree the old one!
  63.  *---------------------------------------------------------------------
  64.  */
  65. void
  66. appendStringInfo(str, buffer)
  67. StringInfo str;
  68. char *buffer;
  69. {
  70.     StringInfo res;
  71.     int buflen, newlen;
  72.     String s;
  73.  
  74.     Assert((str!=NULL));
  75.  
  76.     /*
  77.      * do we have enough space to append the new string?
  78.      * (don't forget to count the null string terminating char!)
  79.      * If no, then reallocate some more.
  80.      */
  81.     buflen = strlen(buffer);
  82.     if (buflen + str->len >= str->maxlen-1) {
  83.     /*
  84.      * how much more space to allocate ?
  85.      * Let's say double the current space...
  86.      * However we must check if this is enough!
  87.      */
  88.     newlen = 2 * str->len;
  89.     while (buflen + str->len >= newlen-1) {
  90.         newlen = 2 * newlen;
  91.     }
  92.     /*
  93.      * allocate enough space.
  94.      */
  95.     s = (String) palloc(newlen);
  96.     if (s==NULL) {
  97.         elog(WARN,
  98.         "appendStringInfo: Out of memory (%d bytes requested)",
  99.         newlen);
  100.     }
  101.     bcopy(str->data, s, str->len+1);
  102.     pfree(str->data);
  103.     str->maxlen = newlen;
  104.     str->data = s;
  105.     }
  106.  
  107.     /*
  108.      * OK, we have enough space now, append 'buffer' at the
  109.      * end of the string & update the string length.
  110.      * NOTE: this is a text string (i.e. printable characters)
  111.      * so 'strcat' will do the job (no need to use 'bcopy' et all...)
  112.      */
  113.     (void) strcat(str->data, buffer);
  114.     str->len += buflen;
  115. }
  116.