• 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 
91     uint32_t size_;
92     ResType resType_;
93     uint32_t id_;
94     uint16_t valueLen_;
95     bool isArray_ = false;
96     std::string value_;
97     std::vector<std::string> values_;
98     std::string name_;
99 
100 private:
101     static bool sInit;
102     static bool Init();
103 };
104 
105 class IdParam {
106 public:
107     ~IdParam();
108     std::string ToString() const;
109 
110     uint32_t id_;
111     uint32_t offset_;
112     IdItem *idItem_;
113 };
114 
115 class ResId {
116 public:
117     static const uint32_t RESID_HEADER_LEN = 8;
118     static const uint32_t IDPARAM_HEADER_LEN = 8;
119 
120     ~ResId();
121     std::string ToString() const;
122 
123     char tag_[4];
124     uint32_t count_; // ID count
125     std::vector<IdParam *> idParams_;
126 };
127 
128 /**
129  * describe the qualifier
130  */
131 class KeyParam {
132 public:
133     // type of qualifier
134     KeyType type_;
135 
136     // value of qualifiers
137     uint32_t value_;
138 
139     // convert from value_
140     std::string str_;
141 
InitStr()142     void InitStr()
143     {
144         str_ = ConvertToStr();
145     }
146 
GetStr()147     const std::string &GetStr() const
148     {
149         return str_;
150     }
151 
152     std::string ToString() const;
153 
154 private:
155     const std::string ConvertToStr() const;
156     std::string GetScreenDensityStr() const;
157     std::string GetDeviceTypeStr() const;
158     std::string GetColorModeStr() const;
159     std::string GetMccStr() const;
160     std::string GetMncStr() const;
161     std::string GetInputDeviceStr() const;
162 };
163 
164 /**
165  * a ResKey means a Qualifiers Sub-directories
166  *
167  */
168 class ResKey {
169 public:
170     static const uint32_t RESKEY_HEADER_LEN = 12;
171 
172     static const uint32_t KEYPARAM_HEADER_LEN = 8;
173 
174     ~ResKey();
175 
176     std::string ToString() const;
177     // always 'KEYS'
178     char tag_[4];
179 
180     // offset from the beginning of the index file, pointing to the resource ID data block
181     uint32_t offset_;
182 
183     // count of qualifiers
184     uint32_t keyParamsCount_;
185 
186     // the qualifiers
187     std::vector<KeyParam *> keyParams_;
188 
189     // the resource ID data
190     ResId *resId_;
191 
192     // the resConfig of each ResKey and all resConfig_ in ValueUnderQualifierDir will point to this resConfig_
193     ResConfigImpl *resConfig_;
194 };
195 /**
196  * a ResDesc means a index file in hap zip
197  */
198 class ResDesc {
199 public:
200     ResDesc();
201 
202     ~ResDesc();
203 
204     std::string ToString() const;
205 
206     ResHeader *resHeader_;
207 
208     std::vector<ResKey *> keys_;
209 };
210 } // namespace Resource
211 } // namespace Global
212 } // namespace OHOS
213 #endif
214