pjs/xpcom/io/nsUnicharInputStream.cpp

325 строки
9.0 KiB
C++
Исходник Обычный вид История

1998-04-14 00:24:54 +04:00
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* 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/
1998-04-14 00:24:54 +04: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.
1998-04-14 00:24:54 +04:00
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
1998-04-14 00:24:54 +04:00
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* IBM Corp.
1998-04-14 00:24:54 +04:00
*/
#define NS_IMPL_IDS
1998-04-14 00:24:54 +04:00
#include "nsIUnicharInputStream.h"
#include "nsIByteBuffer.h"
#include "nsIUnicharBuffer.h"
#include "nsIServiceManager.h"
#include "nsICharsetConverterManager.h"
#include "nsIUnicodeDecoder.h"
1998-04-14 00:24:54 +04:00
#include "nsString.h"
#include "nsCRT.h"
#include <fcntl.h>
#if defined(NS_WIN32) || defined(XP_OS2_VACPP)
1998-04-14 00:24:54 +04:00
#include <io.h>
#else
#include <unistd.h>
#endif
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
1998-04-14 00:24:54 +04:00
class StringUnicharInputStream : public nsIUnicharInputStream {
public:
StringUnicharInputStream(nsString* aString);
virtual ~StringUnicharInputStream();
1998-04-14 00:24:54 +04:00
NS_DECL_ISUPPORTS
NS_IMETHOD Read(PRUnichar* aBuf,
PRUint32 aOffset,
PRUint32 aCount,
PRUint32 *aReadCount);
NS_IMETHOD Close();
1998-04-14 00:24:54 +04:00
nsString* mString;
PRUint32 mPos;
PRUint32 mLen;
1998-04-14 00:24:54 +04:00
};
StringUnicharInputStream::StringUnicharInputStream(nsString* aString)
{
1998-05-28 22:38:32 +04:00
NS_INIT_REFCNT();
1998-04-14 00:24:54 +04:00
mString = aString;
mPos = 0;
mLen = aString->Length();
}
StringUnicharInputStream::~StringUnicharInputStream()
{
if (nsnull != mString) {
delete mString;
}
}
nsresult StringUnicharInputStream::Read(PRUnichar* aBuf,
PRUint32 aOffset,
PRUint32 aCount,
PRUint32 *aReadCount)
1998-04-14 00:24:54 +04:00
{
if (mPos >= mLen) {
*aReadCount = 0;
return (nsresult)-1;
1998-04-14 00:24:54 +04:00
}
const PRUnichar* us = mString->GetUnicode();
NS_ASSERTION(mLen >= mPos, "unsigned madness");
PRUint32 amount = mLen - mPos;
1998-04-14 00:24:54 +04:00
if (amount > aCount) {
amount = aCount;
}
nsCRT::memcpy(aBuf + aOffset, us + mPos, sizeof(PRUnichar) * amount);
mPos += amount;
*aReadCount = amount;
return NS_OK;
1998-04-14 00:24:54 +04:00
}
nsresult StringUnicharInputStream::Close()
1998-04-14 00:24:54 +04:00
{
mPos = mLen;
if (nsnull != mString) {
delete mString;
2000-05-13 03:05:11 +04:00
mString = 0;
1998-04-14 00:24:54 +04:00
}
return NS_OK;
1998-04-14 00:24:54 +04:00
}
NS_IMPL_ISUPPORTS1(StringUnicharInputStream, nsIUnicharInputStream)
1998-04-14 00:24:54 +04:00
1999-05-26 05:38:36 +04:00
NS_COM nsresult
1998-04-14 00:24:54 +04:00
NS_NewStringUnicharInputStream(nsIUnicharInputStream** aInstancePtrResult,
nsString* aString)
{
NS_PRECONDITION(nsnull != aString, "null ptr");
NS_PRECONDITION(nsnull != aInstancePtrResult, "null ptr");
if ((nsnull == aString) || (nsnull == aInstancePtrResult)) {
return NS_ERROR_NULL_POINTER;
}
StringUnicharInputStream* it = new StringUnicharInputStream(aString);
if (nsnull == it) {
return NS_ERROR_OUT_OF_MEMORY;
}
return it->QueryInterface(NS_GET_IID(nsIUnicharInputStream),
1998-04-14 00:24:54 +04:00
(void**) aInstancePtrResult);
}
//----------------------------------------------------------------------
/**
1999-05-26 05:38:36 +04:00
* This function used to be public, with the NS_COM declaration. I am
* changing it right now into a module private visibility because there are
* better and more xpcom-like ways to get a Converter.
*/
nsresult NS_NewB2UConverter(nsIUnicodeDecoder** aInstancePtrResult, nsISupports* aOuter, nsString* aCharSet);
nsresult
NS_NewB2UConverter(nsIUnicodeDecoder** aInstancePtrResult,
1998-04-14 00:24:54 +04:00
nsISupports* aOuter,
1999-01-25 19:05:04 +03:00
nsString* aCharSet)
1998-04-14 00:24:54 +04:00
{
if (nsnull != aOuter) {
return NS_ERROR_NO_AGGREGATION;
}
// Create converter
nsresult res;
2000-04-01 04:39:02 +04:00
nsAutoString defaultCharset;
defaultCharset.AssignWithConversion("ISO-8859-1");
if (aCharSet == nsnull) aCharSet = &defaultCharset;
1999-09-26 14:05:06 +04:00
NS_WITH_SERVICE(nsICharsetConverterManager, ccm, kCharsetConverterManagerCID, &res);
if (NS_FAILED(res)) return res;
1999-09-26 14:05:06 +04:00
return ccm->GetUnicodeDecoder(aCharSet, aInstancePtrResult);
1998-04-14 00:24:54 +04:00
}
//----------------------------------------------------------------------
class ConverterInputStream : public nsIUnicharInputStream {
public:
ConverterInputStream(nsIInputStream* aStream,
nsIUnicodeDecoder* aConverter,
PRUint32 aBufSize);
virtual ~ConverterInputStream();
1998-04-14 00:24:54 +04:00
NS_DECL_ISUPPORTS
NS_IMETHOD Read(PRUnichar* aBuf,
PRUint32 aOffset,
PRUint32 aCount,
PRUint32 *aReadCount);
NS_IMETHOD Close();
1998-04-14 00:24:54 +04:00
protected:
PRInt32 Fill(nsresult * aErrorCode);
1998-04-14 00:24:54 +04:00
nsIInputStream* mInput;
nsIUnicodeDecoder* mConverter;
1998-04-14 00:24:54 +04:00
nsIByteBuffer* mByteData;
PRUint32 mByteDataOffset;
1998-04-14 00:24:54 +04:00
nsIUnicharBuffer* mUnicharData;
PRUint32 mUnicharDataOffset;
PRUint32 mUnicharDataLength;
1998-04-14 00:24:54 +04:00
};
ConverterInputStream::ConverterInputStream(nsIInputStream* aStream,
nsIUnicodeDecoder* aConverter,
PRUint32 aBufferSize)
1998-04-14 00:24:54 +04:00
{
NS_INIT_REFCNT();
mInput = aStream;
NS_ADDREF(aStream);
mConverter = aConverter;
NS_ADDREF(aConverter);
1998-04-14 00:24:54 +04:00
if (aBufferSize == 0) {
aBufferSize = 8192;
}
1999-07-16 21:40:39 +04:00
// XXX what if these fail?
NS_NewByteBuffer(&mByteData, nsnull, aBufferSize);
NS_NewUnicharBuffer(&mUnicharData, nsnull, aBufferSize);
1998-04-14 00:24:54 +04:00
mByteDataOffset = 0;
mUnicharDataOffset = 0;
mUnicharDataLength = 0;
}
NS_IMPL_ISUPPORTS1(ConverterInputStream,nsIUnicharInputStream)
1998-04-14 00:24:54 +04:00
ConverterInputStream::~ConverterInputStream()
{
Close();
}
nsresult ConverterInputStream::Close()
1998-04-14 00:24:54 +04:00
{
if (nsnull != mInput) {
NS_RELEASE(mInput);
1998-04-14 00:24:54 +04:00
mInput = nsnull;
}
if (nsnull != mConverter) {
NS_RELEASE(mConverter);
1998-04-14 00:24:54 +04:00
mConverter = nsnull;
}
if (nsnull != mByteData) {
NS_RELEASE(mByteData);
1998-04-14 00:24:54 +04:00
mByteData = nsnull;
}
if (nsnull != mUnicharData) {
NS_RELEASE(mUnicharData);
1998-04-14 00:24:54 +04:00
mUnicharData = nsnull;
}
return NS_OK;
1998-04-14 00:24:54 +04:00
}
nsresult ConverterInputStream::Read(PRUnichar* aBuf,
PRUint32 aOffset,
PRUint32 aCount,
PRUint32 *aReadCount)
1998-04-14 00:24:54 +04:00
{
NS_ASSERTION(mUnicharDataLength >= mUnicharDataOffset, "unsigned madness");
PRUint32 rv = mUnicharDataLength - mUnicharDataOffset;
nsresult errorCode;
1998-04-14 00:24:54 +04:00
if (0 == rv) {
// Fill the unichar buffer
rv = Fill(&errorCode);
1998-04-14 00:24:54 +04:00
if (rv <= 0) {
*aReadCount = 0;
return errorCode;
1998-04-14 00:24:54 +04:00
}
}
if (rv > aCount) {
rv = aCount;
}
nsCRT::memcpy(aBuf + aOffset, mUnicharData->GetBuffer() + mUnicharDataOffset,
rv * sizeof(PRUnichar));
mUnicharDataOffset += rv;
*aReadCount = rv;
return NS_OK;
1998-04-14 00:24:54 +04:00
}
PRInt32 ConverterInputStream::Fill(nsresult * aErrorCode)
1998-04-14 00:24:54 +04:00
{
if (nsnull == mInput) {
// We already closed the stream!
*aErrorCode = NS_BASE_STREAM_CLOSED;
1998-04-14 00:24:54 +04:00
return -1;
}
NS_ASSERTION(mByteData->GetLength() >= mByteDataOffset, "unsigned madness");
PRUint32 remainder = mByteData->GetLength() - mByteDataOffset;
1998-04-14 00:24:54 +04:00
mByteDataOffset = remainder;
PRInt32 nb = mByteData->Fill(aErrorCode, mInput, remainder);
if (nb <= 0) {
// Because we assume a many to one conversion, the lingering data
// in the byte buffer must be a partial conversion
// fragment. Because we know that we have recieved no more new
// data to add to it, we can't convert it. Therefore, we discard
// it.
return nb;
}
NS_ASSERTION(remainder + nb == mByteData->GetLength(), "bad nb");
// Now convert as much of the byte buffer to unicode as possible
PRInt32 dstLen = mUnicharData->GetBufferSize();
PRInt32 srcLen = remainder + nb;
*aErrorCode = mConverter->Convert(mByteData->GetBuffer(), &srcLen,
mUnicharData->GetBuffer(), &dstLen);
1998-04-14 00:24:54 +04:00
mUnicharDataOffset = 0;
mUnicharDataLength = dstLen;
mByteDataOffset += srcLen;
return dstLen;
}
// XXX hook up auto-detect here (do we need more info, like the url?)
1999-05-26 05:38:36 +04:00
NS_COM nsresult
1998-04-14 00:24:54 +04:00
NS_NewConverterStream(nsIUnicharInputStream** aInstancePtrResult,
nsISupports* aOuter,
nsIInputStream* aStreamToWrap,
PRInt32 aBufferSize,
1999-01-25 19:05:04 +03:00
nsString* aCharSet)
1998-04-14 00:24:54 +04:00
{
if (nsnull != aOuter) {
return NS_ERROR_NO_AGGREGATION;
}
// Create converter
nsIUnicodeDecoder* converter;
1998-04-14 00:24:54 +04:00
nsresult rv = NS_NewB2UConverter(&converter, nsnull, aCharSet);
if (NS_OK != rv) {
return rv;
}
// Create converter input stream
ConverterInputStream* it =
new ConverterInputStream(aStreamToWrap, converter, aBufferSize);
NS_RELEASE(converter);
1998-04-14 00:24:54 +04:00
if (nsnull == it) {
return NS_ERROR_OUT_OF_MEMORY;
}
return it->QueryInterface(NS_GET_IID(nsIUnicharInputStream),
1998-04-14 00:24:54 +04:00
(void **) aInstancePtrResult);
}