1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <rtl/locale.h>
23 #include <osl/diagnose.h>
25 #include <rtllifecycle.h>
27 #include <unordered_map>
31 void operator() (rtl_Locale
* p
) noexcept
33 rtl_uString_release(p
->Language
);
34 rtl_uString_release(p
->Country
);
35 rtl_uString_release(p
->Variant
);
40 using locale_unique_ptr
= std::unique_ptr
<rtl_Locale
, locale_deleter
>;
42 static std::unordered_map
<sal_Int32
, locale_unique_ptr
>* g_pLocaleTable
= nullptr;
44 static rtl_Locale
* g_pDefaultLocale
= nullptr;
46 void rtl_locale_init()
49 g_pLocaleTable
= new std::unordered_map
<sal_Int32
, locale_unique_ptr
>;
52 void rtl_locale_fini()
56 delete g_pLocaleTable
;
57 g_pLocaleTable
= nullptr;
59 g_pDefaultLocale
= nullptr;
62 rtl_Locale
* SAL_CALL
rtl_locale_register(const sal_Unicode
* language
, const sal_Unicode
* country
, const sal_Unicode
* variant
)
65 rtl_uString
* sLanguage
= nullptr;
66 rtl_uString
* sCountry
= nullptr;
67 rtl_uString
* sVariant
= nullptr;
68 sal_Int32 hashCode
= -1;
76 ensureLocaleSingleton();
78 hashCode
= rtl_ustr_hashCode(language
) ^ rtl_ustr_hashCode(country
) ^ rtl_ustr_hashCode(variant
);
80 auto it
= g_pLocaleTable
->find(hashCode
);
81 if (it
!= g_pLocaleTable
->end())
82 return it
->second
.get();
84 rtl_uString_newFromStr(&sLanguage
, language
);
85 rtl_uString_newFromStr(&sCountry
, country
);
86 rtl_uString_newFromStr(&sVariant
, variant
);
88 locale_unique_ptr
newLocale(new rtl_Locale
);
90 newLocale
->Language
= sLanguage
;
91 newLocale
->Country
= sCountry
;
92 newLocale
->Variant
= sVariant
;
93 newLocale
->HashCode
= hashCode
;
95 auto ret
= newLocale
.get();
96 g_pLocaleTable
->insert(it
, std::pair
<sal_Int32
, locale_unique_ptr
>( hashCode
, std::move(newLocale
) ) );
100 rtl_Locale
* SAL_CALL
rtl_locale_getDefault()
102 return g_pDefaultLocale
;
105 void SAL_CALL
rtl_locale_setDefault(const sal_Unicode
* language
, const sal_Unicode
* country
, const sal_Unicode
* variant
)
107 g_pDefaultLocale
= rtl_locale_register(language
, country
, variant
);
110 rtl_uString
* SAL_CALL
rtl_locale_getLanguage(rtl_Locale
* This
)
112 rtl_uString_acquire(This
->Language
);
113 return This
->Language
;
116 rtl_uString
* SAL_CALL
rtl_locale_getCountry(rtl_Locale
* This
)
118 rtl_uString_acquire(This
->Country
);
119 return This
->Country
;
122 rtl_uString
* SAL_CALL
rtl_locale_getVariant(rtl_Locale
* This
)
124 rtl_uString_acquire(This
->Variant
);
125 return This
->Variant
;
128 sal_Int32 SAL_CALL
rtl_locale_hashCode(rtl_Locale
* This
)
130 return This
->HashCode
;
133 sal_Int32 SAL_CALL
rtl_locale_equals(rtl_Locale
* This
, rtl_Locale
* obj
)
138 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */