• 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 #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 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
28 #include "parameters.h"
29 #endif
30 
31 namespace OHOS {
32 namespace Global {
33 namespace Resource {
34 const std::string PROPERTY_DEVICE_TYPE = "const.product.devicetype";
35 const std::string PROPERTY_DEVICE_TYPE_DEFAULT = "default";
GetScreenDensityStr() const36 std::string KeyParam::GetScreenDensityStr() const
37 {
38     std::string ret("not_screen_density");
39     if (type_ == KeyType::SCREEN_DENSITY) {
40         switch (value_) {
41             case ScreenDensity::SCREEN_DENSITY_SDPI:
42                 ret = std::string(RE_120_STR);
43                 break;
44             case ScreenDensity::SCREEN_DENSITY_MDPI:
45                 ret = std::string(RE_160_STR);
46                 break;
47             case ScreenDensity::SCREEN_DENSITY_LDPI:
48                 ret = std::string(RE_240_STR);
49                 break;
50             case ScreenDensity::SCREEN_DENSITY_XLDPI:
51                 ret = std::string(RE_320_STR);
52                 break;
53             case ScreenDensity::SCREEN_DENSITY_XXLDPI:
54                 ret = std::string(RE_480_STR);
55                 break;
56             case ScreenDensity::SCREEN_DENSITY_XXXLDPI:
57                 ret = std::string(RE_640_STR);
58                 break;
59             default:
60                 break;
61         }
62     }
63     return ret;
64 }
GetDeviceTypeStr() const65 std::string KeyParam::GetDeviceTypeStr() const
66 {
67     std::string ret("not_device_type");
68     if (type_ == KeyType::DEVICETYPE) {
69         switch (value_) {
70             case DeviceType::DEVICE_PHONE:
71                 ret = std::string(PHONE_STR);
72                 break;
73             case DeviceType::DEVICE_TABLET:
74                 ret = std::string(TABLET_STR);
75                 break;
76             case DeviceType::DEVICE_CAR:
77                 ret = std::string(CAR_STR);
78                 break;
79             case DeviceType::DEVICE_PAD:
80                 ret = std::string(PAD_STR);
81                 break;
82             case DeviceType::DEVICE_TV:
83                 ret = std::string(TV_STR);
84                 break;
85             case DeviceType::DEVICE_WEARABLE:
86                 ret = std::string(WEARABLE_STR);
87                 break;
88             case DeviceType::DEVICE_TWOINONE:
89                 ret = std::string(TWOINONE_STR);
90                 break;
91             default:
92                 break;
93         }
94     }
95     return ret;
96 }
97 
GetColorModeStr() const98 std::string KeyParam::GetColorModeStr() const
99 {
100     std::string ret("not_color_mode");
101     if (type_ == KeyType::COLORMODE) {
102         switch (value_) {
103             case ColorMode::DARK:
104                 ret = std::string(DARK_STR);
105                 break;
106             case ColorMode::LIGHT:
107                 ret = std::string(LIGHT_STR);
108                 break;
109             default:
110                 break;
111         }
112     }
113     return ret;
114 }
115 
GetInputDeviceStr() const116 std::string KeyParam::GetInputDeviceStr() const
117 {
118     std::string ret("not_input_device");
119     if (type_ == KeyType::INPUTDEVICE) {
120         if (value_ == InputDevice::INPUTDEVICE_POINTINGDEVICE) {
121             ret = std::string(POINTING_DEVICE_STR);
122         }
123     }
124     return ret;
125 }
126 
GetMccStr() const127 std::string KeyParam::GetMccStr() const
128 {
129     std::string ret("not_mcc");
130     if (type_ == KeyType::MCC) {
131         ret = std::string("mcc");
132     }
133     return ret;
134 }
135 
GetMncStr() const136 std::string KeyParam::GetMncStr() const
137 {
138     std::string ret("not_mnc");
139     if (type_ == KeyType::MNC) {
140         ret = std::string("mnc");
141     }
142     return ret;
143 }
144 
ConvertToStr() const145 const std::string KeyParam::ConvertToStr() const
146 {
147     if ((type_ == KeyType::LANGUAGES) || (type_ == KeyType::REGION) || (type_ == KeyType::SCRIPT)) {
148         char tmp[4];
149         char tmp2[5];
150         errno_t eret = memcpy_s(tmp, sizeof(tmp), &value_, 4);
151         if (eret != OK) {
152             HILOG_ERROR("memcpy_s error : %d", eret);
153         }
154         int j = 0;
155         // 4 means langauges/region/script key value max length
156         for (int i = 0; i < 4; ++i) {
157             // 3 means reverse temp value to temp2
158             if (tmp[3 - i]) {
159                 tmp2[j++] = tmp[3 - i];
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);
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 < 2) {
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     if (idItem_ != nullptr) {
282         delete (idItem_);
283         idItem_ = nullptr;
284     }
285 }
286 
ToString() const287 std::string IdParam::ToString() const
288 {
289     return FormatString("[id:%u, offset:%u, data:%s]", id_, offset_,
290         idItem_->ToString().c_str());
291 }
292 
~ResId()293 ResId::~ResId()
294 {
295     for (size_t i = 0; i < idParams_.size(); ++i) {
296         if (idParams_[i] != nullptr) {
297             delete idParams_[i];
298             idParams_[i] = nullptr;
299         }
300     }
301 }
302 
ToString() const303 std::string ResId::ToString() const
304 {
305     std::string ret = FormatString("idcount:%u, ", count_);
306     for (size_t i = 0; i < idParams_.size(); ++i) {
307         ret.append(idParams_[i]->ToString());
308     }
309     return ret;
310 }
311 
~ResKey()312 ResKey::~ResKey()
313 {
314     HILOG_DEBUG("~ResKey()");
315     if (resConfig_ != nullptr) {
316         delete (resConfig_);
317         resConfig_ = nullptr;
318     }
319 
320     for (size_t i = 0; i < keyParams_.size(); ++i) {
321         if (keyParams_[i] != nullptr) {
322             delete keyParams_[i];
323             keyParams_[i] = nullptr;
324         }
325     }
326     if (resId_ != nullptr) {
327         delete (resId_);
328         resId_ = nullptr;
329     }
330 }
331 
ToString() const332 std::string ResKey::ToString() const
333 {
334     std::string ret = FormatString("offset:%u, keyParamsCount:%u, keyParams:", offset_, keyParamsCount_);
335     for (uint32_t i = 0; i < keyParamsCount_; ++i) {
336         ret.append(keyParams_[i]->ToString());
337     }
338     ret.append("\nid: ");
339     ret.append(resId_->ToString());
340     return ret;
341 }
342 
ResDesc()343 ResDesc::ResDesc() : resHeader_(nullptr)
344 {}
345 
~ResDesc()346 ResDesc::~ResDesc()
347 {
348     HILOG_DEBUG("~ResDesc()");
349     if (resHeader_ != nullptr) {
350         delete (resHeader_);
351         resHeader_ = nullptr;
352     }
353 
354     for (size_t i = 0; i < keys_.size(); ++i) {
355         if (keys_[i] != nullptr) {
356             delete keys_[i];
357             keys_[i] = nullptr;
358         }
359     }
360 }
361 
ToString() const362 std::string ResDesc::ToString() const
363 {
364     if (resHeader_ == nullptr) {
365         return "empty";
366     }
367     std::string ret = FormatString("version:%s, length:%u, keyCount:%u\n",
368         resHeader_->version_, resHeader_->length_, resHeader_->keyCount_);
369     for (size_t i = 0; i < keys_.size(); ++i) {
370         ret.append(keys_[i]->ToString());
371         ret.append("\n");
372     }
373     return ret;
374 }
375 
GetCurrentDeviceType()376 std::string ResDesc::GetCurrentDeviceType()
377 {
378     std::string deviceType;
379 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
380     deviceType = system::GetParameter(PROPERTY_DEVICE_TYPE, PROPERTY_DEVICE_TYPE_DEFAULT);
381 #endif
382     return deviceType;
383 }
384 } // namespace Resource
385 } // namespace Global
386 } // namespace OHOS
387