Back to index

lightning-sunbird  0.9+nobinonly
Functions
PyIInputStream.cpp File Reference
#include "PyXPCOM_std.h"
#include "nsIInputStream.h"

Go to the source code of this file.

Functions

static nsIInputStreamGetI (PyObject *self)
static PyObject * DoPyRead_Buffer (nsIInputStream *pI, PyObject *obBuffer, PRUint32 n)
static PyObject * DoPyRead_Size (nsIInputStream *pI, PRUint32 n)
static PyObject * PyRead (PyObject *self, PyObject *args)

Function Documentation

static PyObject* DoPyRead_Buffer ( nsIInputStream pI,
PyObject *  obBuffer,
PRUint32  n 
) [static]

Definition at line 64 of file PyIInputStream.cpp.

{
       PRUint32 nread;
       void *buf;
       PRUint32 buf_len;
       if (PyObject_AsWriteBuffer(obBuffer, &buf, (int *)&buf_len) != 0) {
              PyErr_Clear();
              PyErr_SetString(PyExc_TypeError, "The buffer object does not have a write buffer!");
              return NULL;
       }
       if (n==(PRUint32)-1) {
              n = buf_len;
       } else {
              if (n > buf_len) {
                     NS_WARNING("Warning: PyIInputStream::read() was passed an integer size greater than the size of the passed buffer!  Buffer size used.\n");
                     n = buf_len;
              }
       }
       nsresult r;
       Py_BEGIN_ALLOW_THREADS;
       r = pI->Read((char *)buf, n, &nread);
       Py_END_ALLOW_THREADS;
       if ( NS_FAILED(r) )
              return PyXPCOM_BuildPyException(r);
       return PyInt_FromLong(nread);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* DoPyRead_Size ( nsIInputStream pI,
PRUint32  n 
) [static]

Definition at line 91 of file PyIInputStream.cpp.

{
       if (n==(PRUint32)-1) {
              nsresult r;
              Py_BEGIN_ALLOW_THREADS;
              r = pI->Available(&n);
              Py_END_ALLOW_THREADS;
              if (NS_FAILED(r))
                     return PyXPCOM_BuildPyException(r);
       }
       if (n==0) { // mozilla will assert if we alloc zero bytes.
              return PyBuffer_New(0);
       }
       char *buf = (char *)nsMemory::Alloc(n);
       if (buf==NULL) {
              PyErr_NoMemory();
              return NULL;
       }
       nsresult r;
       PRUint32 nread;
       Py_BEGIN_ALLOW_THREADS;
       r = pI->Read(buf, n, &nread);
       Py_END_ALLOW_THREADS;
       PyObject *rc = NULL;
       if ( NS_SUCCEEDED(r) ) {
              rc = PyBuffer_New(nread);
              if (rc != NULL) {
                     void *ob_buf;
                     PRUint32 buf_len;
                     if (PyObject_AsWriteBuffer(rc, &ob_buf, (int *)&buf_len) != 0) {
                            // should never fail - we just created it!
                            return NULL;
                     }
                     if (buf_len != nread) {
                            PyErr_SetString(PyExc_RuntimeError, "New buffer isnt the size we create it!");
                            return NULL;
                     }
                     memcpy(ob_buf, buf, nread);
              }
       } else
              PyXPCOM_BuildPyException(r);
       nsMemory::Free(buf);
       return rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static nsIInputStream* GetI ( PyObject *  self) [static]

Definition at line 54 of file PyIInputStream.cpp.

                                            {
       nsIID iid = NS_GET_IID(nsIInputStream);

       if (!Py_nsISupports::Check(self, iid)) {
              PyErr_SetString(PyExc_TypeError, "This object is not the correct interface");
              return NULL;
       }
       return (nsIInputStream *)Py_nsISupports::GetI(self);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* PyRead ( PyObject *  self,
PyObject *  args 
) [static]

Definition at line 136 of file PyIInputStream.cpp.

{
       PyObject *obBuffer = NULL;
       PRUint32 n = (PRUint32)-1;

       nsIInputStream *pI = GetI(self);
       if (pI==NULL)
              return NULL;
       if (PyArg_ParseTuple(args, "|i", (int *)&n))
              // This worked - no args, or just an int.
              return DoPyRead_Size(pI, n);
       // try our other supported arg format.
       PyErr_Clear();
       if (!PyArg_ParseTuple(args, "O|i", &obBuffer, (int *)&n)) {
              PyErr_Clear();
              PyErr_SetString(PyExc_TypeError, "'read()' must be called as (buffer_ob, int_size=-1) or (int_size=-1)");
              return NULL;
       }
       return DoPyRead_Buffer(pI, obBuffer, n);
}

Here is the call graph for this function: