-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpg_array.c
151 lines (144 loc) · 3.96 KB
/
pg_array.c
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
/*-------------------------------------------------------------------------
* pg_array.c
*
* Functions for libpq array manipulation
* This is intended to contain functions to manage libpq arrays.
* This isn't likely to be comprehensive
*
* Copyright (c) 2004-2007, PostgreSQL Global Development Group
* Author: Joshua Tolley
*
* $Id: pg_array.c,v 1.6 2007/09/13 14:20:43 h-saito Exp $
*
*-------------------------------------------------------------------------
*/
#include "pg_array.h"
/* Text array format consists of leading and trailing curly braces. Values containing
* characters that might be confusing (backslashes, double-quotes, etc.) are double-
* quoted, and confusing characters are backslash-escaped */
/* pg_text_array_parse()
* Takes an input string as returned by PQgetvalue(), parses it as a text array, and
* builds an array of strings, which it returns, or NULL on error. pg_text_array also
* sets len to the number of strings in the array. THE USER IS RESPONSIBLE
* FOR FREEING CHAR ***OUTPUT, perhaps by calling pg_text_array_free. */
/* TODO: Add debugging/logging */
char ** pg_text_array_parse(char *input, int *len) {
char **results = NULL, **tmp;
char *incursor, *outcursor, *output, *start_element;
int escaped = 0, done_single_element = 0, done_all_elements = 0, i,
cursorlen, inputlen, elements = 0, outputlen, error = 0, in_quotes = 0;
inputlen = strlen(input);
if (!inputlen || inputlen < 3 || SIZE_MAX / inputlen < sizeof(char*)) return NULL;
incursor = input + 1;
while (!done_all_elements) {
in_quotes = 0;
if (*incursor == '"') {
incursor++;
in_quotes = 1;
}
/* else if (*incursor == ',') break; */
start_element = incursor;
outcursor = strpbrk(incursor, ",}");
if (!outcursor) return NULL;
/* FACT: outcursor - incursor >= 1 */
outputlen = outcursor - incursor + 1;
output = calloc(outputlen, sizeof(char));
if (!output) return NULL;
outcursor = output;
done_single_element = 0;
while (!done_single_element) {
if (!escaped) {
switch (*incursor) {
case '}':
done_single_element = 1;
done_all_elements = 1;
break;
case ',':
if (!in_quotes) done_single_element = 1;
break;
case '"':
in_quotes = 0;
done_single_element = 1;
incursor++;
if (*incursor == '}') done_all_elements = 1;
break;
}
}
if (!done_single_element) {
if (!escaped && *incursor == '\\') escaped = 1;
else {
if (outcursor - output >= outputlen) {
cursorlen = outcursor - output;
outcursor = strpbrk(incursor + 1, ",}");
if (!outcursor) {
error = 1;
break;
}
outputlen = outcursor - start_element;
outcursor = realloc(output, outputlen * sizeof(char));
if (!outcursor) {
error = 1;
break;
}
output = outcursor;
outcursor = output + cursorlen;
}
if (escaped) {
escaped = 0;
*outcursor = *incursor;
outcursor++;
}
else {
*outcursor = *incursor;
outcursor++;
}
}
}
if (done_single_element) {
elements++;
memset(outcursor, 0, outputlen - (outcursor - output));
if (results) {
tmp = realloc(results, elements * sizeof(char*));
if (!tmp) {
error = 1;
break;
}
results = tmp;
}
else {
results = calloc(1, sizeof(char*));
if (!results) {
error = 1;
break;
}
}
results[elements - 1] = output;
}
if (!done_all_elements) {
incursor++;
if (incursor - input >= inputlen) {
error = 1;
break;
}
}
}
if (error) break;
}
if (error) {
for (i = 0; i < elements; i++)
free(results[i]);
free(results);
free(output);
return NULL;
}
*len = elements;
return results;
}
/* pg_text_array_free()
* Frees an array of strings such as is returned by pg_text_array_parse() */
void pg_text_array_free(char **array, int len) {
int i;
for (i = 0; i < len; i++)
free(array[i]);
free(array);
}