160916893Sopenharmony_ci/* 260916893Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 360916893Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 460916893Sopenharmony_ci * you may not use this file except in compliance with the License. 560916893Sopenharmony_ci * You may obtain a copy of the License at 660916893Sopenharmony_ci * 760916893Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 860916893Sopenharmony_ci * 960916893Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1060916893Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1160916893Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1260916893Sopenharmony_ci * See the License for the specific language governing permissions and 1360916893Sopenharmony_ci * limitations under the License. 1460916893Sopenharmony_ci */ 1560916893Sopenharmony_ci 1660916893Sopenharmony_ci#ifndef DATA_RESOURCE_IMPL_H 1760916893Sopenharmony_ci#define DATA_RESOURCE_IMPL_H 1860916893Sopenharmony_ci 1960916893Sopenharmony_ci#include <sys/stat.h> 2060916893Sopenharmony_ci#include <sys/types.h> 2160916893Sopenharmony_ci#include <unistd.h> 2260916893Sopenharmony_ci#include "locale_info.h" 2360916893Sopenharmony_ci#include "fcntl.h" 2460916893Sopenharmony_ci#include "types.h" 2560916893Sopenharmony_ci 2660916893Sopenharmony_ci#define GLOBAL_RESOURCE_HEADER_SIZE 16 2760916893Sopenharmony_ci#define GLOBAL_RESOURCE_HEADER_LEFT 4 2860916893Sopenharmony_ci#define GLOBAL_RESOURCE_HEADER_SKIP 12 2960916893Sopenharmony_ci#define GLOBAL_RESOURCE_INDEX_OFFSET 2 3060916893Sopenharmony_ci#define GLOBAL_RESOURCE_MASK_OFFSET 4 3160916893Sopenharmony_ci#define GLOBAL_LOCALE_MASK_ITEM_SIZE 8 3260916893Sopenharmony_ci#define GLOBAL_RESOURCE_CONFIG_SIZE 6 3360916893Sopenharmony_ci#define MONTH_SEP '_' 3460916893Sopenharmony_ci#define DAY_SEP '_' 3560916893Sopenharmony_ci#define PATTERN_SEP '_' 3660916893Sopenharmony_ci#define AMPM_SEP '_' 3760916893Sopenharmony_ci#define NUM_PATTERN_SIZE 5 3860916893Sopenharmony_ci#define NUM_PAT_INDEX 0 3960916893Sopenharmony_ci#define NUM_PERCENT_PAT_INDEX 1 4060916893Sopenharmony_ci#define NUM_DEC_SIGN_INDEX 2 4160916893Sopenharmony_ci#define NUM_GROUP_SIGN_INDEX 3 4260916893Sopenharmony_ci#define NUM_PERCENT_SIGN_INDEX 4 4360916893Sopenharmony_ci#define NUM_PATTERN_SEP '_' 4460916893Sopenharmony_ci#define NUM_DIGIT_SEP ';' 4560916893Sopenharmony_ci#define PLURAL_SEP '_' 4660916893Sopenharmony_ci#define NUM_DIGIT_SIZE 10 4760916893Sopenharmony_ci#define RULES_NUM 6 4860916893Sopenharmony_ci#define RESOURCE_INDEX 0 4960916893Sopenharmony_ci#define FALLBACK_RESOURCE_INDEX 1 5060916893Sopenharmony_ci#define DEFAULT_RESOURCE_INDEX 2 5160916893Sopenharmony_ci#define SHIFT_ONE_BYTE 8 5260916893Sopenharmony_ci#define SHIFT_TWO_BYTE 16 5360916893Sopenharmony_ci#define SHIFT_THREE_BYTE 24 5460916893Sopenharmony_ci#define MAX_LOCALE_ITEM_SIZE 500 5560916893Sopenharmony_ci#define MEASURE_BASE_ITEM_COUNT 4 5660916893Sopenharmony_ci#define MEASURE_SINGLE_UNIT_COUNT 24 5760916893Sopenharmony_ci#define MEASURE_FORMAT_TYPE_NUM 4 5860916893Sopenharmony_ci#define MEASURE_PLURAL_NUM 6 5960916893Sopenharmony_ci#define MAX_MEASURE_FORMAT_LENGTH 128 6060916893Sopenharmony_ci#define MEASURE_UNIT_SEP '|' 6160916893Sopenharmony_ci 6260916893Sopenharmony_cinamespace OHOS { 6360916893Sopenharmony_cinamespace I18N { 6460916893Sopenharmony_cienum LocaleDataType { 6560916893Sopenharmony_ci RESOURCE = 0, 6660916893Sopenharmony_ci FALLBACK_RESOURCE, 6760916893Sopenharmony_ci DEFAULT_RESOURCE 6860916893Sopenharmony_ci}; 6960916893Sopenharmony_ci 7060916893Sopenharmony_cienum DataResourceType { 7160916893Sopenharmony_ci RESOURCE_TYPE_BEGIN = 0, 7260916893Sopenharmony_ci GREGORIAN_FORMAT_ABBR_MONTH = RESOURCE_TYPE_BEGIN, // 0 7360916893Sopenharmony_ci GREGORIAN_FORMAT_ABBR_DAY, // 1 7460916893Sopenharmony_ci GREGORIAN_TIME_PATTERNS, // 2 7560916893Sopenharmony_ci GREGORIAN_DATE_PATTERNS, // 3 7660916893Sopenharmony_ci GREGORIAN_AM_PMS, // 4 7760916893Sopenharmony_ci PLURAL, // 5 7860916893Sopenharmony_ci NUMBER_FORMAT, // 6 7960916893Sopenharmony_ci NUMBER_DIGIT, // 7 8060916893Sopenharmony_ci TIME_SEPARATOR, // 8 8160916893Sopenharmony_ci DEFAULT_HOUR, // 9 8260916893Sopenharmony_ci GREGORIAN_STANDALONE_ABBR_MONTH, // 10 8360916893Sopenharmony_ci GREGORIAN_STANDALONE_ABBR_DAY, // 11 8460916893Sopenharmony_ci GREGORIAN_FORMAT_WIDE_MONTH, // 12 8560916893Sopenharmony_ci GREGORIAN_HOUR_MINUTE_SECOND_PATTERN, // 13 8660916893Sopenharmony_ci GREGORIAN_FULL_MEDIUM_SHORT_PATTERN, // 14 8760916893Sopenharmony_ci GREGORIAN_FORMAT_WIDE_DAY, // 15 8860916893Sopenharmony_ci GREGORIAN_STANDALONE_WIDE_DAY, // 16 8960916893Sopenharmony_ci GREGORIAN_STANDALONE_WIDE_MONTH, // 17 9060916893Sopenharmony_ci ELAPSED_PATERNS, // 18 9160916893Sopenharmony_ci WEEK_DATA, // 19 9260916893Sopenharmony_ci DECIMAL_PLURAL, // 20 9360916893Sopenharmony_ci MINUS_SIGN, // 21 9460916893Sopenharmony_ci MEASURE_FORMAT_PATTERNS, // 22 9560916893Sopenharmony_ci RESOURCE_TYPE_END // 23 9660916893Sopenharmony_ci}; 9760916893Sopenharmony_ci 9860916893Sopenharmony_ciclass DataResource { 9960916893Sopenharmony_cipublic: 10060916893Sopenharmony_ci explicit DataResource(const LocaleInfo *localeInfo); 10160916893Sopenharmony_ci bool Init(); 10260916893Sopenharmony_ci char *GetString(DataResourceType type) const; 10360916893Sopenharmony_ci virtual ~DataResource(); 10460916893Sopenharmony_ci void GetString(DataResourceType type, std::string &ret) const; 10560916893Sopenharmony_ci 10660916893Sopenharmony_ciprivate: 10760916893Sopenharmony_ci static uint32_t GetFallbackMask(const LocaleInfo &src); 10860916893Sopenharmony_ci bool ReadHeader(int32_t infile); 10960916893Sopenharmony_ci bool PrepareData(int32_t infile); 11060916893Sopenharmony_ci int32_t BinarySearchLocale(const uint32_t mask, unsigned char *locales); 11160916893Sopenharmony_ci bool GetStringFromStringPool(char *configs, const uint32_t configsSize, int32_t infile, LocaleDataType type); 11260916893Sopenharmony_ci uint32_t ConvertUint(unsigned char *src); 11360916893Sopenharmony_ci uint32_t ConvertUChar(unsigned char *src); 11460916893Sopenharmony_ci char *GetString2(DataResourceType type) const; 11560916893Sopenharmony_ci char *GetString(uint32_t index) const; 11660916893Sopenharmony_ci char *BinarySearchString(uint32_t *indexArray, uint32_t length, 11760916893Sopenharmony_ci uint32_t target, char **stringArray, uint32_t stringLength) const; 11860916893Sopenharmony_ci LocaleInfo *GetFallbackLocaleInfo(const LocaleInfo &src); 11960916893Sopenharmony_ci void GetFallbackAndDefaultLocaleIndex(int32_t &fallbackLocaleIndex, int32_t &defaultLocaleIndex, 12060916893Sopenharmony_ci char *locales); 12160916893Sopenharmony_ci void GetFallbackAndDefaultInfo(const int32_t &fallbackLocaleIndex, const int32_t &defaultLocaleIndex, 12260916893Sopenharmony_ci uint32_t &fallbackConfigOffset, uint32_t &defaultConfigOffset, char *locales); 12360916893Sopenharmony_ci bool Retrieve(char *configs, const uint32_t configsSize, int32_t infile, const uint32_t originalCount, 12460916893Sopenharmony_ci LocaleDataType type); 12560916893Sopenharmony_ci bool PrepareLocaleData(int32_t infile, uint32_t configOffset, uint32_t count, LocaleDataType type); 12660916893Sopenharmony_ci bool FullLoaded(); 12760916893Sopenharmony_ci void GetType(char** &adjustResource, uint32_t* &adjustResourceIndex, uint32_t &count, LocaleDataType type); 12860916893Sopenharmony_ci uint32_t GetFinalCount(char *configs, uint32_t configSize, LocaleDataType type); 12960916893Sopenharmony_ci void FreeResource(); 13060916893Sopenharmony_ci bool IsTypeNeeded(int32_t index, uint32_t count); 13160916893Sopenharmony_ci bool ApplyForResource(uint32_t **index, char ***wanted, uint32_t totalCount); 13260916893Sopenharmony_ci uint32_t localeMask = 0; 13360916893Sopenharmony_ci uint32_t fallbackMask = 0; 13460916893Sopenharmony_ci uint32_t defaultMask = 0; 13560916893Sopenharmony_ci uint32_t localesCount = 0; 13660916893Sopenharmony_ci uint32_t stringPoolOffset = 0; 13760916893Sopenharmony_ci uint32_t *resourceIndex = nullptr; 13860916893Sopenharmony_ci uint32_t *defaultResourceIndex = nullptr; 13960916893Sopenharmony_ci char **resource = nullptr; 14060916893Sopenharmony_ci uint32_t resourceCount = 0; 14160916893Sopenharmony_ci uint32_t fallbackResourceCount = 0; 14260916893Sopenharmony_ci uint32_t defaultResourceCount = 0; 14360916893Sopenharmony_ci LocaleDataType currentType = LocaleDataType::DEFAULT_RESOURCE; 14460916893Sopenharmony_ci uint32_t *fallbackResourceIndex = nullptr; 14560916893Sopenharmony_ci char **fallbackResource = nullptr; 14660916893Sopenharmony_ci char **defaultResource = nullptr; 14760916893Sopenharmony_ci uint32_t loaded[DataResourceType::RESOURCE_TYPE_END] = { 0 }; 14860916893Sopenharmony_ci}; 14960916893Sopenharmony_ci} // namespace I18N 15060916893Sopenharmony_ci} // namespace OHOS 15160916893Sopenharmony_ci#endif