-
Notifications
You must be signed in to change notification settings - Fork 2
/
Buffer.c
89 lines (80 loc) · 2.98 KB
/
Buffer.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
//-----------------------------------------------------------------------------
// Buffer.c
// Defines buffer structure and routines for populating it. These are used
// to translate Python objects into the buffers needed for Oracle, including
// Unicode or buffer objects.
//-----------------------------------------------------------------------------
// define structure for abstracting string buffers
typedef struct {
const void *ptr;
Py_ssize_t numCharacters;
Py_ssize_t size;
PyObject *obj;
} udt_Buffer;
//-----------------------------------------------------------------------------
// cxBuffer_Init()
// Initialize the buffer with an empty string. Returns 0 as a convenience to
// the caller.
//-----------------------------------------------------------------------------
static int cxBuffer_Init(
udt_Buffer *buf) // buffer to initialize
{
buf->ptr = NULL;
buf->size = 0;
buf->numCharacters = 0;
buf->obj = NULL;
return 0;
}
//-----------------------------------------------------------------------------
// cxBuffer_Copy()
// Copy the contents of the buffer.
//-----------------------------------------------------------------------------
static int cxBuffer_Copy(
udt_Buffer *buf, // buffer to copy into
udt_Buffer *copyFromBuf) // buffer to copy from
{
buf->ptr = copyFromBuf->ptr;
buf->size = copyFromBuf->size;
buf->numCharacters = copyFromBuf->numCharacters;
Py_XINCREF(copyFromBuf->obj);
buf->obj = copyFromBuf->obj;
return 0;
}
//-----------------------------------------------------------------------------
// cxBuffer_FromObject()
// Populate the string buffer from a unicode object.
//-----------------------------------------------------------------------------
static int cxBuffer_FromObject(
udt_Buffer *buf, // buffer to fill
PyObject *obj, // object (string or Unicode object)
const char *encoding) // encoding to use, if applicable
{
if (!obj)
return cxBuffer_Init(buf);
if (encoding && PyUnicode_Check(obj)) {
buf->obj = PyUnicode_AsEncodedString(obj, encoding, NULL);
if (!buf->obj)
return -1;
buf->ptr = PyBytes_AS_STRING(buf->obj);
buf->size = PyBytes_GET_SIZE(buf->obj);
buf->numCharacters = PyUnicode_GET_SIZE(obj);
} else if (PyBytes_Check(obj)) {
Py_INCREF(obj);
buf->obj = obj;
buf->ptr = PyBytes_AS_STRING(buf->obj);
buf->size = buf->numCharacters = PyBytes_GET_SIZE(buf->obj);
#if PY_MAJOR_VERSION < 3
} else if (PyBuffer_Check(obj)) {
if (PyObject_AsReadBuffer(obj, &buf->ptr, &buf->size) < 0)
return -1;
Py_INCREF(obj);
buf->obj = obj;
buf->numCharacters = buf->size;
#endif
} else {
PyErr_SetString(PyExc_TypeError, CXORA_TYPE_ERROR);
return -1;
}
return 0;
}
#define cxBuffer_Clear(buf) Py_XDECREF((buf)->obj)