2001-09-26 00:40:45 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 14:21:17 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-01-28 23:27:55 +00:00
|
|
|
*
|
2004-04-18 14:21:17 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
1999-01-28 23:27:55 +00:00
|
|
|
*
|
2001-09-26 00:40:45 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-01-28 23:27:55 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 14:21:17 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-26 00:40:45 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 03:43:54 +00:00
|
|
|
*
|
2001-09-26 00:40:45 +00:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 14:21:17 +00:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-26 00:40:45 +00:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 14:21:17 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-26 00:40:45 +00:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 14:21:17 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-26 00:40:45 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-01-28 23:27:55 +00:00
|
|
|
|
|
|
|
#include "nsString.h"
|
2001-09-29 08:28:41 +00:00
|
|
|
#include "nsReadableUtils.h"
|
1999-01-28 23:27:55 +00:00
|
|
|
#include "pratom.h"
|
|
|
|
#include "prtypes.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsILocale.h"
|
|
|
|
#include "nsLocale.h"
|
|
|
|
#include "nsLocaleCID.h"
|
1999-08-02 03:00:23 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2003-10-30 05:04:45 +00:00
|
|
|
#include "nsVoidArray.h"
|
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsCRT.h"
|
1999-01-28 23:27:55 +00:00
|
|
|
|
2003-10-30 05:04:45 +00:00
|
|
|
#define LOCALE_HASH_SIZE 0xFF
|
1999-01-28 23:27:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* nsILocale */
|
2001-08-17 05:52:48 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsLocale, nsILocale)
|
1999-01-28 23:27:55 +00:00
|
|
|
|
1999-08-02 03:00:23 +00:00
|
|
|
nsLocale::nsLocale(void)
|
2003-10-30 05:04:45 +00:00
|
|
|
: fHashtable(nsnull), fCategoryCount(0)
|
1999-08-02 03:00:23 +00:00
|
|
|
{
|
2003-10-30 05:04:45 +00:00
|
|
|
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");
|
1999-08-02 03:00:23 +00:00
|
|
|
}
|
|
|
|
|
1999-01-28 23:27:55 +00:00
|
|
|
nsLocale::~nsLocale(void)
|
|
|
|
{
|
2003-10-30 05:04:45 +00:00
|
|
|
// enumerate all the entries with a delete function to
|
|
|
|
// safely delete all the keys and values
|
|
|
|
PL_HashTableEnumerateEntries(fHashtable, &nsLocale::Hash_EnumerateDelete,
|
|
|
|
NULL);
|
1999-01-28 23:27:55 +00:00
|
|
|
|
2003-10-30 05:04:45 +00:00
|
|
|
PL_HashTableDestroy(fHashtable);
|
1999-01-28 23:27:55 +00:00
|
|
|
}
|
|
|
|
|
1999-06-23 21:01:08 +00:00
|
|
|
NS_IMETHODIMP
|
2003-10-30 05:04:45 +00:00
|
|
|
nsLocale::GetCategory(const nsAString& category, nsAString& result)
|
1999-06-23 21:01:08 +00:00
|
|
|
{
|
2003-10-30 05:04:45 +00:00
|
|
|
const PRUnichar *value = (const PRUnichar*)
|
|
|
|
PL_HashTableLookup(fHashtable, PromiseFlatString(category).get());
|
1999-07-14 16:33:43 +00:00
|
|
|
|
2003-10-30 05:04:45 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
result.Assign(value);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-07-14 16:33:43 +00:00
|
|
|
|
2003-10-30 05:04:45 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-06-23 21:01:08 +00:00
|
|
|
}
|
|
|
|
|
1999-08-02 03:00:23 +00:00
|
|
|
NS_IMETHODIMP
|
2003-10-30 05:04:45 +00:00
|
|
|
nsLocale::AddCategory(const nsAString &category, const nsAString &value)
|
1999-08-02 03:00:23 +00:00
|
|
|
{
|
2003-10-30 05:04:45 +00:00
|
|
|
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;
|
1999-08-02 03:00:23 +00:00
|
|
|
}
|
|
|
|
|
1999-06-23 21:01:08 +00:00
|
|
|
|
1999-01-28 23:27:55 +00:00
|
|
|
PLHashNumber
|
|
|
|
nsLocale::Hash_HashFunction(const void* key)
|
|
|
|
{
|
2003-10-30 05:04:45 +00:00
|
|
|
const PRUnichar* ptr = (const PRUnichar *) key;
|
|
|
|
PLHashNumber hash;
|
1999-01-28 23:27:55 +00:00
|
|
|
|
2003-10-30 05:04:45 +00:00
|
|
|
hash = (PLHashNumber)0;
|
1999-01-28 23:27:55 +00:00
|
|
|
|
2003-10-30 05:04:45 +00:00
|
|
|
while (*ptr)
|
|
|
|
hash += (PLHashNumber) *ptr++;
|
1999-01-28 23:27:55 +00:00
|
|
|
|
2003-10-30 05:04:45 +00:00
|
|
|
return hash;
|
1999-01-28 23:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PRIntn
|
|
|
|
nsLocale::Hash_CompareNSString(const void* s1, const void* s2)
|
|
|
|
{
|
2003-10-30 05:04:45 +00:00
|
|
|
return !nsCRT::strcmp((const PRUnichar *) s1, (const PRUnichar *) s2);
|
1999-01-28 23:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PRIntn
|
2003-10-30 05:04:45 +00:00
|
|
|
nsLocale::Hash_EnumerateDelete(PLHashEntry *he, PRIntn hashIndex, void *arg)
|
1999-01-28 23:27:55 +00:00
|
|
|
{
|
2003-10-30 05:04:45 +00:00
|
|
|
// delete an entry
|
|
|
|
nsMemory::Free((PRUnichar *)he->key);
|
|
|
|
nsMemory::Free((PRUnichar *)he->value);
|
1999-01-28 23:27:55 +00:00
|
|
|
|
2003-10-30 05:04:45 +00:00
|
|
|
return (HT_ENUMERATE_NEXT | HT_ENUMERATE_REMOVE);
|
1999-03-30 01:48:20 +00:00
|
|
|
}
|
1999-08-02 03:00:23 +00:00
|
|
|
|