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 .
20 #include <rtl/locale.h>
22 #include <rtllifecycle.h>
24 #include <unordered_map>
30 void operator() (rtl_Locale
* p
) noexcept
32 rtl_uString_release(p
->Language
);
33 rtl_uString_release(p
->Country
);
34 rtl_uString_release(p
->Variant
);
41 using locale_unique_ptr
= std::unique_ptr
<rtl_Locale
, locale_deleter
>;
43 static std::unordered_map
<sal_Int32
, locale_unique_ptr
> g_aLocaleTable
;
45 static rtl_Locale
* g_pDefaultLocale
= nullptr;
47 void rtl_locale_init()
51 void rtl_locale_fini()
53 g_aLocaleTable
.clear();
54 g_pDefaultLocale
= nullptr;
57 rtl_Locale
* SAL_CALL
rtl_locale_register(const sal_Unicode
* language
, const sal_Unicode
* country
, const sal_Unicode
* variant
)
60 rtl_uString
* sLanguage
= nullptr;
61 rtl_uString
* sCountry
= nullptr;
62 rtl_uString
* sVariant
= nullptr;
63 sal_Int32 hashCode
= -1;
71 ensureLocaleSingleton();
73 hashCode
= rtl_ustr_hashCode(language
) ^ rtl_ustr_hashCode(country
) ^ rtl_ustr_hashCode(variant
);
75 auto it
= g_aLocaleTable
.find(hashCode
);
76 if (it
!= g_aLocaleTable
.end())
77 return it
->second
.get();
79 rtl_uString_newFromStr(&sLanguage
, language
);
80 rtl_uString_newFromStr(&sCountry
, country
);
81 rtl_uString_newFromStr(&sVariant
, variant
);
83 locale_unique_ptr
newLocale(new rtl_Locale
);
85 newLocale
->Language
= sLanguage
;
86 newLocale
->Country
= sCountry
;
87 newLocale
->Variant
= sVariant
;
88 newLocale
->HashCode
= hashCode
;
90 auto ret
= newLocale
.get();
91 g_aLocaleTable
.insert(it
, std::pair
<sal_Int32
, locale_unique_ptr
>( hashCode
, std::move(newLocale
) ) );
95 rtl_Locale
* SAL_CALL
rtl_locale_getDefault()
97 return g_pDefaultLocale
;
100 void SAL_CALL
rtl_locale_setDefault(const sal_Unicode
* language
, const sal_Unicode
* country
, const sal_Unicode
* variant
)
102 g_pDefaultLocale
= rtl_locale_register(language
, country
, variant
);
105 rtl_uString
* SAL_CALL
rtl_locale_getLanguage(rtl_Locale
* This
)
107 rtl_uString_acquire(This
->Language
);
108 return This
->Language
;
111 rtl_uString
* SAL_CALL
rtl_locale_getCountry(rtl_Locale
* This
)
113 rtl_uString_acquire(This
->Country
);
114 return This
->Country
;
117 rtl_uString
* SAL_CALL
rtl_locale_getVariant(rtl_Locale
* This
)
119 rtl_uString_acquire(This
->Variant
);
120 return This
->Variant
;
123 sal_Int32 SAL_CALL
rtl_locale_hashCode(rtl_Locale
* This
)
125 return This
->HashCode
;
128 sal_Int32 SAL_CALL
rtl_locale_equals(rtl_Locale
* This
, rtl_Locale
* obj
)
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */