diff src/bufferproxy.c @ 870:31fa129efd0f

Fixed Mask documentation. Added write() method to BufferProxy.
author marcus
date Wed, 02 Jan 2008 11:36:57 +0000
parents a249a10980f0
children e8926413cb3e
line wrap: on
line diff
--- a/src/bufferproxy.c	Wed Jan 02 11:13:00 2008 +0000
+++ b/src/bufferproxy.c	Wed Jan 02 11:36:57 2008 +0000
@@ -39,7 +39,9 @@
                                    PyObject *kwds);
 static void _bufferproxy_dealloc (PyBufferProxy *self);
 static PyObject* _bufferproxy_get_dict (PyBufferProxy *self, void *closure);
+static PyObject* _bufferproxy_get_raw (PyBufferProxy *buffer, void *closure);
 static PyObject* _bufferproxy_repr (PyBufferProxy *self);
+static PyObject* _bufferproxy_write (PyBufferProxy *buffer, PyObject *args);
 
 /* Buffer methods */
 static Py_ssize_t _bufferproxy_getreadbuf (PyBufferProxy *buffer,
@@ -60,6 +62,8 @@
  */
 static PyMethodDef _bufferproxy_methods[] =
 {
+    { "write", (PyCFunction) _bufferproxy_write, METH_VARARGS,
+      "Writes raw data to the buffer" },
     { NULL, NULL, 0, NULL }
 };
 
@@ -69,6 +73,8 @@
 static PyGetSetDef _bufferproxy_getsets[] =
 {
     { "__dict__", (getter) _bufferproxy_get_dict, NULL, NULL, NULL },
+    { "raw", (getter) _bufferproxy_get_raw, NULL,
+      "The raw buffer data as string", NULL },
     { NULL, NULL, NULL, NULL, NULL }
 };
 
@@ -88,8 +94,8 @@
 {
     PyObject_HEAD_INIT(NULL)
     0,
-    "BufferProxy",               /* tp_name */
-    sizeof (PyBufferProxy),      /* tp_basicsize */
+    "BufferProxy",              /* tp_name */
+    sizeof (PyBufferProxy),     /* tp_basicsize */
     0,                          /* tp_itemsize */
     (destructor) _bufferproxy_dealloc, /* tp_dealloc */
     0,                          /* tp_print */
@@ -124,7 +130,7 @@
     offsetof (PyBufferProxy, dict), /* tp_dictoffset */
     0,                          /* tp_init */
     0,                          /* tp_alloc */
-    _bufferproxy_new,               /* tp_new */
+    _bufferproxy_new,           /* tp_new */
     0,                          /* tp_free */
     0,                          /* tp_is_gc */
     0,                          /* tp_bases */
@@ -188,10 +194,24 @@
     return self->dict;
 }
 
+/**
+ * Getter for PyBufferProxy.raw.
+ */
+static PyObject*
+_bufferproxy_get_raw (PyBufferProxy *buffer, void *closure)
+{
+    return PyString_FromStringAndSize (buffer->buffer, buffer->length);
+}
+
+/**** Methods ****/
+
+/**
+ * Representation method.
+ */
 static PyObject*
 _bufferproxy_repr (PyBufferProxy *self)
 {
-/* zd is for Py_size_t which python < 2.5 doesn't have. */
+    /* zd is for Py_size_t which python < 2.5 doesn't have. */
 #if PY_VERSION_HEX < 0x02050000
     return PyString_FromFormat("<BufferProxy(%d)>", self->length);
 #else
@@ -199,6 +219,29 @@
 #endif
 }
 
+/**
+ * Writes raw data to the buffer.
+ */
+static PyObject*
+_bufferproxy_write (PyBufferProxy *buffer, PyObject *args)
+{
+    Py_ssize_t offset;
+    Py_ssize_t length;
+    char *buf;
+
+    if (!PyArg_ParseTuple (args, "s#i", &buf, &length, &offset))
+        return NULL;
+
+    if (offset + length > buffer->length)
+    {
+        return RAISE (PyExc_IndexError, "bytes to write exceed buffer size");
+    }
+
+    memcpy (buffer->buffer + offset, buf, length);
+
+    Py_RETURN_NONE;
+}   
+
 /**** Buffer interfaces ****/
 
 static Py_ssize_t
@@ -286,7 +329,8 @@
         return;
 
     /* create the module */
-    module = Py_InitModule3 ("bufferproxy", NULL, NULL);
+    module = Py_InitModule3 ("bufferproxy", NULL,
+        "TODO");
     PyBufferProxy_Type.tp_getattro = PyObject_GenericGetAttr;
     Py_INCREF (&PyBufferProxy_Type);
     PyModule_AddObject (module, "BufferProxy", (PyObject *)&PyBufferProxy_Type);