1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
23
* Pierre Phaneuf <pp@ludusdesign.com>
26
* Alternatively, the contents of this file may be used under the terms of
27
* either the GNU General Public License Version 2 or later (the "GPL"), or
28
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29
* in which case the provisions of the GPL or the LGPL are applicable instead
30
* of those above. If you wish to allow use of your version of this file only
31
* under the terms of either the GPL or the LGPL, and not to allow others to
32
* use your version of this file under the terms of the NPL, indicate your
33
* decision by deleting the provisions above and replace them with the notice
34
* and other provisions required by the GPL or the LGPL. If you do not delete
35
* the provisions above, a recipient may use your version of this file under
36
* the terms of any one of the NPL, the GPL or the LGPL.
38
* ***** END LICENSE BLOCK ***** */
40
#include "nsIPlatformCharset.h"
41
#include "nsIServiceManager.h"
42
#include "nsIComponentManager.h"
43
#include "nsCollation.h"
44
#include "nsCollationCID.h"
45
#include "nsUnicharUtilCIID.h"
47
#include "nsReadableUtils.h"
49
////////////////////////////////////////////////////////////////////////////////
51
NS_DEFINE_CID(kCollationCID, NS_COLLATION_CID);
53
NS_IMPL_ISUPPORTS1(nsCollationFactory, nsICollationFactory)
55
nsresult nsCollationFactory::CreateCollation(nsILocale* locale, nsICollation** instancePtr)
57
// Create a collation interface instance.
62
res = CallCreateInstance(kCollationCID, &inst);
67
inst->Initialize(locale);
73
////////////////////////////////////////////////////////////////////////////////
75
nsCollation::nsCollation()
77
MOZ_COUNT_CTOR(nsCollation);
79
mCaseConversion = do_GetService(NS_UNICHARUTIL_CONTRACTID, &res);
80
NS_ASSERTION(NS_SUCCEEDED(res), "CreateInstance failed for kCaseConversionIID");
83
nsCollation::~nsCollation()
85
MOZ_COUNT_DTOR(nsCollation);
88
nsresult nsCollation::NormalizeString(const nsAString& stringIn, nsAString& stringOut)
90
if (!mCaseConversion) {
94
PRInt32 aLength = stringIn.Length();
97
PRUnichar conversionBuffer[64];
98
mCaseConversion->ToLower(PromiseFlatString(stringIn).get(), conversionBuffer, aLength);
99
stringOut.Assign(conversionBuffer, aLength);
102
PRUnichar* conversionBuffer;
103
conversionBuffer = new PRUnichar[aLength];
104
if (!conversionBuffer) {
105
return NS_ERROR_OUT_OF_MEMORY;
107
mCaseConversion->ToLower(PromiseFlatString(stringIn).get(), conversionBuffer, aLength);
108
stringOut.Assign(conversionBuffer, aLength);
109
delete [] conversionBuffer;
115
nsresult nsCollation::SetCharset(const char* aCharset)
117
NS_ENSURE_ARG_POINTER(aCharset);
120
nsCOMPtr <nsICharsetConverterManager> charsetConverterManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
121
if (NS_SUCCEEDED(rv)) {
122
rv = charsetConverterManager->GetUnicodeEncoder(aCharset,
123
getter_AddRefs(mEncoder));
128
nsresult nsCollation::UnicodeToChar(const nsAString& aSrc, char** dst)
130
NS_ENSURE_ARG_POINTER(dst);
132
nsresult res = NS_OK;
134
res = SetCharset("ISO-8859-1");
136
if (NS_SUCCEEDED(res)) {
137
const nsPromiseFlatString& src = PromiseFlatString(aSrc);
138
const PRUnichar *unichars = src.get();
139
PRInt32 unicharLength = src.Length();
141
res = mEncoder->GetMaxLength(unichars, unicharLength, &dstLength);
142
if (NS_SUCCEEDED(res)) {
143
PRInt32 bufLength = dstLength + 1 + 32; // extra 32 bytes for Finish() call
144
*dst = (char *) PR_Malloc(bufLength);
147
res = mEncoder->Convert(unichars, &unicharLength, *dst, &dstLength);
149
if (NS_SUCCEEDED(res) || (NS_ERROR_UENC_NOMAPPING == res)) {
150
// Finishes the conversion. The converter has the possibility to write some
151
// extra data and flush its final state.
152
PRInt32 finishLength = bufLength - dstLength; // remaining unused buffer length
153
if (finishLength > 0) {
154
res = mEncoder->Finish((*dst + dstLength), &finishLength);
155
if (NS_SUCCEEDED(res)) {
156
(*dst)[dstLength + finishLength] = '\0';
160
if (NS_FAILED(res)) {
166
res = NS_ERROR_OUT_OF_MEMORY;