• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      * @return true if open success, other false.
71      */
72     static bool OpenJsonFile(const std::string &path, cJSON **root);
73 
74     /**
75      * @brief save json file.
76      * @param path: json file path.
77      * @param root: json root node
78      * @return true if save success, other false.
79      */
80     static bool SaveToJsonFile(const std::string &path, const cJSON *root);
81 
82     /**
83      * @brief get resource type from directory.
84      * @param name: directory name.
85      * @return resource type.
86      */
87     static ResType GetResTypeByDir(const std::string &name);
88 
89     /**
90      * @brief get all resource type directorys.
91      * @return all resource type.
92      */
93     static std::string GetAllResTypeDirs();
94 
95     /**
96      * @brief ResType to string
97      * @param type: ResType
98      * @return resource type string.
99      */
100     static std::string ResTypeToString(ResType type);
101 
102     /**
103      * @brief get id name
104      * @param name; id name or file name
105      * @param type: ResType
106      * @return return id name.
107      */
108     static std::string GetIdName(const std::string &name, ResType type);
109 
110     /**
111      * @brief compose multi strings to string
112      * @param contents: multi strings
113      * @param addNull: if true, string length contains '\0'.
114      * @return return string, empty if error
115      */
116     static std::string ComposeStrings(const std::vector<std::string> &contents, bool addNull = false);
117 
118     /**
119      * @brief decompose string to multi strings
120      * @param content: string
121      * @return return string vector, empty if error
122      */
123     static std::vector<std::string> DecomposeStrings(const std::string &content);
124 
125     /**
126      * @brief string to ResType
127      * @param type: string
128      * @return return ResType
129      */
130     static ResType GetResTypeFromString(const std::string &type);
131 
132     /**
133      * @brief copy file
134      * @param src: source file path
135      * @param dst: destination file path
136      * @return true if success, other false
137      */
138     static bool CopyFileInner(const std::string &src, const std::string &dst);
139 
140     /**
141      * @brief create directories
142      * @param filePath: directory path
143      * @return true if success, other false
144      */
145     static bool CreateDirs(const std::string &filePath);
146 
147     /**
148      * @brief ignore file or directory
149      * @param filename: file or directory name
150      * @param isFile: ture if is file, other false
151      * @return true if ignore, other false
152      */
153     static bool IsIgnoreFile(const std::string &filename, bool isFile);
154 
155     /**
156      * @brief generate hash string
157      * @param key: string
158      * @return hash string
159      */
160     static std::string GenerateHash(const std::string &key);
161 
162     /**
163      * @brief get an absolute pathname
164      * @param path pathname
165      * @return absolut pathname
166      */
167     static std::string RealPath(const std::string &path);
168 
169     /**
170      * @brief check the directory is legal
171      * @param path pathname
172      * @return true is legal, other false;
173      */
174     static bool IslegalPath(const std::string &path);
175 
176     /**
177      * @brief get an keyParams for limitkey
178      * @param keyParams
179      * @return limitkey
180      */
181     static std::string PaserKeyParam(const std::vector<KeyParam> &keyParams);
182 
183     /**
184      * @brief Decimal to hexadecimal string
185      * @param int32_t Decimal
186      * @return Hexadecimal string
187      */
188     static std::string DecToHexStr(const uint32_t id);
189 
190     /**
191      * @brief Check hexadecimal string
192      * @param string Hexadecimal string
193      * @return ture Hexadecimal string is legal, other false;
194      */
195     static bool CheckHexStr(const std::string &hex);
196 
197     /**
198      * @brief get g_contentClusterMap key string
199      * @return All restype string
200      */
201     static std::string GetAllRestypeString();
202 
203     /**
204      * @brief get \base\element dir
205      * @param string inputpath
206      * @return resource\base\element dir
207      */
208     static FileEntry::FilePath GetBaseElementPath(const std::string input);
209 
210     /**
211      * @brief get main dir
212      * @param string inputpath
213      * @return main dir
214      */
215     static FileEntry::FilePath GetMainPath(const std::string input);
216 
217     /**
218      * @brief Gets the standard size of icons under different qualifier phrases
219      * @param keyParams set of qualifiers
220      * @param uint32_t index
221      * @return standard size of the png
222     */
223     static uint32_t GetNormalSize(const std::vector<KeyParam> &keyParams, uint32_t index);
224 
225     /**
226      * @brief Check if the Unicode code belongs to the 15 plane or 16 plane
227      * @param int unicode
228      * @return ture Unicode code belongs to the 15 plane or 16 plane, other false;
229     */
230     static bool isUnicodeInPlane15or16(int unicode);
231 
232     /**
233      * @brief Remove spaces before and after strings
234      * @param str input string
235     */
236     static void RemoveSpaces(std::string &str);
237 
238     /**
239      * @brief Check whether the value is int
240      * @param snode cJSON node
241     */
242     static bool IsIntValue(const cJSON *node);
243 
244     /**
245      * @brief Check whether the resource name is valid
246      * @param str resource name
247     */
248     static bool IsValidName(const std::string &name);
249 
250     /**
251      * @brief print warning msg
252      * @param noBaseResource set of no base resources
253     */
254     static void PrintWarningMsg(std::vector<std::pair<ResType, std::string>> &noBaseResource);
255 
256     /**
257      * @brief covert KeyParam to str by keytype
258      * @param keyParam: KeyParam
259      * @return limit value string
260     */
261     static std::string GetKeyParamValue(const KeyParam &KeyParam);
262 
263     /**
264      * @brief keyType to string
265      * @param type: KeyType
266      * @return limit type string
267     */
268     static std::string KeyTypeToStr(KeyType type);
269 private:
270     enum class IgnoreType {
271         IGNORE_FILE,
272         IGNORE_DIR,
273         IGNORE_ALL
274     };
275     static const std::map<std::string, IgnoreType> IGNORE_FILE_REGEX;
276     static std::string GetLocaleLimitkey(const KeyParam &KeyParam);
277     static std::string GetDeviceTypeLimitkey(const KeyParam &KeyParam);
278     static std::string GetResolutionLimitkey(const KeyParam &KeyParam);
279 };
280 }
281 }
282 }
283 #endif
284