• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_RESOURCE_MANAGER_RES_DESC_H
17 #define OHOS_RESOURCE_MANAGER_RES_DESC_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <string>
22 #include <vector>
23 #include "res_common.h"
24 #include "res_config_impl.h"
25 
26 namespace OHOS {
27 namespace Global {
28 namespace Resource {
29 static constexpr uint32_t RES_HEADER_LEN = 136;
30 
31 static constexpr uint32_t RES_VERSION_LEN = 128;
32 
33 /**
34  * resource.index file header
35  */
36 typedef struct ResHeader {
37     // Type identifier for this chunk.  The meaning of this value depends
38     // on the containing chunk.
39     char version_[RES_VERSION_LEN];
40 
41     // Size of the resource.index file (in bytes).  Including header
42     uint32_t length_;
43 
44     // determiner key count
45     uint32_t keyCount_;
46 } ResHeader;
47 
48 class IdItem {
49 public:
50     static const uint32_t HEADER_LEN = 12;
51 
52     /**
53      * Whether the resType is array or not
54      * @param type the resType
55      * @return true if the resType is array, else false
56      */
IsArrayOfType(ResType type)57     static bool IsArrayOfType(ResType type)
58     {
59         if (type == ResType::STRINGARRAY || type == ResType::INTARRAY || type == ResType::THEME ||
60             type == ResType::PLURALS || type == ResType::PATTERN) {
61             return true;
62         }
63         return false;
64     }
65 
JudgeArray()66     void JudgeArray()
67     {
68         this->isArray_ = IsArrayOfType(resType_);
69     }
70 
71     /**
72      * only theme and pattern may have parent
73      * @return true when have parent
74      */
75     bool HaveParent() const;
76 
77     static std::map<ResType, std::string> resTypeStrList;
78     /**
79      * judge the std::string value is ref or not
80      * ref start with '$' end with id
81      * sample: "$string:16777225"
82      * @param value
83      * @param resType when return true, set resType. as sample : ResType:STRING
84      * @param id      when return true, set id. as sample : 16777225
85      * @return        true: value is ref
86      */
87     static bool IsRef(const std::string &value, ResType &resType, int &id);
88 
89     std::string ToString() const;
90 
GetItemResName()91     std::string GetItemResName() const
92     {
93         return name_;
94     }
95 
GetItemResId()96     uint32_t GetItemResId() const
97     {
98         return id_;
99     }
100 
GetItemResType()101     ResType GetItemResType() const
102     {
103         return resType_;
104     }
105 
106     uint32_t size_;
107     ResType resType_;
108     uint32_t id_;
109     uint16_t valueLen_;
110     bool isArray_ = false;
111     std::string value_;
112     std::vector<std::string> values_;
113     std::string name_;
114 
115 private:
116     static bool sInit;
117     static bool Init();
118 };
119 
120 class IdParam {
121 public:
122     ~IdParam();
123     std::string ToString() const;
124 
125     uint32_t id_;
126     uint32_t offset_;
127     std::shared_ptr<IdItem> idItem_;
128 };
129 
130 class ResId {
131 public:
132     static const uint32_t RESID_HEADER_LEN = 8;
133     static const uint32_t IDPARAM_HEADER_LEN = 8;
134 
135     ~ResId();
136     std::string ToString() const;
137 
138     char tag_[4];
139     uint32_t count_; // ID count
140     std::vector<std::shared_ptr<IdParam>> idParams_;
141 };
142 
143 /**
144  * describe the qualifier
145  */
146 class KeyParam {
147 public:
148     // type of qualifier
149     KeyType type_;
150 
151     // value of qualifiers
152     uint32_t value_;
153 
154     // convert from value_
155     std::string str_;
156 
InitStr()157     void InitStr()
158     {
159         str_ = ConvertToStr();
160     }
161 
GetStr()162     const std::string &GetStr() const
163     {
164         return str_;
165     }
166 
167     std::string ToString() const;
168 
169     std::string GetDeviceTypeStr() const;
170 
171 private:
172     const std::string ConvertToStr() const;
173     std::string GetScreenDensityStr() const;
174     std::string GetColorModeStr() const;
175     std::string GetMccStr() const;
176     std::string GetMncStr() const;
177     std::string GetInputDeviceStr() const;
178 };
179 
180 /**
181  * a ResKey means a Qualifiers Sub-directories
182  *
183  */
184 class ResKey {
185 public:
186     static const uint32_t RESKEY_HEADER_LEN = 12;
187 
188     static const uint32_t KEYPARAM_HEADER_LEN = 8;
189 
190     ~ResKey();
191 
192     std::string ToString() const;
193     // always 'KEYS'
194     char tag_[4];
195 
196     // offset from the beginning of the index file, pointing to the resource ID data block
197     uint32_t offset_;
198 
199     // count of qualifiers
200     uint32_t keyParamsCount_;
201 
202     // the qualifiers
203     std::vector<std::shared_ptr<KeyParam>> keyParams_;
204 
205     // the resource ID data
206     std::shared_ptr<ResId> resId_;
207 
208     // the resConfig of each ResKey and all resConfig_ in ValueUnderQualifierDir will point to this resConfig_
209     std::shared_ptr<ResConfigImpl> resConfig_;
210 };
211 /**
212  * a ResDesc means a index file in hap zip
213  */
214 class ResDesc {
215 public:
216     ResDesc();
217 
218     ~ResDesc();
219 
220     std::string ToString() const;
221 
222     ResHeader *resHeader_;
223 
224     std::vector<std::shared_ptr<ResKey>> keys_;
225 
226     std::string GetCurrentDeviceType();
227 };
228 } // namespace Resource
229 } // namespace Global
230 } // namespace OHOS
231 #endif
232