• 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/common.h"
26 #include "utils/errors.h"
27 #include "utils/string_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             default:
85                 break;
86         }
87     }
88     return ret;
89 }
90 
GetColorModeStr() const91 std::string KeyParam::GetColorModeStr() const
92 {
93     std::string ret("not_color_mode");
94     if (type_ == KeyType::COLORMODE) {
95         switch (value_) {
96             case ColorMode::DARK:
97                 ret = std::string(DARK_STR);
98                 break;
99             case ColorMode::LIGHT:
100                 ret = std::string(LIGHT_STR);
101                 break;
102             default:
103                 break;
104         }
105     }
106     return ret;
107 }
108 
ConvertToStr() const109 const std::string KeyParam::ConvertToStr() const
110 {
111     if ((type_ == KeyType::LANGUAGES) || (type_ == KeyType::REGION) || (type_ == KeyType::SCRIPT)) {
112         char tmp[4], tmp2[5];
113         errno_t eret = memcpy_s(tmp, sizeof(tmp), &value_, 4);
114         if (eret != OK) {
115             HILOG_ERROR("memcpy_s error : %d", eret);
116         }
117         int j = 0;
118         // 4 means langauges/region/script key value max length
119         for (int i = 0; i < 4; ++i) {
120             // 3 means reverse temp value to temp2
121             if (tmp[3 - i]) {
122                 tmp2[j++] = tmp[3 - i];
123             }
124         }
125         tmp2[j] = '\0';
126         return std::string(tmp2);
127     }
128     if (type_ == KeyType::DIRECTION) {
129         return std::string((value_ == 0) ? VERTICAL : HORIZONTAL);
130     }
131     if (type_ == KeyType::DEVICETYPE) {
132         return GetDeviceTypeStr();
133     }
134     if (type_ == KeyType::COLORMODE) {
135         return GetColorModeStr();
136     }
137     if (type_ == KeyType::SCREEN_DENSITY) {
138         return GetScreenDensityStr();
139     }
140     return std::string();
141 }
142 
ToString() const143 std::string KeyParam::ToString() const
144 {
145     std::string ret = FormatString("[type:%d, value:%u", type_, value_);
146     if (str_.length() > 0) {
147         ret.append(FormatString(", str:%s", str_.c_str()));
148     }
149     ret.append("]");
150     return ret;
151 }
152 
153 // IdItem
154 
155 std::map<ResType, std::string> IdItem::resTypeStrList;
156 
157 bool IdItem::sInit = IdItem::Init();
158 
Init()159 bool IdItem::Init()
160 {
161     resTypeStrList.insert(make_pair(ResType::STRING, std::string("string")));
162     resTypeStrList.insert(make_pair(ResType::BOOLEAN, std::string("boolean")));
163     resTypeStrList.insert(make_pair(ResType::COLOR, std::string("color")));
164     resTypeStrList.insert(make_pair(ResType::FLOAT, std::string("float")));
165     resTypeStrList.insert(make_pair(ResType::INTEGER, std::string("integer")));
166     resTypeStrList.insert(make_pair(ResType::PATTERN, std::string("pattern")));
167     resTypeStrList.insert(make_pair(ResType::THEME, std::string("theme")));
168     resTypeStrList.insert(make_pair(ResType::MEDIA, std::string("media")));
169     return true;
170 }
171 
HaveParent() const172 bool IdItem::HaveParent() const
173 {
174     if (!(resType_ == THEME || resType_ == PATTERN)) {
175         return false;
176     }
177     return (values_.size() % 2 == 1);
178 }
179 
IsRef(const std::string & value,ResType & resType,int & id)180 bool IdItem::IsRef(const std::string &value, ResType &resType, int &id)
181 {
182     const char *it = value.c_str();
183     const char *st = it;
184     if (*st != '$') {
185         return false;
186     }
187     auto index = value.find(":");
188     if (index == std::string::npos || index < 2) {
189         return false;
190     }
191     std::string typeStr, idStr;
192     typeStr.assign(it + 1, index - 1);
193     idStr.assign(it + index + 1, value.size() - index);
194 
195     int idd = atoi(idStr.c_str());
196     if (idd <= 0) {
197         return false;
198     }
199 
200     for (auto iit = resTypeStrList.begin(); iit != resTypeStrList.end(); ++iit) {
201         auto tValue = iit->second;
202         auto type = iit->first;
203         if (typeStr == tValue) {
204             id = idd;
205             resType = type;
206             return true;
207         }
208     }
209 
210     return false;
211 }
212 
ToString() const213 std::string IdItem::ToString() const
214 {
215     std::string ret = FormatString(
216         "[size:%u, resType:%d, id:%u, valueLen:%u, isArray:%d, name:'%s', value:",
217         size_, resType_, id_, valueLen_, isArray_, name_.c_str());
218     if (isArray_) {
219         ret.append("[");
220         for (size_t i = 0; i < values_.size(); ++i) {
221             ret.append(FormatString("'%s',", values_[i].c_str()));
222         }
223         ret.append("]");
224     } else {
225         ret.append(FormatString("'%s'", value_.c_str()));
226     }
227     ret.append("]");
228     return ret;
229 }
230 
~IdParam()231 IdParam::~IdParam()
232 {
233     delete (idItem_);
234 }
235 
ToString() const236 std::string IdParam::ToString() const
237 {
238     return FormatString("[id:%u, offset:%u, data:%s]", id_, offset_,
239         idItem_->ToString().c_str());
240 }
241 
~ResId()242 ResId::~ResId()
243 {
244     for (size_t i = 0; i < idParams_.size(); ++i) {
245         auto ptr = idParams_[i];
246         delete (ptr);
247     }
248 }
249 
ToString() const250 std::string ResId::ToString() const
251 {
252     std::string ret = FormatString("idcount:%u, ", count_);
253     for (size_t i = 0; i < idParams_.size(); ++i) {
254         ret.append(idParams_[i]->ToString());
255     }
256     return ret;
257 }
258 
~ResKey()259 ResKey::~ResKey()
260 {
261     HILOG_DEBUG("~ResKey()");
262     for (size_t i = 0; i < keyParams_.size(); ++i) {
263         auto ptr = keyParams_[i];
264         delete (ptr);
265     }
266     delete (resId_);
267 }
268 
ToString() const269 std::string ResKey::ToString() const
270 {
271     std::string ret = FormatString("offset:%u, keyParamsCount:%u, keyParams:", offset_, keyParamsCount_);
272     for (uint32_t i = 0; i < keyParamsCount_; ++i) {
273         ret.append(keyParams_[i]->ToString());
274     }
275     ret.append("\nid: ");
276     ret.append(resId_->ToString());
277     return ret;
278 }
279 
ResDesc()280 ResDesc::ResDesc() : resHeader_(nullptr)
281 {}
282 
~ResDesc()283 ResDesc::~ResDesc()
284 {
285     HILOG_DEBUG("~ResDesc()");
286     delete (resHeader_);
287     for (size_t i = 0; i < keys_.size(); ++i) {
288         auto ptr = keys_[i];
289         delete (ptr);
290     }
291 }
292 
ToString() const293 std::string ResDesc::ToString() const
294 {
295     if (resHeader_ == nullptr) {
296         return "empty";
297     }
298     std::string ret = FormatString("version:%s, length:%u, keyCount:%u\n",
299         resHeader_->version_, resHeader_->length_, resHeader_->keyCount_);
300     for (size_t i = 0; i < keys_.size(); ++i) {
301         ret.append(keys_[i]->ToString());
302         ret.append("\n");
303     }
304     return ret;
305 }
306 } // namespace Resource
307 } // namespace Global
308 } // namespace OHOS
309