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