1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_RESOURCE_MANAGER_RES_LOCALE_H
17 #define OHOS_RESOURCE_MANAGER_RES_LOCALE_H
18 
19 #include <cstdint>
20 #include <cstddef>
21 #ifdef SUPPORT_GRAPHICS
22 #include <unicode/locid.h>
23 #endif
24 #include "rstate.h"
25 #include "lock.h"
26 
27 #ifdef SUPPORT_GRAPHICS
28 using icu::Locale;
29 #endif
30 namespace OHOS {
31 namespace Global {
32 namespace Resource {
33 struct ParseResult {
34     const char *tempLanguage = nullptr;
35     const char *tempScript = nullptr;
36     const char *tempRegion = nullptr;
37     int16_t languageTagLen = 0;
38     int16_t scriptTagLen = 0;
39     int16_t regionTagLen = 0;
40 };
41 
42 class ResLocale {
43 public:
44     const char *GetLanguage() const;
45 
46     const char *GetRegion() const;
47 
48     const char *GetScript() const;
49 
50     ResLocale();
51 
52 #ifdef SUPPORT_GRAPHICS
53     /**
54      * Copy from other LocaleInfo to this
55      * @param other the other locale information copy to this localeInfo
56      * @return SUCCESS if copy other LocaleInfo success, else ERROR
57      */
58     RState CopyFromLocaleInfo(const Locale *other);
59 #endif
60     /**
61      * Copy from other ResLocale to this
62      * @param other the other ResLocale copy to this ResLocale
63      * @return SUCCESS if copy other ResLocale success, else ERROR
64      */
65     RState Copy(const ResLocale *other);
66 
67 #ifdef SUPPORT_GRAPHICS
68     static const Locale *GetDefault();
69 
70     static bool UpdateDefault(const Locale &localeInfo, bool needNotify);
71 #endif
72      /**
73      * Build resLocal from string
74      * @param bcp47String the target string
75      * @param sep the parse string position
76      * @param rState the parse status
77      * @return the resLocal after parse bcp47String
78      */
79     static ResLocale *BuildFromString(const char *bcp47String, char sep, RState &rState);
80 
81     /**
82      * Build resLocal from parts
83      * @param language the resLocal language
84      * @param script the resLocal script
85      * @param region the resLocal region
86      * @param rState the Build status
87      * @return the resLocal after Build resLocal from parts if success, else return nullptr
88      */
89     static ResLocale *BuildFromParts(const char *language, const char *script, const char *region, RState &rState);
90 
91     ~ResLocale();
92 
93     static constexpr uint16_t END_TYPE = 0x0000;
94     // language parse
95     static constexpr uint16_t LANG_TYPE = 0x0001;
96     // script parse
97     static constexpr uint16_t SCRIPT_TYPE = 0x0002;
98     // region parse
99     static constexpr uint16_t REGION_TYPE = 0x0004;
100 
101     static constexpr size_t MIN_BCP47_STR_LEN = 2;
102 
103 private:
104     RState SetLanguage(const char *language, size_t len);
105 
106     RState SetScript(const char *script, size_t len);
107 
108     RState SetRegion(const char *region, size_t len);
109 
110     static ResLocale *DoParse(const char *bcp47String, char sep, RState &rState);
111 
112     static ResLocale *CreateResLocale(ParseResult &parseResult, RState &rState);
113 
114     RState Init(const char *language, size_t languageLen, const char *script, size_t scriptLen,
115         const char *region, size_t regionLen);
116 
117     const char *language_;
118 
119     const char *region_;
120 
121     const char *script_;
122 
123 #ifdef SUPPORT_GRAPHICS
124     static Locale *defaultLocale_;
125 #endif
126 
127     static Lock lock_;
128 
129     friend class LocaleMatcher;
130 };
131 } // namespace Resource
132 } // namespace Global
133 } // namespace OHOS
134 #endif
135