psycopg2/psycopg/typecast_array.c

234 lines
6.5 KiB
C
Raw Normal View History

2005-03-22 17:20:20 +03:00
/* typecast_array.c - array typecasters
*
* Copyright (C) 2005 Federico Di Gregorio <fog@debian.org>
*
* This file is part of the psycopg module.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
2005-03-23 13:32:30 +03:00
#define MAX_DIMENSIONS 16
2005-03-22 17:20:20 +03:00
/** typecast_array_scan - scan a string looking for array items **/
2005-03-23 13:32:30 +03:00
#define ASCAN_ERROR -1
#define ASCAN_EOF 0
#define ASCAN_BEGIN 1
#define ASCAN_END 2
#define ASCAN_TOKEN 3
#define ASCAN_QUOTED 4
static int
2005-10-18 05:29:47 +04:00
typecast_array_tokenize(char *str, int strlength,
int *pos, char** token, int *length)
{
2005-03-23 20:17:48 +03:00
/* FORTRAN glory */
int i, j, q, b, l, res;
2005-03-23 20:17:48 +03:00
Dprintf("typecast_array_tokenize: '%s', %d/%d",
2005-03-23 13:32:30 +03:00
&str[*pos], *pos, strlength);
2005-03-23 20:17:48 +03:00
/* we always get called with pos pointing at the start of a token, so a
fast check is enough for ASCAN_EOF, ASCAN_BEGIN and ASCAN_END */
if (*pos == strlength) {
return ASCAN_EOF;
}
else if (str[*pos] == '{') {
*pos += 1;
return ASCAN_BEGIN;
}
else if (str[*pos] == '}') {
*pos += 1;
if (str[*pos] == ',')
*pos += 1;
return ASCAN_END;
}
/* now we start looking for the first unquoted ',' or '}', the only two
tokens that can limit an array element */
q = 0; /* if q is odd we're inside quotes */
b = 0; /* if b is 1 we just encountered a backslash */
res = ASCAN_TOKEN;
for (i = *pos ; i < strlength ; i++) {
switch (str[i]) {
2005-03-23 13:32:30 +03:00
case '"':
2005-03-23 20:17:48 +03:00
if (b == 0)
q += 1;
else
b = 0;
break;
2005-03-23 13:32:30 +03:00
case '\\':
res = ASCAN_QUOTED;
2005-03-23 20:17:48 +03:00
if (b == 0)
b = 1;
2005-03-23 13:32:30 +03:00
else
2005-03-23 20:17:48 +03:00
/* we're backslashing a backslash */
b = 0;
break;
case '}':
case ',':
if (b == 0 && ((q&1) == 0))
2005-03-23 13:32:30 +03:00
goto tokenize;
2005-03-23 20:17:48 +03:00
break;
default:
/* reset the backslash counter */
b = 0;
break;
}
}
2005-03-23 13:32:30 +03:00
tokenize:
2005-03-23 20:17:48 +03:00
/* remove initial quoting character and calculate raw length */
l = i - *pos;
if (str[*pos] == '"') {
*pos += 1;
l -= 2;
}
if (res == ASCAN_QUOTED) {
2005-10-18 05:29:47 +04:00
char *buffer = PyMem_Malloc(l+1);
2005-03-23 13:32:30 +03:00
if (buffer == NULL) return ASCAN_ERROR;
*token = buffer;
2005-03-23 20:17:48 +03:00
for (j = *pos; j < *pos+l; j++) {
if (str[j] != '\\'
|| (j > *pos && str[j-1] == '\\'))
*(buffer++) = str[j];
2005-03-23 13:32:30 +03:00
}
2005-03-23 20:17:48 +03:00
2005-03-23 13:32:30 +03:00
*buffer = '\0';
*length = (int)buffer - (int)*token;
}
else {
*token = &str[*pos];
*length = l;
}
2005-03-23 20:17:48 +03:00
*pos = i;
2005-03-23 20:17:48 +03:00
/* skip the comma and set position to the start of next token */
if (str[i] == ',') *pos += 1;
2005-03-23 13:32:30 +03:00
return res;
}
static int
2005-10-18 05:29:47 +04:00
typecast_array_scan(char *str, int strlength,
PyObject *curs, PyObject *base, PyObject *array)
{
2005-10-18 05:29:47 +04:00
int state, length = 0, pos = 0;
char *token;
2005-03-23 13:32:30 +03:00
PyObject *stack[MAX_DIMENSIONS];
int stack_index = 0;
while (1) {
2005-03-23 20:17:48 +03:00
token = NULL;
state = typecast_array_tokenize(str, strlength, &pos, &token, &length);
2005-03-23 20:17:48 +03:00
Dprintf("typecast_array_scan: state = %d, length = %d, token = '%s'",
state, length, token);
if (state == ASCAN_TOKEN || state == ASCAN_QUOTED) {
PyObject *obj = typecast_cast(base, token, length, curs);
2005-03-23 13:32:30 +03:00
/* before anything else we free the memory */
if (state == ASCAN_QUOTED) PyMem_Free(token);
if (obj == NULL) return 0;
2005-03-23 13:32:30 +03:00
PyList_Append(array, obj);
Py_DECREF(obj);
}
2005-03-23 20:17:48 +03:00
2005-03-23 13:32:30 +03:00
else if (state == ASCAN_BEGIN) {
PyObject *sub = PyList_New(0);
if (sub == NULL) return 0;
PyList_Append(array, sub);
Py_DECREF(sub);
if (stack_index == MAX_DIMENSIONS)
return 0;
stack[stack_index++] = array;
array = sub;
}
2005-03-23 20:17:48 +03:00
2005-03-23 13:32:30 +03:00
else if (state == ASCAN_ERROR) {
return 0;
}
2005-03-23 20:17:48 +03:00
else if (state == ASCAN_END) {
2005-03-23 13:32:30 +03:00
if (--stack_index < 0)
return 0;
array = stack[stack_index];
}
2005-03-23 20:17:48 +03:00
else if (state == ASCAN_EOF)
break;
}
return 1;
}
2005-03-22 17:20:20 +03:00
/** GENERIC - a generic typecaster that can be used when no special actions
have to be taken on the single items **/
2005-03-22 17:20:20 +03:00
static PyObject *
2005-10-18 05:29:47 +04:00
typecast_GENERIC_ARRAY_cast(char *str, int len, PyObject *curs)
2005-03-22 17:20:20 +03:00
{
PyObject *obj = NULL;
PyObject *base = ((typecastObject*)((cursorObject*)curs)->caster)->bcast;
2005-03-22 17:20:20 +03:00
if (str == NULL) {Py_INCREF(Py_None); return Py_None;}
if (str[0] != '{') {
PyErr_SetString(Error, "array does not start with '{'");
return NULL;
}
2005-03-23 13:32:30 +03:00
Dprintf("typecast_GENERIC_ARRAY_cast: scanning %s", str);
obj = PyList_New(0);
2005-03-22 17:20:20 +03:00
/* scan the array skipping the first level of {} */
if (typecast_array_scan(&str[1], len-2, curs, base, obj) == 0) {
Py_DECREF(obj);
obj = NULL;
}
2005-03-22 17:20:20 +03:00
return obj;
}
2005-03-23 14:02:13 +03:00
/** almost all the basic array typecasters are derived from GENERIC **/
#define typecast_LONGINTEGERARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_INTEGERARRAY_cast typecast_GENERIC_ARRAY_cast
2005-03-23 14:02:13 +03:00
#define typecast_FLOATARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_DECIMALARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_STRINGARRAY_cast typecast_GENERIC_ARRAY_cast
2005-03-23 14:02:13 +03:00
#define typecast_UNICODEARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_BOOLEANARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_DATETIMEARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_DATEARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_TIMEARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_INTERVALARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_BINARYARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_ROWIDARRAY_cast typecast_GENERIC_ARRAY_cast