Buffer.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //-----------------------------------------------------------------------------
  2. // Buffer.c
  3. // Defines buffer structure and routines for populating it. These are used
  4. // to translate Python objects into the buffers needed for Oracle, including
  5. // Unicode or buffer objects.
  6. //-----------------------------------------------------------------------------
  7. // define structure for abstracting string buffers
  8. typedef struct {
  9. const void *ptr;
  10. Py_ssize_t numCharacters;
  11. Py_ssize_t size;
  12. PyObject *obj;
  13. } udt_Buffer;
  14. //-----------------------------------------------------------------------------
  15. // cxBuffer_Init()
  16. // Initialize the buffer with an empty string. Returns 0 as a convenience to
  17. // the caller.
  18. //-----------------------------------------------------------------------------
  19. static int cxBuffer_Init(
  20. udt_Buffer *buf) // buffer to initialize
  21. {
  22. buf->ptr = NULL;
  23. buf->size = 0;
  24. buf->numCharacters = 0;
  25. buf->obj = NULL;
  26. return 0;
  27. }
  28. //-----------------------------------------------------------------------------
  29. // cxBuffer_Copy()
  30. // Copy the contents of the buffer.
  31. //-----------------------------------------------------------------------------
  32. static int cxBuffer_Copy(
  33. udt_Buffer *buf, // buffer to copy into
  34. udt_Buffer *copyFromBuf) // buffer to copy from
  35. {
  36. buf->ptr = copyFromBuf->ptr;
  37. buf->size = copyFromBuf->size;
  38. buf->numCharacters = copyFromBuf->numCharacters;
  39. Py_XINCREF(copyFromBuf->obj);
  40. buf->obj = copyFromBuf->obj;
  41. return 0;
  42. }
  43. //-----------------------------------------------------------------------------
  44. // cxBuffer_FromObject()
  45. // Populate the string buffer from a unicode object.
  46. //-----------------------------------------------------------------------------
  47. static int cxBuffer_FromObject(
  48. udt_Buffer *buf, // buffer to fill
  49. PyObject *obj, // object (string or Unicode object)
  50. const char *encoding) // encoding to use, if applicable
  51. {
  52. if (!obj)
  53. return cxBuffer_Init(buf);
  54. if (encoding && PyUnicode_Check(obj)) {
  55. buf->obj = PyUnicode_AsEncodedString(obj, encoding, NULL);
  56. if (!buf->obj)
  57. return -1;
  58. buf->ptr = PyBytes_AS_STRING(buf->obj);
  59. buf->size = PyBytes_GET_SIZE(buf->obj);
  60. buf->numCharacters = PyUnicode_GET_SIZE(obj);
  61. } else if (PyBytes_Check(obj)) {
  62. Py_INCREF(obj);
  63. buf->obj = obj;
  64. buf->ptr = PyBytes_AS_STRING(buf->obj);
  65. buf->size = buf->numCharacters = PyBytes_GET_SIZE(buf->obj);
  66. #if PY_MAJOR_VERSION < 3
  67. } else if (PyBuffer_Check(obj)) {
  68. if (PyObject_AsReadBuffer(obj, &buf->ptr, &buf->size) < 0)
  69. return -1;
  70. Py_INCREF(obj);
  71. buf->obj = obj;
  72. buf->numCharacters = buf->size;
  73. #endif
  74. } else {
  75. PyErr_SetString(PyExc_TypeError, CXORA_TYPE_ERROR);
  76. return -1;
  77. }
  78. return 0;
  79. }
  80. #define cxBuffer_Clear(buf) Py_XDECREF((buf)->obj)