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

« back to all changes in this revision

Viewing changes to mozilla/intl/locale/src/nsLocale.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
 *
 
24
 *
 
25
 * Alternatively, the contents of this file may be used under the terms of
 
26
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
27
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
28
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
29
 * of those above. If you wish to allow use of your version of this file only
 
30
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
31
 * use your version of this file under the terms of the NPL, indicate your
 
32
 * decision by deleting the provisions above and replace them with the notice
 
33
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
34
 * the provisions above, a recipient may use your version of this file under
 
35
 * the terms of any one of the NPL, the GPL or the LGPL.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
#include "nsString.h"
 
40
#include "nsReadableUtils.h"
 
41
#include "pratom.h"
 
42
#include "prtypes.h"
 
43
#include "nsISupports.h"
 
44
#include "nsILocale.h"
 
45
#include "nsLocale.h"
 
46
#include "nsLocaleCID.h"
 
47
#include "nsCOMPtr.h"
 
48
#include "nsVoidArray.h"
 
49
#include "nsMemory.h"
 
50
#include "nsCRT.h"
 
51
 
 
52
#define LOCALE_HASH_SIZE  0xFF
 
53
 
 
54
 
 
55
/* nsILocale */
 
56
NS_IMPL_THREADSAFE_ISUPPORTS1(nsLocale, nsILocale)
 
57
 
 
58
nsLocale::nsLocale(void)
 
59
:  fHashtable(nsnull), fCategoryCount(0)
 
60
{
 
61
  fHashtable = PL_NewHashTable(LOCALE_HASH_SIZE,&nsLocale::Hash_HashFunction,
 
62
                               &nsLocale::Hash_CompareNSString,
 
63
                               &nsLocale::Hash_CompareNSString, NULL, NULL);
 
64
  NS_ASSERTION(fHashtable, "nsLocale: failed to allocate PR_Hashtable");
 
65
}
 
66
 
 
67
nsLocale::nsLocale(nsLocale* other) : fHashtable(nsnull), fCategoryCount(0)
 
68
{
 
69
  fHashtable = PL_NewHashTable(LOCALE_HASH_SIZE,&nsLocale::Hash_HashFunction,
 
70
                               &nsLocale::Hash_CompareNSString,
 
71
                               &nsLocale::Hash_CompareNSString, NULL, NULL);
 
72
  NS_ASSERTION(fHashtable, "nsLocale: failed to allocate PR_Hashtable");
 
73
 
 
74
  //
 
75
  // enumerate Hash and copy
 
76
  //
 
77
  PL_HashTableEnumerateEntries(other->fHashtable, 
 
78
                               &nsLocale::Hash_EnumerateCopy, fHashtable);
 
79
}
 
80
 
 
81
 
 
82
nsLocale::nsLocale(const nsStringArray& categoryList, 
 
83
                   const nsStringArray& valueList) 
 
84
                  : fHashtable(NULL), fCategoryCount(0)
 
85
{
 
86
  PRInt32 i;
 
87
  PRUnichar* key, *value;
 
88
 
 
89
  fHashtable = PL_NewHashTable(LOCALE_HASH_SIZE,&nsLocale::Hash_HashFunction,
 
90
                               &nsLocale::Hash_CompareNSString,
 
91
                               &nsLocale::Hash_CompareNSString,
 
92
                               NULL, NULL);
 
93
  NS_ASSERTION(fHashtable, "nsLocale: failed to allocate PR_Hashtable");
 
94
 
 
95
  if (fHashtable)
 
96
  {
 
97
    for(i=0; i < categoryList.Count(); ++i) 
 
98
    {
 
99
      key = ToNewUnicode(*categoryList.StringAt(i));
 
100
      NS_ASSERTION(key, "nsLocale: failed to allocate internal hash key");
 
101
      value = ToNewUnicode(*valueList.StringAt(i));
 
102
      NS_ASSERTION(value, "nsLocale: failed to allocate internal hash value");
 
103
      if (!PL_HashTableAdd(fHashtable,key,value)) {
 
104
          nsMemory::Free(key);
 
105
          nsMemory::Free(value);
 
106
      }
 
107
    }
 
108
  }
 
109
}
 
110
 
 
111
nsLocale::~nsLocale(void)
 
112
{
 
113
  // enumerate all the entries with a delete function to
 
114
  // safely delete all the keys and values
 
115
  PL_HashTableEnumerateEntries(fHashtable, &nsLocale::Hash_EnumerateDelete,
 
116
                               NULL);
 
117
 
 
118
  PL_HashTableDestroy(fHashtable);
 
119
}
 
120
 
 
121
NS_IMETHODIMP
 
122
nsLocale::GetCategory(const nsAString& category, nsAString& result)
 
123
{
 
124
  const PRUnichar *value = (const PRUnichar*) 
 
125
    PL_HashTableLookup(fHashtable, PromiseFlatString(category).get());
 
126
 
 
127
  if (value)
 
128
  {
 
129
    result.Assign(value);
 
130
    return NS_OK;
 
131
  }
 
132
 
 
133
  return NS_ERROR_FAILURE;
 
134
}
 
135
 
 
136
NS_IMETHODIMP
 
137
nsLocale::AddCategory(const nsAString &category, const nsAString &value)
 
138
{
 
139
  PRUnichar* newKey = ToNewUnicode(category);
 
140
  if (!newKey)
 
141
    return NS_ERROR_OUT_OF_MEMORY;
 
142
 
 
143
  PRUnichar* newValue = ToNewUnicode(value);
 
144
  if (!newValue) {
 
145
    nsMemory::Free(newKey);
 
146
    return NS_ERROR_OUT_OF_MEMORY;
 
147
  }
 
148
 
 
149
  if (!PL_HashTableAdd(fHashtable, newKey, newValue)) {
 
150
    nsMemory::Free(newKey);
 
151
    nsMemory::Free(newValue);
 
152
    return NS_ERROR_OUT_OF_MEMORY;
 
153
  }
 
154
 
 
155
  return NS_OK;
 
156
}
 
157
 
 
158
 
 
159
PLHashNumber
 
160
nsLocale::Hash_HashFunction(const void* key)
 
161
{
 
162
  const PRUnichar* ptr = (const PRUnichar *) key;
 
163
  PLHashNumber hash;
 
164
 
 
165
  hash = (PLHashNumber)0;
 
166
 
 
167
  while (*ptr)
 
168
    hash += (PLHashNumber) *ptr++;
 
169
 
 
170
  return hash;
 
171
}
 
172
 
 
173
 
 
174
PRIntn
 
175
nsLocale::Hash_CompareNSString(const void* s1, const void* s2)
 
176
{
 
177
  return !nsCRT::strcmp((const PRUnichar *) s1, (const PRUnichar *) s2);
 
178
}
 
179
 
 
180
 
 
181
PRIntn
 
182
nsLocale::Hash_EnumerateDelete(PLHashEntry *he, PRIntn hashIndex, void *arg)
 
183
{
 
184
  // delete an entry
 
185
  nsMemory::Free((PRUnichar *)he->key);
 
186
  nsMemory::Free((PRUnichar *)he->value);
 
187
 
 
188
  return (HT_ENUMERATE_NEXT | HT_ENUMERATE_REMOVE);
 
189
}
 
190
 
 
191
PRIntn
 
192
nsLocale::Hash_EnumerateCopy(PLHashEntry *he, PRIntn hashIndex, void* arg)
 
193
{
 
194
  PRUnichar* newKey = ToNewUnicode(nsDependentString((PRUnichar *)he->key));
 
195
  if (!newKey) 
 
196
    return HT_ENUMERATE_STOP;
 
197
 
 
198
  PRUnichar* newValue = ToNewUnicode(nsDependentString((PRUnichar *)he->value));
 
199
  if (!newValue) {
 
200
    nsMemory::Free(newKey);
 
201
    return HT_ENUMERATE_STOP;
 
202
  }
 
203
 
 
204
  if (!PL_HashTableAdd((PLHashTable*)arg, newKey, newValue)) {
 
205
    nsMemory::Free(newKey);
 
206
    nsMemory::Free(newValue);
 
207
    return HT_ENUMERATE_STOP;
 
208
  }
 
209
 
 
210
  return (HT_ENUMERATE_NEXT);
 
211
}
 
212