~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/intl/locale/src/nsCollation.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
9
 *
 
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
 
13
 * License.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
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.
 
21
 *
 
22
 * Contributor(s):
 
23
 *   Pierre Phaneuf <pp@ludusdesign.com>
 
24
 *
 
25
 *
 
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.
 
37
 *
 
38
 * ***** END LICENSE BLOCK ***** */
 
39
 
 
40
#include "nsIPlatformCharset.h"
 
41
#include "nsIServiceManager.h"
 
42
#include "nsIComponentManager.h"
 
43
#include "nsCollation.h"
 
44
#include "nsCollationCID.h"
 
45
#include "nsUnicharUtilCIID.h"
 
46
#include "prmem.h"
 
47
#include "nsReadableUtils.h"
 
48
 
 
49
////////////////////////////////////////////////////////////////////////////////
 
50
 
 
51
NS_DEFINE_CID(kCollationCID, NS_COLLATION_CID);
 
52
 
 
53
NS_IMPL_ISUPPORTS1(nsCollationFactory, nsICollationFactory)
 
54
 
 
55
nsresult nsCollationFactory::CreateCollation(nsILocale* locale, nsICollation** instancePtr)
 
56
{
 
57
  // Create a collation interface instance.
 
58
  //
 
59
  nsICollation *inst;
 
60
  nsresult res;
 
61
  
 
62
  res = CallCreateInstance(kCollationCID, &inst);
 
63
  if (NS_FAILED(res)) {
 
64
    return res;
 
65
  }
 
66
 
 
67
  inst->Initialize(locale);
 
68
  *instancePtr = inst;
 
69
 
 
70
  return res;
 
71
}
 
72
 
 
73
////////////////////////////////////////////////////////////////////////////////
 
74
 
 
75
nsCollation::nsCollation()
 
76
{
 
77
  MOZ_COUNT_CTOR(nsCollation);
 
78
  nsresult res;
 
79
  mCaseConversion = do_GetService(NS_UNICHARUTIL_CONTRACTID, &res);
 
80
  NS_ASSERTION(NS_SUCCEEDED(res), "CreateInstance failed for kCaseConversionIID");
 
81
}
 
82
 
 
83
nsCollation::~nsCollation()
 
84
{
 
85
  MOZ_COUNT_DTOR(nsCollation);
 
86
}
 
87
 
 
88
nsresult nsCollation::NormalizeString(const nsAString& stringIn, nsAString& stringOut)
 
89
{
 
90
  if (!mCaseConversion) {
 
91
    stringOut = stringIn;
 
92
  }
 
93
  else {
 
94
    PRInt32 aLength = stringIn.Length();
 
95
 
 
96
    if (aLength <= 64) {
 
97
      PRUnichar conversionBuffer[64];
 
98
      mCaseConversion->ToLower(PromiseFlatString(stringIn).get(), conversionBuffer, aLength);
 
99
      stringOut.Assign(conversionBuffer, aLength);
 
100
    }
 
101
    else {
 
102
      PRUnichar* conversionBuffer;
 
103
      conversionBuffer = new PRUnichar[aLength];
 
104
      if (!conversionBuffer) {
 
105
        return NS_ERROR_OUT_OF_MEMORY;
 
106
      }
 
107
      mCaseConversion->ToLower(PromiseFlatString(stringIn).get(), conversionBuffer, aLength);
 
108
      stringOut.Assign(conversionBuffer, aLength);
 
109
      delete [] conversionBuffer;
 
110
    }
 
111
  }
 
112
  return NS_OK;
 
113
}
 
114
 
 
115
nsresult nsCollation::SetCharset(const char* aCharset)
 
116
{
 
117
  NS_ENSURE_ARG_POINTER(aCharset);
 
118
 
 
119
  nsresult rv;
 
120
  nsCOMPtr <nsICharsetConverterManager> charsetConverterManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
 
121
  if (NS_SUCCEEDED(rv)) {
 
122
    rv = charsetConverterManager->GetUnicodeEncoder(aCharset,
 
123
                                                    getter_AddRefs(mEncoder));
 
124
  }
 
125
  return rv;
 
126
}
 
127
 
 
128
nsresult nsCollation::UnicodeToChar(const nsAString& aSrc, char** dst)
 
129
{
 
130
  NS_ENSURE_ARG_POINTER(dst);
 
131
 
 
132
  nsresult res = NS_OK;
 
133
  if (!mEncoder)
 
134
    res = SetCharset("ISO-8859-1");
 
135
 
 
136
  if (NS_SUCCEEDED(res)) {
 
137
    const nsPromiseFlatString& src = PromiseFlatString(aSrc);
 
138
    const PRUnichar *unichars = src.get();
 
139
    PRInt32 unicharLength = src.Length();
 
140
    PRInt32 dstLength;
 
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);
 
145
      if (*dst) {
 
146
        **dst = '\0';
 
147
        res = mEncoder->Convert(unichars, &unicharLength, *dst, &dstLength);
 
148
 
 
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';
 
157
            }
 
158
          }
 
159
        }
 
160
        if (NS_FAILED(res)) {
 
161
          PR_Free(*dst);
 
162
          *dst = nsnull;
 
163
        }
 
164
      }
 
165
      else {
 
166
        res = NS_ERROR_OUT_OF_MEMORY;
 
167
      }
 
168
    }
 
169
  }
 
170
 
 
171
  return res;
 
172
}
 
173
 
 
174
 
 
175