From e875c6167004f7ff580da47859be2df85a869e46 Mon Sep 17 00:00:00 2001 From: "valeski%netscape.com" Date: Sat, 4 Dec 1999 20:29:42 +0000 Subject: [PATCH] r=mcafee. 20677 assistance. We now have a scriptable input stream implementation and interface for js. Hurray! git-svn-id: svn://10.0.0.236/trunk@55308 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/xpcom/io/Makefile.in | 3 + mozilla/xpcom/io/makefile.win | 10 ++- mozilla/xpcom/io/nsIScriptableInputStream.idl | 45 ++++++++++ mozilla/xpcom/io/nsScriptableInputStream.cpp | 87 +++++++++++++++++++ mozilla/xpcom/io/nsScriptableInputStream.h | 57 ++++++++++++ 5 files changed, 198 insertions(+), 4 deletions(-) create mode 100644 mozilla/xpcom/io/nsIScriptableInputStream.idl create mode 100644 mozilla/xpcom/io/nsScriptableInputStream.cpp create mode 100644 mozilla/xpcom/io/nsScriptableInputStream.h diff --git a/mozilla/xpcom/io/Makefile.in b/mozilla/xpcom/io/Makefile.in index df5bc712383..ca0cfd1232c 100644 --- a/mozilla/xpcom/io/Makefile.in +++ b/mozilla/xpcom/io/Makefile.in @@ -47,6 +47,7 @@ CPPSRCS = \ nsUnicharInputStream.cpp \ nsBinaryStream.cpp \ nsStorageStream.cpp \ + nsScriptableInputStream.cpp \ $(NULL) EXPORTS = \ @@ -59,6 +60,7 @@ EXPORTS = \ nsIStringStream.h \ nsIUnicharInputStream.h \ nsSpecialSystemDirectory.h \ + nsScriptableInputStream.h \ $(NULL) XPIDLSRCS = \ @@ -72,6 +74,7 @@ XPIDLSRCS = \ nsIBinaryInputStream.idl \ nsIBinaryOutputStream.idl \ nsIStorageStream.idl \ + nsIScriptableInputStream.idl \ $(NULL) EXPORTS := $(addprefix $(srcdir)/, $(EXPORTS)) diff --git a/mozilla/xpcom/io/makefile.win b/mozilla/xpcom/io/makefile.win index ee9565cfd7d..d90b5cc0751 100644 --- a/mozilla/xpcom/io/makefile.win +++ b/mozilla/xpcom/io/makefile.win @@ -38,7 +38,8 @@ EXPORTS = \ nsIUnicharInputStream.h \ nsSpecialSystemDirectory.h \ nsStorageStream.h \ - $(NULL) + nsScriptableInputStream.h \ + $(NULL) NO_XPT_GEN=1 XPIDL_MODULE = xpcom_io @@ -53,6 +54,7 @@ XPIDLSRCS = \ .\nsIOutputStream.idl \ .\nsIBufferInputStream.idl \ .\nsIBufferOutputStream.idl \ + .\nsIScriptableInputStream.idl \ .\nsIStorageStream.idl \ $(NULL) @@ -62,10 +64,10 @@ XPIDLSRCS = \ LIBRARY_NAME=xpcomio_s LINCS = \ - -I$(PUBLIC)\xpcom \ + -I$(PUBLIC)\xpcom \ -I$(PUBLIC)\uconv \ -I$(PUBLIC)\unicharutil \ - $(NULL) + $(NULL) LINCS = $(LINCS) -I$(PUBLIC)\raptor # until we sort out the filespec/widgets dependency @@ -86,12 +88,12 @@ CPP_OBJS = \ .\$(OBJDIR)\nsSpecialSystemDirectory.obj \ .\$(OBJDIR)\nsStorageStream.obj \ .\$(OBJDIR)\nsUnicharInputStream.obj \ + .\$(OBJDIR)\nsScriptableInputStream.obj \ $(NULL) include <$(DEPTH)\config\rules.mak> install:: $(LIBRARY) $(MAKE_INSTALL) $(LIBRARY) $(DIST)\lib - clobber:: rm -f $(DIST)\lib\$(LIBRARY_NAME).lib diff --git a/mozilla/xpcom/io/nsIScriptableInputStream.idl b/mozilla/xpcom/io/nsIScriptableInputStream.idl new file mode 100644 index 00000000000..3fb4fc09af2 --- /dev/null +++ b/mozilla/xpcom/io/nsIScriptableInputStream.idl @@ -0,0 +1,45 @@ +/* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * The contents of this file are subject to the Netscape Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla Communicator client code, + * released March 31, 1998. + * + * The Initial Developer of the Original Code is Netscape Communications + * Corporation. Portions created by Netscape are + * Copyright (C) 1998-1999 Netscape Communications Corporation. All + * Rights Reserved. + */ +#include "nsIBaseStream.idl" + +interface nsIInputStream; + +[scriptable, uuid(a2a32f90-9b90-11d3-a189-0050041caf44)] +interface nsIScriptableInputStream : nsIBaseStream +{ + /** Wrap the given nsIInputStream with this nsIScriptableInputStream. + * @param aInputStream [in] parameter providing the stream to wrap + */ + void init(in nsIInputStream aInputStream); + + /** Return the number of bytes currently available in the stream + * @param _retval [out] parameter to hold the number of bytes + * if an error occurs, the parameter will be undefined + * @return error status + */ + unsigned long available(); + + /** Read data from the stream. + * @param aCount [in] the maximum number of bytes to read + * @param _retval [out] the data + */ + string read(in unsigned long aCount); +}; diff --git a/mozilla/xpcom/io/nsScriptableInputStream.cpp b/mozilla/xpcom/io/nsScriptableInputStream.cpp new file mode 100644 index 00000000000..a9298c898e2 --- /dev/null +++ b/mozilla/xpcom/io/nsScriptableInputStream.cpp @@ -0,0 +1,87 @@ +/* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * The contents of this file are subject to the Netscape Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): Jud Valeski & Brendan Eich + */ + +#include "nsScriptableInputStream.h" +#include "nsIAllocator.h" + +NS_IMPL_ISUPPORTS2(nsScriptableInputStream, nsIBaseStream, nsIScriptableInputStream); + +// nsIBaseStream methods +NS_IMETHODIMP +nsScriptableInputStream::Close(void) { + if (mInputStream) return NS_ERROR_NOT_INITIALIZED; + return mInputStream->Close(); +} + +// nsIScriptableInputStream methods +NS_IMETHODIMP +nsScriptableInputStream::Init(nsIInputStream *aInputStream) { + if (!aInputStream) return NS_ERROR_NULL_POINTER; + mInputStream = aInputStream; + return NS_OK; +} + +NS_IMETHODIMP +nsScriptableInputStream::Available(PRUint32 *_retval) { + if (mInputStream) return NS_ERROR_NOT_INITIALIZED; + return mInputStream->Available(_retval); +} + +NS_IMETHODIMP +nsScriptableInputStream::Read(PRUint32 aCount, char **_retval) { + nsresult rv = NS_OK; + PRUint32 count = 0; + char *buffer = nsnull; + + if (!mInputStream) return NS_ERROR_NOT_INITIALIZED; + + rv = mInputStream->Available(&count); + if (NS_FAILED(rv)) return rv; + + count = PR_MIN(count, aCount); + buffer = (char*)nsAllocator::Alloc(count+1); // make room for '\0' + if (!buffer) return NS_ERROR_OUT_OF_MEMORY; + + PRUint32 amtRead = 0; + rv = mInputStream->Read(buffer, count, &amtRead); + if (NS_FAILED(rv)) { + nsAllocator::Free(buffer); + return rv; + } + + buffer[amtRead] = '\0'; + *_retval = buffer; + return NS_OK; +} + +NS_METHOD +nsScriptableInputStream::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult) { + if (aOuter) return NS_ERROR_NO_AGGREGATION; + + nsScriptableInputStream *sis = new nsScriptableInputStream(); + if (!sis) return NS_ERROR_OUT_OF_MEMORY; + + NS_ADDREF(sis); + nsresult rv = sis->QueryInterface(aIID, aResult); + NS_RELEASE(sis); + return rv; +} diff --git a/mozilla/xpcom/io/nsScriptableInputStream.h b/mozilla/xpcom/io/nsScriptableInputStream.h new file mode 100644 index 00000000000..77b1a6d420c --- /dev/null +++ b/mozilla/xpcom/io/nsScriptableInputStream.h @@ -0,0 +1,57 @@ +/* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * The contents of this file are subject to the Netscape Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): Jud Valeski & Brendan Eich + */ + +#ifndef ___nsscriptableinputstream___h_ +#define ___nsscriptableinputstream___h_ + +#include "nsIScriptableInputStream.h" +#include "nsIInputStream.h" +#include "nsCOMPtr.h" + +#define NS_SCRIPTABLEINPUTSTREAM_CID \ +{ 0x7225c040, 0xa9bf, 0x11d3, { 0xa1, 0x97, 0x0, 0x50, 0x4, 0x1c, 0xaf, 0x44 } } + +#define NS_SCRIPTABLEINPUTSTREAM_PROGID "component://netscape/scriptableinputstream" +#define NS_SCRIPTABLEINPUTSTREAM_CLASSNAME "Scriptable Input Stream" + +class nsScriptableInputStream : public nsIScriptableInputStream { +public: + // nsISupports methods + NS_DECL_ISUPPORTS + + // nsIBaseStream methods + NS_DECL_NSIBASESTREAM + + // nsIScriptableInputStream methods + NS_DECL_NSISCRIPTABLEINPUTSTREAM + + // nsScriptableInputStream methods + nsScriptableInputStream() { NS_INIT_REFCNT(); }; + virtual ~nsScriptableInputStream() {}; + + static NS_METHOD + Create(nsISupports *aOuter, REFNSIID aIID, void **aResult); +private: + nsCOMPtr mInputStream; +}; + +#endif // ___nsscriptableinputstream___h_