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