LCOV - code coverage report
Current view: top level - sal/rtl - locale.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 83 139 59.7 %
Date: 2014-04-14 Functions: 9 17 52.9 %
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 const 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       85011 : extern "C" void rtl_hashentry_destroy(RTL_HASHENTRY* entry)
      53             : {
      54       85011 :     rtl_uString_release(entry->Entry->Language);
      55       85011 :     rtl_uString_release(entry->Entry->Country);
      56       85011 :     rtl_uString_release(entry->Entry->Variant);
      57       85011 :     if (entry->Next)
      58           0 :         rtl_hashentry_destroy(entry->Next);
      59             : 
      60       85011 :     rtl_freeMemory(entry->Entry);
      61       85011 :     rtl_freeMemory(entry);
      62       85011 : }
      63             : 
      64       85011 : extern "C" void rtl_hashtable_destroy(RTL_HASHTABLE* table)
      65             : {
      66       85011 :     sal_Int32 size = 0;
      67             : 
      68       85011 :     if (!table)
      69       85011 :         return;
      70             : 
      71       85011 :     size = table->Size;
      72             : 
      73     2805363 :     while (size)
      74             :     {
      75     2635341 :         if (table->Table[size - 1])
      76       85011 :             rtl_hashentry_destroy(table->Table[size - 1]);
      77     2635341 :         size--;
      78             :     }
      79             : 
      80       85011 :     rtl_freeMemory(table->Table);
      81       85011 :     rtl_freeMemory(table);
      82             : }
      83             : 
      84       85011 : extern "C" void rtl_hashtable_init(RTL_HASHTABLE** table, sal_Int8 sizeIndex)
      85             : {
      86       85011 :     sal_Int32 nSize = RTL_HASHTABLE_SIZE[sizeIndex];
      87             : 
      88       85011 :     if (*table)
      89           0 :         rtl_hashtable_destroy(*table);
      90             : 
      91       85011 :     *table = (RTL_HASHTABLE*)rtl_allocateMemory( sizeof(RTL_HASHTABLE) );
      92             : 
      93       85011 :     (*table)->iSize = sizeIndex;
      94       85011 :     (*table)->Size = nSize;
      95       85011 :     (*table)->Elements = 0;
      96       85011 :     (*table)->Table = (RTL_HASHENTRY**)rtl_allocateMemory( (*table)->Size * sizeof(RTL_HASHENTRY*) );
      97             : 
      98     2805363 :     while (nSize)
      99             :     {
     100     2635341 :         (*table)->Table[nSize - 1] = NULL;
     101     2635341 :         nSize--;
     102             :     }
     103       85011 : }
     104             : 
     105      170022 : extern "C" sal_Int32 rtl_hashfunc(RTL_HASHTABLE* table, sal_Int32 key)
     106             : {
     107      170022 :     return ( (sal_uInt32) key % table->Size);
     108             : }
     109             : 
     110             : extern "C" sal_Bool rtl_hashtable_grow(RTL_HASHTABLE** table);
     111             : 
     112       85011 : extern "C" rtl_Locale* rtl_hashtable_add(RTL_HASHTABLE** table, rtl_Locale* value)
     113             : {
     114       85011 :     sal_Int32 key = 0;
     115             : 
     116       85011 :     if (!(*table))
     117           0 :         return NULL;
     118             : 
     119       85011 :     if ((*table)->Elements > ((*table)->Size / 2))
     120           0 :         rtl_hashtable_grow(table);
     121             : 
     122       85011 :     key = rtl_hashfunc(*table, value->HashCode);
     123             : 
     124       85011 :     RTL_HASHENTRY **pEntry = &(*table)->Table[key];
     125      170022 :     while (*pEntry)
     126             :     {
     127           0 :         if (value->HashCode == (*pEntry)->Entry->HashCode)
     128           0 :             return (*pEntry)->Entry;
     129           0 :         pEntry = &(*pEntry)->Next;
     130             :     }
     131             : 
     132       85011 :     RTL_HASHENTRY *newEntry = (RTL_HASHENTRY*)rtl_allocateMemory( sizeof(RTL_HASHENTRY) );
     133       85011 :     newEntry->Entry = value;
     134       85011 :     newEntry->Next = NULL;
     135       85011 :     *pEntry = newEntry;
     136       85011 :     (*table)->Elements++;
     137       85011 :     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       85011 : extern "C" sal_Bool rtl_hashtable_find(RTL_HASHTABLE * table, sal_Int32 key, sal_Int32 hashCode, rtl_Locale** pValue)
     177             : {
     178       85011 :     if (!table)
     179           0 :         return sal_False;
     180             : 
     181       85011 :     if (table->Table[key])
     182             :     {
     183           0 :         RTL_HASHENTRY *pEntry = table->Table[key];
     184             : 
     185           0 :         while (pEntry && hashCode != pEntry->Entry->HashCode)
     186           0 :             pEntry = pEntry->Next;
     187             : 
     188           0 :         if (pEntry)
     189           0 :             *pValue = pEntry->Entry;
     190             :         else
     191           0 :             return sal_False;
     192             :     } else
     193       85011 :         return sal_False;
     194             : 
     195           0 :     return sal_True;
     196             : }
     197             : 
     198             : /*************************************************************************
     199             :  *  rtl_locale_init
     200             :  */
     201       85011 : void rtl_locale_init()
     202             : {
     203             :   OSL_ASSERT(g_pLocaleTable == 0);
     204       85011 :   rtl_hashtable_init(&g_pLocaleTable, 1);
     205       85011 : }
     206             : 
     207             : /*************************************************************************
     208             :  *  rtl_locale_fini
     209             :  */
     210       85011 : void rtl_locale_fini()
     211             : {
     212       85011 :   if (g_pLocaleTable != 0)
     213             :   {
     214       85011 :     rtl_hashtable_destroy (g_pLocaleTable);
     215       85011 :     g_pLocaleTable = 0;
     216             :   }
     217       85011 : }
     218             : 
     219             : /*************************************************************************
     220             :  *  rtl_locale_register
     221             :  */
     222       85011 : rtl_Locale * SAL_CALL rtl_locale_register( const sal_Unicode * language, const sal_Unicode * country, const sal_Unicode * variant )
     223             : {
     224       85011 :     sal_Unicode c = 0;
     225       85011 :     rtl_uString* sLanguage = NULL;
     226       85011 :     rtl_uString* sCountry = NULL;
     227       85011 :     rtl_uString* sVariant = NULL;
     228       85011 :     rtl_Locale *newLocale = NULL;
     229       85011 :     sal_Int32 hashCode = -1;
     230       85011 :     sal_Int32 key = 0;
     231             : 
     232       85011 :     if ( !country )
     233           0 :         country = &c;
     234       85011 :     if ( !variant )
     235           0 :         variant = &c;
     236             : 
     237       85011 :     ensureLocaleSingleton();
     238       85011 :     if (!g_pLocaleTable)
     239           0 :       return NULL;
     240             : 
     241       85011 :     hashCode = rtl_ustr_hashCode(language) ^ rtl_ustr_hashCode(country) ^ rtl_ustr_hashCode(variant);
     242       85011 :     key = rtl_hashfunc(g_pLocaleTable, hashCode);
     243             : 
     244       85011 :     if (rtl_hashtable_find(g_pLocaleTable, key, hashCode, &newLocale))
     245           0 :         return newLocale;
     246             : 
     247       85011 :     rtl_uString_newFromStr(&sLanguage, language);
     248       85011 :     rtl_uString_newFromStr(&sCountry, country);
     249       85011 :     rtl_uString_newFromStr(&sVariant, variant);
     250             : 
     251       85011 :     newLocale = (rtl_Locale*)rtl_allocateMemory( sizeof(rtl_Locale) );
     252             : 
     253       85011 :     newLocale->Language = sLanguage;
     254       85011 :     newLocale->Country = sCountry;
     255       85011 :     newLocale->Variant = sVariant;
     256       85011 :     newLocale->HashCode = hashCode;
     257             : 
     258       85011 :     rtl_hashtable_add(&g_pLocaleTable, newLocale);
     259             : 
     260       85011 :     return newLocale;
     261             : }
     262             : 
     263             : /*************************************************************************
     264             :  *  rtl_locale_getDefault
     265             :  */
     266           0 : rtl_Locale * SAL_CALL rtl_locale_getDefault()
     267             : {
     268           0 :     return g_pDefaultLocale;
     269             : }
     270             : 
     271             : /*************************************************************************
     272             :  *  rtl_locale_setDefault
     273             :  */
     274           0 : void SAL_CALL rtl_locale_setDefault( const sal_Unicode * language, const sal_Unicode * country, const sal_Unicode * variant )
     275             : {
     276           0 :     g_pDefaultLocale = rtl_locale_register(language, country, variant);
     277           0 : }
     278             : 
     279             : /*************************************************************************
     280             :  *  rtl_locale_getLanguage
     281             :  */
     282           0 : rtl_uString * SAL_CALL rtl_locale_getLanguage( rtl_Locale * This )
     283             : {
     284           0 :     rtl_uString_acquire(This->Language);
     285           0 :     return This->Language;
     286             : }
     287             : 
     288             : /*************************************************************************
     289             :  *  rtl_locale_getCountry
     290             :  */
     291           0 : rtl_uString * SAL_CALL rtl_locale_getCountry( rtl_Locale * This )
     292             : {
     293           0 :     rtl_uString_acquire(This->Country);
     294           0 :     return This->Country;
     295             : }
     296             : 
     297             : /*************************************************************************
     298             :  *  rtl_locale_getVariant
     299             :  */
     300           0 : rtl_uString * SAL_CALL rtl_locale_getVariant( rtl_Locale * This )
     301             : {
     302           0 :     rtl_uString_acquire(This->Variant);
     303           0 :     return This->Variant;
     304             : }
     305             : 
     306             : /*************************************************************************
     307             :  *  rtl_locale_hashCode
     308             :  */
     309           0 : sal_Int32 SAL_CALL rtl_locale_hashCode( rtl_Locale * This )
     310             : {
     311           0 :     return This->HashCode;
     312             : }
     313             : 
     314             : /*************************************************************************
     315             :  *  rtl_locale_equals
     316             :  */
     317           0 : sal_Int32 SAL_CALL rtl_locale_equals( rtl_Locale * This, rtl_Locale * obj  )
     318             : {
     319           0 :     return This == obj;
     320             : }
     321             : 
     322             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10