2001-09-26 04:40:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04: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/. */
|
1999-01-29 02:27:55 +03:00
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsILocale.h"
|
|
|
|
#include "nsLocale.h"
|
2003-10-30 08:04:45 +03:00
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsCRT.h"
|
1999-01-29 02:27:55 +03:00
|
|
|
|
2003-10-30 08:04:45 +03:00
|
|
|
#define LOCALE_HASH_SIZE 0xFF
|
1999-01-29 02:27:55 +03:00
|
|
|
|
|
|
|
|
|
|
|
/* nsILocale */
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsLocale, nsILocale)
|
1999-01-29 02:27:55 +03:00
|
|
|
|
1999-08-02 07:00:23 +04:00
|
|
|
nsLocale::nsLocale(void)
|
2012-07-30 18:20:58 +04:00
|
|
|
: fHashtable(nullptr), fCategoryCount(0)
|
1999-08-02 07:00:23 +04:00
|
|
|
{
|
2003-10-30 08:04:45 +03:00
|
|
|
fHashtable = PL_NewHashTable(LOCALE_HASH_SIZE,&nsLocale::Hash_HashFunction,
|
|
|
|
&nsLocale::Hash_CompareNSString,
|
2012-08-24 11:49:00 +04:00
|
|
|
&nsLocale::Hash_CompareNSString,
|
|
|
|
nullptr, nullptr);
|
2003-10-30 08:04:45 +03:00
|
|
|
NS_ASSERTION(fHashtable, "nsLocale: failed to allocate PR_Hashtable");
|
1999-08-02 07:00:23 +04:00
|
|
|
}
|
|
|
|
|
1999-01-29 02:27:55 +03:00
|
|
|
nsLocale::~nsLocale(void)
|
|
|
|
{
|
2003-10-30 08:04:45 +03:00
|
|
|
// enumerate all the entries with a delete function to
|
|
|
|
// safely delete all the keys and values
|
|
|
|
PL_HashTableEnumerateEntries(fHashtable, &nsLocale::Hash_EnumerateDelete,
|
2012-08-24 11:49:00 +04:00
|
|
|
nullptr);
|
1999-01-29 02:27:55 +03:00
|
|
|
|
2003-10-30 08:04:45 +03:00
|
|
|
PL_HashTableDestroy(fHashtable);
|
1999-01-29 02:27:55 +03:00
|
|
|
}
|
|
|
|
|
1999-06-24 01:01:08 +04:00
|
|
|
NS_IMETHODIMP
|
2003-10-30 08:04:45 +03:00
|
|
|
nsLocale::GetCategory(const nsAString& category, nsAString& result)
|
1999-06-24 01:01:08 +04:00
|
|
|
{
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *value = (const char16_t*)
|
2003-10-30 08:04:45 +03:00
|
|
|
PL_HashTableLookup(fHashtable, PromiseFlatString(category).get());
|
1999-07-14 20:33:43 +04:00
|
|
|
|
2003-10-30 08:04:45 +03:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
result.Assign(value);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-07-14 20:33:43 +04:00
|
|
|
|
2003-10-30 08:04:45 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-06-24 01:01:08 +04:00
|
|
|
}
|
|
|
|
|
1999-08-02 07:00:23 +04:00
|
|
|
NS_IMETHODIMP
|
2003-10-30 08:04:45 +03:00
|
|
|
nsLocale::AddCategory(const nsAString &category, const nsAString &value)
|
1999-08-02 07:00:23 +04:00
|
|
|
{
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* newKey = ToNewUnicode(category);
|
2003-10-30 08:04:45 +03:00
|
|
|
if (!newKey)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* newValue = ToNewUnicode(value);
|
2003-10-30 08:04:45 +03:00
|
|
|
if (!newValue) {
|
2015-03-27 03:01:12 +03:00
|
|
|
free(newKey);
|
2003-10-30 08:04:45 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PL_HashTableAdd(fHashtable, newKey, newValue)) {
|
2015-03-27 03:01:12 +03:00
|
|
|
free(newKey);
|
|
|
|
free(newValue);
|
2003-10-30 08:04:45 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-08-02 07:00:23 +04:00
|
|
|
}
|
|
|
|
|
1999-06-24 01:01:08 +04:00
|
|
|
|
1999-01-29 02:27:55 +03:00
|
|
|
PLHashNumber
|
|
|
|
nsLocale::Hash_HashFunction(const void* key)
|
|
|
|
{
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* ptr = (const char16_t *) key;
|
2003-10-30 08:04:45 +03:00
|
|
|
PLHashNumber hash;
|
1999-01-29 02:27:55 +03:00
|
|
|
|
2003-10-30 08:04:45 +03:00
|
|
|
hash = (PLHashNumber)0;
|
1999-01-29 02:27:55 +03:00
|
|
|
|
2003-10-30 08:04:45 +03:00
|
|
|
while (*ptr)
|
|
|
|
hash += (PLHashNumber) *ptr++;
|
1999-01-29 02:27:55 +03:00
|
|
|
|
2003-10-30 08:04:45 +03:00
|
|
|
return hash;
|
1999-01-29 02:27:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-09 11:09:40 +04:00
|
|
|
int
|
1999-01-29 02:27:55 +03:00
|
|
|
nsLocale::Hash_CompareNSString(const void* s1, const void* s2)
|
|
|
|
{
|
2014-01-04 19:02:17 +04:00
|
|
|
return !nsCRT::strcmp((const char16_t *) s1, (const char16_t *) s2);
|
1999-01-29 02:27:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-09 11:09:40 +04:00
|
|
|
int
|
|
|
|
nsLocale::Hash_EnumerateDelete(PLHashEntry *he, int hashIndex, void *arg)
|
1999-01-29 02:27:55 +03:00
|
|
|
{
|
2003-10-30 08:04:45 +03:00
|
|
|
// delete an entry
|
2015-03-27 03:01:12 +03:00
|
|
|
free((char16_t *)he->key);
|
|
|
|
free((char16_t *)he->value);
|
1999-01-29 02:27:55 +03:00
|
|
|
|
2003-10-30 08:04:45 +03:00
|
|
|
return (HT_ENUMERATE_NEXT | HT_ENUMERATE_REMOVE);
|
1999-03-30 05:48:20 +04:00
|
|
|
}
|
1999-08-02 07:00:23 +04:00
|
|
|
|