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