This commit was generated by cvs2svn to compensate for changes in r431,
[plcapi.git] / psycopg2 / psycopg / typecast_basic.c
1 /* pgcasts_basic.c - basic typecasting functions to python types
2  *
3  * Copyright (C) 2001-2003 Federico Di Gregorio <fog@debian.org>
4  *
5  * This file is part of the psycopg module.
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 /** INTEGER - cast normal integers (4 bytes) to python int **/
23
24 static PyObject *
25 typecast_INTEGER_cast(char *s, int len, PyObject *curs)
26 {
27     char buffer[12];
28     
29     if (s == NULL) {Py_INCREF(Py_None); return Py_None;}
30     if (s[len] != '\0') {
31         strncpy(buffer, s, len); buffer[len] = '\0';
32         s = buffer;
33     }
34     return PyInt_FromString(s, NULL, 0);
35 }
36
37 /** LONGINTEGER - cast long integers (8 bytes) to python long **/
38
39 static PyObject *
40 typecast_LONGINTEGER_cast(char *s, int len, PyObject *curs)
41 {
42     char buffer[24];
43     
44     if (s == NULL) {Py_INCREF(Py_None); return Py_None;}
45     if (s[len] != '\0') {
46         strncpy(buffer, s, len); buffer[len] = '\0';
47         s = buffer;
48     }
49     return PyLong_FromString(s, NULL, 0);
50 }
51
52 /** FLOAT - cast floating point numbers to python float **/
53
54 static PyObject *
55 typecast_FLOAT_cast(char *s, int len, PyObject *curs)
56 {
57     PyObject *str = NULL, *flo = NULL;
58     char *pend;
59     
60     if (s == NULL) {Py_INCREF(Py_None); return Py_None;}
61     str = PyString_FromStringAndSize(s, len);
62     flo = PyFloat_FromString(str, &pend);
63     Py_DECREF(str);
64     return flo;
65 }
66
67 /** STRING - cast strings of any type to python string **/
68
69 static PyObject *
70 typecast_STRING_cast(char *s, int len, PyObject *curs)
71 {
72     if (s == NULL) {Py_INCREF(Py_None); return Py_None;}
73     return PyString_FromStringAndSize(s, len);
74 }
75
76 /** UNICODE - cast strings of any type to a python unicode object **/
77
78 static PyObject *
79 typecast_UNICODE_cast(char *s, int len, PyObject *curs)
80 {
81     PyObject *enc;
82
83     if (s == NULL) {Py_INCREF(Py_None); return Py_None;}
84
85     enc = PyDict_GetItemString(psycoEncodings,
86                                ((cursorObject*)curs)->conn->encoding);
87     if (enc) {
88         return PyUnicode_Decode(s, len, PyString_AsString(enc), NULL);
89     }
90     else {
91        PyErr_Format(InterfaceError,
92                     "can't decode into unicode string from %s",
93                     ((cursorObject*)curs)->conn->encoding);
94        return NULL; 
95     }
96 }
97
98 /** BOOLEAN - cast boolean value into right python object **/
99
100 static PyObject *
101 typecast_BOOLEAN_cast(char *s, int len, PyObject *curs)
102 {
103     PyObject *res;
104
105     if (s == NULL) {Py_INCREF(Py_None); return Py_None;}
106
107     if (s[0] == 't')
108         res = Py_True;
109     else
110         res = Py_False;
111
112     Py_INCREF(res);
113     return res;
114 }
115
116 /** DECIMAL - cast any kind of number into a Python Decimal object **/
117
118 #ifdef HAVE_DECIMAL
119 static PyObject *
120 typecast_DECIMAL_cast(char *s, int len, PyObject *curs)
121 {
122     PyObject *res = NULL;
123     char *buffer;
124     
125     if (s == NULL) {Py_INCREF(Py_None); return Py_None;}
126
127     if ((buffer = PyMem_Malloc(len+1)) == NULL)
128         PyErr_NoMemory();
129     strncpy(buffer, s, len); buffer[len] = '\0';
130     res = PyObject_CallFunction(decimalType, "s", buffer);
131     PyMem_Free(buffer);
132
133     return res;
134 }
135 #else
136 #define typecast_DECIMAL_cast  typecast_FLOAT_cast
137 #endif
138      
139 /* some needed aliases */
140 #define typecast_NUMBER_cast   typecast_FLOAT_cast
141 #define typecast_ROWID_cast    typecast_INTEGER_cast