1 /* 2 * Copyright (c) 2021-2024 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 OHOS_RESTOOL_RESOURCE_UTIL_H 17 #define OHOS_RESTOOL_RESOURCE_UTIL_H 18 19 #include <vector> 20 #include <cJSON.h> 21 #include "file_entry.h" 22 #include "resource_data.h" 23 24 namespace OHOS { 25 namespace Global { 26 namespace Restool { 27 class ResourceUtil { 28 public: 29 /** 30 * @brief split the string with given splitter. 31 * @param str: input string. 32 * @param out: the array of strings computed by splitter. 33 * @param splitter: the split string. 34 */ 35 static void Split(const std::string &str, std::vector<std::string> &out, const std::string &splitter); 36 37 /** 38 * @brief Replace sub-string in string 39 * @param sourceStr: The original string to operate on 40 * @param oldStr: The string to be replaced 41 * @param newStr: The new string used 42 */ 43 static void StringReplace(std::string &sourceStr, const std::string &oldStr, const std::string &newStr); 44 45 /** 46 * @brief check file exist. 47 * @param path: file path. 48 * @return true if exist, other false. 49 */ 50 static bool FileExist(const std::string &path); 51 52 /** 53 * @brief remove all files in the directory. 54 * @param path: input directory. 55 * @return true if remove success, other false. 56 */ 57 static bool RmoveAllDir(const std::string &path); 58 59 /** 60 * @brief remove file. 61 * @param path: input file. 62 * @return true if remove success, other false. 63 */ 64 static bool RmoveFile(const std::string &path); 65 66 /** 67 * @brief open json file. 68 * @param path: json file path. 69 * @param root: json root node 70 * @param printError: if true, print error message. 71 * @return true if open success, other false. 72 */ 73 static bool OpenJsonFile(const std::string &path, cJSON **root, const bool &printError = true); 74 75 /** 76 * @brief save json file. 77 * @param path: json file path. 78 * @param root: json root node 79 * @return true if save success, other false. 80 */ 81 static bool SaveToJsonFile(const std::string &path, const cJSON *root); 82 83 /** 84 * @brief get resource type from directory. 85 * @param name: directory name. 86 * @return resource type. 87 */ 88 static ResType GetResTypeByDir(const std::string &name); 89 90 /** 91 * @brief get all resource type directorys. 92 * @return all resource type. 93 */ 94 static std::string GetAllResTypeDirs(); 95 96 /** 97 * @brief ResType to string 98 * @param type: ResType 99 * @return resource type string. 100 */ 101 static std::string ResTypeToString(ResType type); 102 103 /** 104 * @brief get id name 105 * @param name; id name or file name 106 * @param type: ResType 107 * @return return id name. 108 */ 109 static std::string GetIdName(const std::string &name, ResType type); 110 111 /** 112 * @brief compose multi strings to string 113 * @param contents: multi strings 114 * @param addNull: if true, string length contains '\0'. 115 * @return return string, empty if error 116 */ 117 static std::string ComposeStrings(const std::vector<std::string> &contents, bool addNull = false); 118 119 /** 120 * @brief decompose string to multi strings 121 * @param content: string 122 * @return return string vector, empty if error 123 */ 124 static std::vector<std::string> DecomposeStrings(const std::string &content); 125 126 /** 127 * @brief string to ResType 128 * @param type: string 129 * @return return ResType 130 */ 131 static ResType GetResTypeFromString(const std::string &type); 132 133 /** 134 * @brief copy file 135 * @param src: source file path 136 * @param dst: destination file path 137 * @return true if success, other false 138 */ 139 static bool CopyFileInner(const std::string &src, const std::string &dst); 140 141 /** 142 * @brief create directories 143 * @param filePath: directory path 144 * @return true if success, other false 145 */ 146 static bool CreateDirs(const std::string &filePath); 147 148 /** 149 * @brief ignore file or directory 150 * @param filename: file or directory name 151 * @param isFile: ture if is file, other false 152 * @return true if ignore, other false 153 */ 154 static bool IsIgnoreFile(const std::string &filename, bool isFile); 155 156 /** 157 * @brief generate hash string 158 * @param key: string 159 * @return hash string 160 */ 161 static std::string GenerateHash(const std::string &key); 162 163 /** 164 * @brief get an absolute pathname 165 * @param path pathname 166 * @return absolut pathname 167 */ 168 static std::string RealPath(const std::string &path); 169 170 /** 171 * @brief check the directory is legal 172 * @param path pathname 173 * @return true is legal, other false; 174 */ 175 static bool IslegalPath(const std::string &path); 176 177 /** 178 * @brief get an keyParams for limitkey 179 * @param keyParams 180 * @return limitkey 181 */ 182 static std::string PaserKeyParam(const std::vector<KeyParam> &keyParams); 183 184 /** 185 * @brief Decimal to hexadecimal string 186 * @param int32_t Decimal 187 * @return Hexadecimal string 188 */ 189 static std::string DecToHexStr(const uint32_t id); 190 191 /** 192 * @brief Check hexadecimal string 193 * @param string Hexadecimal string 194 * @return ture Hexadecimal string is legal, other false; 195 */ 196 static bool CheckHexStr(const std::string &hex); 197 198 /** 199 * @brief get g_contentClusterMap key string 200 * @return All restype string 201 */ 202 static std::string GetAllRestypeString(); 203 204 /** 205 * @brief get \base\element dir 206 * @param string inputpath 207 * @return resource\base\element dir 208 */ 209 static FileEntry::FilePath GetBaseElementPath(const std::string input); 210 211 /** 212 * @brief get main dir 213 * @param string inputpath 214 * @return main dir 215 */ 216 static FileEntry::FilePath GetMainPath(const std::string input); 217 218 /** 219 * @brief Gets the standard size of icons under different qualifier phrases 220 * @param keyParams set of qualifiers 221 * @param uint32_t index 222 * @return standard size of the png 223 */ 224 static uint32_t GetNormalSize(const std::vector<KeyParam> &keyParams, uint32_t index); 225 226 /** 227 * @brief Check if the Unicode code belongs to the 15 plane or 16 plane 228 * @param int unicode 229 * @return ture Unicode code belongs to the 15 plane or 16 plane, other false; 230 */ 231 static bool isUnicodeInPlane15or16(int unicode); 232 233 /** 234 * @brief Remove spaces before and after strings 235 * @param str input string 236 */ 237 static void RemoveSpaces(std::string &str); 238 239 /** 240 * @brief Check whether the value is int 241 * @param snode cJSON node 242 */ 243 static bool IsIntValue(const cJSON *node); 244 245 /** 246 * @brief Check whether the resource name is valid 247 * @param str resource name 248 */ 249 static bool IsValidName(const std::string &name); 250 251 /** 252 * @brief print warning msg 253 * @param noBaseResource set of no base resources 254 */ 255 static void PrintWarningMsg(std::vector<std::pair<ResType, std::string>> &noBaseResource); 256 257 /** 258 * @brief covert KeyParam to str by keytype 259 * @param keyParam: KeyParam 260 * @return limit value string 261 */ 262 static std::string GetKeyParamValue(const KeyParam &KeyParam); 263 264 /** 265 * @brief keyType to string 266 * @param type: KeyType 267 * @return limit type string 268 */ 269 static std::string KeyTypeToStr(KeyType type); 270 271 /** 272 * @brief add ignore file regex pattern 273 * @param regex: the regex pattern 274 * @param ignoreType: the ignore file type 275 * @return if add succeed, return true 276 */ 277 static bool AddIgnoreFileRegex(const std::string ®ex, IgnoreType ignoreType); 278 279 /** 280 * @brief set whether use custom ignore regex pattern 281 * @param isUseCustomRegex: true is use custom ignore file regex 282 */ 283 static void SetUseCustomIgnoreRegex(const bool &isUseCustomRegex); 284 285 private: 286 static const std::map<std::string, IgnoreType> DEFAULT_IGNORE_FILE_REGEX; 287 static std::string GetLocaleLimitkey(const KeyParam &KeyParam); 288 static std::string GetDeviceTypeLimitkey(const KeyParam &KeyParam); 289 static std::string GetResolutionLimitkey(const KeyParam &KeyParam); 290 }; 291 } 292 } 293 } 294 #endif 295