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