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_aLocaleTable
;
48 static rtl_Locale
* g_pDefaultLocale
= nullptr;
50 void rtl_locale_init()
54 void rtl_locale_fini()
56 g_aLocaleTable
.clear();
57 g_pDefaultLocale
= nullptr;
60 rtl_Locale
* SAL_CALL
rtl_locale_register(const sal_Unicode
* language
, const sal_Unicode
* country
, const sal_Unicode
* variant
)
63 rtl_uString
* sLanguage
= nullptr;
64 rtl_uString
* sCountry
= nullptr;
65 rtl_uString
* sVariant
= nullptr;
66 sal_Int32 hashCode
= -1;
74 ensureLocaleSingleton();
76 hashCode
= rtl_ustr_hashCode(language
) ^ rtl_ustr_hashCode(country
) ^ rtl_ustr_hashCode(variant
);
78 auto it
= g_aLocaleTable
.find(hashCode
);
79 if (it
!= g_aLocaleTable
.end())
80 return it
->second
.get();
82 rtl_uString_newFromStr(&sLanguage
, language
);
83 rtl_uString_newFromStr(&sCountry
, country
);
84 rtl_uString_newFromStr(&sVariant
, variant
);
86 locale_unique_ptr
newLocale(new rtl_Locale
);
88 newLocale
->Language
= sLanguage
;
89 newLocale
->Country
= sCountry
;
90 newLocale
->Variant
= sVariant
;
91 newLocale
->HashCode
= hashCode
;
93 auto ret
= newLocale
.get();
94 g_aLocaleTable
.insert(it
, std::pair
<sal_Int32
, locale_unique_ptr
>( hashCode
, std::move(newLocale
) ) );
98 rtl_Locale
* SAL_CALL
rtl_locale_getDefault()
100 return g_pDefaultLocale
;
103 void SAL_CALL
rtl_locale_setDefault(const sal_Unicode
* language
, const sal_Unicode
* country
, const sal_Unicode
* variant
)
105 g_pDefaultLocale
= rtl_locale_register(language
, country
, variant
);
108 rtl_uString
* SAL_CALL
rtl_locale_getLanguage(rtl_Locale
* This
)
110 rtl_uString_acquire(This
->Language
);
111 return This
->Language
;
114 rtl_uString
* SAL_CALL
rtl_locale_getCountry(rtl_Locale
* This
)
116 rtl_uString_acquire(This
->Country
);
117 return This
->Country
;
120 rtl_uString
* SAL_CALL
rtl_locale_getVariant(rtl_Locale
* This
)
122 rtl_uString_acquire(This
->Variant
);
123 return This
->Variant
;
126 sal_Int32 SAL_CALL
rtl_locale_hashCode(rtl_Locale
* This
)
128 return This
->HashCode
;
131 sal_Int32 SAL_CALL
rtl_locale_equals(rtl_Locale
* This
, rtl_Locale
* obj
)
136 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */