1 /* typecast_array.c - array typecasters
3 * Copyright (C) 2005 Federico Di Gregorio <fog@debian.org>
5 * This file is part of the psycopg module.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #define MAX_DIMENSIONS 16
24 /** typecast_array_cleanup - remove the horrible [...]= stuff **/
27 typecast_array_cleanup(char **str, int *len)
31 if ((*str)[0] != '[') return -1;
33 for (i=1 ; depth > 0 && i < *len ; i++) {
36 else if ((*str)[i] == ']')
39 if ((*str)[i] != '=') return -1;
41 *str = &((*str)[i+1]);
46 /** typecast_array_scan - scan a string looking for array items **/
48 #define ASCAN_ERROR -1
53 #define ASCAN_QUOTED 4
56 typecast_array_tokenize(char *str, int strlength,
57 int *pos, char** token, int *length)
60 int i, j, q, b, l, res;
62 Dprintf("typecast_array_tokenize: '%s', %d/%d",
63 &str[*pos], *pos, strlength);
65 /* we always get called with pos pointing at the start of a token, so a
66 fast check is enough for ASCAN_EOF, ASCAN_BEGIN and ASCAN_END */
67 if (*pos == strlength) {
70 else if (str[*pos] == '{') {
74 else if (str[*pos] == '}') {
81 /* now we start looking for the first unquoted ',' or '}', the only two
82 tokens that can limit an array element */
83 q = 0; /* if q is odd we're inside quotes */
84 b = 0; /* if b is 1 we just encountered a backslash */
87 for (i = *pos ; i < strlength ; i++) {
101 /* we're backslashing a backslash */
107 if (b == 0 && ((q&1) == 0))
112 /* reset the backslash counter */
119 /* remove initial quoting character and calculate raw length */
121 if (str[*pos] == '"') {
126 if (res == ASCAN_QUOTED) {
127 char *buffer = PyMem_Malloc(l+1);
128 if (buffer == NULL) return ASCAN_ERROR;
132 for (j = *pos; j < *pos+l; j++) {
134 || (j > *pos && str[j-1] == '\\'))
135 *(buffer++) = str[j];
139 *length = buffer - *token;
148 /* skip the comma and set position to the start of next token */
149 if (str[i] == ',') *pos += 1;
155 typecast_array_scan(char *str, int strlength,
156 PyObject *curs, PyObject *base, PyObject *array)
158 int state, length = 0, pos = 0;
161 PyObject *stack[MAX_DIMENSIONS];
166 state = typecast_array_tokenize(str, strlength, &pos, &token, &length);
167 Dprintf("typecast_array_scan: state = %d, length = %d, token = '%s'",
168 state, length, token);
169 if (state == ASCAN_TOKEN || state == ASCAN_QUOTED) {
170 PyObject *obj = typecast_cast(base, token, length, curs);
172 /* before anything else we free the memory */
173 if (state == ASCAN_QUOTED) PyMem_Free(token);
174 if (obj == NULL) return 0;
176 PyList_Append(array, obj);
180 else if (state == ASCAN_BEGIN) {
181 PyObject *sub = PyList_New(0);
182 if (sub == NULL) return 0;
184 PyList_Append(array, sub);
187 if (stack_index == MAX_DIMENSIONS)
190 stack[stack_index++] = array;
194 else if (state == ASCAN_ERROR) {
198 else if (state == ASCAN_END) {
199 if (--stack_index < 0)
201 array = stack[stack_index];
204 else if (state == ASCAN_EOF)
212 /** GENERIC - a generic typecaster that can be used when no special actions
213 have to be taken on the single items **/
216 typecast_GENERIC_ARRAY_cast(char *str, int len, PyObject *curs)
218 PyObject *obj = NULL;
219 PyObject *base = ((typecastObject*)((cursorObject*)curs)->caster)->bcast;
221 Dprintf("typecast_GENERIC_ARRAY_cast: str = '%s', len = %d", str, len);
223 if (str == NULL) {Py_INCREF(Py_None); return Py_None;}
225 typecast_array_cleanup(&str, &len);
227 PyErr_SetString(Error, "array does not start with '{'");
231 Dprintf("typecast_GENERIC_ARRAY_cast: str = '%s', len = %d", str, len);
235 /* scan the array skipping the first level of {} */
236 if (typecast_array_scan(&str[1], len-2, curs, base, obj) == 0) {
244 /** almost all the basic array typecasters are derived from GENERIC **/
246 #define typecast_LONGINTEGERARRAY_cast typecast_GENERIC_ARRAY_cast
247 #define typecast_INTEGERARRAY_cast typecast_GENERIC_ARRAY_cast
248 #define typecast_FLOATARRAY_cast typecast_GENERIC_ARRAY_cast
249 #define typecast_DECIMALARRAY_cast typecast_GENERIC_ARRAY_cast
250 #define typecast_STRINGARRAY_cast typecast_GENERIC_ARRAY_cast
251 #define typecast_UNICODEARRAY_cast typecast_GENERIC_ARRAY_cast
252 #define typecast_BOOLEANARRAY_cast typecast_GENERIC_ARRAY_cast
253 #define typecast_DATETIMEARRAY_cast typecast_GENERIC_ARRAY_cast
254 #define typecast_DATEARRAY_cast typecast_GENERIC_ARRAY_cast
255 #define typecast_TIMEARRAY_cast typecast_GENERIC_ARRAY_cast
256 #define typecast_INTERVALARRAY_cast typecast_GENERIC_ARRAY_cast
257 #define typecast_BINARYARRAY_cast typecast_GENERIC_ARRAY_cast
258 #define typecast_ROWIDARRAY_cast typecast_GENERIC_ARRAY_cast