bug 110531 and bug 108285 - move ConverterInputStream into uconv and out of xpcom. sr=darin, r=dougt

This commit is contained in:
alecf%netscape.com 2001-11-30 22:48:47 +00:00
Родитель 6db8af531a
Коммит 94c1b65a41
5 изменённых файлов: 254 добавлений и 14 удалений

Просмотреть файл

@ -38,7 +38,6 @@ REQUIRES = xpcom \
string \
intl \
locale \
chardet \
unicharutil \
necko \
$(NULL)
@ -68,6 +67,7 @@ CPPSRCS = \
nsUnicodeToUTF8.cpp \
nsUCvMinSupport.cpp \
nsScriptableUConv.cpp \
nsConverterInputStream.cpp \
$(NULL)
EXPORT_RESOURCE = \

Просмотреть файл

@ -25,7 +25,6 @@ REQUIRES = xpcom \
string \
intl \
locale \
chardet \
unicharutil \
necko \
$(NULL)
@ -56,6 +55,7 @@ CPPSRCS = \
nsUnicodeToUTF8.cpp \
nsUCvMinSupport.cpp \
nsScriptableUConv.cpp \
nsConverterInputStream.cpp \
$(NULL)
CPP_OBJS= \
@ -78,6 +78,7 @@ CPP_OBJS= \
.\$(OBJDIR)\nsUnicodeToUTF8.obj \
.\$(OBJDIR)\nsUCvMinSupport.obj \
.\$(OBJDIR)\nsScriptableUConv.obj \
.\$(OBJDIR)\nsConverterInputStream.obj \
$(NULL)
CSRCS = \

Просмотреть файл

