1 /* 2 * Copyright (c) 2021-2025 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 #ifndef OHOS_RESOURCE_MANAGER_UTILS_H 16 #define OHOS_RESOURCE_MANAGER_UTILS_H 17 #include <cstddef> 18 #include <cstdint> 19 #include "res_locale.h" 20 #include "rstate.h" 21 #include <dirent.h> 22 #include <set> 23 #include <vector> 24 #include "res_common.h" 25 26 namespace OHOS { 27 namespace Global { 28 namespace Resource { 29 enum BitOperatorNum { 30 BIT_TWO = 2, 31 BIT_THREE = 3, 32 BIT_FOUR = 4, 33 BIT_FIVE = 5, 34 BIT_SIX = 6, 35 BIT_EIGHT = 8, 36 BIT_TWELVE = 12, 37 BIT_SIXTEEN = 16, 38 BIT_TWENTY = 20, 39 BIT_TWENTY_FOUR = 24, 40 BIT_TWENTY_EIGHT = 28, 41 BIT_FORTY_EIGHT = 48 42 }; 43 44 enum ArrayIndex { 45 INDEX_ZERO = 0, 46 INDEX_ONE = 1, 47 INDEX_TWO = 2, 48 INDEX_THREE = 3, 49 INDEX_FOUR = 4, 50 INDEX_FIVE = 5, 51 INDEX_SIX = 6, 52 INDEX_SEVEN = 7, 53 INDEX_EIGHT = 8 54 }; 55 56 enum ArrayLen { 57 LEN_THREE = 3, 58 LEN_FOUR = 4, 59 LEN_FIVE = 5, 60 LEN_SEVEN = 7, 61 LEN_NINE = 9 62 }; 63 class Utils { 64 public: 65 static bool IsAlphaString(const char *s, int32_t len); 66 67 static bool IsNumericString(const char *s, int32_t len); 68 69 static bool IsStrEmpty(const char *s); 70 71 static size_t StrLen(const char *s); 72 73 static uint16_t EncodeLanguage(const char *language); 74 75 static uint16_t EncodeLanguageByResLocale(const ResLocale *locale); 76 77 static uint32_t EncodeScript(const char *script); 78 79 static uint32_t EncodeScriptByResLocale(const ResLocale *locale); 80 81 static void DecodeScript(uint32_t encodeScript, char *outValue); 82 83 static uint16_t EncodeRegion(const char *region); 84 85 static uint16_t EncodeRegionByResLocale(const ResLocale *locale); 86 87 static uint64_t EncodeLocale(const char *language, 88 const char *script, 89 const char *region); 90 91 static RState ConvertColorToUInt32(const char *s, uint32_t &outValue); 92 93 static std::unique_ptr<uint8_t[]> LoadResourceFile(const std::string &path, size_t &len); 94 95 static RState EncodeBase64(std::unique_ptr<uint8_t[]> &data, int srcLen, 96 const std::string &imgType, std::string &outValue); 97 98 static constexpr float DPI_BASE = 160.0f; 99 100 static bool endWithTail(const std::string& path, const std::string& tail); 101 102 static bool IsFileExist(const std::string& filePath); 103 104 static bool ContainsTail(std::string hapPath, std::set<std::string> tailSet); 105 106 static const std::set<std::string> tailSet; 107 108 static RState GetMediaBase64Data(const std::string& iconPath, std::string &base64Data); 109 110 static void CanonicalizePath(const char *path, char *outPath, size_t len); 111 112 static RState GetFiles(const std::string &strCurrentDir, std::vector<std::string> &vFiles); 113 114 static RState GetFilesForWin(const std::string &strCurrentDir, std::vector<std::string> &vFiles); 115 116 static bool convertToInteger(const std::string& str, int& outValue); 117 118 static bool convertToUnsignedLong(const std::string& str, unsigned long& outValue); 119 120 static bool convertToDouble(const std::string& str, double& outValue); 121 122 static bool IsSystemPath(const std::string& path); 123 124 EXPORT_FUNC static std::string GetSystemParameter(const std::string& paramKey); 125 private: 126 static bool IsValidValue(const char* end, const std::string& str); 127 128 static uint16_t EncodeLanguageOrRegion(const char *str, char base); 129 130 static constexpr uint64_t ROOT_LOCALE = 0x0; 131 static constexpr uint16_t NULL_LANGUAGE = 0x00; 132 static constexpr uint16_t NULL_REGION = 0x00; 133 static constexpr uint16_t NULL_SCRIPT = 0x0000; 134 static constexpr uint64_t NULL_LOCALE = 0x0; 135 }; 136 } // namespace Resource 137 } // namespace Global 138 } // namespace OHOS 139 #endif