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 OHOS_RESTOOL_RESOURCE_UTIL_H 17 #define OHOS_RESTOOL_RESOURCE_UTIL_H 18 19 #include<vector> 20 #include "resource_data.h" 21 #include "json/json.h" 22 23 namespace OHOS { 24 namespace Global { 25 namespace Restool { 26 class ResourceUtil { 27 public: 28 /** 29 * @brief split the string with given splitter. 30 * @param str: input string. 31 * @param out: the array of strings computed by splitter. 32 * @param splitter: the split string. 33 */ 34 static void Split(const std::string &str, std::vector<std::string> &out, const std::string &splitter); 35 36 /** 37 * @brief Replace sub-string in string 38 * @param sourceStr: The original string to operate on 39 * @param oldStr: The string to be replaced 40 * @param newStr: The new string used 41 */ 42 static void StringReplace(std::string &sourceStr, const std::string &oldStr, const std::string &newStr); 43 44 /** 45 * @brief check file exist. 46 * @param path: file path. 47 * @return true if exist, other false. 48 */ 49 static bool FileExist(const std::string &path); 50 51 /** 52 * @brief remove all files in the directory. 53 * @param path: input directory. 54 * @return true if remove success, other false. 55 */ 56 static bool RmoveAllDir(const std::string &path); 57 58 /** 59 * @brief open json file. 60 * @param path: json file path. 61 * @param root: json root node 62 * @return true if open success, other false. 63 */ 64 static bool OpenJsonFile(const std::string &path, Json::Value &root); 65 66 /** 67 * @brief save json file. 68 * @param path: json file path. 69 * @param root: json root node 70 * @return true if save success, other false. 71 */ 72 static bool SaveToJsonFile(const std::string &path, const Json::Value &root); 73 74 /** 75 * @brief get resource type from directory. 76 * @param name: directory name. 77 * @return resource type. 78 */ 79 static ResType GetResTypeByDir(const std::string &name); 80 81 /** 82 * @brief ResType to string 83 * @param type: ResType 84 * @return resource type string. 85 */ 86 static std::string ResTypeToString(ResType type); 87 88 /** 89 * @brief get id name 90 * @param name; id name or file name 91 * @param type: ResType 92 * @return return id name. 93 */ 94 static std::string GetIdName(const std::string &name, ResType type); 95 96 /** 97 * @brief compose multi strings to string 98 * @param contents: multi strings 99 * @param addNull: if true, string length contains '\0'. 100 * @return return string, empty if error 101 */ 102 static std::string ComposeStrings(const std::vector<std::string> &contents, bool addNull = false); 103 104 /** 105 * @brief decompose string to multi strings 106 * @param content: string 107 * @return return string vector, empty if error 108 */ 109 static std::vector<std::string> DecomposeStrings(const std::string &content); 110 111 /** 112 * @brief string to ResType 113 * @param type: string 114 * @return return ResType 115 */ 116 static ResType GetResTypeFromString(const std::string &type); 117 118 /** 119 * @brief copy file 120 * @param src: source file path 121 * @param dst: destination file path 122 * @return true if success, other false 123 */ 124 static bool CopyFleInner(const std::string &src, const std::string &dst); 125 126 /** 127 * @brief create directories 128 * @param filePath: directory path 129 * @return true if success, other false 130 */ 131 static bool CreateDirs(const std::string &filePath); 132 133 /** 134 * @brief ignore file or directory 135 * @param filename: file or directory name 136 * @param isFile: ture if is file, other false 137 * @return true if ignore, other false 138 */ 139 static bool IsIgnoreFile(const std::string &filename, bool isFile); 140 141 /** 142 * @brief need convert to solid xml 143 * @param resType: ResType 144 * @return true if need, other false 145 */ 146 static bool NeedConverToSolidXml(ResType resType); 147 148 /** 149 * @brief generate hash string 150 * @param key: string 151 * @return hash string 152 */ 153 static std::string GenerateHash(const std::string key); 154 155 /** 156 * @brief get an keyParams for limitkey 157 * @param keyParams 158 * @return limitkey 159 */ 160 static std::string PaserKeyParam(const std::vector<KeyParam> &keyParams); 161 private: 162 enum class IgnoreType { 163 IGNORE_FILE, 164 IGNORE_DIR, 165 IGNORE_ALL 166 }; 167 static const std::map<std::string, IgnoreType> IGNORE_FILE_REGEX; 168 static std::string GetLocaleLimitkey(const KeyParam &KeyParam); 169 static std::string GetDeviceTypeLimitkey(const KeyParam &KeyParam); 170 static std::string GetResolutionLimitkey(const KeyParam &KeyParam); 171 static std::string GetKeyParamValue(const KeyParam &KeyParam); 172 }; 173 } 174 } 175 } 176 #endif 177