Back to index

lightning-sunbird  0.9+nobinonly
PyGInputStream.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Python XPCOM language bindings.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * ActiveState Tool Corp.
00018  * Portions created by the Initial Developer are Copyright (C) 2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Mark Hammond <MarkH@ActiveState.com> (original author)
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 // PyGInputStream.cpp
00039 //
00040 // This code is part of the XPCOM extensions for Python.
00041 //
00042 // Written October 2000 by Mark Hammond.
00043 //
00044 // Based heavily on the Python COM support, which is
00045 // (c) Mark Hammond and Greg Stein.
00046 //
00047 // (c) 2000, ActiveState corp.
00048 
00049 #include "PyXPCOM_std.h"
00050 #include <nsIInputStream.h>
00051 
00052 class PyG_nsIInputStream : public PyG_Base, public nsIInputStream
00053 {
00054 public:
00055        PyG_nsIInputStream(PyObject *instance) : PyG_Base(instance, NS_GET_IID(nsIInputStream)) {;}
00056        PYGATEWAY_BASE_SUPPORT(nsIInputStream, PyG_Base);
00057 
00058        NS_IMETHOD Close(void);
00059        NS_IMETHOD Available(PRUint32 *_retval);
00060        NS_IMETHOD Read(char * buf, PRUint32 count, PRUint32 *_retval);
00061        NS_IMETHOD ReadSegments(nsWriteSegmentFun writer, void * closure, PRUint32 count, PRUint32 *_retval);
00062        NS_IMETHOD IsNonBlocking(PRBool *aNonBlocking);
00063 };
00064 
00065 
00066 PyG_Base *MakePyG_nsIInputStream(PyObject *instance)
00067 {
00068        return new PyG_nsIInputStream(instance);
00069 }
00070 
00071 NS_IMETHODIMP
00072 PyG_nsIInputStream::Close()
00073 {
00074        CEnterLeavePython _celp;
00075        const char *methodName = "close";
00076        return InvokeNativeViaPolicy(methodName, NULL);
00077 }
00078 
00079 NS_IMETHODIMP
00080 PyG_nsIInputStream::Available(PRUint32 *_retval)
00081 {
00082        NS_PRECONDITION(_retval, "null pointer");
00083        CEnterLeavePython _celp;
00084        PyObject *ret;
00085        const char *methodName = "available";
00086        nsresult nr = InvokeNativeViaPolicy(methodName, &ret);
00087        if (NS_SUCCEEDED(nr)) {
00088               *_retval = PyInt_AsLong(ret);
00089               if (PyErr_Occurred())
00090                      nr = HandleNativeGatewayError(methodName);
00091               Py_XDECREF(ret);
00092        }
00093        return nr;
00094 }
00095 
00096 NS_IMETHODIMP
00097 PyG_nsIInputStream::Read(char * buf, PRUint32 count, PRUint32 *_retval)
00098 {
00099        NS_PRECONDITION(_retval, "null pointer");
00100        NS_PRECONDITION(buf, "null pointer");
00101        CEnterLeavePython _celp;
00102        PyObject *ret;
00103        const char *methodName = "read";
00104        nsresult nr = InvokeNativeViaPolicy(methodName, &ret, "i", count);
00105        if (NS_SUCCEEDED(nr)) {
00106               PRUint32 py_size;
00107               const void *py_buf;
00108               if (PyObject_AsReadBuffer(ret, &py_buf, (int *)&py_size)!=0) {
00109                      PyErr_Format(PyExc_TypeError, "nsIInputStream::read() method must return a buffer object - not a '%s' object", ret->ob_type->tp_name);
00110                      nr = HandleNativeGatewayError(methodName);
00111               } else {
00112                      if (py_size > count) {
00113                             PyXPCOM_LogWarning("nsIInputStream::read() was asked for %d bytes, but the string returned is %d bytes - truncating!\n", count, py_size);
00114                             py_size = count;
00115                      }
00116                      memcpy(buf, py_buf, py_size);
00117                      *_retval = py_size;
00118               }
00119        }
00120        return nr;
00121 }
00122 
00123 
00124 NS_IMETHODIMP
00125 PyG_nsIInputStream::ReadSegments(nsWriteSegmentFun writer, void * closure, PRUint32 count, PRUint32 *_retval)
00126 {
00127        NS_WARNING("ReadSegments() not implemented!!!");
00128        return NS_ERROR_NOT_IMPLEMENTED;
00129 }
00130 
00131 NS_IMETHODIMP
00132 PyG_nsIInputStream::IsNonBlocking(PRBool *aNonBlocking)
00133 {
00134        NS_PRECONDITION(aNonBlocking, "null pointer");
00135        CEnterLeavePython _celp;
00136        PyObject *ret;
00137        const char *methodName = "isNonBlocking";
00138        nsresult nr = InvokeNativeViaPolicy(methodName, &ret);
00139        if (NS_SUCCEEDED(nr)) {
00140               *aNonBlocking = PyInt_AsLong(ret);
00141               if (PyErr_Occurred())
00142                      nr = HandleNativeGatewayError(methodName);
00143               Py_XDECREF(ret);
00144        }
00145        return nr;
00146 }