2
* The contents of this file are subject to the Mozilla Public License Version
3
* 1.1 (the "License"); you may not use this file except in compliance with the
4
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
6
* Software distributed under the License is distributed on an "AS IS" basis,
7
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
8
* the specific language governing rights and limitations under the License.
10
* The Original Code is the Python XPCOM language bindings.
12
* The Initial Developer of the Original Code is ActiveState Tool Corp.
13
* Portions created by ActiveState Tool Corp. are Copyright (C) 2000, 2001
14
* ActiveState Tool Corp. All Rights Reserved.
16
* Contributor(s): Mark Hammond <mhammond@skippinet.com.au> (original author)
21
// This code is part of the XPCOM extensions for Python.
23
// Written September 2000 by Mark Hammond.
25
// Based heavily on the Python COM support, which is
26
// (c) Mark Hammond and Greg Stein.
28
// (c) 2000, ActiveState corp.
30
#include "PyXPCOM_std.h"
31
#include "nsIInputStream.h"
33
// Prevents us needing to use an nsIScriptableInputStream
34
// (and even that can't read binary data!!!)
36
static nsIInputStream *GetI(PyObject *self) {
37
nsIID iid = NS_GET_IID(nsIInputStream);
39
if (!Py_nsISupports::Check(self, iid)) {
40
PyErr_SetString(PyExc_TypeError, "This object is not the correct interface");
43
return (nsIInputStream *)Py_nsISupports::GetI(self);
46
static PyObject *DoPyRead_Buffer(nsIInputStream *pI, PyObject *obBuffer, PRUint32 n)
51
if (PyObject_AsWriteBuffer(obBuffer, &buf, (int *)&buf_len) != 0) {
53
PyErr_SetString(PyExc_TypeError, "The buffer object does not have a write buffer!");
56
if (n==(PRUint32)-1) {
60
NS_WARNING("Warning: PyIInputStream::read() was passed an integer size greater than the size of the passed buffer! Buffer size used.\n");
65
Py_BEGIN_ALLOW_THREADS;
66
r = pI->Read((char *)buf, n, &nread);
69
return PyXPCOM_BuildPyException(r);
70
return PyInt_FromLong(nread);
73
static PyObject *DoPyRead_Size(nsIInputStream *pI, PRUint32 n)
75
if (n==(PRUint32)-1) {
77
Py_BEGIN_ALLOW_THREADS;
78
r = pI->Available(&n);
81
return PyXPCOM_BuildPyException(r);
83
if (n==0) { // mozilla will assert if we alloc zero bytes.
84
return PyBuffer_New(0);
86
char *buf = (char *)nsMemory::Alloc(n);
93
Py_BEGIN_ALLOW_THREADS;
94
r = pI->Read(buf, n, &nread);
97
if ( NS_SUCCEEDED(r) ) {
98
rc = PyBuffer_New(nread);
102
if (PyObject_AsWriteBuffer(rc, &ob_buf, (int *)&buf_len) != 0) {
103
// should never fail - we just created it!
106
if (buf_len != nread) {
107
PyErr_SetString(PyExc_RuntimeError, "New buffer isnt the size we create it!");
110
memcpy(ob_buf, buf, nread);
113
PyXPCOM_BuildPyException(r);
118
static PyObject *PyRead(PyObject *self, PyObject *args)
120
PyObject *obBuffer = NULL;
121
PRUint32 n = (PRUint32)-1;
123
nsIInputStream *pI = GetI(self);
126
if (PyArg_ParseTuple(args, "|i", (int *)&n))
127
// This worked - no args, or just an int.
128
return DoPyRead_Size(pI, n);
129
// try our other supported arg format.
131
if (!PyArg_ParseTuple(args, "O|i", &obBuffer, (int *)&n)) {
133
PyErr_SetString(PyExc_TypeError, "'read()' must be called as (buffer_ob, int_size=-1) or (int_size=-1)");
136
return DoPyRead_Buffer(pI, obBuffer, n);
141
PyMethods_IInputStream[] =
143
{ "read", PyRead, 1},
144
// The rest are handled as normal