• 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 
56 namespace OHOS {
57 namespace I18N {
58 #ifdef I18N_PRODUCT
59 static const char *gDataResourcePath = "system/i18n/i18n.dat";
60 #else
61 static const char *gDataResourcePath = "/storage/data/i18n.dat";
62 #endif
63 enum LocaleDataType {
64     RESOURCE = 0,
65     FALLBACK_RESOURCE,
66     DEFAULT_RESOURCE
67 };
68 
69 enum DataResourceType {
70     RESOURCE_TYPE_BEGIN = 0,
71     GREGORIAN_FORMAT_ABBR_MONTH = RESOURCE_TYPE_BEGIN, // 0
72     GREGORIAN_FORMAT_ABBR_DAY, // 1
73     GREGORIAN_TIME_PATTERNS, // 2
74     GREGORIAN_DATE_PATTERNS, // 3
75     GREGORIAN_AM_PMS, // 4
76     PLURAL, // 5
77     NUMBER_FORMAT, // 6
78     NUMBER_DIGIT, // 7
79     TIME_SEPARATOR, // 8
80     DEFAULT_HOUR, // 9
81     GREGORIAN_STANDALONE_ABBR_MONTH, // 10
82     GREGORIAN_STANDALONE_ABBR_DAY, // 11
83     GREGORIAN_FORMAT_WIDE_MONTH, // 12
84     GREGORIAN_HOUR_MINUTE_SECOND_PATTERN, // 13
85     GREGORIAN_FULL_MEDIUM_SHORT_PATTERN, // 14
86     GREGORIAN_FORMAT_WIDE_DAY, // 15
87     GREGORIAN_STANDALONE_WIDE_DAY, // 16
88     GREGORIAN_STANDALONE_WIDE_MONTH, // 17
89     ELAPSED_PATERNS, // 18
90     WEEK_DATA, // 19
91     DECIMAL_PLURAL, // 20
92     MINUS_SIGN, // 21
93     RESOURCE_TYPE_END // 22
94 };
95 
96 class DataResource {
97 public:
98     explicit DataResource(const LocaleInfo *localeInfo);
99     bool Init();
100     char *GetString(DataResourceType type) const;
101     virtual ~DataResource();
102 
103 private:
104     static uint32_t GetFallbackMask(const LocaleInfo &src);
105     bool ReadHeader(int32_t infile);
106     bool PrepareData(int32_t infile);
107     int32_t BinarySearchLocale(const uint32_t mask, unsigned char *locales);
108     bool GetStringFromStringPool(char *configs, const uint32_t configsSize, int32_t infile, LocaleDataType type);
109     uint32_t ConvertUint(unsigned char *src);
110     uint32_t ConvertUChar(unsigned char *src);
111     char *GetString2(DataResourceType type) const;
112     char *GetString(uint32_t index) const;
113     char *BinarySearchString(uint32_t *indexArray, uint32_t length,
114         uint32_t target, char **stringArray, uint32_t stringLength) const;
115     LocaleInfo *GetFallbackLocaleInfo(const LocaleInfo &src);
116     void GetFallbackAndDefaultLocaleIndex(int32_t &fallbackLocaleIndex, int32_t &defaultLocaleIndex,
117         char *locales);
118     void GetFallbackAndDefaultInfo(const int32_t &fallbackLocaleIndex, const int32_t &defaultLocaleIndex,
119         uint32_t &fallbackConfigOffset, uint32_t &defaultConfigOffset, char *locales);
120     bool Retrieve(char *configs, const uint32_t configsSize, int32_t infile, const uint32_t originalCount,
121         LocaleDataType type);
122     bool PrepareLocaleData(int32_t infile, uint32_t configOffset, uint32_t count, LocaleDataType type);
123     bool FullLoaded();
124     void GetType(char** &adjustResource, uint32_t* &adjustResourceIndex, uint32_t &count, LocaleDataType type);
125     uint32_t GetFinalCount(char *configs, uint32_t configSize, LocaleDataType type);
126     void FreeResource();
127     bool IsTypeNeeded(int32_t index, uint32_t count);
128     bool ApplyForResource(uint32_t **index, char ***wanted, uint32_t totalCount);
129     uint32_t localeMask = 0;
130     uint32_t fallbackMask = 0;
131     uint32_t defaultMask = 0;
132     uint32_t localesCount = 0;
133     uint32_t stringPoolOffset = 0;
134     uint32_t *resourceIndex = nullptr;
135     uint32_t *defaultResourceIndex = nullptr;
136     char **resource = nullptr;
137     uint32_t resourceCount = 0;
138     uint32_t fallbackResourceCount = 0;
139     uint32_t defaultResourceCount = 0;
140     LocaleDataType currentType = LocaleDataType::DEFAULT_RESOURCE;
141     uint32_t *fallbackResourceIndex = nullptr;
142     char **fallbackResource = nullptr;
143     char **defaultResource = nullptr;
144     uint32_t loaded[DataResourceType::RESOURCE_TYPE_END] = { 0 };
145 };
146 } // I18N
147 } // OHOS
148 #endif