forked from oracle/python-cx_Oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuffer.c
More file actions
83 lines (75 loc) · 2.8 KB
/
Buffer.c
File metadata and controls
83 lines (75 loc) · 2.8 KB
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
//-----------------------------------------------------------------------------
// Copyright 2016, 2017, Oracle and/or its affiliates. All rights reserved.
//
// Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved.
//
// Portions Copyright 2001-2007, Computronix (Canada) Ltd., Edmonton, Alberta,
// Canada. All rights reserved.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// 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 char *ptr;
uint32_t numCharacters;
uint32_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)
{
buf->ptr = NULL;
buf->size = 0;
buf->numCharacters = 0;
buf->obj = NULL;
return 0;
}
//-----------------------------------------------------------------------------
// cxBuffer_FromObject()
// Populate the string buffer from a unicode object.
//-----------------------------------------------------------------------------
static int cxBuffer_FromObject(udt_Buffer *buf, PyObject *obj,
const char *encoding)
{
if (!obj)
return cxBuffer_Init(buf);
if (PyUnicode_Check(obj)) {
buf->obj = PyUnicode_AsEncodedString(obj, encoding, NULL);
if (!buf->obj)
return -1;
buf->ptr = PyBytes_AS_STRING(buf->obj);
buf->size = (uint32_t) PyBytes_GET_SIZE(buf->obj);
#if PY_MAJOR_VERSION < 3
buf->numCharacters = (uint32_t) PyUnicode_GET_SIZE(obj);
#else
buf->numCharacters = (uint32_t) PyUnicode_GET_LENGTH(obj);
#endif
} else if (PyBytes_Check(obj)) {
Py_INCREF(obj);
buf->obj = obj;
buf->ptr = PyBytes_AS_STRING(buf->obj);
buf->size = buf->numCharacters = (uint32_t) PyBytes_GET_SIZE(buf->obj);
#if PY_MAJOR_VERSION < 3
} else if (PyBuffer_Check(obj)) {
Py_ssize_t temp;
if (PyObject_AsReadBuffer(obj, (void*) &buf->ptr, &temp) < 0)
return -1;
Py_INCREF(obj);
buf->obj = obj;
buf->numCharacters = buf->size = (uint32_t) temp;
#endif
} else {
PyErr_SetString(PyExc_TypeError, CXORA_TYPE_ERROR);
return -1;
}
return 0;
}
#define cxBuffer_Clear(buf) Py_CLEAR((buf)->obj)