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 #ifndef OHOS_RESOURCE_MANAGER_UTILS_H
16 #define OHOS_RESOURCE_MANAGER_UTILS_H
17 #include <cstddef>
18 #include <cstdint>
19 #include "res_locale.h"
20 #include "rstate.h"
21 #include <dirent.h>
22 #include <set>
23 #include <vector>
24 
25 namespace OHOS {
26 namespace Global {
27 namespace Resource {
28 enum BitOperatorNum {
29     BIT_TWO = 2,
30     BIT_THREE = 3,
31     BIT_FOUR = 4,
32     BIT_FIVE = 5,
33     BIT_SIX = 6,
34     BIT_EIGHT = 8,
35     BIT_TWELVE = 12,
36     BIT_SIXTEEN = 16,
37     BIT_TWENTY = 20,
38     BIT_TWENTY_FOUR = 24,
39     BIT_TWENTY_EIGHT = 28,
40     BIT_FORTY_EIGHT = 48
41 };
42 
43 enum ArrayIndex {
44     INDEX_ZERO = 0,
45     INDEX_ONE = 1,
46     INDEX_TWO = 2,
47     INDEX_THREE = 3,
48     INDEX_FOUR = 4,
49     INDEX_FIVE = 5,
50     INDEX_SIX = 6,
51     INDEX_SEVEN = 7,
52     INDEX_EIGHT = 8
53 };
54 
55 enum ArrayLen {
56     LEN_THREE = 3,
57     LEN_FOUR = 4,
58     LEN_FIVE = 5,
59     LEN_SEVEN = 7,
60     LEN_NINE = 9
61 };
62 class Utils {
63 public:
64     static bool IsAlphaString(const char *s, int32_t len);
65 
66     static bool IsNumericString(const char *s, int32_t len);
67 
68     static bool IsStrEmpty(const char *s);
69 
70     static size_t StrLen(const char *s);
71 
72     static uint16_t EncodeLanguage(const char *language);
73 
74     static uint16_t EncodeLanguageByResLocale(const ResLocale *locale);
75 
76     static uint32_t EncodeScript(const char *script);
77 
78     static uint32_t EncodeScriptByResLocale(const ResLocale *locale);
79 
80     static void DecodeScript(uint32_t encodeScript, char *outValue);
81 
82     static uint16_t EncodeRegion(const char *region);
83 
84     static uint16_t EncodeRegionByResLocale(const ResLocale *locale);
85 
86     static uint64_t EncodeLocale(const char *language,
87                                  const char *script,
88                                  const char *region);
89 
90     static RState ConvertColorToUInt32(const char *s, uint32_t &outValue);
91 
92     static std::unique_ptr<uint8_t[]> LoadResourceFile(const std::string &path, size_t &len);
93 
94     static RState EncodeBase64(std::unique_ptr<uint8_t[]> &data, int srcLen,
95                               const std::string &imgType, std::string &outValue);
96 
97     static constexpr float DPI_BASE = 160.0f;
98 
99     static bool endWithTail(const std::string& path, const std::string& tail);
100 
101     static bool IsFileExist(const std::string& filePath);
102 
103     static bool ContainsTail(std::string hapPath, std::set<std::string> tailSet);
104 
105     static const std::set<std::string> tailSet;
106 
107     static RState GetMediaBase64Data(const std::string& iconPath, std::string &base64Data);
108 
109     static void CanonicalizePath(const char *path, char *outPath, size_t len);
110 
111     static RState GetFiles(const std::string &strCurrentDir, std::vector<std::string> &vFiles);
112 
113     static bool convertToInteger(const std::string& str, int& outValue);
114 
115     static bool convertToUnsignedLong(const std::string& str, unsigned long& outValue);
116 
117     static bool convertToDouble(const std::string& str, double& outValue);
118 private:
119     static bool IsValidValue(const char* end, const std::string& str);
120     static uint16_t EncodeLanguageOrRegion(const char *str, char base);
121 
122     static bool StrCompare(const char *left, const char *right, size_t len, bool isCaseSensitive);
123     static constexpr uint64_t ROOT_LOCALE = 0x0;
124     static constexpr uint16_t NULL_LANGUAGE = 0x00;
125     static constexpr uint16_t NULL_REGION = 0x00;
126     static constexpr uint16_t NULL_SCRIPT = 0x0000;
127     static constexpr uint64_t NULL_LOCALE = 0x0;
128 };
129 } // namespace Resource
130 } // namespace Global
131 } // namespace OHOS
132 #endif