xref: /AOO41X/main/sal/rtl/source/locale.c (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "rtl/locale.h"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include "osl/diagnose.h"
31*cdf0e10cSrcweir #include "rtl/alloc.h"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "internal/once.h"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir static sal_Int32 RTL_HASHTABLE_SIZE[] =
36*cdf0e10cSrcweir {
37*cdf0e10cSrcweir     7, 31, 127, 251, 509, 1021, 2039, 4093
38*cdf0e10cSrcweir };
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir typedef struct rtl_hashentry RTL_HASHENTRY;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir struct rtl_hashentry
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir     rtl_Locale* Entry;
45*cdf0e10cSrcweir     RTL_HASHENTRY* Next;
46*cdf0e10cSrcweir };
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir typedef struct rtl_hashtable
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir     sal_Int8        iSize;
51*cdf0e10cSrcweir     sal_Int32       Size;
52*cdf0e10cSrcweir     sal_Int32       Elements;
53*cdf0e10cSrcweir     RTL_HASHENTRY** Table;
54*cdf0e10cSrcweir } RTL_HASHTABLE;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir static RTL_HASHTABLE* g_pLocaleTable = NULL;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir static rtl_Locale* g_pDefaultLocale = NULL;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir static int rtl_locale_init (void);
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir /*************************************************************************
63*cdf0e10cSrcweir  */
64*cdf0e10cSrcweir void rtl_hashentry_destroy(RTL_HASHENTRY* entry)
65*cdf0e10cSrcweir {
66*cdf0e10cSrcweir     rtl_uString_release(entry->Entry->Language);
67*cdf0e10cSrcweir     rtl_uString_release(entry->Entry->Country);
68*cdf0e10cSrcweir     rtl_uString_release(entry->Entry->Variant);
69*cdf0e10cSrcweir     if (entry->Next)
70*cdf0e10cSrcweir         rtl_hashentry_destroy(entry->Next);
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     rtl_freeMemory(entry->Entry);
73*cdf0e10cSrcweir     rtl_freeMemory(entry);
74*cdf0e10cSrcweir }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir void rtl_hashtable_destroy(RTL_HASHTABLE* table)
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir     sal_Int32 size = 0;
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     if (!table)
81*cdf0e10cSrcweir         return;
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     size = table->Size;
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     while (size)
86*cdf0e10cSrcweir     {
87*cdf0e10cSrcweir         if (table->Table[size - 1])
88*cdf0e10cSrcweir             rtl_hashentry_destroy(table->Table[size - 1]);
89*cdf0e10cSrcweir         size--;
90*cdf0e10cSrcweir     }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     rtl_freeMemory(table->Table);
93*cdf0e10cSrcweir     rtl_freeMemory(table);
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir void rtl_hashtable_init(RTL_HASHTABLE** table, sal_Int8 sizeIndex)
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir     sal_Int32 nSize = RTL_HASHTABLE_SIZE[sizeIndex];
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir     if (*table)
101*cdf0e10cSrcweir         rtl_hashtable_destroy(*table);
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir     *table = (RTL_HASHTABLE*)rtl_allocateMemory( sizeof(RTL_HASHTABLE) );
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     (*table)->iSize = sizeIndex;
106*cdf0e10cSrcweir     (*table)->Size = nSize;
107*cdf0e10cSrcweir     (*table)->Elements = 0;
108*cdf0e10cSrcweir     (*table)->Table = (RTL_HASHENTRY**)rtl_allocateMemory( (*table)->Size * sizeof(RTL_HASHENTRY*) );
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir     while (nSize)
111*cdf0e10cSrcweir     {
112*cdf0e10cSrcweir         (*table)->Table[nSize - 1] = NULL;
113*cdf0e10cSrcweir         nSize--;
114*cdf0e10cSrcweir     }
115*cdf0e10cSrcweir }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir sal_Int32 rtl_hashfunc(RTL_HASHTABLE* table, sal_Int32 key)
118*cdf0e10cSrcweir {
119*cdf0e10cSrcweir     return ( (sal_uInt32) key % table->Size);
120*cdf0e10cSrcweir }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir sal_Bool rtl_hashtable_grow(RTL_HASHTABLE** table);
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir rtl_Locale* rtl_hashtable_add(RTL_HASHTABLE** table, rtl_Locale* value)
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir     sal_Int32 key = 0;
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir     if (!(*table))
129*cdf0e10cSrcweir         return NULL;
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir     if ((*table)->Elements > ((*table)->Size / 2))
132*cdf0e10cSrcweir         rtl_hashtable_grow(table);
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir     key = rtl_hashfunc(*table, value->HashCode);
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir     if (!(*table)->Table[key])
137*cdf0e10cSrcweir     {
138*cdf0e10cSrcweir         RTL_HASHENTRY *newEntry = (RTL_HASHENTRY*)rtl_allocateMemory( sizeof(RTL_HASHENTRY) );
139*cdf0e10cSrcweir         newEntry->Entry = value;
140*cdf0e10cSrcweir         newEntry->Next = NULL;
141*cdf0e10cSrcweir         (*table)->Table[key] = newEntry;
142*cdf0e10cSrcweir         (*table)->Elements++;
143*cdf0e10cSrcweir         return NULL;
144*cdf0e10cSrcweir     } else
145*cdf0e10cSrcweir     {
146*cdf0e10cSrcweir         RTL_HASHENTRY *pEntry = (*table)->Table[key];
147*cdf0e10cSrcweir         RTL_HASHENTRY *newEntry = NULL;
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir         while (pEntry)
150*cdf0e10cSrcweir         {
151*cdf0e10cSrcweir             if (value->HashCode == pEntry->Entry->HashCode)
152*cdf0e10cSrcweir                 return pEntry->Entry;
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir             if (!pEntry->Next)
155*cdf0e10cSrcweir                 break;
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir             pEntry = pEntry->Next;
158*cdf0e10cSrcweir         }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir         newEntry = (RTL_HASHENTRY*)rtl_allocateMemory( sizeof(RTL_HASHENTRY) );
161*cdf0e10cSrcweir         newEntry->Entry = value;
162*cdf0e10cSrcweir         newEntry->Next = NULL;
163*cdf0e10cSrcweir         pEntry->Next = newEntry;
164*cdf0e10cSrcweir         (*table)->Elements++;
165*cdf0e10cSrcweir         return NULL;
166*cdf0e10cSrcweir     }
167*cdf0e10cSrcweir }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir sal_Bool rtl_hashtable_grow(RTL_HASHTABLE** table)
170*cdf0e10cSrcweir {
171*cdf0e10cSrcweir     RTL_HASHTABLE* pNewTable = NULL;
172*cdf0e10cSrcweir     sal_Int32 i = 0;
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir     rtl_hashtable_init(&pNewTable, (sal_Int8)((*table)->iSize + 1));
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     while (i < (*table)->Size)
177*cdf0e10cSrcweir     {
178*cdf0e10cSrcweir         if ((*table)->Table[i])
179*cdf0e10cSrcweir         {
180*cdf0e10cSrcweir             RTL_HASHENTRY *pNext;
181*cdf0e10cSrcweir             RTL_HASHENTRY *pEntry = (*table)->Table[i];
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir             rtl_hashtable_add(&pNewTable, pEntry->Entry);
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir             while (pEntry->Next)
186*cdf0e10cSrcweir             {
187*cdf0e10cSrcweir                 rtl_hashtable_add(&pNewTable, pEntry->Next->Entry);
188*cdf0e10cSrcweir                 pNext = pEntry->Next;
189*cdf0e10cSrcweir                 rtl_freeMemory(pEntry);
190*cdf0e10cSrcweir                 pEntry = pNext;
191*cdf0e10cSrcweir             }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir             rtl_freeMemory(pEntry);
194*cdf0e10cSrcweir         }
195*cdf0e10cSrcweir         i++;
196*cdf0e10cSrcweir     }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir     rtl_freeMemory((*table)->Table);
199*cdf0e10cSrcweir     rtl_freeMemory((*table));
200*cdf0e10cSrcweir     (*table) = pNewTable;
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir     return sal_True;
203*cdf0e10cSrcweir }
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir sal_Bool rtl_hashtable_find(RTL_HASHTABLE * table, sal_Int32 key, sal_Int32 hashCode, rtl_Locale** pValue)
206*cdf0e10cSrcweir {
207*cdf0e10cSrcweir     if (!table)
208*cdf0e10cSrcweir         return sal_False;
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir     if (table->Table[key])
211*cdf0e10cSrcweir     {
212*cdf0e10cSrcweir         RTL_HASHENTRY *pEntry = table->Table[key];
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir         while (pEntry && hashCode != pEntry->Entry->HashCode)
215*cdf0e10cSrcweir             pEntry = pEntry->Next;
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir         if (pEntry)
218*cdf0e10cSrcweir             *pValue = pEntry->Entry;
219*cdf0e10cSrcweir         else
220*cdf0e10cSrcweir             return sal_False;
221*cdf0e10cSrcweir     } else
222*cdf0e10cSrcweir         return sal_False;
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir     return sal_True;
225*cdf0e10cSrcweir }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir /*************************************************************************
228*cdf0e10cSrcweir  *  rtl_locale_init
229*cdf0e10cSrcweir  */
230*cdf0e10cSrcweir static void rtl_locale_once_init (void)
231*cdf0e10cSrcweir {
232*cdf0e10cSrcweir   OSL_ASSERT(g_pLocaleTable == 0);
233*cdf0e10cSrcweir   rtl_hashtable_init(&g_pLocaleTable, 1);
234*cdf0e10cSrcweir }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir static int rtl_locale_init (void)
237*cdf0e10cSrcweir {
238*cdf0e10cSrcweir   static sal_once_type g_once = SAL_ONCE_INIT;
239*cdf0e10cSrcweir   SAL_ONCE(&g_once, rtl_locale_once_init);
240*cdf0e10cSrcweir   return (g_pLocaleTable != 0);
241*cdf0e10cSrcweir }
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir /*************************************************************************
244*cdf0e10cSrcweir  *  rtl_locale_fini
245*cdf0e10cSrcweir  */
246*cdf0e10cSrcweir #if defined(__GNUC__)
247*cdf0e10cSrcweir static void rtl_locale_fini (void) __attribute__((destructor));
248*cdf0e10cSrcweir #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
249*cdf0e10cSrcweir #pragma fini(rtl_locale_fini)
250*cdf0e10cSrcweir static void rtl_locale_fini (void);
251*cdf0e10cSrcweir #endif /* __GNUC__ || __SUNPRO_C */
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir void rtl_locale_fini (void)
254*cdf0e10cSrcweir {
255*cdf0e10cSrcweir   if (g_pLocaleTable != 0)
256*cdf0e10cSrcweir   {
257*cdf0e10cSrcweir     rtl_hashtable_destroy (g_pLocaleTable);
258*cdf0e10cSrcweir     g_pLocaleTable = 0;
259*cdf0e10cSrcweir   }
260*cdf0e10cSrcweir }
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir /*************************************************************************
263*cdf0e10cSrcweir  *  rtl_locale_register
264*cdf0e10cSrcweir  */
265*cdf0e10cSrcweir rtl_Locale * SAL_CALL rtl_locale_register( const sal_Unicode * language, const sal_Unicode * country, const sal_Unicode * variant )
266*cdf0e10cSrcweir {
267*cdf0e10cSrcweir     sal_Unicode c = 0;
268*cdf0e10cSrcweir     rtl_uString* sLanguage = NULL;
269*cdf0e10cSrcweir     rtl_uString* sCountry = NULL;
270*cdf0e10cSrcweir     rtl_uString* sVariant = NULL;
271*cdf0e10cSrcweir     rtl_Locale *newLocale = NULL;
272*cdf0e10cSrcweir     sal_Int32 hashCode = -1;
273*cdf0e10cSrcweir     sal_Int32 key = 0;
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir     if ( !country )
276*cdf0e10cSrcweir         country = &c;
277*cdf0e10cSrcweir     if ( !variant )
278*cdf0e10cSrcweir         variant = &c;
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir     if (!rtl_locale_init())
281*cdf0e10cSrcweir       return NULL;
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir     hashCode = rtl_ustr_hashCode(language) ^ rtl_ustr_hashCode(country) ^ rtl_ustr_hashCode(variant);
284*cdf0e10cSrcweir     key = rtl_hashfunc(g_pLocaleTable, hashCode);
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir     if (rtl_hashtable_find(g_pLocaleTable, key, hashCode, &newLocale))
287*cdf0e10cSrcweir         return newLocale;
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir     rtl_uString_newFromStr(&sLanguage, language);
290*cdf0e10cSrcweir     rtl_uString_newFromStr(&sCountry, country);
291*cdf0e10cSrcweir     rtl_uString_newFromStr(&sVariant, variant);
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir     newLocale = (rtl_Locale*)rtl_allocateMemory( sizeof(rtl_Locale) );
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir     newLocale->Language = sLanguage;
296*cdf0e10cSrcweir     newLocale->Country = sCountry;
297*cdf0e10cSrcweir     newLocale->Variant = sVariant;
298*cdf0e10cSrcweir     newLocale->HashCode = hashCode;
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir     rtl_hashtable_add(&g_pLocaleTable, newLocale);
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir     return newLocale;
303*cdf0e10cSrcweir }
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir /*************************************************************************
306*cdf0e10cSrcweir  *  rtl_locale_getDefault
307*cdf0e10cSrcweir  */
308*cdf0e10cSrcweir rtl_Locale * SAL_CALL rtl_locale_getDefault()
309*cdf0e10cSrcweir {
310*cdf0e10cSrcweir     return g_pDefaultLocale;
311*cdf0e10cSrcweir }
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir /*************************************************************************
314*cdf0e10cSrcweir  *  rtl_locale_setDefault
315*cdf0e10cSrcweir  */
316*cdf0e10cSrcweir void SAL_CALL rtl_locale_setDefault( const sal_Unicode * language, const sal_Unicode * country, const sal_Unicode * variant )
317*cdf0e10cSrcweir {
318*cdf0e10cSrcweir     g_pDefaultLocale = rtl_locale_register(language, country, variant);
319*cdf0e10cSrcweir }
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir /*************************************************************************
322*cdf0e10cSrcweir  *  rtl_locale_getLanguage
323*cdf0e10cSrcweir  */
324*cdf0e10cSrcweir rtl_uString * SAL_CALL rtl_locale_getLanguage( rtl_Locale * This )
325*cdf0e10cSrcweir {
326*cdf0e10cSrcweir     rtl_uString_acquire(This->Language);
327*cdf0e10cSrcweir     return This->Language;
328*cdf0e10cSrcweir }
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir /*************************************************************************
331*cdf0e10cSrcweir  *  rtl_locale_getCountry
332*cdf0e10cSrcweir  */
333*cdf0e10cSrcweir rtl_uString * SAL_CALL rtl_locale_getCountry( rtl_Locale * This )
334*cdf0e10cSrcweir {
335*cdf0e10cSrcweir     rtl_uString_acquire(This->Country);
336*cdf0e10cSrcweir     return This->Country;
337*cdf0e10cSrcweir }
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir /*************************************************************************
340*cdf0e10cSrcweir  *  rtl_locale_getVariant
341*cdf0e10cSrcweir  */
342*cdf0e10cSrcweir rtl_uString * SAL_CALL rtl_locale_getVariant( rtl_Locale * This )
343*cdf0e10cSrcweir {
344*cdf0e10cSrcweir     rtl_uString_acquire(This->Variant);
345*cdf0e10cSrcweir     return This->Variant;
346*cdf0e10cSrcweir }
347*cdf0e10cSrcweir 
348*cdf0e10cSrcweir /*************************************************************************
349*cdf0e10cSrcweir  *  rtl_locale_hashCode
350*cdf0e10cSrcweir  */
351*cdf0e10cSrcweir sal_Int32 SAL_CALL rtl_locale_hashCode( rtl_Locale * This )
352*cdf0e10cSrcweir {
353*cdf0e10cSrcweir     return This->HashCode;
354*cdf0e10cSrcweir }
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir /*************************************************************************
357*cdf0e10cSrcweir  *  rtl_locale_equals
358*cdf0e10cSrcweir  */
359*cdf0e10cSrcweir sal_Int32 SAL_CALL rtl_locale_equals( rtl_Locale * This, rtl_Locale * obj  )
360*cdf0e10cSrcweir {
361*cdf0e10cSrcweir     return This == obj;
362*cdf0e10cSrcweir }
363