intl/locale/src/nsLocale.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsString.h"
michael@0 7 #include "nsISupports.h"
michael@0 8 #include "nsILocale.h"
michael@0 9 #include "nsLocale.h"
michael@0 10 #include "nsMemory.h"
michael@0 11 #include "nsCRT.h"
michael@0 12
michael@0 13 #define LOCALE_HASH_SIZE 0xFF
michael@0 14
michael@0 15
michael@0 16 /* nsILocale */
michael@0 17 NS_IMPL_ISUPPORTS(nsLocale, nsILocale)
michael@0 18
michael@0 19 nsLocale::nsLocale(void)
michael@0 20 : fHashtable(nullptr), fCategoryCount(0)
michael@0 21 {
michael@0 22 fHashtable = PL_NewHashTable(LOCALE_HASH_SIZE,&nsLocale::Hash_HashFunction,
michael@0 23 &nsLocale::Hash_CompareNSString,
michael@0 24 &nsLocale::Hash_CompareNSString,
michael@0 25 nullptr, nullptr);
michael@0 26 NS_ASSERTION(fHashtable, "nsLocale: failed to allocate PR_Hashtable");
michael@0 27 }
michael@0 28
michael@0 29 nsLocale::~nsLocale(void)
michael@0 30 {
michael@0 31 // enumerate all the entries with a delete function to
michael@0 32 // safely delete all the keys and values
michael@0 33 PL_HashTableEnumerateEntries(fHashtable, &nsLocale::Hash_EnumerateDelete,
michael@0 34 nullptr);
michael@0 35
michael@0 36 PL_HashTableDestroy(fHashtable);
michael@0 37 }
michael@0 38
michael@0 39 NS_IMETHODIMP
michael@0 40 nsLocale::GetCategory(const nsAString& category, nsAString& result)
michael@0 41 {
michael@0 42 const char16_t *value = (const char16_t*)
michael@0 43 PL_HashTableLookup(fHashtable, PromiseFlatString(category).get());
michael@0 44
michael@0 45 if (value)
michael@0 46 {
michael@0 47 result.Assign(value);
michael@0 48 return NS_OK;
michael@0 49 }
michael@0 50
michael@0 51 return NS_ERROR_FAILURE;
michael@0 52 }
michael@0 53
michael@0 54 NS_IMETHODIMP
michael@0 55 nsLocale::AddCategory(const nsAString &category, const nsAString &value)
michael@0 56 {
michael@0 57 char16_t* newKey = ToNewUnicode(category);
michael@0 58 if (!newKey)
michael@0 59 return NS_ERROR_OUT_OF_MEMORY;
michael@0 60
michael@0 61 char16_t* newValue = ToNewUnicode(value);
michael@0 62 if (!newValue) {
michael@0 63 nsMemory::Free(newKey);
michael@0 64 return NS_ERROR_OUT_OF_MEMORY;
michael@0 65 }
michael@0 66
michael@0 67 if (!PL_HashTableAdd(fHashtable, newKey, newValue)) {
michael@0 68 nsMemory::Free(newKey);
michael@0 69 nsMemory::Free(newValue);
michael@0 70 return NS_ERROR_OUT_OF_MEMORY;
michael@0 71 }
michael@0 72
michael@0 73 return NS_OK;
michael@0 74 }
michael@0 75
michael@0 76
michael@0 77 PLHashNumber
michael@0 78 nsLocale::Hash_HashFunction(const void* key)
michael@0 79 {
michael@0 80 const char16_t* ptr = (const char16_t *) key;
michael@0 81 PLHashNumber hash;
michael@0 82
michael@0 83 hash = (PLHashNumber)0;
michael@0 84
michael@0 85 while (*ptr)
michael@0 86 hash += (PLHashNumber) *ptr++;
michael@0 87
michael@0 88 return hash;
michael@0 89 }
michael@0 90
michael@0 91
michael@0 92 int
michael@0 93 nsLocale::Hash_CompareNSString(const void* s1, const void* s2)
michael@0 94 {
michael@0 95 return !nsCRT::strcmp((const char16_t *) s1, (const char16_t *) s2);
michael@0 96 }
michael@0 97
michael@0 98
michael@0 99 int
michael@0 100 nsLocale::Hash_EnumerateDelete(PLHashEntry *he, int hashIndex, void *arg)
michael@0 101 {
michael@0 102 // delete an entry
michael@0 103 nsMemory::Free((char16_t *)he->key);
michael@0 104 nsMemory::Free((char16_t *)he->value);
michael@0 105
michael@0 106 return (HT_ENUMERATE_NEXT | HT_ENUMERATE_REMOVE);
michael@0 107 }
michael@0 108

mercurial