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.
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.
37
* ***** END LICENSE BLOCK ***** */
40
#include "nsReadableUtils.h"
43
#include "nsISupports.h"
44
#include "nsILocale.h"
46
#include "nsLocaleCID.h"
48
#include "nsVoidArray.h"
52
#define LOCALE_HASH_SIZE 0xFF
56
NS_IMPL_THREADSAFE_ISUPPORTS1(nsLocale, nsILocale)
58
nsLocale::nsLocale(void)
59
: fHashtable(nsnull), fCategoryCount(0)
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");
67
nsLocale::nsLocale(nsLocale* other) : fHashtable(nsnull), fCategoryCount(0)
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");
75
// enumerate Hash and copy
77
PL_HashTableEnumerateEntries(other->fHashtable,
78
&nsLocale::Hash_EnumerateCopy, fHashtable);
82
nsLocale::nsLocale(const nsStringArray& categoryList,
83
const nsStringArray& valueList)
84
: fHashtable(NULL), fCategoryCount(0)
87
PRUnichar* key, *value;
89
fHashtable = PL_NewHashTable(LOCALE_HASH_SIZE,&nsLocale::Hash_HashFunction,
90
&nsLocale::Hash_CompareNSString,
91
&nsLocale::Hash_CompareNSString,
93
NS_ASSERTION(fHashtable, "nsLocale: failed to allocate PR_Hashtable");
97
for(i=0; i < categoryList.Count(); ++i)
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)) {
105
nsMemory::Free(value);
111
nsLocale::~nsLocale(void)
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,
118
PL_HashTableDestroy(fHashtable);
122
nsLocale::GetCategory(const nsAString& category, nsAString& result)
124
const PRUnichar *value = (const PRUnichar*)
125
PL_HashTableLookup(fHashtable, PromiseFlatString(category).get());
129
result.Assign(value);
133
return NS_ERROR_FAILURE;
137
nsLocale::AddCategory(const nsAString &category, const nsAString &value)
139
PRUnichar* newKey = ToNewUnicode(category);
141
return NS_ERROR_OUT_OF_MEMORY;
143
PRUnichar* newValue = ToNewUnicode(value);
145
nsMemory::Free(newKey);
146
return NS_ERROR_OUT_OF_MEMORY;
149
if (!PL_HashTableAdd(fHashtable, newKey, newValue)) {
150
nsMemory::Free(newKey);
151
nsMemory::Free(newValue);
152
return NS_ERROR_OUT_OF_MEMORY;
160
nsLocale::Hash_HashFunction(const void* key)
162
const PRUnichar* ptr = (const PRUnichar *) key;
165
hash = (PLHashNumber)0;
168
hash += (PLHashNumber) *ptr++;
175
nsLocale::Hash_CompareNSString(const void* s1, const void* s2)
177
return !nsCRT::strcmp((const PRUnichar *) s1, (const PRUnichar *) s2);
182
nsLocale::Hash_EnumerateDelete(PLHashEntry *he, PRIntn hashIndex, void *arg)
185
nsMemory::Free((PRUnichar *)he->key);
186
nsMemory::Free((PRUnichar *)he->value);
188
return (HT_ENUMERATE_NEXT | HT_ENUMERATE_REMOVE);
192
nsLocale::Hash_EnumerateCopy(PLHashEntry *he, PRIntn hashIndex, void* arg)
194
PRUnichar* newKey = ToNewUnicode(nsDependentString((PRUnichar *)he->key));
196
return HT_ENUMERATE_STOP;
198
PRUnichar* newValue = ToNewUnicode(nsDependentString((PRUnichar *)he->value));
200
nsMemory::Free(newKey);
201
return HT_ENUMERATE_STOP;
204
if (!PL_HashTableAdd((PLHashTable*)arg, newKey, newValue)) {
205
nsMemory::Free(newKey);
206
nsMemory::Free(newValue);
207
return HT_ENUMERATE_STOP;
210
return (HT_ENUMERATE_NEXT);