• 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 
16 #include "ohos/aafwk/content/patterns_matcher.h"
17 #include "parcel_macro_base.h"
18 using namespace OHOS::AppExecFwk;
19 
20 namespace OHOS {
21 namespace AAFwk {
22 
23 /**
24  * @brief A parameterized constructor used to create a PatternsMatcher instance.
25  *
26  */
PatternsMatcher()27 PatternsMatcher::PatternsMatcher()
28 {
29     pattern_ = "";
30     type_ = MatchType::DEFAULT;
31 }
32 
33 /**
34  * @brief A parameterized constructor used to create a PatternsMatcher instance.
35  *
36  * @param patternsMatcher Indicates patternsMatcher used to create a patternsMatcher instance.
37  */
PatternsMatcher(const PatternsMatcher & patternsMatcher)38 PatternsMatcher::PatternsMatcher(const PatternsMatcher &patternsMatcher)
39 {
40     pattern_ = patternsMatcher.GetPattern();
41     type_ = patternsMatcher.GetType();
42 }
43 
44 /**
45  * @brief A parameterized constructor used to create a PatternsMatcher instance.
46  *
47  * @param pattern Indicates pattern used to create a patternsMatcher instance.
48  * @param type Indicates type used to create a patternsMatcher instance.
49  */
PatternsMatcher(std::string pattern,MatchType type)50 PatternsMatcher::PatternsMatcher(std::string pattern, MatchType type)
51 {
52     pattern_ = pattern;
53     type_ = type;
54 }
55 
~PatternsMatcher()56 PatternsMatcher::~PatternsMatcher()
57 {}
58 
59 /**
60  * @brief Obtains the pattern.
61  *
62  * @return the specified pattern.
63  */
GetPattern() const64 std::string PatternsMatcher::GetPattern() const
65 {
66     return pattern_;
67 }
68 
69 /**
70  * @brief Obtains the specified type.
71  *
72  * @return the specified type.
73  */
GetType() const74 MatchType PatternsMatcher::GetType() const
75 {
76     return type_;
77 }
78 
79 /**
80  * @brief Match this PatternsMatcher against a string data.
81  *
82  * @param str The desired string to look for.
83  * @return Returns either a valid match constant.
84  */
match(std::string match)85 bool PatternsMatcher::match(std::string match)
86 {
87     return MatchPattern(pattern_, match, type_);
88 }
89 
90 /**
91  * @brief Match this PatternsMatcher against an Pattern's data.
92  *
93  * @param pattern The desired data to look for.
94  * @param match The full data string to match against.
95  * @param type The desired tyoe to look for.
96  *
97  * @return Returns either a valid match constant.
98  */
MatchPattern(std::string pattern,std::string match,MatchType type)99 bool PatternsMatcher::MatchPattern(std::string pattern, std::string match, MatchType type)
100 {
101     if (match.empty()) {
102         return false;
103     }
104     switch (type) {
105         case MatchType::DEFAULT: {
106             return pattern == match;
107         }
108         case MatchType::PREFIX: {
109             return match.find(pattern) == 0;
110         }
111         case MatchType::PATTERN: {
112             std::regex regex_(pattern);
113             return regex_match(match, regex_);
114         }
115         case MatchType::GLOBAL: {
116             return GlobPattern(pattern, match);
117         }
118         default: {
119             ABILITYBASE_LOGE("MatchPattern::The other type");
120             return false;
121         }
122     }
123 }
124 
125 /**
126  * @brief Match this PatternsMatcher against an Pattern's data.
127  *
128  * @param pattern The desired data to look for.
129  * @param match The full data string to match against.
130  *
131  * @return Returns either a valid match constant.
132  */
GlobPattern(std::string pattern,std::string match)133 bool PatternsMatcher::GlobPattern(std::string pattern, std::string match)
134 {
135     size_t indexP = 0;
136     size_t find_pos = 0;
137     size_t indexM = 0;
138     while (indexP < pattern.length() && find_pos != std::string::npos) {
139         find_pos = pattern.find("*", indexP);
140         std::string p;
141         if (find_pos == std::string::npos) {
142             p = pattern.substr(indexP, pattern.length());
143         } else {
144             p = pattern.substr(indexP, find_pos - indexP);
145         }
146         if (p.length() == 0) {
147             indexP = indexP + 1;
148             continue;
149         }
150         size_t find_pos_m = match.find(p, indexM);
151         if (find_pos_m == std::string::npos) {
152             return false;
153         }
154         indexP = find_pos;
155         indexM = find_pos_m + p.length();
156     }
157     if (indexM < match.length() && !(pattern.rfind("*") == pattern.length() - 1)) {
158         return false;
159     }
160     return true;
161 }
162 
163 /**
164  * @brief Marshals this Sequenceable object into a Parcel.
165  *
166  * @param outParcel Indicates the Parcel object to which the Sequenceable object will be marshaled.
167  */
Marshalling(Parcel & parcel) const168 bool PatternsMatcher::Marshalling(Parcel &parcel) const
169 {
170     // write pattern_
171     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(pattern_));
172     // type_
173     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(type_));
174 
175     return true;
176 }
177 
178 /**
179  * @brief Unmarshals this Sequenceable object from a Parcel.
180  *
181  * @param inParcel Indicates the Parcel object into which the Sequenceable object has been marshaled.
182  */
Unmarshalling(Parcel & parcel)183 PatternsMatcher *PatternsMatcher::Unmarshalling(Parcel &parcel)
184 {
185     PatternsMatcher *patternsMatcher = new (std::nothrow) PatternsMatcher();
186     if (patternsMatcher != nullptr && !patternsMatcher->ReadFromParcel(parcel)) {
187         delete patternsMatcher;
188         patternsMatcher = nullptr;
189     }
190     return patternsMatcher;
191 }
192 
ReadFromParcel(Parcel & parcel)193 bool PatternsMatcher::ReadFromParcel(Parcel &parcel)
194 {
195     // pattern_
196     std::u16string readString16;
197     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, readString16);
198     pattern_ = Str16ToStr8(readString16);
199 
200     // flags_
201     int32_t type;
202     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, type);
203     type_ = static_cast<MatchType>(type);
204 
205     return true;
206 }
207 }  // namespace AAFwk
208 }  // namespace OHOS