Back to index

lightning-sunbird  0.9+nobinonly
nsLocale.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsString.h"
00039 #include "nsReadableUtils.h"
00040 #include "pratom.h"
00041 #include "prtypes.h"
00042 #include "nsISupports.h"
00043 #include "nsILocale.h"
00044 #include "nsLocale.h"
00045 #include "nsLocaleCID.h"
00046 #include "nsCOMPtr.h"
00047 #include "nsVoidArray.h"
00048 #include "nsMemory.h"
00049 #include "nsCRT.h"
00050 
00051 #define LOCALE_HASH_SIZE  0xFF
00052 
00053 
00054 /* nsILocale */
00055 NS_IMPL_THREADSAFE_ISUPPORTS1(nsLocale, nsILocale)
00056 
00057 nsLocale::nsLocale(void)
00058 :  fHashtable(nsnull), fCategoryCount(0)
00059 {
00060   fHashtable = PL_NewHashTable(LOCALE_HASH_SIZE,&nsLocale::Hash_HashFunction,
00061                                &nsLocale::Hash_CompareNSString,
00062                                &nsLocale::Hash_CompareNSString, NULL, NULL);
00063   NS_ASSERTION(fHashtable, "nsLocale: failed to allocate PR_Hashtable");
00064 }
00065 
00066 nsLocale::nsLocale(nsLocale* other) : fHashtable(nsnull), fCategoryCount(0)
00067 {
00068   fHashtable = PL_NewHashTable(LOCALE_HASH_SIZE,&nsLocale::Hash_HashFunction,
00069                                &nsLocale::Hash_CompareNSString,
00070                                &nsLocale::Hash_CompareNSString, NULL, NULL);
00071   NS_ASSERTION(fHashtable, "nsLocale: failed to allocate PR_Hashtable");
00072 
00073   //
00074   // enumerate Hash and copy
00075   //
00076   PL_HashTableEnumerateEntries(other->fHashtable, 
00077                                &nsLocale::Hash_EnumerateCopy, fHashtable);
00078 }
00079 
00080 
00081 nsLocale::nsLocale(const nsStringArray& categoryList, 
00082                    const nsStringArray& valueList) 
00083                   : fHashtable(NULL), fCategoryCount(0)
00084 {
00085   PRInt32 i;
00086   PRUnichar* key, *value;
00087 
00088   fHashtable = PL_NewHashTable(LOCALE_HASH_SIZE,&nsLocale::Hash_HashFunction,
00089                                &nsLocale::Hash_CompareNSString,
00090                                &nsLocale::Hash_CompareNSString,
00091                                NULL, NULL);
00092   NS_ASSERTION(fHashtable, "nsLocale: failed to allocate PR_Hashtable");
00093 
00094   if (fHashtable)
00095   {
00096     for(i=0; i < categoryList.Count(); ++i) 
00097     {
00098       key = ToNewUnicode(*categoryList.StringAt(i));
00099       NS_ASSERTION(key, "nsLocale: failed to allocate internal hash key");
00100       value = ToNewUnicode(*valueList.StringAt(i));
00101       NS_ASSERTION(value, "nsLocale: failed to allocate internal hash value");
00102       if (!PL_HashTableAdd(fHashtable,key,value)) {
00103           nsMemory::Free(key);
00104           nsMemory::Free(value);
00105       }
00106     }
00107   }
00108 }
00109 
00110 nsLocale::~nsLocale(void)
00111 {
00112   // enumerate all the entries with a delete function to
00113   // safely delete all the keys and values
00114   PL_HashTableEnumerateEntries(fHashtable, &nsLocale::Hash_EnumerateDelete,
00115                                NULL);
00116 
00117   PL_HashTableDestroy(fHashtable);
00118 }
00119 
00120 NS_IMETHODIMP
00121 nsLocale::GetCategory(const nsAString& category, nsAString& result)
00122 {
00123   const PRUnichar *value = (const PRUnichar*) 
00124     PL_HashTableLookup(fHashtable, PromiseFlatString(category).get());
00125 
00126   if (value)
00127   {
00128     result.Assign(value);
00129     return NS_OK;
00130   }
00131 
00132   return NS_ERROR_FAILURE;
00133 }
00134 
00135 NS_IMETHODIMP
00136 nsLocale::AddCategory(const nsAString &category, const nsAString &value)
00137 {
00138   PRUnichar* newKey = ToNewUnicode(category);
00139   if (!newKey)
00140     return NS_ERROR_OUT_OF_MEMORY;
00141 
00142   PRUnichar* newValue = ToNewUnicode(value);
00143   if (!newValue) {
00144     nsMemory::Free(newKey);
00145     return NS_ERROR_OUT_OF_MEMORY;
00146   }
00147 
00148   if (!PL_HashTableAdd(fHashtable, newKey, newValue)) {
00149     nsMemory::Free(newKey);
00150     nsMemory::Free(newValue);
00151     return NS_ERROR_OUT_OF_MEMORY;
00152   }
00153 
00154   return NS_OK;
00155 }
00156 
00157 
00158 PLHashNumber
00159 nsLocale::Hash_HashFunction(const void* key)
00160 {
00161   const PRUnichar* ptr = (const PRUnichar *) key;
00162   PLHashNumber hash;
00163 
00164   hash = (PLHashNumber)0;
00165 
00166   while (*ptr)
00167     hash += (PLHashNumber) *ptr++;
00168 
00169   return hash;
00170 }
00171 
00172 
00173 PRIntn
00174 nsLocale::Hash_CompareNSString(const void* s1, const void* s2)
00175 {
00176   return !nsCRT::strcmp((const PRUnichar *) s1, (const PRUnichar *) s2);
00177 }
00178 
00179 
00180 PRIntn
00181 nsLocale::Hash_EnumerateDelete(PLHashEntry *he, PRIntn hashIndex, void *arg)
00182 {
00183   // delete an entry
00184   nsMemory::Free((PRUnichar *)he->key);
00185   nsMemory::Free((PRUnichar *)he->value);
00186 
00187   return (HT_ENUMERATE_NEXT | HT_ENUMERATE_REMOVE);
00188 }
00189 
00190 PRIntn
00191 nsLocale::Hash_EnumerateCopy(PLHashEntry *he, PRIntn hashIndex, void* arg)
00192 {
00193   PRUnichar* newKey = ToNewUnicode(nsDependentString((PRUnichar *)he->key));
00194   if (!newKey) 
00195     return HT_ENUMERATE_STOP;
00196 
00197   PRUnichar* newValue = ToNewUnicode(nsDependentString((PRUnichar *)he->value));
00198   if (!newValue) {
00199     nsMemory::Free(newKey);
00200     return HT_ENUMERATE_STOP;
00201   }
00202 
00203   if (!PL_HashTableAdd((PLHashTable*)arg, newKey, newValue)) {
00204     nsMemory::Free(newKey);
00205     nsMemory::Free(newValue);
00206     return HT_ENUMERATE_STOP;
00207   }
00208 
00209   return (HT_ENUMERATE_NEXT);
00210 }
00211