3 * $Header: /cvsroot/pgpool/pgpool-II/parser/pool_string.c,v 1.4 2008/01/29 01:56:39 y-asaba Exp $
5 * Copyright (c) 2006-2008, pgpool Global Development Group
7 * Permission to use, copy, modify, and distribute this software and
8 * its documentation for any purpose and without fee is hereby
9 * granted, provided that the above copyright notice appear in all
10 * copies and that both that copyright notice and this permission
11 * notice appear in supporting documentation, and that the name of the
12 * author not be used in advertising or publicity pertaining to
13 * distribution of the software without specific, written prior
14 * permission. The author makes no representations about the
15 * suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
19 #include "pool_parser.h"
25 #include "pool_memory.h"
26 #include "pool_string.h"
30 String *init_string(char *str)
32 String *string = palloc(sizeof(String));
37 pool_error("init_string: palloc failed: %s", strerror(errno));
41 size = (strlen(str) + 1) / STRING_SIZE + 1;
43 string->data = palloc(STRING_SIZE * size);
44 if (string->data == NULL)
46 pool_error("init_string: palloc failed: %s", strerror(errno));
51 memset(string->data, 0, STRING_SIZE * size);
59 string->len = strlen(str);
60 memcpy(string->data, str, string->len);
66 void string_append_string(String *string, String *data)
68 string_append_char(string, data->data);
71 void string_append_char(String *string, char *append_data)
73 int len = strlen(append_data);
75 if (string->len + len + 1 > string->size * STRING_SIZE)
78 size = (string->len + len + 1) / STRING_SIZE + 1;
79 old_size = string->size;
81 string->data = repalloc(string->data, string->size * STRING_SIZE);
82 if (string->data == NULL)
84 pool_error("string_append_char: realloc failed: %s", strerror(errno));
87 memset(string->data + (old_size * STRING_SIZE),
88 0, STRING_SIZE * (string->size - old_size));
90 memcpy(string->data + string->len, append_data, len);
94 void free_string(String *string)
100 String *copy_string(String *string)
102 String *copy = palloc(sizeof(String));
106 pool_error("copy_string: palloc failed: %s", strerror(errno));
109 copy->size = string->size;
110 copy->len = string->len;
111 copy->data = palloc(string->size * STRING_SIZE);
112 if (copy->data == NULL)
114 pool_error("copy_string: palloc failed: %s", strerror(errno));
118 memcpy(copy->data, string->data, string->size * STRING_SIZE);