From e12a432e4b6fcf558c042764f7dcdefcb04bef1f Mon Sep 17 00:00:00 2001 From: huangjie Date: Thu, 24 Mar 2022 14:34:18 +0800 Subject: [PATCH] =?UTF-8?q?L1=E7=BC=96=E7=A0=81=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huangjie --- frameworks/resmgr_lite/include/hap_manager.h | 47 +++++ frameworks/resmgr_lite/include/hap_parser.h | 28 +++ frameworks/resmgr_lite/include/hap_resource.h | 28 ++- .../resmgr_lite/include/locale_matcher.h | 36 ++++ .../resmgr_lite/include/res_config_impl.h | 49 ++++- frameworks/resmgr_lite/include/res_desc.h | 5 + frameworks/resmgr_lite/include/res_locale.h | 25 +++ .../include/resource_manager_impl.h | 186 ++++++++++++++++++ frameworks/resmgr_lite/src/locale_matcher.cpp | 6 +- .../resmgr_lite/src/res_config_impl.cpp | 2 +- frameworks/resmgr_lite/src/res_locale.cpp | 2 +- .../resmgr_lite/src/resource_manager_impl.cpp | 4 +- .../unittest/lite/common/res_config_test.cpp | 2 +- 13 files changed, 410 insertions(+), 10 deletions(-) diff --git a/frameworks/resmgr_lite/include/hap_manager.h b/frameworks/resmgr_lite/include/hap_manager.h index 6b9cc2e..721b6c6 100644 --- a/frameworks/resmgr_lite/include/hap_manager.h +++ b/frameworks/resmgr_lite/include/hap_manager.h @@ -29,24 +29,71 @@ namespace Global { namespace Resource { class HapManager { public: + /** + * The constructor of HapManager + */ explicit HapManager(ResConfigImpl *resConfig); + /** + * The destructor of HapManager + */ ~HapManager(); + /** + * Update the resConfig + * @param resConfig the resource config + * @return the resConfig after update + */ RState UpdateResConfig(ResConfig &resConfig); + /** + * Get the resConfig + * @param resConfig the resource config + */ void GetResConfig(ResConfig &resConfig); + /** + * Add resource path to hap paths + * @param path the resource path + * @return true if add resource path success, else false + */ bool AddResource(const char *path); + /** + * Find resource by resource id + * @param id the resource id + * @return the resources corresponding resource id + */ const IdItem *FindResourceById(uint32_t id); + /** + * Find resource by resource name + * @param name the resource name + * @param resType the resource type + * @return the resources corresponding resource name + */ const IdItem *FindResourceByName(const char *name, const ResType resType); + /** + * Find best resource path by resource id + * @param id the resource id + * @return the best resource path + */ const HapResource::ValueUnderQualifierDir *FindQualifierValueById(uint32_t id); + /** + * Find best resource path by resource name + * @param name the resource name + * @param resType the resource type + * @return the best resource path + */ const HapResource::ValueUnderQualifierDir *FindQualifierValueByName(const char *name, const ResType resType); + /** + * Get the language pluralRule corresponding to quantity + * @param quantity the language quantity + * @return the language pluralRule corresponding to quantity + */ std::string GetPluralRulesAndSelect(int quantity); private: diff --git a/frameworks/resmgr_lite/include/hap_parser.h b/frameworks/resmgr_lite/include/hap_parser.h index fdd5ac9..ec7f31e 100644 --- a/frameworks/resmgr_lite/include/hap_parser.h +++ b/frameworks/resmgr_lite/include/hap_parser.h @@ -51,15 +51,43 @@ public: static int32_t ReadIndexFromFile(const char *zipFile, void **buffer, size_t &bufLen, std::string &errInfo); + /** + * Parse resource hex to resDesc + * @param buffer the resource bytes + * @param bufLen length in bytes + * @param resDesc index file in hap + * @param defaultConfig the default config + * @return OK if the resource hex parse success, else SYS_ERROR + */ static int32_t ParseResHex(const char *buffer, const size_t bufLen, ResDesc &resDesc, const ResConfigImpl *defaultConfig = nullptr); + /** + * Create resource config from KeyParams + * @param keyParams the keyParams contain type and value + * @return the resource config corresponding the keyParams + */ static ResConfigImpl *CreateResConfigFromKeyParams(const std::vector &keyParams); + /** + * To resource folder path + * @param keyParams the keyParams contain type and value + * @return the resources folder path + */ static std::string ToFolderPath(const std::vector &keyParams); + /** + * Get screen density + * @param value the type of screen density + * @return the screen density corresponding the value + */ static ScreenDensity GetScreenDensity(uint32_t value); + /** + * Get device type + * @param value the type of device + * @return the device type corresponding the value + */ static DeviceType GetDeviceType(uint32_t value); private: diff --git a/frameworks/resmgr_lite/include/hap_resource.h b/frameworks/resmgr_lite/include/hap_resource.h index 0f5d6c9..1334210 100644 --- a/frameworks/resmgr_lite/include/hap_resource.h +++ b/frameworks/resmgr_lite/include/hap_resource.h @@ -43,20 +43,29 @@ public: */ static const HapResource *LoadFromIndex(const char *path, const ResConfigImpl *defaultConfig, bool system = false); + /** + * The destructor of HapResource + */ ~HapResource(); + /** + * Get the resource.index file path + */ inline const std::string GetIndexPath() const { return indexPath_; } + /** + * Get the resource path + */ inline const std::string GetResourcePath() const { return resourcePath_; } /** - * describe limitpath and value under the path + * Describe limitpath and value under the path */ class ValueUnderQualifierDir { public: @@ -133,10 +142,27 @@ public: std::vector limitPaths_; }; + /** + * Get the resource value by resource id + * @param id the resource id + * @return the rsource value corresponding id + */ const IdValues *GetIdValues(const uint32_t id) const; + /** + * Get the resource value by resource name + * @param name the resource name + * @param resType the resource type + * @return the rsource value corresponding resource name + */ const IdValues *GetIdValuesByName(const std::string name, const ResType resType) const; + /** + * Get the resource id by resource name + * @param name the resource name + * @param resType the resource type + * @return the resource id corresponding resource name + */ int GetIdByName(const char *name, const ResType resType) const; size_t IdSize() const diff --git a/frameworks/resmgr_lite/include/locale_matcher.h b/frameworks/resmgr_lite/include/locale_matcher.h index 4e92c92..e997b98 100644 --- a/frameworks/resmgr_lite/include/locale_matcher.h +++ b/frameworks/resmgr_lite/include/locale_matcher.h @@ -25,16 +25,52 @@ public: const ResLocale *other, const ResLocale *request); + /** + * Whether the current ResLocale same to the other ResLocale + * @param current the current ResLocale + * @param other the other ResLocale + * @return true if the current ResLocale same to the other ResLocale, else false + */ static bool Match(const ResLocale *current, const ResLocale *other); + /** + * Whether the string is the language tag + * @param str the tag string + * @param len the tag length + * @return true if the str is language tag, else false + */ static bool IsLanguageTag(const char *str, int32_t len); + /** + * Whether the string is the script tag + * @param str the tag string + * @param len the tag length + * @return true if the str is script tag, else false + */ static bool IsScriptTag(const char *str, int32_t len); + /** + * Whether the string is the Region tag + * @param str the tag string + * @param len the tag length + * @return true if the str is Region tag, else false + */ static bool IsRegionTag(const char *str, int32_t len); + /** + * Get the resLocale script + * @param resLocale the resLocale infomation + * @return true if get the resLocale script, else false + */ static bool Normalize(ResLocale *resLocale); + /** + * Whether the current resLocale more specific than target resLocale + * @param current the current resLocale + * @param target the target resLocale + * @return 0 means current resLocale same to target resLocale, + * 1 means current resLocale more specific than target resLocale, else -1 + */ static int8_t IsMoreSpecificThan(const ResLocale *current, const ResLocale *target); public: diff --git a/frameworks/resmgr_lite/include/res_config_impl.h b/frameworks/resmgr_lite/include/res_config_impl.h index c8ca03c..49639f7 100644 --- a/frameworks/resmgr_lite/include/res_config_impl.h +++ b/frameworks/resmgr_lite/include/res_config_impl.h @@ -29,16 +29,46 @@ class ResConfigImpl : public ResConfig { public: ResConfigImpl(); + /** + * Whether the this resConfig more match request resConfig + * @param other the other resConfig + * @param request the request resConfig + * @return true if this resConfig more match request resConfig than other resConfig, else false + */ bool IsMoreSuitable(const ResConfigImpl *other, const ResConfigImpl *request) const; + /** + * Set locale information + * @param language the locale language + * @param script the locale script + * @param region the locale region + * @return SUCCESS if set locale information success, else false + */ RState SetLocaleInfo(const char *language, const char *script, const char *region); - + + /** + * Set locale information + * @param localeInfo the localeInfo + * @return SUCCESS if set locale information success, else false + */ RState SetLocaleInfo(LocaleInfo &localeInfo); + /** + * Set resConfig device type + * @param deviceType the device type + */ void SetDeviceType(DeviceType deviceType); + /** + * Set resConfig direction + * @param direction the resConfig direction + */ void SetDirection(Direction direction); + /** + * Set resConfig screenDensity + * @param screenDensity the resConfig screenDensity + */ void SetScreenDensity(ScreenDensity screenDensity); const LocaleInfo *GetLocaleInfo() const; @@ -51,12 +81,29 @@ public: DeviceType GetDeviceType() const; + /** + * Whether this resConfig match other resConfig + * @param other the other resConfig + * @return true if this resConfig match other resConfig, else false + */ bool Match(const ResConfigImpl *other) const; + /** + * Copy this resConfig match other resConfig + * @param other the other resConfig + * @return true if copy other resConfig to this resConfig success, else false + */ bool Copy(ResConfig &other); + /** + * Complete the local script + */ void CompleteScript(); + /** + * Whether the script completed + * @return true if copy other resConfig to this resConfig success, else false + */ bool IsCompletedScript() const; virtual ~ResConfigImpl(); diff --git a/frameworks/resmgr_lite/include/res_desc.h b/frameworks/resmgr_lite/include/res_desc.h index 15ac4aa..f335659 100644 --- a/frameworks/resmgr_lite/include/res_desc.h +++ b/frameworks/resmgr_lite/include/res_desc.h @@ -48,6 +48,11 @@ class IdItem { public: static const uint32_t HEADER_LEN = 12; + /** + * Whether the resType is array or not + * @param type the resType + * @return true if the resType is array, else false + */ static bool IsArrayOfType(ResType type) { if (type == ResType::STRINGARRAY || type == ResType::INTARRAY || type == ResType::THEME || diff --git a/frameworks/resmgr_lite/include/res_locale.h b/frameworks/resmgr_lite/include/res_locale.h index 832eb7c..0851727 100644 --- a/frameworks/resmgr_lite/include/res_locale.h +++ b/frameworks/resmgr_lite/include/res_locale.h @@ -45,16 +45,41 @@ public: ResLocale(); + /** + * Copy from other LocaleInfo to this + * @param other the other LocaleInfo copy to this localeInfo + * @return SUCCESS if copy other LocaleInfo success, else ERROR + */ RState CopyFromLocaleInfo(const LocaleInfo *other); + /** + * Copy from other ResLocale to this + * @param other the other ResLocale copy to this ResLocale + * @return SUCCESS if copy other ResLocale success, else ERROR + */ RState Copy(const ResLocale *other); static const LocaleInfo *GetDefault(); static bool UpdateDefault(const LocaleInfo &localeInfo, bool needNotify); + /** + * Build resLocal from string + * @param bcp47String the target string + * @param sep the parse string position + * @param rState the parse status + * @return the resLocal after parse bcp47String + */ static ResLocale *BuildFromString(const char *bcp47String, char sep, RState &rState); + /** + * Build resLocal from parts + * @param language the resLocal language + * @param script the resLocal script + * @param region the resLocal region + * @param rState the Build status + * @return the resLocal after Build resLocal from parts if success, else return nullptr + */ static ResLocale *BuildFromParts(const char *language, const char *script, const char *region, RState &rState); ~ResLocale(); diff --git a/frameworks/resmgr_lite/include/resource_manager_impl.h b/frameworks/resmgr_lite/include/resource_manager_impl.h index f402e8c..d39f8c4 100644 --- a/frameworks/resmgr_lite/include/resource_manager_impl.h +++ b/frameworks/resmgr_lite/include/resource_manager_impl.h @@ -32,66 +32,252 @@ public: bool Init(); + /** + * Add resource path to hap paths + * @param path the resource path + * @return true if add resource path success, else false + */ virtual bool AddResource(const char *path); + /** + * Update the resConfig + * @param resConfig the resource config + * @return the resConfig after update + */ virtual RState UpdateResConfig(ResConfig &resConfig); + /** + * Get the resConfig + * @param resConfig the resource config + */ virtual void GetResConfig(ResConfig &resConfig); + /** + * Get string resource by Id + * @param id the resource Id + * @param outValue the string resource write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetStringById(uint32_t id, std::string &outValue); + /** + * Get string by resource name + * @param name the resource name + * @param outValue the resource write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetStringByName(const char *name, std::string &outValue); + /** + * Get formatstring by resource id + * @param id the resource id + * @param outValue the resource write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetStringFormatById(std::string &outValue, uint32_t id, ...); + /** + * Get formatstring by resource name + * @param name the resource name + * @param outValue the resource write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetStringFormatByName(std::string &outValue, const char *name, ...); + /** + * Get the ResType is STRINGARRAY resource by resource id + * @param id the resource id + * @param outValue the resource write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetStringArrayById(uint32_t id, std::vector &outValue); + /** + * Get the ResType is STRINGARRAY resource by resource name + * @param name the resource name + * @param outValue the resource write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetStringArrayByName(const char *name, std::vector &outValue); + /** + * Get the ResType is PATTERN resource by resource id + * @param id the resource id + * @param outValue the resource write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetPatternById(uint32_t id, std::map &outValue); + /** + * Get the ResType is PATTERN resource by resource name + * @param name the resource name + * @param outValue the resource write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetPatternByName(const char *name, std::map &outValue); + /** + * Get the plural string by resource id + * @param id the resource id + * @param quantity the language quantity + * @param outValue the resource write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetPluralStringById(uint32_t id, int quantity, std::string &outValue); + /** + * Get the plural string by resource name + * @param name the resource name + * @param quantity the language quantity + * @param outValue the resource write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetPluralStringByName(const char *name, int quantity, std::string &outValue); + /** + * Get the plural format string by resource id + * @param outValue the resource write to + * @param id the resource id + * @param quantity the language quantity + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetPluralStringByIdFormat(std::string &outValue, uint32_t id, int quantity, ...); + /** + * Get the plural format string by resource name + * @param outValue the resource write to + * @param id the resource id + * @param quantity the language quantity + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetPluralStringByNameFormat(std::string &outValue, const char *name, int quantity, ...); + /** + * Get the ResType is THEME resource by resource id + * @param id the resource id + * @param outValue the resource write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetThemeById(uint32_t id, std::map &outValue); + /** + * Get the ResType is THEME resource by resource name + * @param name the resource name + * @param outValue the resource write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetThemeByName(const char *name, std::map &outValue); + /** + * Get the ResType is BOOLEAN resource by resource id + * @param id the resource id + * @param outValue the obtain boolean value write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetBooleanById(uint32_t id, bool &outValue); + /** + * Get the ResType is BOOLEAN resource by resource name + * @param name the resource name + * @param outValue the obtain boolean value write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetBooleanByName(const char *name, bool &outValue); + /** + * Get the ResType is INTEGER resource by resource id + * @param id the resource id + * @param outValue the obtain Integer value write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetIntegerById(uint32_t id, int &outValue); + /** + * Get the ResType is INTEGER resource by resource name + * @param name the resource name + * @param outValue the obtain Integer value write to + * @return SUCCESS if resource path exist, else NOT_FOUND + */ virtual RState GetIntegerByName(const char *name, int &outValue); + /** + * Get the ResType is FLOAT resource by resource id + * @param id the resource id + * @param outValue the obtain float value write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetFloatById(uint32_t id, float &outValue); + /** + * Get the ResType is FLOAT resource by resource name + * @param name the resource name + * @param outValue the obtain float value write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetFloatByName(const char *name, float &outValue); + /** + * Get the ResType is INTARRAY resource by resource id + * @param id the resource id + * @param outValue the obtain resource value convert to vector write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetIntArrayById(uint32_t id, std::vector &outValue); + /** + * Get the ResType is INTARRAY resource by resource name + * @param name the resource name + * @param outValue the obtain resource value convert to vector write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetIntArrayByName(const char *name, std::vector &outValue); + /** + * Get the ResType is COLOR resource by resource id + * @param id the resource id + * @param outValue the obtain resource value convert to uint32_t write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetColorById(uint32_t id, uint32_t &outValue); + /** + * Get the ResType is COLOR resource by resource name + * @param name the resource name + * @param outValue the obtain resource value convert to uint32_t write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ virtual RState GetColorByName(const char *name, uint32_t &outValue); + /** + * Get the ResType is PROF resource by resource id + * @param id the resource id + * @param outValue the obtain resource path write to + * @return SUCCESS if resource path exist, else NOT_FOUND + */ virtual RState GetProfileById(uint32_t id, std::string &outValue); + /** + * Get the ResType is PROF resource by resource name + * @param name the resource name + * @param outValue the obtain resource path write to + * @return SUCCESS if resource path exist, else NOT_FOUND + */ virtual RState GetProfileByName(const char *name, std::string &outValue); + /** + * Get the ResType is MEDIA resource by resource id + * @param id the resource id + * @param outValue the obtain resource path write to + * @return SUCCESS if resource path exist, else NOT_FOUND + */ virtual RState GetMediaById(uint32_t id, std::string &outValue); + /** + * Get the ResType is PROF resource by resource name + * @param name the resource name + * @param outValue the obtain resource path write to + * @return SUCCESS if resource path exist, else NOT_FOUND + */ virtual RState GetMediaByName(const char *name, std::string &outValue); private: diff --git a/frameworks/resmgr_lite/src/locale_matcher.cpp b/frameworks/resmgr_lite/src/locale_matcher.cpp index b54f059..2d197df 100644 --- a/frameworks/resmgr_lite/src/locale_matcher.cpp +++ b/frameworks/resmgr_lite/src/locale_matcher.cpp @@ -525,7 +525,7 @@ bool LocaleMatcher::Normalize(ResLocale *localeInfo) } uint32_t encodedScript = FindDefaultScriptEncode(localeInfo->GetLanguage(), localeInfo->GetRegion()); - if (encodedScript == NULL_SCRIPT) { + if (encodedScript == LocaleMatcher::NULL_SCRIPT) { return true; } char *tempScript = new(std::nothrow) char[SCRIPT_ARRAY_LEN]; @@ -609,10 +609,10 @@ int8_t LocaleMatcher::IsMoreSuitable(const ResLocale *current, return 0; } bool isLangEqual = CompareLanguage(current, other); - if (isLangEqual == false) { + if (!isLangEqual) { // current or other language is null, not null language is better bool result = CompareRegionWhenLangIsNotEqual(current, other, request); - return (result == true) ? 1 : -1; + return result ? 1 : -1; } uint16_t currentEncodedRegion = Utils::EncodeRegionByResLocale(current); diff --git a/frameworks/resmgr_lite/src/res_config_impl.cpp b/frameworks/resmgr_lite/src/res_config_impl.cpp index 950cfd7..a758bec 100644 --- a/frameworks/resmgr_lite/src/res_config_impl.cpp +++ b/frameworks/resmgr_lite/src/res_config_impl.cpp @@ -184,7 +184,7 @@ bool ResConfigImpl::Match(const ResConfigImpl *other) const if (other == nullptr) { return false; } - if (LocaleMatcher::Match(this->resLocale_, other->GetResLocale()) == false) { + if (!(LocaleMatcher::Match(this->resLocale_, other->GetResLocale()))) { return false; } if (this->direction_ != DIRECTION_NOT_SET && diff --git a/frameworks/resmgr_lite/src/res_locale.cpp b/frameworks/resmgr_lite/src/res_locale.cpp index 8313791..45e99a5 100644 --- a/frameworks/resmgr_lite/src/res_locale.cpp +++ b/frameworks/resmgr_lite/src/res_locale.cpp @@ -385,7 +385,7 @@ LocaleInfo *BuildFromParts(const char *language, const char *script, const char rState = INVALID_BCP47_LANGUAGE_SUBTAG; return nullptr; } - if (LocaleMatcher::IsLanguageTag(language, len) == false) { + if (!(LocaleMatcher::IsLanguageTag(language, len))) { rState = INVALID_BCP47_LANGUAGE_SUBTAG; return nullptr; } diff --git a/frameworks/resmgr_lite/src/resource_manager_impl.cpp b/frameworks/resmgr_lite/src/resource_manager_impl.cpp index c3956e0..ec0be78 100644 --- a/frameworks/resmgr_lite/src/resource_manager_impl.cpp +++ b/frameworks/resmgr_lite/src/resource_manager_impl.cpp @@ -451,7 +451,7 @@ RState ResourceManagerImpl::GetInteger(const IdItem *idItem, int &outValue) std::string temp; RState state = ResolveReference(idItem->value_, temp); if (state == SUCCESS) { - outValue = atoi(temp.c_str()); + outValue = stoi(temp); return SUCCESS; } return state; @@ -509,7 +509,7 @@ RState ResourceManagerImpl::GetIntArray(const IdItem *idItem, std::vector & HILOG_ERROR("ResolveReference failed, value:%s", idItem->values_[i].c_str()); return ERROR; } - outValue.push_back(atoi(resolvedValue.c_str())); + outValue.push_back(stoi(resolvedValue)); } return SUCCESS; } diff --git a/frameworks/resmgr_lite/test/unittest/lite/common/res_config_test.cpp b/frameworks/resmgr_lite/test/unittest/lite/common/res_config_test.cpp index a839f4d..0e25998 100644 --- a/frameworks/resmgr_lite/test/unittest/lite/common/res_config_test.cpp +++ b/frameworks/resmgr_lite/test/unittest/lite/common/res_config_test.cpp @@ -70,7 +70,7 @@ HWTEST_F(ResConfigTest, ResConfigFuncTest001, TestSize.Level1) ResConfigImpl *target = new ResConfigImpl; target->SetLocaleInfo("zh", nullptr, "CN"); EXPECT_TRUE(rc->Match(current)); - EXPECT_TRUE(rc->Match(target) == false); + EXPECT_TRUE(!(rc->Match(target))); delete target; delete current; delete rc; -- Gitee