nsLocale.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/. */
  5. #include "nsString.h"
  6. #include "nsISupports.h"
  7. #include "nsILocale.h"
  8. #include "nsLocale.h"
  9. #include "nsMemory.h"
  10. #include "nsCRT.h"
  11. #define LOCALE_HASH_SIZE 0xFF
  12. /* nsILocale */
  13. NS_IMPL_ISUPPORTS(nsLocale, nsILocale)
  14. nsLocale::nsLocale(void)
  15. : fHashtable(nullptr), fCategoryCount(0)
  16. {
  17. fHashtable = PL_NewHashTable(LOCALE_HASH_SIZE,&nsLocale::Hash_HashFunction,
  18. &nsLocale::Hash_CompareNSString,
  19. &nsLocale::Hash_CompareNSString,
  20. nullptr, nullptr);
  21. NS_ASSERTION(fHashtable, "nsLocale: failed to allocate PR_Hashtable");
  22. }
  23. nsLocale::~nsLocale(void)
  24. {
  25. // enumerate all the entries with a delete function to
  26. // safely delete all the keys and values
  27. PL_HashTableEnumerateEntries(fHashtable, &nsLocale::Hash_EnumerateDelete,
  28. nullptr);
  29. PL_HashTableDestroy(fHashtable);
  30. }
  31. NS_IMETHODIMP
  32. nsLocale::GetCategory(const nsAString& category, nsAString& result)
  33. {
  34. const char16_t *value = (const char16_t*)
  35. PL_HashTableLookup(fHashtable, PromiseFlatString(category).get());
  36. if (value)
  37. {
  38. result.Assign(value);
  39. return NS_OK;
  40. }
  41. return NS_ERROR_FAILURE;
  42. }
  43. NS_IMETHODIMP
  44. nsLocale::AddCategory(const nsAString &category, const nsAString &value)
  45. {
  46. char16_t* newKey = ToNewUnicode(category);
  47. if (!newKey)
  48. return NS_ERROR_OUT_OF_MEMORY;
  49. char16_t* newValue = ToNewUnicode(value);
  50. if (!newValue) {
  51. free(newKey);
  52. return NS_ERROR_OUT_OF_MEMORY;
  53. }
  54. if (!PL_HashTableAdd(fHashtable, newKey, newValue)) {
  55. free(newKey);
  56. free(newValue);
  57. return NS_ERROR_OUT_OF_MEMORY;
  58. }
  59. return NS_OK;
  60. }
  61. PLHashNumber
  62. nsLocale::Hash_HashFunction(const void* key)
  63. {
  64. const char16_t* ptr = (const char16_t *) key;
  65. PLHashNumber hash;
  66. hash = (PLHashNumber)0;
  67. while (*ptr)
  68. hash += (PLHashNumber) *ptr++;
  69. return hash;
  70. }
  71. int
  72. nsLocale::Hash_CompareNSString(const void* s1, const void* s2)
  73. {
  74. return !nsCRT::strcmp((const char16_t *) s1, (const char16_t *) s2);
  75. }
  76. int
  77. nsLocale::Hash_EnumerateDelete(PLHashEntry *he, int hashIndex, void *arg)
  78. {
  79. // delete an entry
  80. free((char16_t *)he->key);
  81. free((char16_t *)he->value);
  82. return (HT_ENUMERATE_NEXT | HT_ENUMERATE_REMOVE);
  83. }