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.

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

mercurial