3 * $Header: /cvsroot/pgpool/pgpool-II/pool_params.c,v 1.3.2.1 2009/08/22 04:19:49 t-ishii Exp $
5 * pgpool: a language independent connection pool server for PostgreSQL
6 * written by Tatsuo Ishii
8 * Copyright (c) 2003-2008 PgPool Global Development Group
10 * Permission to use, copy, modify, and distribute this software and
11 * its documentation for any purpose and without fee is hereby
12 * granted, provided that the above copyright notice appear in all
13 * copies and that both that copyright notice and this permission
14 * notice appear in supporting documentation, and that the name of the
15 * author not be used in advertising or publicity pertaining to
16 * distribution of the software without specific, written prior
17 * permission. The author makes no representations about the
18 * suitability of this software for any purpose. It is provided "as
19 * is" without express or implied warranty.
21 * params.c: Paramter Status handling routines
31 #define MAX_PARAM_ITEMS 128
34 * initialize parameter structure
36 int pool_init_params(ParamStatus *params)
39 params->names = malloc(MAX_PARAM_ITEMS*sizeof(char *));
40 if (params->names == NULL)
42 pool_error("pool_init_params: cannot allocate memory");
45 params->values = malloc(MAX_PARAM_ITEMS*sizeof(char *));
46 if (params->values == NULL)
48 pool_error("pool_init_params: cannot allocate memory");
55 * discard parameter structure
57 void pool_discard_params(ParamStatus *params)
61 for (i=0;i<params->num;i++)
63 free(params->names[i]);
64 free(params->values[i]);
71 * find param value by name. if found, its value is returned
73 * if not found, NULL is returned
75 char *pool_find_name(ParamStatus *params, char *name, int *pos)
79 for (i=0;i<params->num;i++)
81 if (!strcmp(name, params->names[i]))
84 return params->values[i];
91 * return name and value by index.
93 int pool_get_param(ParamStatus *params, int index, char **name, char **value)
95 if (index < 0 || index >= params->num)
98 *name = params->names[index];
99 *value = params->values[index];
105 * add or replace name/value pair
107 int pool_add_param(ParamStatus *params, char *name, char *value)
111 if (pool_find_name(params, name, &pos))
113 /* name already exists */
114 if (strlen(params->values[pos]) < strlen(value))
116 params->values[pos] = realloc(params->values[pos], strlen(value) + 1);
117 if (params->values[pos] == NULL)
119 pool_error("pool_init_params: cannot allocate memory");
123 strcpy(params->values[pos], value);
129 /* add name/value pair */
130 if (params->num >= MAX_PARAM_ITEMS)
132 pool_error("pool_add_param: no more room for num");
136 params->names[num] = strdup(name);
137 if (params->names[num] == NULL)
139 pool_error("pool_init_params: cannot allocate memory");
142 params->values[num] = strdup(value);
143 if (params->values[num] == NULL)
145 pool_error("pool_init_params: cannot allocate memory");
153 void pool_param_debug_print(ParamStatus *params)
157 for (i=0;i<params->num;i++)
159 pool_debug("No.%d: name: %s value: %s", i, params->names[i], params->values[i]);