File: pool_params.c

package info (click to toggle)
pgpool2 3.3.4-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 9,196 kB
  • sloc: ansic: 59,850; sh: 12,893; yacc: 10,787; lex: 4,637; sql: 743; makefile: 481; java: 469; php: 125; ruby: 98; asm: 5
file content (163 lines) | stat: -rw-r--r-- 3,595 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* -*-pgsql-c-*- */
/*
 * $Header$
 *
 * pgpool: a language independent connection pool server for PostgreSQL
 * written by Tatsuo Ishii
 *
 * Copyright (c) 2003-2008	PgPool Global Development Group
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that copyright notice and this permission
 * notice appear in supporting documentation, and that the name of the
 * author not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. The author makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * params.c: Parameter Status handling routines
 *
 */
#include "config.h"

#include <stdlib.h>
#include <string.h>

#include "pool.h"
#include "parser/parser.h"

#define MAX_PARAM_ITEMS 128

/*
 * initialize parameter structure
 */
int pool_init_params(ParamStatus *params)
{
    params->num = 0;
    params->names = malloc(MAX_PARAM_ITEMS*sizeof(char *));
	if (params->names == NULL)
	{
		pool_error("pool_init_params: cannot allocate memory");
		return -1;
	}
    params->values = malloc(MAX_PARAM_ITEMS*sizeof(char *));
	if (params->values == NULL)
	{
		pool_error("pool_init_params: cannot allocate memory");
		return -1;
	}
	return 0;
}

/*
 * discard parameter structure
 */
void pool_discard_params(ParamStatus *params)
{
    int i;

    for (i=0;i<params->num;i++)
    {
		free(params->names[i]);
		free(params->values[i]);
    }
    free(params->names);
    free(params->values);
}

/*
 * find param value by name. if found, its value is returned
 * also, pos is set
 * if not found, NULL is returned
 */
char *pool_find_name(ParamStatus *params, char *name, int *pos)
{
    int i;

    for (i=0;i<params->num;i++)
    {
		if (!strcmp(name, params->names[i]))
		{
			*pos = i;
			return params->values[i];
		}
    }
    return NULL;
}

/*
 * return name and value by index.
 */
int pool_get_param(ParamStatus *params, int index, char **name, char **value)
{
	if (index < 0 || index >= params->num)
		return -1;

	*name = params->names[index];
	*value = params->values[index];

	return 0;
}

/*
 * add or replace name/value pair
 */
int pool_add_param(ParamStatus *params, char *name, char *value)
{
    int pos;

    if (pool_find_name(params, name, &pos))
    {
		/* name already exists */
		if (strlen(params->values[pos]) < strlen(value))
		{
			params->values[pos] = realloc(params->values[pos], strlen(value) + 1);
			if (params->values[pos] == NULL)
			{
				pool_error("pool_init_params: cannot allocate memory");
				return -1;
			}
		}
		strcpy(params->values[pos], value);
    }
    else
    {
		int num;

		/* add name/value pair */
		if (params->num >= MAX_PARAM_ITEMS)
		{
			pool_error("pool_add_param: no more room for num");
			return -1;
		}
		num = params->num;
		params->names[num] = strdup(name);
		if (params->names[num] == NULL)
		{
			pool_error("pool_init_params: cannot allocate memory");
			return -1;
		}
		params->values[num] = strdup(value);
		if (params->values[num] == NULL)
		{
			pool_error("pool_init_params: cannot allocate memory");
			return -1;
		}
		params->num++;
    }
	parser_set_param(name, value);
	return 0;
}

void pool_param_debug_print(ParamStatus *params)
{
	int i;

    for (i=0;i<params->num;i++)
    {
		pool_debug("No.%d: name: %s value: %s", i, params->names[i], params->values[i]);
	}
}