]> git.8kb.co.uk Git - pgpool-ii/pgpool-ii_2.2.5/blob - parser/pool_string.c
Attempt to send a proper failure message to frontend when authentication
[pgpool-ii/pgpool-ii_2.2.5] / parser / pool_string.c
1 /* -*-pgsql-c-*- */
2 /*
3  * $Header: /cvsroot/pgpool/pgpool-II/parser/pool_string.c,v 1.4 2008/01/29 01:56:39 y-asaba Exp $
4  *
5  * Copyright (c) 2006-2008, pgpool Global Development Group
6  *
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.
17  */
18 #include "pool.h"
19 #include "pool_parser.h"
20
21 #include <errno.h>
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include "pool_memory.h"
26 #include "pool_string.h"
27 #include "value.h"
28
29 /* String Library */
30 String *init_string(char *str)
31 {
32         String *string = palloc(sizeof(String));
33         int size;
34
35         if (string == NULL)
36         {
37                 pool_error("init_string: palloc failed: %s", strerror(errno));
38                 child_exit(1);
39         }
40
41         size = (strlen(str) + 1) / STRING_SIZE + 1;
42         string->size = size;
43         string->data = palloc(STRING_SIZE * size);
44         if (string->data == NULL)
45         {
46                 pool_error("init_string: palloc failed: %s", strerror(errno));
47                 pfree(string);
48                 child_exit(1);
49         }
50
51         memset(string->data, 0, STRING_SIZE * size);
52
53         if (str == NULL)
54         {
55                 string->len = 0;
56         }
57         else
58         {
59                 string->len = strlen(str);
60                 memcpy(string->data, str, string->len);
61         }
62         
63         return string;
64 }
65
66 void string_append_string(String *string, String *data)
67 {
68         string_append_char(string, data->data);
69 }
70
71 void string_append_char(String *string, char *append_data)
72 {
73         int len = strlen(append_data);
74         
75         if (string->len + len + 1 > string->size * STRING_SIZE)
76         {
77                 int size, old_size;
78                 size = (string->len + len + 1) / STRING_SIZE + 1;
79                 old_size = string->size; 
80                 string->size = size;
81                 string->data = repalloc(string->data, string->size * STRING_SIZE);
82                 if (string->data == NULL)
83                 {
84                         pool_error("string_append_char: realloc failed: %s", strerror(errno));
85                         child_exit(1);
86                 }
87                 memset(string->data + (old_size * STRING_SIZE),
88                            0, STRING_SIZE * (string->size - old_size));
89         }
90         memcpy(string->data + string->len, append_data, len);
91         string->len += len;
92 }
93
94 void free_string(String *string)
95 {
96         pfree(string->data);
97         pfree(string);
98 }
99
100 String *copy_string(String *string)
101 {
102         String *copy = palloc(sizeof(String));
103
104         if (copy == NULL)
105         {
106                 pool_error("copy_string: palloc failed: %s", strerror(errno));
107                 child_exit(1);
108         }
109         copy->size = string->size;
110         copy->len = string->len;
111         copy->data = palloc(string->size * STRING_SIZE);
112         if (copy->data == NULL)
113         {
114                 pool_error("copy_string: palloc failed: %s", strerror(errno));
115                 pfree(copy);
116                 child_exit(1);
117         }               
118         memcpy(copy->data, string->data, string->size * STRING_SIZE);
119         
120         return copy;
121 }