xref: /aoo42x/main/sal/rtl/source/locale.c (revision 509a48ff)
1647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3647f063dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4647f063dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5647f063dSAndrew Rist  * distributed with this work for additional information
6647f063dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7647f063dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8647f063dSAndrew Rist  * "License"); you may not use this file except in compliance
9647f063dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10647f063dSAndrew Rist  *
11647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12647f063dSAndrew Rist  *
13647f063dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14647f063dSAndrew Rist  * software distributed under the License is distributed on an
15647f063dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16647f063dSAndrew Rist  * KIND, either express or implied.  See the License for the
17647f063dSAndrew Rist  * specific language governing permissions and limitations
18647f063dSAndrew Rist  * under the License.
19647f063dSAndrew Rist  *
20647f063dSAndrew Rist  *************************************************************/
21647f063dSAndrew Rist 
22647f063dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "rtl/locale.h"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "osl/diagnose.h"
27cdf0e10cSrcweir #include "rtl/alloc.h"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "internal/once.h"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir static sal_Int32 RTL_HASHTABLE_SIZE[] =
32cdf0e10cSrcweir {
33cdf0e10cSrcweir     7, 31, 127, 251, 509, 1021, 2039, 4093
34cdf0e10cSrcweir };
35cdf0e10cSrcweir 
36cdf0e10cSrcweir typedef struct rtl_hashentry RTL_HASHENTRY;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir struct rtl_hashentry
39cdf0e10cSrcweir {
40cdf0e10cSrcweir     rtl_Locale* Entry;
41cdf0e10cSrcweir     RTL_HASHENTRY* Next;
42cdf0e10cSrcweir };
43cdf0e10cSrcweir 
44cdf0e10cSrcweir typedef struct rtl_hashtable
45cdf0e10cSrcweir {
46cdf0e10cSrcweir     sal_Int8        iSize;
47cdf0e10cSrcweir     sal_Int32       Size;
48cdf0e10cSrcweir     sal_Int32       Elements;
49cdf0e10cSrcweir     RTL_HASHENTRY** Table;
50cdf0e10cSrcweir } RTL_HASHTABLE;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir static RTL_HASHTABLE* g_pLocaleTable = NULL;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir static rtl_Locale* g_pDefaultLocale = NULL;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir static int rtl_locale_init (void);
57cdf0e10cSrcweir 
58cdf0e10cSrcweir /*************************************************************************
59cdf0e10cSrcweir  */
rtl_hashentry_destroy(RTL_HASHENTRY * entry)60cdf0e10cSrcweir void rtl_hashentry_destroy(RTL_HASHENTRY* entry)
61cdf0e10cSrcweir {
62cdf0e10cSrcweir     rtl_uString_release(entry->Entry->Language);
63cdf0e10cSrcweir     rtl_uString_release(entry->Entry->Country);
64cdf0e10cSrcweir     rtl_uString_release(entry->Entry->Variant);
65cdf0e10cSrcweir     if (entry->Next)
66cdf0e10cSrcweir         rtl_hashentry_destroy(entry->Next);
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     rtl_freeMemory(entry->Entry);
69cdf0e10cSrcweir     rtl_freeMemory(entry);
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
rtl_hashtable_destroy(RTL_HASHTABLE * table)72cdf0e10cSrcweir void rtl_hashtable_destroy(RTL_HASHTABLE* table)
73cdf0e10cSrcweir {
74cdf0e10cSrcweir     sal_Int32 size = 0;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     if (!table)
77cdf0e10cSrcweir         return;
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     size = table->Size;
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     while (size)
82cdf0e10cSrcweir     {
83cdf0e10cSrcweir         if (table->Table[size - 1])
84cdf0e10cSrcweir             rtl_hashentry_destroy(table->Table[size - 1]);
85cdf0e10cSrcweir         size--;
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     rtl_freeMemory(table->Table);
89cdf0e10cSrcweir     rtl_freeMemory(table);
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
rtl_hashtable_init(RTL_HASHTABLE ** table,sal_Int8 sizeIndex)92cdf0e10cSrcweir void rtl_hashtable_init(RTL_HASHTABLE** table, sal_Int8 sizeIndex)
93cdf0e10cSrcweir {
94cdf0e10cSrcweir     sal_Int32 nSize = RTL_HASHTABLE_SIZE[sizeIndex];
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     if (*table)
97cdf0e10cSrcweir         rtl_hashtable_destroy(*table);
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     *table = (RTL_HASHTABLE*)rtl_allocateMemory( sizeof(RTL_HASHTABLE) );
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     (*table)->iSize = sizeIndex;
102cdf0e10cSrcweir     (*table)->Size = nSize;
103cdf0e10cSrcweir     (*table)->Elements = 0;
104cdf0e10cSrcweir     (*table)->Table = (RTL_HASHENTRY**)rtl_allocateMemory( (*table)->Size * sizeof(RTL_HASHENTRY*) );
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     while (nSize)
107cdf0e10cSrcweir     {
108cdf0e10cSrcweir         (*table)->Table[nSize - 1] = NULL;
109cdf0e10cSrcweir         nSize--;
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
rtl_hashfunc(RTL_HASHTABLE * table,sal_Int32 key)113cdf0e10cSrcweir sal_Int32 rtl_hashfunc(RTL_HASHTABLE* table, sal_Int32 key)
114cdf0e10cSrcweir {
115cdf0e10cSrcweir     return ( (sal_uInt32) key % table->Size);
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir sal_Bool rtl_hashtable_grow(RTL_HASHTABLE** table);
119cdf0e10cSrcweir 
rtl_hashtable_add(RTL_HASHTABLE ** table,rtl_Locale * value)120cdf0e10cSrcweir rtl_Locale* rtl_hashtable_add(RTL_HASHTABLE** table, rtl_Locale* value)
121cdf0e10cSrcweir {
122cdf0e10cSrcweir     sal_Int32 key = 0;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     if (!(*table))
125cdf0e10cSrcweir         return NULL;
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     if ((*table)->Elements > ((*table)->Size / 2))
128cdf0e10cSrcweir         rtl_hashtable_grow(table);
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     key = rtl_hashfunc(*table, value->HashCode);
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     if (!(*table)->Table[key])
133cdf0e10cSrcweir     {
134cdf0e10cSrcweir         RTL_HASHENTRY *newEntry = (RTL_HASHENTRY*)rtl_allocateMemory( sizeof(RTL_HASHENTRY) );
135cdf0e10cSrcweir         newEntry->Entry = value;
136cdf0e10cSrcweir         newEntry->Next = NULL;
137cdf0e10cSrcweir         (*table)->Table[key] = newEntry;
138cdf0e10cSrcweir         (*table)->Elements++;
139cdf0e10cSrcweir         return NULL;
140cdf0e10cSrcweir     } else
141cdf0e10cSrcweir     {
142cdf0e10cSrcweir         RTL_HASHENTRY *pEntry = (*table)->Table[key];
143cdf0e10cSrcweir         RTL_HASHENTRY *newEntry = NULL;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         while (pEntry)
146cdf0e10cSrcweir         {
147cdf0e10cSrcweir             if (value->HashCode == pEntry->Entry->HashCode)
148cdf0e10cSrcweir                 return pEntry->Entry;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir             if (!pEntry->Next)
151cdf0e10cSrcweir                 break;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir             pEntry = pEntry->Next;
154cdf0e10cSrcweir         }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         newEntry = (RTL_HASHENTRY*)rtl_allocateMemory( sizeof(RTL_HASHENTRY) );
157cdf0e10cSrcweir         newEntry->Entry = value;
158cdf0e10cSrcweir         newEntry->Next = NULL;
159cdf0e10cSrcweir         pEntry->Next = newEntry;
160cdf0e10cSrcweir         (*table)->Elements++;
161cdf0e10cSrcweir         return NULL;
162cdf0e10cSrcweir     }
163cdf0e10cSrcweir }
164cdf0e10cSrcweir 
rtl_hashtable_grow(RTL_HASHTABLE ** table)165cdf0e10cSrcweir sal_Bool rtl_hashtable_grow(RTL_HASHTABLE** table)
166cdf0e10cSrcweir {
167cdf0e10cSrcweir     RTL_HASHTABLE* pNewTable = NULL;
168cdf0e10cSrcweir     sal_Int32 i = 0;
169cdf0e10cSrcweir 
170cdf0e10cSrcweir     rtl_hashtable_init(&pNewTable, (sal_Int8)((*table)->iSize + 1));
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     while (i < (*table)->Size)
173cdf0e10cSrcweir     {
174cdf0e10cSrcweir         if ((*table)->Table[i])
175cdf0e10cSrcweir         {
176cdf0e10cSrcweir             RTL_HASHENTRY *pNext;
177cdf0e10cSrcweir             RTL_HASHENTRY *pEntry = (*table)->Table[i];
178cdf0e10cSrcweir 
179cdf0e10cSrcweir             rtl_hashtable_add(&pNewTable, pEntry->Entry);
180cdf0e10cSrcweir 
181cdf0e10cSrcweir             while (pEntry->Next)
182cdf0e10cSrcweir             {
183cdf0e10cSrcweir                 rtl_hashtable_add(&pNewTable, pEntry->Next->Entry);
184cdf0e10cSrcweir                 pNext = pEntry->Next;
185cdf0e10cSrcweir                 rtl_freeMemory(pEntry);
186cdf0e10cSrcweir                 pEntry = pNext;
187cdf0e10cSrcweir             }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir             rtl_freeMemory(pEntry);
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir         i++;
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir     rtl_freeMemory((*table)->Table);
195cdf0e10cSrcweir     rtl_freeMemory((*table));
196cdf0e10cSrcweir     (*table) = pNewTable;
197cdf0e10cSrcweir 
198cdf0e10cSrcweir     return sal_True;
199cdf0e10cSrcweir }
200cdf0e10cSrcweir 
rtl_hashtable_find(RTL_HASHTABLE * table,sal_Int32 key,sal_Int32 hashCode,rtl_Locale ** pValue)201cdf0e10cSrcweir sal_Bool rtl_hashtable_find(RTL_HASHTABLE * table, sal_Int32 key, sal_Int32 hashCode, rtl_Locale** pValue)
202cdf0e10cSrcweir {
203cdf0e10cSrcweir     if (!table)
204cdf0e10cSrcweir         return sal_False;
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     if (table->Table[key])
207cdf0e10cSrcweir     {
208cdf0e10cSrcweir         RTL_HASHENTRY *pEntry = table->Table[key];
209cdf0e10cSrcweir 
210cdf0e10cSrcweir         while (pEntry && hashCode != pEntry->Entry->HashCode)
211cdf0e10cSrcweir             pEntry = pEntry->Next;
212cdf0e10cSrcweir 
213cdf0e10cSrcweir         if (pEntry)
214cdf0e10cSrcweir             *pValue = pEntry->Entry;
215cdf0e10cSrcweir         else
216cdf0e10cSrcweir             return sal_False;
217cdf0e10cSrcweir     } else
218cdf0e10cSrcweir         return sal_False;
219cdf0e10cSrcweir 
220cdf0e10cSrcweir     return sal_True;
221cdf0e10cSrcweir }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir /*************************************************************************
224cdf0e10cSrcweir  *  rtl_locale_init
225cdf0e10cSrcweir  */
rtl_locale_once_init(void)226cdf0e10cSrcweir static void rtl_locale_once_init (void)
227cdf0e10cSrcweir {
228*509a48ffSpfg   OSL_ASSERT(g_pLocaleTable == NULL);
229cdf0e10cSrcweir   rtl_hashtable_init(&g_pLocaleTable, 1);
230cdf0e10cSrcweir }
231cdf0e10cSrcweir 
rtl_locale_init(void)232cdf0e10cSrcweir static int rtl_locale_init (void)
233cdf0e10cSrcweir {
234cdf0e10cSrcweir   static sal_once_type g_once = SAL_ONCE_INIT;
235cdf0e10cSrcweir   SAL_ONCE(&g_once, rtl_locale_once_init);
236*509a48ffSpfg   return (g_pLocaleTable != NULL);
237cdf0e10cSrcweir }
238cdf0e10cSrcweir 
239cdf0e10cSrcweir /*************************************************************************
240cdf0e10cSrcweir  *  rtl_locale_fini
241cdf0e10cSrcweir  */
242cdf0e10cSrcweir #if defined(__GNUC__)
243cdf0e10cSrcweir static void rtl_locale_fini (void) __attribute__((destructor));
244cdf0e10cSrcweir #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
245cdf0e10cSrcweir #pragma fini(rtl_locale_fini)
246cdf0e10cSrcweir static void rtl_locale_fini (void);
247cdf0e10cSrcweir #endif /* __GNUC__ || __SUNPRO_C */
248cdf0e10cSrcweir 
rtl_locale_fini(void)249cdf0e10cSrcweir void rtl_locale_fini (void)
250cdf0e10cSrcweir {
251*509a48ffSpfg   if (g_pLocaleTable != NULL)
252cdf0e10cSrcweir   {
253cdf0e10cSrcweir     rtl_hashtable_destroy (g_pLocaleTable);
254*509a48ffSpfg     g_pLocaleTable = NULL;
255cdf0e10cSrcweir   }
256cdf0e10cSrcweir }
257cdf0e10cSrcweir 
258cdf0e10cSrcweir /*************************************************************************
259cdf0e10cSrcweir  *  rtl_locale_register
260cdf0e10cSrcweir  */
rtl_locale_register(const sal_Unicode * language,const sal_Unicode * country,const sal_Unicode * variant)261cdf0e10cSrcweir rtl_Locale * SAL_CALL rtl_locale_register( const sal_Unicode * language, const sal_Unicode * country, const sal_Unicode * variant )
262cdf0e10cSrcweir {
263cdf0e10cSrcweir     sal_Unicode c = 0;
264cdf0e10cSrcweir     rtl_uString* sLanguage = NULL;
265cdf0e10cSrcweir     rtl_uString* sCountry = NULL;
266cdf0e10cSrcweir     rtl_uString* sVariant = NULL;
267cdf0e10cSrcweir     rtl_Locale *newLocale = NULL;
268cdf0e10cSrcweir     sal_Int32 hashCode = -1;
269cdf0e10cSrcweir     sal_Int32 key = 0;
270cdf0e10cSrcweir 
271cdf0e10cSrcweir     if ( !country )
272cdf0e10cSrcweir         country = &c;
273cdf0e10cSrcweir     if ( !variant )
274cdf0e10cSrcweir         variant = &c;
275cdf0e10cSrcweir 
276cdf0e10cSrcweir     if (!rtl_locale_init())
277cdf0e10cSrcweir       return NULL;
278cdf0e10cSrcweir 
279cdf0e10cSrcweir     hashCode = rtl_ustr_hashCode(language) ^ rtl_ustr_hashCode(country) ^ rtl_ustr_hashCode(variant);
280cdf0e10cSrcweir     key = rtl_hashfunc(g_pLocaleTable, hashCode);
281cdf0e10cSrcweir 
282cdf0e10cSrcweir     if (rtl_hashtable_find(g_pLocaleTable, key, hashCode, &newLocale))
283cdf0e10cSrcweir         return newLocale;
284cdf0e10cSrcweir 
285cdf0e10cSrcweir     rtl_uString_newFromStr(&sLanguage, language);
286cdf0e10cSrcweir     rtl_uString_newFromStr(&sCountry, country);
287cdf0e10cSrcweir     rtl_uString_newFromStr(&sVariant, variant);
288cdf0e10cSrcweir 
289cdf0e10cSrcweir     newLocale = (rtl_Locale*)rtl_allocateMemory( sizeof(rtl_Locale) );
290cdf0e10cSrcweir 
291cdf0e10cSrcweir     newLocale->Language = sLanguage;
292cdf0e10cSrcweir     newLocale->Country = sCountry;
293cdf0e10cSrcweir     newLocale->Variant = sVariant;
294cdf0e10cSrcweir     newLocale->HashCode = hashCode;
295cdf0e10cSrcweir 
296cdf0e10cSrcweir     rtl_hashtable_add(&g_pLocaleTable, newLocale);
297cdf0e10cSrcweir 
298cdf0e10cSrcweir     return newLocale;
299cdf0e10cSrcweir }
300cdf0e10cSrcweir 
301cdf0e10cSrcweir /*************************************************************************
302cdf0e10cSrcweir  *  rtl_locale_getDefault
303cdf0e10cSrcweir  */
rtl_locale_getDefault()304cdf0e10cSrcweir rtl_Locale * SAL_CALL rtl_locale_getDefault()
305cdf0e10cSrcweir {
306cdf0e10cSrcweir     return g_pDefaultLocale;
307cdf0e10cSrcweir }
308cdf0e10cSrcweir 
309cdf0e10cSrcweir /*************************************************************************
310cdf0e10cSrcweir  *  rtl_locale_setDefault
311cdf0e10cSrcweir  */
rtl_locale_setDefault(const sal_Unicode * language,const sal_Unicode * country,const sal_Unicode * variant)312cdf0e10cSrcweir void SAL_CALL rtl_locale_setDefault( const sal_Unicode * language, const sal_Unicode * country, const sal_Unicode * variant )
313cdf0e10cSrcweir {
314cdf0e10cSrcweir     g_pDefaultLocale = rtl_locale_register(language, country, variant);
315cdf0e10cSrcweir }
316cdf0e10cSrcweir 
317cdf0e10cSrcweir /*************************************************************************
318cdf0e10cSrcweir  *  rtl_locale_getLanguage
319cdf0e10cSrcweir  */
rtl_locale_getLanguage(rtl_Locale * This)320cdf0e10cSrcweir rtl_uString * SAL_CALL rtl_locale_getLanguage( rtl_Locale * This )
321cdf0e10cSrcweir {
322cdf0e10cSrcweir     rtl_uString_acquire(This->Language);
323cdf0e10cSrcweir     return This->Language;
324cdf0e10cSrcweir }
325cdf0e10cSrcweir 
326cdf0e10cSrcweir /*************************************************************************
327cdf0e10cSrcweir  *  rtl_locale_getCountry
328cdf0e10cSrcweir  */
rtl_locale_getCountry(rtl_Locale * This)329cdf0e10cSrcweir rtl_uString * SAL_CALL rtl_locale_getCountry( rtl_Locale * This )
330cdf0e10cSrcweir {
331cdf0e10cSrcweir     rtl_uString_acquire(This->Country);
332cdf0e10cSrcweir     return This->Country;
333cdf0e10cSrcweir }
334cdf0e10cSrcweir 
335cdf0e10cSrcweir /*************************************************************************
336cdf0e10cSrcweir  *  rtl_locale_getVariant
337cdf0e10cSrcweir  */
rtl_locale_getVariant(rtl_Locale * This)338cdf0e10cSrcweir rtl_uString * SAL_CALL rtl_locale_getVariant( rtl_Locale * This )
339cdf0e10cSrcweir {
340cdf0e10cSrcweir     rtl_uString_acquire(This->Variant);
341cdf0e10cSrcweir     return This->Variant;
342cdf0e10cSrcweir }
343cdf0e10cSrcweir 
344cdf0e10cSrcweir /*************************************************************************
345cdf0e10cSrcweir  *  rtl_locale_hashCode
346cdf0e10cSrcweir  */
rtl_locale_hashCode(rtl_Locale * This)347cdf0e10cSrcweir sal_Int32 SAL_CALL rtl_locale_hashCode( rtl_Locale * This )
348cdf0e10cSrcweir {
349cdf0e10cSrcweir     return This->HashCode;
350cdf0e10cSrcweir }
351cdf0e10cSrcweir 
352cdf0e10cSrcweir /*************************************************************************
353cdf0e10cSrcweir  *  rtl_locale_equals
354cdf0e10cSrcweir  */
rtl_locale_equals(rtl_Locale * This,rtl_Locale * obj)355cdf0e10cSrcweir sal_Int32 SAL_CALL rtl_locale_equals( rtl_Locale * This, rtl_Locale * obj  )
356cdf0e10cSrcweir {
357cdf0e10cSrcweir     return This == obj;
358cdf0e10cSrcweir }
359