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