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>
33 void operator() (rtl_Locale
* p
) noexcept
35 rtl_uString_release(p
->Language
);
36 rtl_uString_release(p
->Country
);
37 rtl_uString_release(p
->Variant
);
44 using locale_unique_ptr
= std::unique_ptr
<rtl_Locale
, locale_deleter
>;
46 static std::unordered_map
<sal_Int32
, locale_unique_ptr
>* g_pLocaleTable
= nullptr;
48 static rtl_Locale
* g_pDefaultLocale
= nullptr;
50 void rtl_locale_init()
53 g_pLocaleTable
= new std::unordered_map
<sal_Int32
, locale_unique_ptr
>;
56 void rtl_locale_fini()
60 delete g_pLocaleTable
;
61 g_pLocaleTable
= nullptr;
63 g_pDefaultLocale
= nullptr;
66 rtl_Locale
* SAL_CALL
rtl_locale_register(const sal_Unicode
* language
, const sal_Unicode
* country
, const sal_Unicode
* variant
)
69 rtl_uString
* sLanguage
= nullptr;
70 rtl_uString
* sCountry
= nullptr;
71 rtl_uString
* sVariant
= nullptr;
72 sal_Int32 hashCode
= -1;
80 ensureLocaleSingleton();
82 hashCode
= rtl_ustr_hashCode(language
) ^ rtl_ustr_hashCode(country
) ^ rtl_ustr_hashCode(variant
);
84 auto it
= g_pLocaleTable
->find(hashCode
);
85 if (it
!= g_pLocaleTable
->end())
86 return it
->second
.get();
88 rtl_uString_newFromStr(&sLanguage
, language
);
89 rtl_uString_newFromStr(&sCountry
, country
);
90 rtl_uString_newFromStr(&sVariant
, variant
);
92 locale_unique_ptr
newLocale(new rtl_Locale
);
94 newLocale
->Language
= sLanguage
;
95 newLocale
->Country
= sCountry
;
96 newLocale
->Variant
= sVariant
;
97 newLocale
->HashCode
= hashCode
;
99 auto ret
= newLocale
.get();
100 g_pLocaleTable
->insert(it
, std::pair
<sal_Int32
, locale_unique_ptr
>( hashCode
, std::move(newLocale
) ) );
104 rtl_Locale
* SAL_CALL
rtl_locale_getDefault()
106 return g_pDefaultLocale
;
109 void SAL_CALL
rtl_locale_setDefault(const sal_Unicode
* language
, const sal_Unicode
* country
, const sal_Unicode
* variant
)
111 g_pDefaultLocale
= rtl_locale_register(language
, country
, variant
);
114 rtl_uString
* SAL_CALL
rtl_locale_getLanguage(rtl_Locale
* This
)
116 rtl_uString_acquire(This
->Language
);
117 return This
->Language
;
120 rtl_uString
* SAL_CALL
rtl_locale_getCountry(rtl_Locale
* This
)
122 rtl_uString_acquire(This
->Country
);
123 return This
->Country
;
126 rtl_uString
* SAL_CALL
rtl_locale_getVariant(rtl_Locale
* This
)
128 rtl_uString_acquire(This
->Variant
);
129 return This
->Variant
;
132 sal_Int32 SAL_CALL
rtl_locale_hashCode(rtl_Locale
* This
)
134 return This
->HashCode
;
137 sal_Int32 SAL_CALL
rtl_locale_equals(rtl_Locale
* This
, rtl_Locale
* obj
)
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */