LCOV - code coverage report
Current view: directory - intl/locale/src - nsLocale.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 41 34 82.9 %
Date: 2012-06-02 Functions: 11 11 100.0 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is mozilla.org code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  * Netscape Communications Corporation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 1998
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *
      24                 :  * Alternatively, the contents of this file may be used under the terms of
      25                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
      26                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      27                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      28                 :  * of those above. If you wish to allow use of your version of this file only
      29                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      30                 :  * use your version of this file under the terms of the MPL, indicate your
      31                 :  * decision by deleting the provisions above and replace them with the notice
      32                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      33                 :  * the provisions above, a recipient may use your version of this file under
      34                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      35                 :  *
      36                 :  * ***** END LICENSE BLOCK ***** */
      37                 : 
      38                 : #include "nsString.h"
      39                 : #include "nsReadableUtils.h"
      40                 : #include "pratom.h"
      41                 : #include "prtypes.h"
      42                 : #include "nsISupports.h"
      43                 : #include "nsILocale.h"
      44                 : #include "nsLocale.h"
      45                 : #include "nsLocaleCID.h"
      46                 : #include "nsCOMPtr.h"
      47                 : #include "nsVoidArray.h"
      48                 : #include "nsMemory.h"
      49                 : #include "nsCRT.h"
      50                 : 
      51                 : #define LOCALE_HASH_SIZE  0xFF
      52                 : 
      53                 : 
      54                 : /* nsILocale */
      55            2384 : NS_IMPL_THREADSAFE_ISUPPORTS1(nsLocale, nsILocale)
      56                 : 
      57              83 : nsLocale::nsLocale(void)
      58              83 : :  fHashtable(nsnull), fCategoryCount(0)
      59                 : {
      60                 :   fHashtable = PL_NewHashTable(LOCALE_HASH_SIZE,&nsLocale::Hash_HashFunction,
      61                 :                                &nsLocale::Hash_CompareNSString,
      62              83 :                                &nsLocale::Hash_CompareNSString, NULL, NULL);
      63              83 :   NS_ASSERTION(fHashtable, "nsLocale: failed to allocate PR_Hashtable");
      64              83 : }
      65                 : 
      66             166 : nsLocale::~nsLocale(void)
      67                 : {
      68                 :   // enumerate all the entries with a delete function to
      69                 :   // safely delete all the keys and values
      70                 :   PL_HashTableEnumerateEntries(fHashtable, &nsLocale::Hash_EnumerateDelete,
      71              83 :                                NULL);
      72                 : 
      73              83 :   PL_HashTableDestroy(fHashtable);
      74             332 : }
      75                 : 
      76                 : NS_IMETHODIMP
      77             113 : nsLocale::GetCategory(const nsAString& category, nsAString& result)
      78                 : {
      79                 :   const PRUnichar *value = (const PRUnichar*) 
      80             113 :     PL_HashTableLookup(fHashtable, PromiseFlatString(category).get());
      81                 : 
      82             113 :   if (value)
      83                 :   {
      84             113 :     result.Assign(value);
      85             113 :     return NS_OK;
      86                 :   }
      87                 : 
      88               0 :   return NS_ERROR_FAILURE;
      89                 : }
      90                 : 
      91                 : NS_IMETHODIMP
      92             996 : nsLocale::AddCategory(const nsAString &category, const nsAString &value)
      93                 : {
      94             996 :   PRUnichar* newKey = ToNewUnicode(category);
      95             996 :   if (!newKey)
      96               0 :     return NS_ERROR_OUT_OF_MEMORY;
      97                 : 
      98             996 :   PRUnichar* newValue = ToNewUnicode(value);
      99             996 :   if (!newValue) {
     100               0 :     nsMemory::Free(newKey);
     101               0 :     return NS_ERROR_OUT_OF_MEMORY;
     102                 :   }
     103                 : 
     104             996 :   if (!PL_HashTableAdd(fHashtable, newKey, newValue)) {
     105               0 :     nsMemory::Free(newKey);
     106               0 :     nsMemory::Free(newValue);
     107               0 :     return NS_ERROR_OUT_OF_MEMORY;
     108                 :   }
     109                 : 
     110             996 :   return NS_OK;
     111                 : }
     112                 : 
     113                 : 
     114                 : PLHashNumber
     115            1109 : nsLocale::Hash_HashFunction(const void* key)
     116                 : {
     117            1109 :   const PRUnichar* ptr = (const PRUnichar *) key;
     118                 :   PLHashNumber hash;
     119                 : 
     120            1109 :   hash = (PLHashNumber)0;
     121                 : 
     122           25639 :   while (*ptr)
     123           23421 :     hash += (PLHashNumber) *ptr++;
     124                 : 
     125            1109 :   return hash;
     126                 : }
     127                 : 
     128                 : 
     129                 : PRIntn
     130             113 : nsLocale::Hash_CompareNSString(const void* s1, const void* s2)
     131                 : {
     132             113 :   return !nsCRT::strcmp((const PRUnichar *) s1, (const PRUnichar *) s2);
     133                 : }
     134                 : 
     135                 : 
     136                 : PRIntn
     137             996 : nsLocale::Hash_EnumerateDelete(PLHashEntry *he, PRIntn hashIndex, void *arg)
     138                 : {
     139                 :   // delete an entry
     140             996 :   nsMemory::Free((PRUnichar *)he->key);
     141             996 :   nsMemory::Free((PRUnichar *)he->value);
     142                 : 
     143             996 :   return (HT_ENUMERATE_NEXT | HT_ENUMERATE_REMOVE);
     144                 : }
     145                 : 

Generated by: LCOV version 1.7