• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "file_entry.h"
21 #include "json/json.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 open json file.
61      * @param path: json file path.
62      * @param root: json root node
63      * @return true if open success, other false.
64      */
65     static bool OpenJsonFile(const std::string &path, Json::Value &root);
66 
67     /**
68      * @brief save json file.
69      * @param path: json file path.
70      * @param root: json root node
71      * @return true if save success, other false.
72      */
73     static bool SaveToJsonFile(const std::string &path, const Json::Value &root);
74 
75     /**
76      * @brief get resource type from directory.
77      * @param name: directory name.
78      * @return resource type.
79      */
80     static ResType GetResTypeByDir(const std::string &name);
81 
82     /**
83      * @brief ResType to string
84      * @param type: ResType
85      * @return resource type string.
86      */
87     static std::string ResTypeToString(ResType type);
88 
89     /**
90      * @brief get id name
91      * @param name; id name or file name
92      * @param type: ResType
93      * @return return id name.
94      */
95     static std::string GetIdName(const std::string &name, ResType type);
96 
97     /**
98      * @brief compose multi strings to string
99      * @param contents: multi strings
100      * @param addNull: if true, string length contains '\0'.
101      * @return return string, empty if error
102      */
103     static std::string ComposeStrings(const std::vector<std::string> &contents, bool addNull = false);
104 
105     /**
106      * @brief decompose string to multi strings
107      * @param content: string
108      * @return return string vector, empty if error
109      */
110     static std::vector<std::string> DecomposeStrings(const std::string &content);
111 
112     /**
113      * @brief string to ResType
114      * @param type: string
115      * @return return ResType
116      */
117     static ResType GetResTypeFromString(const std::string &type);
118 
119     /**
120      * @brief copy file
121      * @param src: source file path
122      * @param dst: destination file path
123      * @return true if success, other false
124      */
125     static bool CopyFleInner(const std::string &src, const std::string &dst);
126 
127     /**
128      * @brief create directories
129      * @param filePath: directory path
130      * @return true if success, other false
131      */
132     static bool CreateDirs(const std::string &filePath);
133 
134     /**
135      * @brief ignore file or directory
136      * @param filename: file or directory name
137      * @param isFile: ture if is file, other false
138      * @return true if ignore, other false
139      */
140     static bool IsIgnoreFile(const std::string &filename, bool isFile);
141 
142     /**
143      * @brief generate hash string
144      * @param key: string
145      * @return hash string
146      */
147     static std::string GenerateHash(const std::string &key);
148 
149     /**
150      * @brief get an absolute pathname
151      * @param path pathname
152      * @return absolut pathname
153      */
154     static std::string RealPath(const std::string &path);
155 
156     /**
157      * @brief check the directory is legal
158      * @param path pathname
159      * @return true is legal, other false;
160      */
161     static bool IslegalPath(const std::string &path);
162 
163     /**
164      * @brief get an keyParams for limitkey
165      * @param keyParams
166      * @return limitkey
167      */
168     static std::string PaserKeyParam(const std::vector<KeyParam> &keyParams);
169 
170     /**
171      * @brief Decimal to hexadecimal string
172      * @param int32_t Decimal
173      * @return Hexadecimal string
174      */
175     static std::string DecToHexStr(const int32_t id);
176 
177     /**
178      * @brief Check hexadecimal string
179      * @param string Hexadecimal string
180      * @return ture Hexadecimal string is legal, other false;
181      */
182     static bool CheckHexStr(const std::string &hex);
183 
184     /**
185      * @brief get g_contentClusterMap key string
186      * @return All restype string
187      */
188     static std::string GetAllRestypeString();
189 
190     /**
191      * @brief get \base\element dir
192      * @param string inputpath
193      * @return resource\base\element dir
194      */
195     static FileEntry::FilePath GetBaseElementPath(const std::string input);
196 
197     /**
198      * @brief get main dir
199      * @param string inputpath
200      * @return main dir
201      */
202     static FileEntry::FilePath GetMainPath(const std::string input);
203 
204     /**
205      * @brief Gets the standard size of icons under different qualifier phrases
206      * @param keyParams set of qualifiers
207      * @param uint32_t index
208      * @return standard size of the png
209     */
210     static uint32_t GetNormalSize(const std::vector<KeyParam> &keyParams, uint32_t index);
211 
212 private:
213     enum class IgnoreType {
214         IGNORE_FILE,
215         IGNORE_DIR,
216         IGNORE_ALL
217     };
218     static const std::map<std::string, IgnoreType> IGNORE_FILE_REGEX;
219     static std::string GetLocaleLimitkey(const KeyParam &KeyParam);
220     static std::string GetDeviceTypeLimitkey(const KeyParam &KeyParam);
221     static std::string GetResolutionLimitkey(const KeyParam &KeyParam);
222     static std::string GetKeyParamValue(const KeyParam &KeyParam);
223 };
224 }
225 }
226 }
227 #endif
228