2001-11-22 02:55:10 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2006-01-02 05:30:32 +03:00
|
|
|
/* vim:set ts=4 sts=4 sw=4 cin et: */
|
2001-11-22 02:55:10 +03:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 18:21:17 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2001-11-22 02:55:10 +03:00
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The contents of this file are subject to the Mozilla 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/MPL/
|
2001-11-22 02:55:10 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-11-22 02:55:10 +03:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* mcmullen@netscape.com (original author)
|
|
|
|
* warren@netscape.com
|
|
|
|
* alecf@netscape.com
|
|
|
|
* scc@mozilla.org
|
|
|
|
* david.gardiner@unisa.edu.au
|
|
|
|
* fur@netscape.com
|
|
|
|
* norris@netscape.com
|
|
|
|
* pinkerton@netscape.com
|
|
|
|
* davidm@netscape.com
|
|
|
|
* sfraser@netscape.com
|
|
|
|
* darin@netscape.com
|
2003-07-12 03:10:27 +04:00
|
|
|
* bzbarsky@mit.edu
|
2001-11-22 02:55:10 +03:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 18:21:17 +04:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-11-22 02:55:10 +03:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 18:21:17 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-11-22 02:55:10 +03:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 18:21:17 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-11-22 02:55:10 +03:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Based on original code from nsIStringStream.cpp
|
|
|
|
*/
|
|
|
|
|
2010-10-21 22:36:13 +04:00
|
|
|
#include "IPC/IPCMessageUtils.h"
|
|
|
|
|
2001-11-22 02:55:10 +03:00
|
|
|
#include "nsStringStream.h"
|
2006-01-02 05:30:32 +03:00
|
|
|
#include "nsStreamUtils.h"
|
2001-11-22 02:55:10 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2003-03-15 04:04:32 +03:00
|
|
|
#include "nsISeekableStream.h"
|
2006-01-20 22:17:58 +03:00
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "prerror.h"
|
|
|
|
#include "plstr.h"
|
2006-03-15 07:59:42 +03:00
|
|
|
#include "nsIClassInfoImpl.h"
|
2010-10-21 22:36:13 +04:00
|
|
|
#include "nsIIPCSerializable.h"
|
2003-03-15 04:04:32 +03:00
|
|
|
|
2003-07-12 03:10:27 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIStringInputStream implementation
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2006-01-20 22:17:58 +03:00
|
|
|
class nsStringInputStream : public nsIStringInputStream
|
2005-06-17 07:47:11 +04:00
|
|
|
, public nsISeekableStream
|
2006-01-20 22:17:58 +03:00
|
|
|
, public nsISupportsCString
|
2010-10-21 22:36:13 +04:00
|
|
|
, public nsIIPCSerializable
|
2003-07-12 03:10:27 +04:00
|
|
|
{
|
|
|
|
public:
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIINPUTSTREAM
|
2006-01-10 04:56:00 +03:00
|
|
|
NS_DECL_NSISTRINGINPUTSTREAM
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_DECL_NSISEEKABLESTREAM
|
2006-01-20 22:17:58 +03:00
|
|
|
NS_DECL_NSISUPPORTSPRIMITIVE
|
|
|
|
NS_DECL_NSISUPPORTSCSTRING
|
2010-10-21 22:36:13 +04:00
|
|
|
NS_DECL_NSIIPCSERIALIZABLE
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2003-07-12 03:10:27 +04:00
|
|
|
nsStringInputStream()
|
2011-11-01 12:28:31 +04:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2004-02-11 09:19:03 +03:00
|
|
|
private:
|
|
|
|
~nsStringInputStream()
|
2011-11-01 12:28:31 +04:00
|
|
|
{}
|
|
|
|
|
|
|
|
PRUint32 Length() const
|
2003-07-12 03:10:27 +04:00
|
|
|
{
|
2011-11-01 12:28:31 +04:00
|
|
|
return mData.Length();
|
2003-07-12 03:10:27 +04:00
|
|
|
}
|
|
|
|
|
2011-11-01 12:28:31 +04:00
|
|
|
PRUint32 LengthRemaining() const
|
2003-07-12 03:10:27 +04:00
|
|
|
{
|
2011-11-01 12:28:31 +04:00
|
|
|
return Length() - mOffset;
|
2003-07-12 03:10:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
2011-11-01 12:28:31 +04:00
|
|
|
mData.SetIsVoid(true);
|
|
|
|
}
|
2003-07-12 03:10:27 +04:00
|
|
|
|
2011-11-01 12:28:31 +04:00
|
|
|
bool Closed()
|
|
|
|
{
|
|
|
|
return mData.IsVoid();
|
2003-07-12 03:10:27 +04:00
|
|
|
}
|
|
|
|
|
2011-11-01 12:28:31 +04:00
|
|
|
nsDependentCSubstring mData;
|
|
|
|
PRUint32 mOffset;
|
2003-07-12 03:10:27 +04:00
|
|
|
};
|
|
|
|
|
2006-01-02 05:30:32 +03:00
|
|
|
// This class needs to support threadsafe refcounting since people often
|
|
|
|
// allocate a string stream, and then read it from a background thread.
|
2006-01-20 22:17:58 +03:00
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsStringInputStream)
|
|
|
|
NS_IMPL_THREADSAFE_RELEASE(nsStringInputStream)
|
|
|
|
|
2010-06-22 20:59:57 +04:00
|
|
|
NS_IMPL_CLASSINFO(nsStringInputStream, NULL, nsIClassInfo::THREADSAFE,
|
|
|
|
NS_STRINGINPUTSTREAM_CID)
|
2010-10-21 22:36:13 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE5_CI(nsStringInputStream,
|
2006-01-20 22:17:58 +03:00
|
|
|
nsIStringInputStream,
|
|
|
|
nsIInputStream,
|
|
|
|
nsISupportsCString,
|
2010-10-21 22:36:13 +04:00
|
|
|
nsISeekableStream,
|
|
|
|
nsIIPCSerializable)
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER5(nsStringInputStream,
|
2006-01-20 22:17:58 +03:00
|
|
|
nsIStringInputStream,
|
|
|
|
nsIInputStream,
|
|
|
|
nsISupportsCString,
|
2010-10-21 22:36:13 +04:00
|
|
|
nsISeekableStream,
|
|
|
|
nsIIPCSerializable)
|
2003-07-12 03:10:27 +04:00
|
|
|
|
|
|
|
/////////
|
2006-01-20 22:17:58 +03:00
|
|
|
// nsISupportsCString implementation
|
2003-07-12 03:10:27 +04:00
|
|
|
/////////
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2006-01-20 22:17:58 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::GetType(PRUint16 *type)
|
|
|
|
{
|
|
|
|
*type = TYPE_CSTRING;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::GetData(nsACString &data)
|
|
|
|
{
|
2006-01-20 22:17:58 +03:00
|
|
|
// The stream doesn't have any data when it is closed. We could fake it
|
|
|
|
// and return an empty string here, but it seems better to keep this return
|
|
|
|
// value consistent with the behavior of the other 'getter' methods.
|
2011-11-01 12:28:31 +04:00
|
|
|
NS_ENSURE_TRUE(!Closed(), NS_BASE_STREAM_CLOSED);
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2011-11-01 12:28:31 +04:00
|
|
|
data.Assign(mData);
|
2006-01-02 05:30:32 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::SetData(const nsACString &data)
|
|
|
|
{
|
2011-11-01 12:28:31 +04:00
|
|
|
mData.Assign(data);
|
|
|
|
mOffset = 0;
|
|
|
|
return NS_OK;
|
2006-01-02 05:30:32 +03:00
|
|
|
}
|
|
|
|
|
2006-01-20 22:17:58 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::ToString(char **result)
|
|
|
|
{
|
|
|
|
// NOTE: This method may result in data loss, so we do not implement it.
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2006-01-10 04:56:00 +03:00
|
|
|
/////////
|
|
|
|
// nsIStringInputStream implementation
|
|
|
|
/////////
|
|
|
|
|
2003-07-12 03:10:27 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::SetData(const char *data, PRInt32 dataLen)
|
|
|
|
{
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(data);
|
2011-11-01 12:28:31 +04:00
|
|
|
mData.Assign(data, dataLen);
|
|
|
|
mOffset = 0;
|
|
|
|
return NS_OK;
|
2003-07-12 03:10:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::AdoptData(char *data, PRInt32 dataLen)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(data);
|
2011-11-01 12:28:31 +04:00
|
|
|
mData.Adopt(data, dataLen);
|
|
|
|
mOffset = 0;
|
2003-07-12 03:10:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2003-07-12 03:10:27 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::ShareData(const char *data, PRInt32 dataLen)
|
2001-11-22 02:55:10 +03:00
|
|
|
{
|
2003-07-12 03:10:27 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(data);
|
|
|
|
|
|
|
|
if (dataLen < 0)
|
|
|
|
dataLen = strlen(data);
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2011-12-09 12:35:41 +04:00
|
|
|
mData.Rebind(data, dataLen);
|
2011-11-01 12:28:31 +04:00
|
|
|
mOffset = 0;
|
2003-07-12 03:10:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////
|
|
|
|
// nsIInputStream implementation
|
|
|
|
/////////
|
2006-01-02 05:30:32 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::Close()
|
2003-07-12 03:10:27 +04:00
|
|
|
{
|
2006-01-02 05:30:32 +03:00
|
|
|
Clear();
|
2003-07-12 03:10:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::Available(PRUint32 *aLength)
|
2003-07-12 03:10:27 +04:00
|
|
|
{
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_ASSERTION(aLength, "null ptr");
|
|
|
|
|
2011-11-01 12:28:31 +04:00
|
|
|
if (Closed())
|
2006-01-02 05:30:32 +03:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
2003-07-12 03:10:27 +04:00
|
|
|
*aLength = LengthRemaining();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::Read(char* aBuf, PRUint32 aCount, PRUint32 *aReadCount)
|
2001-11-22 02:55:10 +03:00
|
|
|
{
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_ASSERTION(aBuf, "null ptr");
|
|
|
|
return ReadSegments(NS_CopySegmentToBuffer, aBuf, aCount, aReadCount);
|
2001-11-22 02:55:10 +03:00
|
|
|
}
|
|
|
|
|
2003-07-12 03:10:27 +04:00
|
|
|
NS_IMETHODIMP
|
2006-01-02 05:30:32 +03:00
|
|
|
nsStringInputStream::ReadSegments(nsWriteSegmentFun writer, void *closure,
|
|
|
|
PRUint32 aCount, PRUint32 *result)
|
2003-07-12 03:10:27 +04:00
|
|
|
{
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_ASSERTION(result, "null ptr");
|
2011-11-01 12:28:31 +04:00
|
|
|
NS_ASSERTION(Length() >= mOffset, "bad stream state");
|
|
|
|
|
|
|
|
if (Closed())
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
2006-01-02 05:30:32 +03:00
|
|
|
|
|
|
|
// We may be at end-of-file
|
2006-12-06 21:46:29 +03:00
|
|
|
PRUint32 maxCount = LengthRemaining();
|
2003-07-12 03:10:27 +04:00
|
|
|
if (maxCount == 0) {
|
|
|
|
*result = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2005-02-23 05:31:58 +03:00
|
|
|
if (aCount > maxCount)
|
2003-07-12 03:10:27 +04:00
|
|
|
aCount = maxCount;
|
2011-11-01 12:28:31 +04:00
|
|
|
nsresult rv = writer(this, closure, mData.BeginReading() + mOffset, 0, aCount, result);
|
2005-02-23 05:31:58 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_ASSERTION(*result <= aCount,
|
|
|
|
"writer should not write more than we asked it to write");
|
2003-07-12 03:10:27 +04:00
|
|
|
mOffset += *result;
|
2005-02-23 05:31:58 +03:00
|
|
|
}
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2003-07-12 03:10:27 +04:00
|
|
|
// errors returned from the writer end here!
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsStringInputStream::IsNonBlocking(bool *aNonBlocking)
|
2001-11-22 02:55:10 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*aNonBlocking = true;
|
2003-07-12 03:10:27 +04:00
|
|
|
return NS_OK;
|
2001-11-22 02:55:10 +03:00
|
|
|
}
|
|
|
|
|
2003-07-12 03:10:27 +04:00
|
|
|
/////////
|
|
|
|
// nsISeekableStream implementation
|
|
|
|
/////////
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2005-06-17 07:47:11 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::Seek(PRInt32 whence, PRInt64 offset)
|
2001-11-22 02:55:10 +03:00
|
|
|
{
|
2011-11-01 12:28:31 +04:00
|
|
|
if (Closed())
|
2006-01-02 05:30:32 +03:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
|
|
|
// Compute new stream position. The given offset may be a negative value.
|
|
|
|
|
|
|
|
PRInt64 newPos = offset;
|
|
|
|
switch (whence) {
|
|
|
|
case NS_SEEK_SET:
|
|
|
|
break;
|
|
|
|
case NS_SEEK_CUR:
|
2011-11-01 12:28:31 +04:00
|
|
|
newPos += mOffset;
|
2006-01-02 05:30:32 +03:00
|
|
|
break;
|
|
|
|
case NS_SEEK_END:
|
2011-11-01 12:28:31 +04:00
|
|
|
newPos += Length();
|
2006-01-02 05:30:32 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("invalid whence");
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2001-11-22 02:55:10 +03:00
|
|
|
}
|
|
|
|
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_ENSURE_ARG(newPos >= 0);
|
2011-11-01 12:28:31 +04:00
|
|
|
NS_ENSURE_ARG(newPos <= Length());
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2011-11-01 12:28:31 +04:00
|
|
|
mOffset = (PRUint32)newPos;
|
2006-01-02 05:30:32 +03:00
|
|
|
return NS_OK;
|
2001-11-22 02:55:10 +03:00
|
|
|
}
|
|
|
|
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::Tell(PRInt64* outWhere)
|
2001-11-22 02:55:10 +03:00
|
|
|
{
|
2011-11-01 12:28:31 +04:00
|
|
|
if (Closed())
|
2006-01-02 05:30:32 +03:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2006-01-02 05:30:32 +03:00
|
|
|
*outWhere = mOffset;
|
2001-11-22 02:55:10 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::SetEOF()
|
2001-11-22 02:55:10 +03:00
|
|
|
{
|
2011-11-01 12:28:31 +04:00
|
|
|
if (Closed())
|
2006-01-02 05:30:32 +03:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2011-11-01 12:28:31 +04:00
|
|
|
mOffset = Length();
|
2001-11-22 02:55:10 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-10-21 22:36:13 +04:00
|
|
|
/////////
|
|
|
|
// nsIIPCSerializable implementation
|
|
|
|
/////////
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2010-10-21 22:36:13 +04:00
|
|
|
nsStringInputStream::Read(const IPC::Message *aMsg, void **aIter)
|
|
|
|
{
|
|
|
|
using IPC::ReadParam;
|
|
|
|
|
2011-11-01 12:28:31 +04:00
|
|
|
nsCString value;
|
2010-10-21 22:36:13 +04:00
|
|
|
|
|
|
|
if (!ReadParam(aMsg, aIter, &value))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-10-21 22:36:13 +04:00
|
|
|
|
2011-11-01 12:28:31 +04:00
|
|
|
nsresult rv = SetData(value);
|
2010-10-21 22:36:13 +04:00
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-10-21 22:36:13 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-10-21 22:36:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsStringInputStream::Write(IPC::Message *aMsg)
|
|
|
|
{
|
|
|
|
using IPC::WriteParam;
|
|
|
|
|
2011-11-01 12:28:31 +04:00
|
|
|
WriteParam(aMsg, static_cast<const nsCString&>(PromiseFlatCString(mData)));
|
2010-10-21 22:36:13 +04:00
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_NewByteInputStream(nsIInputStream** aStreamResult,
|
|
|
|
const char* aStringToRead, PRInt32 aLength,
|
|
|
|
nsAssignmentType aAssignment)
|
2001-11-22 02:55:10 +03:00
|
|
|
{
|
2003-07-12 03:10:27 +04:00
|
|
|
NS_PRECONDITION(aStreamResult, "null out ptr");
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2003-07-12 03:10:27 +04:00
|
|
|
nsStringInputStream* stream = new nsStringInputStream();
|
2001-11-22 02:55:10 +03:00
|
|
|
if (! stream)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2004-02-11 09:19:03 +03:00
|
|
|
NS_ADDREF(stream);
|
|
|
|
|
2006-01-02 05:30:32 +03:00
|
|
|
nsresult rv;
|
|
|
|
switch (aAssignment) {
|
|
|
|
case NS_ASSIGNMENT_COPY:
|
|
|
|
rv = stream->SetData(aStringToRead, aLength);
|
|
|
|
break;
|
|
|
|
case NS_ASSIGNMENT_DEPEND:
|
|
|
|
rv = stream->ShareData(aStringToRead, aLength);
|
|
|
|
break;
|
|
|
|
case NS_ASSIGNMENT_ADOPT:
|
2007-07-08 11:08:04 +04:00
|
|
|
rv = stream->AdoptData(const_cast<char*>(aStringToRead), aLength);
|
2006-01-02 05:30:32 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("invalid assignment type");
|
|
|
|
rv = NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2003-07-12 03:10:27 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2004-02-11 09:19:03 +03:00
|
|
|
NS_RELEASE(stream);
|
2003-07-12 03:10:27 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2004-02-11 09:19:03 +03:00
|
|
|
*aStreamResult = stream;
|
2001-11-22 02:55:10 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_NewStringInputStream(nsIInputStream** aStreamResult,
|
|
|
|
const nsAString& aStringToRead)
|
2001-11-22 02:55:10 +03:00
|
|
|
{
|
2011-11-01 12:28:31 +04:00
|
|
|
NS_LossyConvertUTF16toASCII data(aStringToRead); // truncates high-order bytes
|
|
|
|
return NS_NewCStringInputStream(aStreamResult, data);
|
2006-01-02 05:30:32 +03:00
|
|
|
}
|
2004-02-11 09:19:03 +03:00
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_NewCStringInputStream(nsIInputStream** aStreamResult,
|
|
|
|
const nsACString& aStringToRead)
|
|
|
|
{
|
2011-11-01 12:28:31 +04:00
|
|
|
NS_PRECONDITION(aStreamResult, "null out ptr");
|
|
|
|
|
|
|
|
nsStringInputStream* stream = new nsStringInputStream();
|
|
|
|
if (! stream)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2011-11-01 12:28:31 +04:00
|
|
|
NS_ADDREF(stream);
|
|
|
|
|
|
|
|
stream->SetData(aStringToRead);
|
|
|
|
|
|
|
|
*aStreamResult = stream;
|
|
|
|
return NS_OK;
|
2001-11-22 02:55:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// factory method for constructing a nsStringInputStream object
|
2010-06-10 22:11:11 +04:00
|
|
|
nsresult
|
2001-11-22 02:55:10 +03:00
|
|
|
nsStringInputStreamConstructor(nsISupports *outer, REFNSIID iid, void **result)
|
|
|
|
{
|
|
|
|
*result = nsnull;
|
|
|
|
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_ENSURE_TRUE(!outer, NS_ERROR_NO_AGGREGATION);
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2006-01-02 05:30:32 +03:00
|
|
|
nsStringInputStream *inst = new nsStringInputStream();
|
2001-11-22 02:55:10 +03:00
|
|
|
if (!inst)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(inst);
|
|
|
|
nsresult rv = inst->QueryInterface(iid, result);
|
|
|
|
NS_RELEASE(inst);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|