@ -48,7 +48,6 @@
#include "nsICharsetConverterManager.h"
#include "nsICharsetConverterManager2.h"
#include "nsIStringBundle.h"
#include "nsICharsetDetector.h"
#include "nsILocaleService.h"
#include "nsUConvDll.h"
#include "prmem.h"
@ -99,10 +98,10 @@ private:
const char * aRegistryPath);
nsresult GetBundleValue(nsIStringBundle * aBundle, const nsIAtom * aName,
nsString * aProp, PRUnichar ** aResult);
const nsAFlatString& aProp, PRUnichar ** aResult);
nsresult GetBundleValue(nsIStringBundle * aBundle, const nsIAtom * aName,
nsString * aProp, nsIAtom ** aResult);
const nsAFlatString& aProp, nsIAtom ** aResult);
nsresult GetRegistryEnumeration(const char * aRegistryKey,
const char * aAddPrefix, nsISupportsArray ** aArray);
@ -295,7 +294,7 @@ nsresult nsCharsetConverterManager::LoadExtensibleBundle(
nsresult nsCharsetConverterManager::GetBundleValue(nsIStringBundle * aBundle,
const nsIAtom * aName,
nsString * aProp,
const nsAFlatString& aProp,
PRUnichar ** aResult)
{
nsresult res = NS_OK;
@ -305,7 +304,7 @@ nsresult nsCharsetConverterManager::GetBundleValue(nsIStringBundle * aBundle,
if (NS_FAILED(res)) return res;
key.ToLowerCase(); // we lowercase the main comparison key
if (aProp != NULL) key.Append(*aProp); // yes, this param may be NULL
if (!aProp.IsEmpty()) key.Append(aProp.get()); // yes, this param may be NULL
res = aBundle->GetStringFromName(key.get(), aResult);
return res;
@ -313,7 +312,7 @@ nsresult nsCharsetConverterManager::GetBundleValue(nsIStringBundle * aBundle,
nsresult nsCharsetConverterManager::GetBundleValue(nsIStringBundle * aBundle,
const nsIAtom * aName,
nsString * aProp,
const nsAFlatString& aProp,
nsIAtom ** aResult)
{
nsresult res = NS_OK;
@ -632,7 +631,7 @@ NS_IMETHODIMP nsCharsetConverterManager::GetCharsetDetectorList(
if (aResult == NULL) return NS_ERROR_NULL_POINTER;
*aResult = NULL;
return GetRegistryEnumeration(NS_CHARSET_DETECTOR_REG_BASE, "chardet.",
return GetRegistryEnumeration("software/netscape/intl/charsetdetector/", "chardet.",
aResult);
}
@ -693,7 +692,7 @@ NS_IMETHODIMP nsCharsetConverterManager::GetCharsetTitle(
if (NS_FAILED(res)) return res;
}
res = GetBundleValue(mTitleBundle, aCharset, &prop, aResult);
res = GetBundleValue(mTitleBundle, aCharset, NS_LITERAL_STRING(".title"), aResult);
return res;
}
@ -725,14 +724,13 @@ NS_IMETHODIMP nsCharsetConverterManager::GetCharsetData(
*aResult = NULL;
nsresult res = NS_OK;
nsAutoString prop(aProp);
if (mDataBundle == NULL) {
res = LoadExtensibleBundle(NS_DATA_BUNDLE_REGISTRY_KEY, &mDataBundle);
if (NS_FAILED(res)) return res;
}
res = GetBundleValue(mDataBundle, aCharset, &prop, aResult);
res = GetBundleValue(mDataBundle, aCharset, nsDependentString(aProp), aResult);
return res;
}
@ -763,13 +761,12 @@ NS_IMETHODIMP nsCharsetConverterManager::GetCharsetLangGroup(
*aResult = NULL;
nsresult res = NS_OK;
nsAutoString prop; prop.AssignWithConversion(".LangGroup");
if (mDataBundle == NULL) {
res = LoadExtensibleBundle(NS_DATA_BUNDLE_REGISTRY_KEY, &mDataBundle);
if (NS_FAILED(res)) return res;
}
res = GetBundleValue(mDataBundle, aCharset, &prop, aResult);
res = GetBundleValue(mDataBundle, aCharset, NS_LITERAL_STRING(".LangGroup"), aResult);
return res;
}

Просмотреть файл

@ -0,0 +1,153 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
*
* 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 the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
*
* Alternatively, the contents of this file may be used under the terms of
* either 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"),
* 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
* use your version of this file under the terms of the NPL, indicate your
* 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
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsConverterInputStream.h"
#include "nsICharsetConverterManager.h"
#include "nsIServiceManager.h"
#define CONVERTER_BUFFER_SIZE 8192
NS_IMPL_ISUPPORTS1(nsConverterInputStream, nsIConverterInputStream)
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
NS_IMETHODIMP
nsConverterInputStream::Init(nsIInputStream* aStream,
const PRUnichar *aCharset,
PRInt32 aBufferSize)
{
nsresult rv;
if (aBufferSize <=0) aBufferSize=CONVERTER_BUFFER_SIZE;
// get the decoder
nsCOMPtr<nsICharsetConverterManager> ccm =
do_GetService(kCharsetConverterManagerCID, &rv);
if (NS_FAILED(rv)) return nsnull;
nsAutoString charset;
if (aCharset)
charset.Assign(aCharset);
else
charset.Assign(NS_LITERAL_STRING("ISO-8859-1"));
rv = ccm->GetUnicodeDecoder(&charset, getter_AddRefs(mConverter));
if (NS_FAILED(rv)) return rv;
// set up our buffers
rv = NS_NewByteBuffer(getter_AddRefs(mByteData), nsnull, aBufferSize);
if (NS_FAILED(rv)) return rv;
rv = NS_NewUnicharBuffer(getter_AddRefs(mUnicharData), nsnull, aBufferSize);
if (NS_FAILED(rv)) return rv;
mInput = aStream;
return NS_OK;
}
NS_IMETHODIMP
nsConverterInputStream::Close()
{
mInput = nsnull;
mConverter = nsnull;
mByteData = nsnull;
mUnicharData = nsnull;
return NS_OK;
}
NS_IMETHODIMP
nsConverterInputStream::Read(PRUnichar* aBuf,
PRUint32 aOffset,
PRUint32 aCount,
PRUint32 *aReadCount)
{
NS_ASSERTION(mUnicharDataLength >= mUnicharDataOffset, "unsigned madness");
PRUint32 rv = mUnicharDataLength - mUnicharDataOffset;
nsresult errorCode;
if (0 == rv) {
// Fill the unichar buffer
rv = Fill(&errorCode);
if (rv <= 0) {
*aReadCount = 0;
return errorCode;
}
}
if (rv > aCount) {
rv = aCount;
}
nsCRT::memcpy(aBuf + aOffset, mUnicharData->GetBuffer() + mUnicharDataOffset,
rv * sizeof(PRUnichar));
mUnicharDataOffset += rv;
*aReadCount = rv;
return NS_OK;
}
PRInt32
nsConverterInputStream::Fill(nsresult * aErrorCode)
{
if (nsnull == mInput) {
// We already closed the stream!
*aErrorCode = NS_BASE_STREAM_CLOSED;
return -1;
}
NS_ASSERTION(mByteData->GetLength() >= mByteDataOffset, "unsigned madness");
PRUint32 remainder = mByteData->GetLength() - mByteDataOffset;
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);
mUnicharDataOffset = 0;
mUnicharDataLength = dstLen;
mByteDataOffset += srcLen;
return dstLen;
}

Просмотреть файл

@ -0,0 +1,89 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
*
* 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 the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
*
* Alternatively, the contents of this file may be used under the terms of
* either 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"),
* 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
* use your version of this file under the terms of the NPL, indicate your
* 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
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsIConverterInputStream.h"
#include "nsString.h"
#include "nsCOMPtr.h"
#include "nsIUnicodeDecoder.h"
#include "nsIByteBuffer.h"
#include "nsIUnicharBuffer.h"
#define NS_CONVERTERINPUTSTREAM_CONTRACTID "@mozilla.org/intl/converter-input-stream;1"
// {2BC2AD62-AD5D-4b7b-A9DB-F74AE203C527}
#define NS_CONVERTERINPUTSTREAM_CID \
{ 0x2bc2ad62, 0xad5d, 0x4b7b, \
{ 0xa9, 0xdb, 0xf7, 0x4a, 0xe2, 0x3, 0xc5, 0x27 } }
class nsConverterInputStream : nsIConverterInputStream {
public:
NS_DECL_ISUPPORTS
NS_IMETHOD Read(PRUnichar* aBuf,
PRUint32 aOffset,
PRUint32 aCount,
PRUint32 *aReadCount);
NS_IMETHOD Close();
NS_IMETHOD Init(nsIInputStream* aStream, const PRUnichar *aCharset,
PRInt32 aBufferSize);
nsConverterInputStream() :
mByteDataOffset(0),
mUnicharDataOffset(0),
mUnicharDataLength(0) { NS_INIT_REFCNT(); }
virtual ~nsConverterInputStream() {}
private:
PRInt32 Fill(nsresult *aErrorCode);
nsCOMPtr<nsIUnicodeDecoder> mConverter;
nsCOMPtr<nsIByteBuffer> mByteData;
nsCOMPtr<nsIUnicharBuffer> mUnicharData;
nsCOMPtr<nsIInputStream> mInput;
PRUint32 mByteDataOffset;
PRUint32 mUnicharDataOffset;
PRUint32 mUnicharDataLength;
};