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 #include "res_desc.h"
16
17 #include <cstdlib>
18
19 #include "hilog_wrapper.h"
20 #if defined(__WINNT__)
21 #include <cstring>
22 #else
23 #include "securec.h"
24 #endif
25 #include "utils/errors.h"
26 #include "utils/string_utils.h"
27
28 namespace OHOS {
29 namespace Global {
30 namespace Resource {
GetScreenDensityStr() const31 std::string KeyParam::GetScreenDensityStr() const
32 {
33 std::string ret("not_screen_density");
34 if (type_ == KeyType::SCREEN_DENSITY) {
35 switch (value_) {
36 case ScreenDensity::SCREEN_DENSITY_SDPI:
37 ret = std::string(RE_120_STR);
38 break;
39 case ScreenDensity::SCREEN_DENSITY_MDPI:
40 ret = std::string(RE_160_STR);
41 break;
42 case ScreenDensity::SCREEN_DENSITY_LDPI:
43 ret = std::string(RE_240_STR);
44 break;
45 case ScreenDensity::SCREEN_DENSITY_XLDPI:
46 ret = std::string(RE_320_STR);
47 break;
48 case ScreenDensity::SCREEN_DENSITY_XXLDPI:
49 ret = std::string(RE_480_STR);
50 break;
51 case ScreenDensity::SCREEN_DENSITY_XXXLDPI:
52 ret = std::string(RE_640_STR);
53 break;
54 default:
55 break;
56 }
57 }
58 return ret;
59 }
GetDeviceTypeStr() const60 std::string KeyParam::GetDeviceTypeStr() const
61 {
62 std::string ret("not_device_type");
63 if (type_ == KeyType::DEVICETYPE) {
64 switch (value_) {
65 case DeviceType::DEVICE_PHONE:
66 ret = std::string(PHONE_STR);
67 break;
68 case DeviceType::DEVICE_TABLET:
69 ret = std::string(TABLET_STR);
70 break;
71 case DeviceType::DEVICE_CAR:
72 ret = std::string(CAR_STR);
73 break;
74 case DeviceType::DEVICE_PAD:
75 ret = std::string(PAD_STR);
76 break;
77 case DeviceType::DEVICE_TV:
78 ret = std::string(TV_STR);
79 break;
80 case DeviceType::DEVICE_WEARABLE:
81 ret = std::string(WEARABLE_STR);
82 break;
83 default:
84 break;
85 }
86 }
87 return ret;
88 }
89
GetColorModeStr() const90 std::string KeyParam::GetColorModeStr() const
91 {
92 std::string ret("not_color_mode");
93 if (type_ == KeyType::COLORMODE) {
94 switch (value_) {
95 case ColorMode::DARK:
96 ret = std::string(DARK_STR);
97 break;
98 case ColorMode::LIGHT:
99 ret = std::string(LIGHT_STR);
100 break;
101 default:
102 break;
103 }
104 }
105 return ret;
106 }
107
GetInputDeviceStr() const108 std::string KeyParam::GetInputDeviceStr() const
109 {
110 std::string ret("not_input_device");
111 if (type_ == KeyType::INPUTDEVICE) {
112 if (value_ == InputDevice::INPUTDEVICE_POINTINGDEVICE) {
113 ret = std::string(POINTING_DEVICE_STR);
114 }
115 }
116 return ret;
117 }
118
GetMccStr() const119 std::string KeyParam::GetMccStr() const
120 {
121 std::string ret("not_mcc");
122 if (type_ == KeyType::MCC) {
123 ret = std::string("mcc");
124 }
125 return ret;
126 }
127
GetMncStr() const128 std::string KeyParam::GetMncStr() const
129 {
130 std::string ret("not_mnc");
131 if (type_ == KeyType::MNC) {
132 ret = std::string("mnc");
133 }
134 return ret;
135 }
136
ConvertToStr() const137 const std::string KeyParam::ConvertToStr() const
138 {
139 if ((type_ == KeyType::LANGUAGES) || (type_ == KeyType::REGION) || (type_ == KeyType::SCRIPT)) {
140 char tmp[4];
141 char tmp2[5];
142 errno_t eret = memcpy_s(tmp, sizeof(tmp), &value_, 4);
143 if (eret != OK) {
144 HILOG_ERROR("memcpy_s error : %d", eret);
145 }
146 int j = 0;
147 // 4 means langauges/region/script key value max length
148 for (int i = 0; i < 4; ++i) {
149 // 3 means reverse temp value to temp2
150 if (tmp[3 - i]) {
151 tmp2[j++] = tmp[3 - i];
152 }
153 }
154 tmp2[j] = '\0';
155 return std::string(tmp2);
156 }
157 if (type_ == KeyType::DIRECTION) {
158 return std::string((value_ == 0) ? VERTICAL : HORIZONTAL);
159 }
160 if (type_ == KeyType::DEVICETYPE) {
161 return GetDeviceTypeStr();
162 }
163 if (type_ == KeyType::COLORMODE) {
164 return GetColorModeStr();
165 }
166 if (type_ == KeyType::INPUTDEVICE) {
167 return GetInputDeviceStr();
168 }
169 if (type_ == KeyType::MCC) {
170 return GetMccStr();
171 }
172 if (type_ == KeyType::MNC) {
173 return GetMncStr();
174 }
175 if (type_ == KeyType::SCREEN_DENSITY) {
176 return GetScreenDensityStr();
177 }
178 return std::string();
179 }
180
ToString() const181 std::string KeyParam::ToString() const
182 {
183 std::string ret = FormatString("[type:%d, value:%u", type_, value_);
184 if (str_.length() > 0) {
185 ret.append(FormatString(", str:%s", str_.c_str()));
186 }
187 ret.append("]");
188 return ret;
189 }
190
191 // IdItem
192
193 std::map<ResType, std::string> IdItem::resTypeStrList;
194
195 bool IdItem::sInit = IdItem::Init();
196
Init()197 bool IdItem::Init()
198 {
199 resTypeStrList.insert(make_pair(ResType::STRING, std::string("string")));
200 resTypeStrList.insert(make_pair(ResType::BOOLEAN, std::string("boolean")));
201 resTypeStrList.insert(make_pair(ResType::COLOR, std::string("color")));
202 resTypeStrList.insert(make_pair(ResType::FLOAT, std::string("float")));
203 resTypeStrList.insert(make_pair(ResType::INTEGER, std::string("integer")));
204 resTypeStrList.insert(make_pair(ResType::PATTERN, std::string("pattern")));
205 resTypeStrList.insert(make_pair(ResType::THEME, std::string("theme")));
206 resTypeStrList.insert(make_pair(ResType::MEDIA, std::string("media")));
207 return true;
208 }
209
HaveParent() const210 bool IdItem::HaveParent() const
211 {
212 if (!(resType_ == THEME || resType_ == PATTERN)) {
213 return false;
214 }
215 return (values_.size() % 2 == 1);
216 }
217
IsRef(const std::string & value,ResType & resType,int & id)218 bool IdItem::IsRef(const std::string &value, ResType &resType, int &id)
219 {
220 const char *it = value.c_str();
221 const char *st = it;
222 if (*st != '$') {
223 return false;
224 }
225 auto index = value.find(":");
226 if (index == std::string::npos || index < 2) {
227 return false;
228 }
229 std::string typeStr;
230 std::string idStr;
231 typeStr.assign(it + 1, index - 1);
232 idStr.assign(it + index + 1, value.size() - index);
233
234 int idd = atoi(idStr.c_str());
235 if (idd <= 0) {
236 return false;
237 }
238
239 for (auto iit = resTypeStrList.begin(); iit != resTypeStrList.end(); ++iit) {
240 auto tValue = iit->second;
241 auto type = iit->first;
242 if (typeStr == tValue) {
243 id = idd;
244 resType = type;
245 return true;
246 }
247 }
248
249 return false;
250 }
251
ToString() const252 std::string IdItem::ToString() const
253 {
254 std::string ret = FormatString(
255 "[size:%u, resType:%d, id:%u, valueLen:%u, isArray:%d, name:'%s', value:",
256 size_, resType_, id_, valueLen_, isArray_, name_.c_str());
257 if (isArray_) {
258 ret.append("[");
259 for (size_t i = 0; i < values_.size(); ++i) {
260 ret.append(FormatString("'%s',", values_[i].c_str()));
261 }
262 ret.append("]");
263 } else {
264 ret.append(FormatString("'%s'", value_.c_str()));
265 }
266 ret.append("]");
267 return ret;
268 }
269
~IdParam()270 IdParam::~IdParam()
271 {
272 if (idItem_ != nullptr) {
273 delete (idItem_);
274 idItem_ = nullptr;
275 }
276 }
277
ToString() const278 std::string IdParam::ToString() const
279 {
280 return FormatString("[id:%u, offset:%u, data:%s]", id_, offset_,
281 idItem_->ToString().c_str());
282 }
283
~ResId()284 ResId::~ResId()
285 {
286 for (size_t i = 0; i < idParams_.size(); ++i) {
287 if (idParams_[i] != nullptr) {
288 delete idParams_[i];
289 idParams_[i] = nullptr;
290 }
291 }
292 }
293
ToString() const294 std::string ResId::ToString() const
295 {
296 std::string ret = FormatString("idcount:%u, ", count_);
297 for (size_t i = 0; i < idParams_.size(); ++i) {
298 ret.append(idParams_[i]->ToString());
299 }
300 return ret;
301 }
302
~ResKey()303 ResKey::~ResKey()
304 {
305 HILOG_DEBUG("~ResKey()");
306 if (resConfig_ != nullptr) {
307 delete (resConfig_);
308 resConfig_ = nullptr;
309 }
310
311 for (size_t i = 0; i < keyParams_.size(); ++i) {
312 if (keyParams_[i] != nullptr) {
313 delete keyParams_[i];
314 keyParams_[i] = nullptr;
315 }
316 }
317 if (resId_ != nullptr) {
318 delete (resId_);
319 resId_ = nullptr;
320 }
321 }
322
ToString() const323 std::string ResKey::ToString() const
324 {
325 std::string ret = FormatString("offset:%u, keyParamsCount:%u, keyParams:", offset_, keyParamsCount_);
326 for (uint32_t i = 0; i < keyParamsCount_; ++i) {
327 ret.append(keyParams_[i]->ToString());
328 }
329 ret.append("\nid: ");
330 ret.append(resId_->ToString());
331 return ret;
332 }
333
ResDesc()334 ResDesc::ResDesc() : resHeader_(nullptr)
335 {}
336
~ResDesc()337 ResDesc::~ResDesc()
338 {
339 HILOG_DEBUG("~ResDesc()");
340 if (resHeader_ != nullptr) {
341 delete (resHeader_);
342 resHeader_ = nullptr;
343 }
344
345 for (size_t i = 0; i < keys_.size(); ++i) {
346 if (keys_[i] != nullptr) {
347 delete keys_[i];
348 keys_[i] = nullptr;
349 }
350 }
351 }
352
ToString() const353 std::string ResDesc::ToString() const
354 {
355 if (resHeader_ == nullptr) {
356 return "empty";
357 }
358 std::string ret = FormatString("version:%s, length:%u, keyCount:%u\n",
359 resHeader_->version_, resHeader_->length_, resHeader_->keyCount_);
360 for (size_t i = 0; i < keys_.size(); ++i) {
361 ret.append(keys_[i]->ToString());
362 ret.append("\n");
363 }
364 return ret;
365 }
366 } // namespace Resource
367 } // namespace Global
368 } // namespace OHOS
369