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 "want_params_wrapper.h"
16
17 #include <algorithm>
18
19 namespace OHOS {
20 namespace AAFwk {
21 constexpr int32_t WANT_PARAM_WRAPPER_TWO = 2;
22
23 IINTERFACE_IMPL_1(WantParamWrapper, Object, IWantParams);
24 const InterfaceID g_IID_IWantParams = {
25 0xa75b9db6, 0x9813, 0x4371, 0x8848, {0xd, 0x2, 0x9, 0x6, 0x6, 0xc, 0xe, 0x6, 0xe, 0xc, 0x6, 0x8}
26 };
GetValue(WantParams & value)27 ErrCode WantParamWrapper::GetValue(WantParams &value)
28 {
29 value = wantParams_;
30 return ERR_OK;
31 }
32
Equals(IObject & other)33 bool WantParamWrapper::Equals(IObject &other)
34 {
35 WantParamWrapper *otherObj = static_cast<WantParamWrapper *>(IWantParams::Query(&other));
36 return otherObj != nullptr && otherObj->wantParams_ == wantParams_;
37 }
38
ToString()39 std::string WantParamWrapper::ToString()
40 {
41 std::string result;
42 if (wantParams_.Size() != 0) {
43 result += "{";
44 for (auto it : wantParams_.GetParams()) {
45 int typeId = WantParams::GetDataType(it.second);
46 result = result + "\"" + it.first + "\":{\"" + std::to_string(typeId) + "\":";
47 if (IWantParams::Query(it.second) != nullptr) {
48 result = result + static_cast<WantParamWrapper *>(IWantParams::Query(it.second))->ToString();
49 } else {
50 result = result + "\"" + WantParams::GetStringByType(it.second, typeId) + "\"";
51 }
52 if (it == *wantParams_.GetParams().rbegin()) {
53 result += "}";
54 } else {
55 result += "},";
56 }
57 }
58 result += "}";
59 } else {
60 result = "{}";
61 }
62 return result;
63 }
64
Box(const WantParams & value)65 sptr<IWantParams> WantParamWrapper::Box(const WantParams &value)
66 {
67 sptr<IWantParams> object = new (std::nothrow)WantParamWrapper(value);
68 return object;
69 }
70
Box(WantParams && value)71 sptr<IWantParams> WantParamWrapper::Box(WantParams &&value)
72 {
73 sptr<IWantParams> object = new (std::nothrow) WantParamWrapper(std::move(value));
74 return object;
75 }
76
Unbox(IWantParams * object)77 WantParams WantParamWrapper::Unbox(IWantParams *object)
78 {
79 WantParams value;
80 if (object != nullptr) {
81 object->GetValue(value);
82 }
83 return value;
84 }
ValidateStr(const std::string & str)85 bool WantParamWrapper::ValidateStr(const std::string &str)
86 {
87 if (str == "" || str == "{}" || str == "{\"\"}") {
88 return false;
89 }
90 if (count(str.begin(), str.end(), '\"') % WANT_PARAM_WRAPPER_TWO != 0) {
91 return false;
92 }
93 if (count(str.begin(), str.end(), '{') != count(str.begin(), str.end(), '}')) {
94 return false;
95 }
96 int count = 0;
97 for (auto it : str) {
98 if (it == '{') {
99 count++;
100 }
101 if (it == '}') {
102 count--;
103 }
104 if (count < 0) {
105 return false;
106 }
107 }
108 return true;
109 }
110
Parse(const std::string & str)111 sptr<IWantParams> WantParamWrapper::Parse(const std::string &str)
112 {
113 WantParams wantPaqrams;
114 std::string key = "";
115 int typeId = 0;
116 if (ValidateStr(str)) {
117 for (size_t strnum = 0; strnum < str.size(); strnum++) {
118 if (str[strnum] == '{' && key != "" && typeId == WantParams::VALUE_TYPE_WANTPARAMS) {
119 size_t num;
120 int count = 0;
121 for (num = strnum; num < str.size(); num++) {
122 if (str[num] == '{') {
123 count++;
124 } else if (str[num] == '}') {
125 count--;
126 }
127 if (count == 0) {
128 break;
129 }
130 }
131 wantPaqrams.SetParam(key, WantParamWrapper::Parse(str.substr(strnum, num - strnum + 1)));
132 key = "";
133 typeId = 0;
134 strnum = num + 1;
135 } else if (str[strnum] == '"') {
136 if (key == "") {
137 strnum++;
138 key = str.substr(strnum, str.find('"', strnum) - strnum);
139 strnum = str.find('"', strnum);
140 } else if (typeId == 0) {
141 strnum++;
142 typeId = atoi(str.substr(strnum, str.find('"', strnum) - strnum).c_str());
143 if (errno == ERANGE) {
144 return nullptr;
145 }
146 strnum = str.find('"', strnum);
147 } else {
148 strnum++;
149 wantPaqrams.SetParam(key,
150 WantParams::GetInterfaceByType(typeId, str.substr(strnum, str.find('"', strnum) - strnum)));
151 strnum = str.find('"', strnum);
152 typeId = 0;
153 key = "";
154 }
155 }
156 }
157 }
158 sptr<IWantParams> iwantParams = new (std::nothrow) WantParamWrapper(wantPaqrams);
159 return iwantParams;
160 }
161
ParseWantParams(const std::string & str)162 WantParams WantParamWrapper::ParseWantParams(const std::string &str)
163 {
164 WantParams wantPaqrams;
165 std::string key = "";
166 int typeId = 0;
167 if (!ValidateStr(str)) {
168 return wantPaqrams;
169 }
170 for (size_t strnum = 0; strnum < str.size(); strnum++) {
171 if (str[strnum] == '{' && key != "" && typeId == WantParams::VALUE_TYPE_WANTPARAMS) {
172 size_t num;
173 int count = 0;
174 for (num = strnum; num < str.size(); num++) {
175 if (str[num] == '{') {
176 count++;
177 } else if (str[num] == '}') {
178 count--;
179 }
180 if (count == 0) {
181 break;
182 }
183 }
184 wantPaqrams.SetParam(key, WantParamWrapper::Parse(str.substr(strnum, num - strnum)));
185 key = "";
186 typeId = 0;
187 strnum = num + 1;
188 } else if (str[strnum] == '"') {
189 if (key == "") {
190 strnum++;
191 key = str.substr(strnum, str.find('"', strnum) - strnum);
192 strnum = str.find('"', strnum);
193 } else if (typeId == 0) {
194 strnum++;
195 typeId = atoi(str.substr(strnum, str.find('"', strnum) - strnum).c_str());
196 if (errno == ERANGE) {
197 return wantPaqrams;
198 }
199 strnum = str.find('"', strnum);
200 } else {
201 strnum++;
202 wantPaqrams.SetParam(key,
203 WantParams::GetInterfaceByType(typeId, str.substr(strnum, str.find('"', strnum) - strnum)));
204 strnum = str.find('"', strnum);
205 typeId = 0;
206 key = "";
207 }
208 }
209 }
210 return wantPaqrams;
211 }
212 } // namespace AAFwk
213 } // namespace OHOS