LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sal/rtl - locale.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 107 139 77.0 %
Date: 2013-07-09 Functions: 16 17 94.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "rtl/locale.h"
      21             : 
      22             : #include "osl/diagnose.h"
      23             : #include "rtl/alloc.h"
      24             : 
      25             : #include "internal/rtllifecycle.h"
      26             : 
      27             : static sal_Int32 RTL_HASHTABLE_SIZE[] =
      28             : {
      29             :     7, 31, 127, 251, 509, 1021, 2039, 4093
      30             : };
      31             : 
      32             : struct RTL_HASHENTRY
      33             : {
      34             :     rtl_Locale* Entry;
      35             :     RTL_HASHENTRY* Next;
      36             : };
      37             : 
      38             : struct RTL_HASHTABLE
      39             : {
      40             :     sal_Int8        iSize;
      41             :     sal_Int32       Size;
      42             :     sal_Int32       Elements;
      43             :     RTL_HASHENTRY** Table;
      44             : };
      45             : 
      46             : static RTL_HASHTABLE* g_pLocaleTable = NULL;
      47             : 
      48             : static rtl_Locale* g_pDefaultLocale = NULL;
      49             : 
      50             : /*************************************************************************
      51             :  */
      52        1787 : extern "C" void rtl_hashentry_destroy(RTL_HASHENTRY* entry)
      53             : {
      54        1787 :     rtl_uString_release(entry->Entry->Language);
      55        1787 :     rtl_uString_release(entry->Entry->Country);
      56        1787 :     rtl_uString_release(entry->Entry->Variant);
      57        1787 :     if (entry->Next)
      58           0 :         rtl_hashentry_destroy(entry->Next);
      59             : 
      60        1787 :     rtl_freeMemory(entry->Entry);
      61        1787 :     rtl_freeMemory(entry);
      62        1787 : }
      63             : 
      64        1785 : extern "C" void rtl_hashtable_destroy(RTL_HASHTABLE* table)
      65             : {
      66        1785 :     sal_Int32 size = 0;
      67             : 
      68        1785 :     if (!table)
      69        1785 :         return;
      70             : 
      71        1785 :     size = table->Size;
      72             : 
      73       58905 :     while (size)
      74             :     {
      75       55335 :         if (table->Table[size - 1])
      76        1787 :             rtl_hashentry_destroy(table->Table[size - 1]);
      77       55335 :         size--;
      78             :     }
      79             : 
      80        1785 :     rtl_freeMemory(table->Table);
      81        1785 :     rtl_freeMemory(table);
      82             : }
      83             : 
      84        1785 : extern "C" void rtl_hashtable_init(RTL_HASHTABLE** table, sal_Int8 sizeIndex)
      85             : {
      86        1785 :     sal_Int32 nSize = RTL_HASHTABLE_SIZE[sizeIndex];
      87             : 
      88        1785 :     if (*table)
      89           0 :         rtl_hashtable_destroy(*table);
      90             : 
      91        1785 :     *table = (RTL_HASHTABLE*)rtl_allocateMemory( sizeof(RTL_HASHTABLE) );
      92             : 
      93        1785 :     (*table)->iSize = sizeIndex;
      94        1785 :     (*table)->Size = nSize;
      95        1785 :     (*table)->Elements = 0;
      96        1785 :     (*table)->Table = (RTL_HASHENTRY**)rtl_allocateMemory( (*table)->Size * sizeof(RTL_HASHENTRY*) );
      97             : 
      98       58905 :     while (nSize)
      99             :     {
     100       55335 :         (*table)->Table[nSize - 1] = NULL;
     101       55335 :         nSize--;
     102             :     }
     103        1785 : }
     104             : 
     105        3590 : extern "C" sal_Int32 rtl_hashfunc(RTL_HASHTABLE* table, sal_Int32 key)
     106             : {
     107        3590 :     return ( (sal_uInt32) key % table->Size);
     108             : }
     109             : 
     110             : extern "C" sal_Bool rtl_hashtable_grow(RTL_HASHTABLE** table);
     111             : 
     112        1787 : extern "C" rtl_Locale* rtl_hashtable_add(RTL_HASHTABLE** table, rtl_Locale* value)
     113             : {
     114        1787 :     sal_Int32 key = 0;
     115             : 
     116        1787 :     if (!(*table))
     117           0 :         return NULL;
     118             : 
     119        1787 :     if ((*table)->Elements > ((*table)->Size / 2))
     120           0 :         rtl_hashtable_grow(table);
     121             : 
     122        1787 :     key = rtl_hashfunc(*table, value->HashCode);
     123             : 
     124        1787 :     RTL_HASHENTRY **pEntry = &(*table)->Table[key];
     125        3574 :     while (*pEntry)
     126             :     {
     127           0 :         if (value->HashCode == (*pEntry)->Entry->HashCode)
     128           0 :             return (*pEntry)->Entry;
     129           0 :         pEntry = &(*pEntry)->Next;
     130             :     }
     131             : 
     132        1787 :     RTL_HASHENTRY *newEntry = (RTL_HASHENTRY*)rtl_allocateMemory( sizeof(RTL_HASHENTRY) );
     133        1787 :     newEntry->Entry = value;
     134        1787 :     newEntry->Next = NULL;
     135        1787 :     *pEntry = newEntry;
     136        1787 :     (*table)->Elements++;
     137        1787 :     return NULL;
     138             : }
     139             : 
     140           0 : sal_Bool rtl_hashtable_grow(RTL_HASHTABLE** table)
     141             : {
     142           0 :     RTL_HASHTABLE* pNewTable = NULL;
     143           0 :     sal_Int32 i = 0;
     144             : 
     145           0 :     rtl_hashtable_init(&pNewTable, (sal_Int8)((*table)->iSize + 1));
     146             : 
     147           0 :     while (i < (*table)->Size)
     148             :     {
     149           0 :         if ((*table)->Table[i])
     150             :         {
     151             :             RTL_HASHENTRY *pNext;
     152           0 :             RTL_HASHENTRY *pEntry = (*table)->Table[i];
     153             : 
     154           0 :             rtl_hashtable_add(&pNewTable, pEntry->Entry);
     155             : 
     156           0 :             while (pEntry->Next)
     157             :             {
     158           0 :                 rtl_hashtable_add(&pNewTable, pEntry->Next->Entry);
     159           0 :                 pNext = pEntry->Next;
     160           0 :                 rtl_freeMemory(pEntry);
     161           0 :                 pEntry = pNext;
     162             :             }
     163             : 
     164           0 :             rtl_freeMemory(pEntry);
     165             :         }
     166           0 :         i++;
     167             :     }
     168             : 
     169           0 :     rtl_freeMemory((*table)->Table);
     170           0 :     rtl_freeMemory((*table));
     171           0 :     (*table) = pNewTable;
     172             : 
     173           0 :     return sal_True;
     174             : }
     175             : 
     176        1803 : extern "C" sal_Bool rtl_hashtable_find(RTL_HASHTABLE * table, sal_Int32 key, sal_Int32 hashCode, rtl_Locale** pValue)
     177             : {
     178        1803 :     if (!table)
     179           0 :         return sal_False;
     180             : 
     181        1803 :     if (table->Table[key])
     182             :     {
     183          16 :         RTL_HASHENTRY *pEntry = table->Table[key];
     184             : 
     185          32 :         while (pEntry && hashCode != pEntry->Entry->HashCode)
     186           0 :             pEntry = pEntry->Next;
     187             : 
     188          16 :         if (pEntry)
     189          16 :             *pValue = pEntry->Entry;
     190             :         else
     191           0 :             return sal_False;
     192             :     } else
     193        1787 :         return sal_False;
     194             : 
     195          16 :     return sal_True;
     196             : }
     197             : 
     198             : /*************************************************************************
     199             :  *  rtl_locale_init
     200             :  */
     201        1785 : void rtl_locale_init()
     202             : {
     203             :   OSL_ASSERT(g_pLocaleTable == 0);
     204        1785 :   rtl_hashtable_init(&g_pLocaleTable, 1);
     205        1785 : }
     206             : 
     207             : /*************************************************************************
     208             :  *  rtl_locale_fini
     209             :  */
     210        1785 : void rtl_locale_fini()
     211             : {
     212        1785 :   if (g_pLocaleTable != 0)
     213             :   {
     214        1785 :     rtl_hashtable_destroy (g_pLocaleTable);
     215        1785 :     g_pLocaleTable = 0;
     216             :   }
     217        1785 : }
     218             : 
     219             : /*************************************************************************
     220             :  *  rtl_locale_register
     221             :  */
     222        1803 : rtl_Locale * SAL_CALL rtl_locale_register( const sal_Unicode * language, const sal_Unicode * country, const sal_Unicode * variant )
     223             : {
     224        1803 :     sal_Unicode c = 0;
     225        1803 :     rtl_uString* sLanguage = NULL;
     226        1803 :     rtl_uString* sCountry = NULL;
     227        1803 :     rtl_uString* sVariant = NULL;
     228        1803 :     rtl_Locale *newLocale = NULL;
     229        1803 :     sal_Int32 hashCode = -1;
     230        1803 :     sal_Int32 key = 0;
     231             : 
     232        1803 :     if ( !country )
     233           0 :         country = &c;
     234        1803 :     if ( !variant )
     235           0 :         variant = &c;
     236             : 
     237        1803 :     ensureLocaleSingleton();
     238        1803 :     if (!g_pLocaleTable)
     239           0 :       return NULL;
     240             : 
     241        1803 :     hashCode = rtl_ustr_hashCode(language) ^ rtl_ustr_hashCode(country) ^ rtl_ustr_hashCode(variant);
     242        1803 :     key = rtl_hashfunc(g_pLocaleTable, hashCode);
     243             : 
     244        1803 :     if (rtl_hashtable_find(g_pLocaleTable, key, hashCode, &newLocale))
     245          16 :         return newLocale;
     246             : 
     247        1787 :     rtl_uString_newFromStr(&sLanguage, language);
     248        1787 :     rtl_uString_newFromStr(&sCountry, country);
     249        1787 :     rtl_uString_newFromStr(&sVariant, variant);
     250             : 
     251        1787 :     newLocale = (rtl_Locale*)rtl_allocateMemory( sizeof(rtl_Locale) );
     252             : 
     253        1787 :     newLocale->Language = sLanguage;
     254        1787 :     newLocale->Country = sCountry;
     255        1787 :     newLocale->Variant = sVariant;
     256        1787 :     newLocale->HashCode = hashCode;
     257             : 
     258        1787 :     rtl_hashtable_add(&g_pLocaleTable, newLocale);
     259             : 
     260        1787 :     return newLocale;
     261             : }
     262             : 
     263             : /*************************************************************************
     264             :  *  rtl_locale_getDefault
     265             :  */
     266          10 : rtl_Locale * SAL_CALL rtl_locale_getDefault()
     267             : {
     268          10 :     return g_pDefaultLocale;
     269             : }
     270             : 
     271             : /*************************************************************************
     272             :  *  rtl_locale_setDefault
     273             :  */
     274          14 : void SAL_CALL rtl_locale_setDefault( const sal_Unicode * language, const sal_Unicode * country, const sal_Unicode * variant )
     275             : {
     276          14 :     g_pDefaultLocale = rtl_locale_register(language, country, variant);
     277          14 : }
     278             : 
     279             : /*************************************************************************
     280             :  *  rtl_locale_getLanguage
     281             :  */
     282           1 : rtl_uString * SAL_CALL rtl_locale_getLanguage( rtl_Locale * This )
     283             : {
     284           1 :     rtl_uString_acquire(This->Language);
     285           1 :     return This->Language;
     286             : }
     287             : 
     288             : /*************************************************************************
     289             :  *  rtl_locale_getCountry
     290             :  */
     291           1 : rtl_uString * SAL_CALL rtl_locale_getCountry( rtl_Locale * This )
     292             : {
     293           1 :     rtl_uString_acquire(This->Country);
     294           1 :     return This->Country;
     295             : }
     296             : 
     297             : /*************************************************************************
     298             :  *  rtl_locale_getVariant
     299             :  */
     300           1 : rtl_uString * SAL_CALL rtl_locale_getVariant( rtl_Locale * This )
     301             : {
     302           1 :     rtl_uString_acquire(This->Variant);
     303           1 :     return This->Variant;
     304             : }
     305             : 
     306             : /*************************************************************************
     307             :  *  rtl_locale_hashCode
     308             :  */
     309           1 : sal_Int32 SAL_CALL rtl_locale_hashCode( rtl_Locale * This )
     310             : {
     311           1 :     return This->HashCode;
     312             : }
     313             : 
     314             : /*************************************************************************
     315             :  *  rtl_locale_equals
     316             :  */
     317           1 : sal_Int32 SAL_CALL rtl_locale_equals( rtl_Locale * This, rtl_Locale * obj  )
     318             : {
     319           1 :     return This == obj;
     320             : }
     321             : 
     322             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10