-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuffer.c
More file actions
92 lines (85 loc) · 2.94 KB
/
Buffer.c
File metadata and controls
92 lines (85 loc) · 2.94 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
83
84
85
86
87
88
89
90
//-----------------------------------------------------------------------------
// Buffer.c
// Defines buffer structure and routines for populating it. These are used
// to translate Python objects into the buffers needed for Dameng, including
// Unicode or buffer objects.
//-----------------------------------------------------------------------------
#include "Buffer.h"
//-----------------------------------------------------------------------------
// pyBuffer_Init()
// Initialize the buffer with an empty string. Returns 0 as a convenience to
// the caller.
//-----------------------------------------------------------------------------
static
sdint2
dmBuffer_Init(
dm_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
dmBuffer_Copy(
dm_Buffer* buf, // buffer to copy into
dm_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.
//-----------------------------------------------------------------------------
sdint2
dmBuffer_FromObject(
dm_Buffer* buf, // buffer to fill
PyObject* obj, // object (string or Unicode object)
const char* encoding // encoding to use, if applicable
)
{
if (!obj)
return dmBuffer_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);
#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION > 11
buf->numCharacters = PyUnicode_GET_LENGTH(obj);
#else
buf->numCharacters = PyUnicode_GET_SIZE(obj);
#endif
} 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, "buffer type error!");
return -1;
}
return 0;
}