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