forked from oracle/python-cx_Oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcxoBuffer.c
More file actions
60 lines (55 loc) · 2.24 KB
/
cxoBuffer.c
File metadata and controls
60 lines (55 loc) · 2.24 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
//-----------------------------------------------------------------------------
// Copyright (c) 2016, 2020, 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.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// cxoBuffer.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.
//-----------------------------------------------------------------------------
#include "cxoModule.h"
//-----------------------------------------------------------------------------
// cxoBuffer_fromObject()
// Populate the string buffer from a unicode object.
//-----------------------------------------------------------------------------
int cxoBuffer_fromObject(cxoBuffer *buf, PyObject *obj, const char *encoding)
{
cxoBuffer_init(buf);
if (!obj || obj == Py_None)
return 0;
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);
buf->numCharacters = (uint32_t) PyUnicode_GET_LENGTH(obj);
} 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);
} else {
PyErr_SetString(PyExc_TypeError, "expecting string or bytes object");
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
// cxoBuffer_init()
// Initialize the buffer with an empty string. Returns 0 as a convenience to
// the caller.
//-----------------------------------------------------------------------------
int cxoBuffer_init(cxoBuffer *buf)
{
buf->ptr = NULL;
buf->size = 0;
buf->numCharacters = 0;
buf->obj = NULL;
return 0;
}