1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/locale/src/nsLocale.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,108 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsString.h" 1.10 +#include "nsISupports.h" 1.11 +#include "nsILocale.h" 1.12 +#include "nsLocale.h" 1.13 +#include "nsMemory.h" 1.14 +#include "nsCRT.h" 1.15 + 1.16 +#define LOCALE_HASH_SIZE 0xFF 1.17 + 1.18 + 1.19 +/* nsILocale */ 1.20 +NS_IMPL_ISUPPORTS(nsLocale, nsILocale) 1.21 + 1.22 +nsLocale::nsLocale(void) 1.23 +: fHashtable(nullptr), fCategoryCount(0) 1.24 +{ 1.25 + fHashtable = PL_NewHashTable(LOCALE_HASH_SIZE,&nsLocale::Hash_HashFunction, 1.26 + &nsLocale::Hash_CompareNSString, 1.27 + &nsLocale::Hash_CompareNSString, 1.28 + nullptr, nullptr); 1.29 + NS_ASSERTION(fHashtable, "nsLocale: failed to allocate PR_Hashtable"); 1.30 +} 1.31 + 1.32 +nsLocale::~nsLocale(void) 1.33 +{ 1.34 + // enumerate all the entries with a delete function to 1.35 + // safely delete all the keys and values 1.36 + PL_HashTableEnumerateEntries(fHashtable, &nsLocale::Hash_EnumerateDelete, 1.37 + nullptr); 1.38 + 1.39 + PL_HashTableDestroy(fHashtable); 1.40 +} 1.41 + 1.42 +NS_IMETHODIMP 1.43 +nsLocale::GetCategory(const nsAString& category, nsAString& result) 1.44 +{ 1.45 + const char16_t *value = (const char16_t*) 1.46 + PL_HashTableLookup(fHashtable, PromiseFlatString(category).get()); 1.47 + 1.48 + if (value) 1.49 + { 1.50 + result.Assign(value); 1.51 + return NS_OK; 1.52 + } 1.53 + 1.54 + return NS_ERROR_FAILURE; 1.55 +} 1.56 + 1.57 +NS_IMETHODIMP 1.58 +nsLocale::AddCategory(const nsAString &category, const nsAString &value) 1.59 +{ 1.60 + char16_t* newKey = ToNewUnicode(category); 1.61 + if (!newKey) 1.62 + return NS_ERROR_OUT_OF_MEMORY; 1.63 + 1.64 + char16_t* newValue = ToNewUnicode(value); 1.65 + if (!newValue) { 1.66 + nsMemory::Free(newKey); 1.67 + return NS_ERROR_OUT_OF_MEMORY; 1.68 + } 1.69 + 1.70 + if (!PL_HashTableAdd(fHashtable, newKey, newValue)) { 1.71 + nsMemory::Free(newKey); 1.72 + nsMemory::Free(newValue); 1.73 + return NS_ERROR_OUT_OF_MEMORY; 1.74 + } 1.75 + 1.76 + return NS_OK; 1.77 +} 1.78 + 1.79 + 1.80 +PLHashNumber 1.81 +nsLocale::Hash_HashFunction(const void* key) 1.82 +{ 1.83 + const char16_t* ptr = (const char16_t *) key; 1.84 + PLHashNumber hash; 1.85 + 1.86 + hash = (PLHashNumber)0; 1.87 + 1.88 + while (*ptr) 1.89 + hash += (PLHashNumber) *ptr++; 1.90 + 1.91 + return hash; 1.92 +} 1.93 + 1.94 + 1.95 +int 1.96 +nsLocale::Hash_CompareNSString(const void* s1, const void* s2) 1.97 +{ 1.98 + return !nsCRT::strcmp((const char16_t *) s1, (const char16_t *) s2); 1.99 +} 1.100 + 1.101 + 1.102 +int 1.103 +nsLocale::Hash_EnumerateDelete(PLHashEntry *he, int hashIndex, void *arg) 1.104 +{ 1.105 + // delete an entry 1.106 + nsMemory::Free((char16_t *)he->key); 1.107 + nsMemory::Free((char16_t *)he->value); 1.108 + 1.109 + return (HT_ENUMERATE_NEXT | HT_ENUMERATE_REMOVE); 1.110 +} 1.111 +