• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 DATA_RESOURCE_IMPL_H
17 #define DATA_RESOURCE_IMPL_H
18 
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include "locale_info.h"
23 #include "fcntl.h"
24 #include "types.h"
25 
26 #define GLOBAL_RESOURCE_HEADER_SIZE 16
27 #define GLOBAL_RESOURCE_HEADER_LEFT 4
28 #define GLOBAL_RESOURCE_HEADER_SKIP 12
29 #define GLOBAL_RESOURCE_INDEX_OFFSET 2
30 #define GLOBAL_RESOURCE_MASK_OFFSET 4
31 #define GLOBAL_LOCALE_MASK_ITEM_SIZE 8
32 #define GLOBAL_RESOURCE_CONFIG_SIZE 6
33 #define MONTH_SEP '_'
34 #define DAY_SEP '_'
35 #define PATTERN_SEP '_'
36 #define AMPM_SEP '_'
37 #define NUM_PATTERN_SIZE 5
38 #define NUM_PAT_INDEX 0
39 #define NUM_PERCENT_PAT_INDEX 1
40 #define NUM_DEC_SIGN_INDEX 2
41 #define NUM_GROUP_SIGN_INDEX 3
42 #define NUM_PERCENT_SIGN_INDEX 4
43 #define NUM_PATTERN_SEP '_'
44 #define NUM_DIGIT_SEP ';'
45 #define PLURAL_SEP '_'
46 #define NUM_DIGIT_SIZE 10
47 #define RULES_NUM 6
48 #define RESOURCE_INDEX 0
49 #define FALLBACK_RESOURCE_INDEX 1
50 #define DEFAULT_RESOURCE_INDEX 2
51 #define SHIFT_ONE_BYTE 8
52 #define SHIFT_TWO_BYTE 16
53 #define SHIFT_THREE_BYTE 24
54 #define MAX_LOCALE_ITEM_SIZE 500
55 #define MEASURE_BASE_ITEM_COUNT 4
56 #define MEASURE_SINGLE_UNIT_COUNT 24
57 #define MEASURE_FORMAT_TYPE_NUM 4
58 #define MEASURE_PLURAL_NUM 6
59 #define MAX_MEASURE_FORMAT_LENGTH 128
60 #define MEASURE_UNIT_SEP '|'
61 
62 namespace OHOS {
63 namespace I18N {
64 enum LocaleDataType {
65     RESOURCE = 0,
66     FALLBACK_RESOURCE,
67     DEFAULT_RESOURCE
68 };
69 
70 enum DataResourceType {
71     RESOURCE_TYPE_BEGIN = 0,
72     GREGORIAN_FORMAT_ABBR_MONTH = RESOURCE_TYPE_BEGIN, // 0
73     GREGORIAN_FORMAT_ABBR_DAY, // 1
74     GREGORIAN_TIME_PATTERNS, // 2
75     GREGORIAN_DATE_PATTERNS, // 3
76     GREGORIAN_AM_PMS, // 4
77     PLURAL, // 5
78     NUMBER_FORMAT, // 6
79     NUMBER_DIGIT, // 7
80     TIME_SEPARATOR, // 8
81     DEFAULT_HOUR, // 9
82     GREGORIAN_STANDALONE_ABBR_MONTH, // 10
83     GREGORIAN_STANDALONE_ABBR_DAY, // 11
84     GREGORIAN_FORMAT_WIDE_MONTH, // 12
85     GREGORIAN_HOUR_MINUTE_SECOND_PATTERN, // 13
86     GREGORIAN_FULL_MEDIUM_SHORT_PATTERN, // 14
87     GREGORIAN_FORMAT_WIDE_DAY, // 15
88     GREGORIAN_STANDALONE_WIDE_DAY, // 16
89     GREGORIAN_STANDALONE_WIDE_MONTH, // 17
90     ELAPSED_PATERNS, // 18
91     WEEK_DATA, // 19
92     DECIMAL_PLURAL, // 20
93     MINUS_SIGN, // 21
94     MEASURE_FORMAT_PATTERNS, // 22
95     RESOURCE_TYPE_END // 23
96 };
97 
98 class DataResource {
99 public:
100     explicit DataResource(const LocaleInfo *localeInfo);
101     bool Init();
102     char *GetString(DataResourceType type) const;
103     virtual ~DataResource();
104     void GetString(DataResourceType type, std::string &ret) const;
105 
106 private:
107     static uint32_t GetFallbackMask(const LocaleInfo &src);
108     bool ReadHeader(int32_t infile);
109     bool PrepareData(int32_t infile);
110     int32_t BinarySearchLocale(const uint32_t mask, unsigned char *locales);
111     bool GetStringFromStringPool(char *configs, const uint32_t configsSize, int32_t infile, LocaleDataType type);
112     uint32_t ConvertUint(unsigned char *src);
113     uint32_t ConvertUChar(unsigned char *src);
114     char *GetString2(DataResourceType type) const;
115     char *GetString(uint32_t index) const;
116     char *BinarySearchString(uint32_t *indexArray, uint32_t length,
117         uint32_t target, char **stringArray, uint32_t stringLength) const;
118     LocaleInfo *GetFallbackLocaleInfo(const LocaleInfo &src);
119     void GetFallbackAndDefaultLocaleIndex(int32_t &fallbackLocaleIndex, int32_t &defaultLocaleIndex,
120         char *locales);
121     void GetFallbackAndDefaultInfo(const int32_t &fallbackLocaleIndex, const int32_t &defaultLocaleIndex,
122         uint32_t &fallbackConfigOffset, uint32_t &defaultConfigOffset, char *locales);
123     bool Retrieve(char *configs, const uint32_t configsSize, int32_t infile, const uint32_t originalCount,
124         LocaleDataType type);
125     bool PrepareLocaleData(int32_t infile, uint32_t configOffset, uint32_t count, LocaleDataType type);
126     bool FullLoaded();
127     void GetType(char** &adjustResource, uint32_t* &adjustResourceIndex, uint32_t &count, LocaleDataType type);
128     uint32_t GetFinalCount(char *configs, uint32_t configSize, LocaleDataType type);
129     void FreeResource();
130     bool IsTypeNeeded(int32_t index, uint32_t count);
131     bool ApplyForResource(uint32_t **index, char ***wanted, uint32_t totalCount);
132     uint32_t localeMask = 0;
133     uint32_t fallbackMask = 0;
134     uint32_t defaultMask = 0;
135     uint32_t localesCount = 0;
136     uint32_t stringPoolOffset = 0;
137     uint32_t *resourceIndex = nullptr;
138     uint32_t *defaultResourceIndex = nullptr;
139     char **resource = nullptr;
140     uint32_t resourceCount = 0;
141     uint32_t fallbackResourceCount = 0;
142     uint32_t defaultResourceCount = 0;
143     LocaleDataType currentType = LocaleDataType::DEFAULT_RESOURCE;
144     uint32_t *fallbackResourceIndex = nullptr;
145     char **fallbackResource = nullptr;
146     char **defaultResource = nullptr;
147     uint32_t loaded[DataResourceType::RESOURCE_TYPE_END] = { 0 };
148 };
149 } // namespace I18N
150 } // namespace OHOS
151 #endif