Repository navigation
Expand file tree
/
Copy pathregex_array.c
More file actions
234 lines (198 loc) · 4.7 KB
/
Copy pathregex_array.c
File metadata and controls
234 lines (198 loc) · 4.7 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* -*-pgsql-c-*- */
/*
* $Header$
*
* pgpool: a language independent connection pool server for PostgreSQL
* written by Tatsuo Ishii
*
* Copyright (c) 2003-2016 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.
*
*/
/*
* This module handles regular expressio arrary.
*/
#include <string.h>
#include "pool.h"
#include "pool_config.h"
#include "utils/regex_array.h"
#ifndef POOL_PRIVATE
#include "utils/elog.h"
#include "utils/palloc.h"
#else
#include "utils/fe_ports.h"
#endif
/*
* Create RegArray object
*/
RegArray *create_regex_array(void)
{
RegArray *ar;
ar = palloc(sizeof(RegArray));
ar->pos = 0;
ar->size = AR_ALLOC_UNIT;
ar->regex = (regex_t **)palloc(sizeof(regex_t *) * ar->size);
return ar;
}
/*
* Add an regular expression pattern
*/
int add_regex_array(RegArray *ar, char *pattern)
{
int regex_flags;
regex_t *regex;
char *pat;
int len;
if (ar == NULL)
{
ereport(WARNING,
(errmsg("failed to add regex pattern, regex array is NULL")));
return -1;
}
if (pattern == NULL)
{
ereport(WARNING,
(errmsg("failed to add regex pattern, regex pattern is NULL")));
return -1;
}
len = strlen(pattern);
/* Force case insensitive pattern matching */
regex_flags = REG_NOSUB;
regex_flags |= REG_ICASE;
/* Add extended regex search */
regex_flags |= REG_EXTENDED;
pat = palloc(sizeof(char)*(len+3));
if (strncmp(pattern, "^", 1) != 0)
{
strncpy(pat, "^", 2);
strncat(pat, pattern, len + 1);
}
else
{
strncpy(pat, pattern, len + 1);
}
if (len == 0 || (len > 0 && pattern[len - 1] != '$'))
{
strncat(pat, "$", 2);
}
/* Compile our regex */
regex = palloc(sizeof(regex_t));
if (regcomp(regex, pat, regex_flags) != 0)
{
ereport(WARNING,
(errmsg("failed to add regex pattern, invalid regex pattern: \"%s\" (%s)", pattern, pat)));
pfree(regex);
pfree(pat);
return -1;
}
pfree(pat);
if (ar->pos == ar->size)
{
ar->size += AR_ALLOC_UNIT;
ar->regex = repalloc(ar->regex, sizeof(regex_t *) * ar->size);
}
ar->regex[ar->pos] = regex;
ar->pos++;
return 0;
}
/*
* Execute regex matching. Returns matched array index.
*/
int regex_array_match(RegArray *ar, char *pattern)
{
int i;
if (ar == NULL)
{
ereport(WARNING,
(errmsg("failed to execute regex matching, regex array is NULL")));
return -1;
}
if (pattern == NULL)
{
ereport(WARNING,
(errmsg("failed to execute regex matching, pattern is NULL")));
return -1;
}
for (i=0;i<ar->pos;i++)
{
if (regexec(ar->regex[i], pattern, 0, 0, 0) == 0)
return i;
}
return -1;
}
/*
* Destroy RegArray object
*/
void destroy_regex_arrary(RegArray *ar)
{
pfree(ar->regex);
pfree(ar);
}
/*
* Create L-R token array
*/
Left_right_tokens *create_lrtoken_array(void)
{
Left_right_tokens *ar;
ar = palloc(sizeof(Left_right_tokens));
ar->pos = 0;
ar->size = AR_ALLOC_UNIT;
ar->token = (Left_right_token *)palloc(sizeof(Left_right_token) * ar->size);
return ar;
}
/*
* Extract tokens separated by delimi from str. Each token is separeted by delim2,
* and token lists are returned to left_tokens and right_tokens respectively.
* Nnumber of tokens is set to *n.
*/
void extract_string_tokens2(char *str, char *delimi, char delimi2, Left_right_tokens *lrtokens)
{
char *token;
char *mystr;
mystr = pstrdup(str);
for (token = strtok(mystr, delimi); token != NULL; token = strtok(NULL, delimi))
{
int len;
char *left_token;
char *right_token;
int i,j;
len = strlen(token)+1;
left_token = palloc(len);
right_token = palloc(len);
for (i=0;token[i] && token[i] != delimi2;i++)
left_token[i] = token[i];
left_token[i] = '\0';
if (i == len -1)
{
pfree(left_token);
pfree(right_token);
pfree(mystr);
return;
}
/* Skip delimiter 2 */
i++;
j = 0;
for (;token[i];i++)
right_token[j++] = token[i];
right_token[j] = '\0';
if (lrtokens->pos == lrtokens->size)
{
lrtokens->size += AR_ALLOC_UNIT;
lrtokens->token = repalloc(lrtokens->token, sizeof(Left_right_token) * lrtokens->size);
}
lrtokens->token[lrtokens->pos].left_token = left_token;
lrtokens->token[lrtokens->pos].right_token = right_token;
lrtokens->pos++;
}
pfree(mystr);
}