michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsString.h" michael@0: #include "nsISupports.h" michael@0: #include "nsILocale.h" michael@0: #include "nsLocale.h" michael@0: #include "nsMemory.h" michael@0: #include "nsCRT.h" michael@0: michael@0: #define LOCALE_HASH_SIZE 0xFF michael@0: michael@0: michael@0: /* nsILocale */ michael@0: NS_IMPL_ISUPPORTS(nsLocale, nsILocale) michael@0: michael@0: nsLocale::nsLocale(void) michael@0: : fHashtable(nullptr), fCategoryCount(0) michael@0: { michael@0: fHashtable = PL_NewHashTable(LOCALE_HASH_SIZE,&nsLocale::Hash_HashFunction, michael@0: &nsLocale::Hash_CompareNSString, michael@0: &nsLocale::Hash_CompareNSString, michael@0: nullptr, nullptr); michael@0: NS_ASSERTION(fHashtable, "nsLocale: failed to allocate PR_Hashtable"); michael@0: } michael@0: michael@0: nsLocale::~nsLocale(void) michael@0: { michael@0: // enumerate all the entries with a delete function to michael@0: // safely delete all the keys and values michael@0: PL_HashTableEnumerateEntries(fHashtable, &nsLocale::Hash_EnumerateDelete, michael@0: nullptr); michael@0: michael@0: PL_HashTableDestroy(fHashtable); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsLocale::GetCategory(const nsAString& category, nsAString& result) michael@0: { michael@0: const char16_t *value = (const char16_t*) michael@0: PL_HashTableLookup(fHashtable, PromiseFlatString(category).get()); michael@0: michael@0: if (value) michael@0: { michael@0: result.Assign(value); michael@0: return NS_OK; michael@0: } michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsLocale::AddCategory(const nsAString &category, const nsAString &value) michael@0: { michael@0: char16_t* newKey = ToNewUnicode(category); michael@0: if (!newKey) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: char16_t* newValue = ToNewUnicode(value); michael@0: if (!newValue) { michael@0: nsMemory::Free(newKey); michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: if (!PL_HashTableAdd(fHashtable, newKey, newValue)) { michael@0: nsMemory::Free(newKey); michael@0: nsMemory::Free(newValue); michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: PLHashNumber michael@0: nsLocale::Hash_HashFunction(const void* key) michael@0: { michael@0: const char16_t* ptr = (const char16_t *) key; michael@0: PLHashNumber hash; michael@0: michael@0: hash = (PLHashNumber)0; michael@0: michael@0: while (*ptr) michael@0: hash += (PLHashNumber) *ptr++; michael@0: michael@0: return hash; michael@0: } michael@0: michael@0: michael@0: int michael@0: nsLocale::Hash_CompareNSString(const void* s1, const void* s2) michael@0: { michael@0: return !nsCRT::strcmp((const char16_t *) s1, (const char16_t *) s2); michael@0: } michael@0: michael@0: michael@0: int michael@0: nsLocale::Hash_EnumerateDelete(PLHashEntry *he, int hashIndex, void *arg) michael@0: { michael@0: // delete an entry michael@0: nsMemory::Free((char16_t *)he->key); michael@0: nsMemory::Free((char16_t *)he->value); michael@0: michael@0: return (HT_ENUMERATE_NEXT | HT_ENUMERATE_REMOVE); michael@0: } michael@0: