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 #ifndef MODULETEST_OHOS_ABILITY_RUNTIME_MODULE_TEST_DUMP_UTIL_H
17 #define MODULETEST_OHOS_ABILITY_RUNTIME_MODULE_TEST_DUMP_UTIL_H
18 
19 #include <mutex>
20 #include <vector>
21 #include <string>
22 #include <regex>
23 #include <unordered_map>
24 
25 namespace OHOS {
26 namespace MTUtil {
27 using str_vec = std::vector<std::string>;
28 using str_iter = std::vector<std::string>::iterator;
29 using dump_info_map = std::unordered_map<std::string, std::string>;
30 using std::regex;
31 using std::string;
32 
33 class MTDumpUtil {
34 public:
35     virtual ~MTDumpUtil();
36     static std::shared_ptr<MTDumpUtil> GetInstance();
37 
38     /**
39      *
40      * @param  {string} args                  : type to find
41      * @param  {std::vector<string>} dumpInfo : dump info
42      * @param  {std::vector<string>} results  : all strings found that include @args
43      * @return {int}                          : size of @results
44      * @Introduction: Find all strings specified by @args in @dumpInfo and store them in @results.
45      */
46     size_t GetAll(const string& args, const str_vec& dumpInfo, str_vec& results);
47 
48     /**
49      *
50      * @param  {string} args                  : type to find
51      * @param  {std::vector<string>} dumpInfo : dump info
52      * @param  {string} result                : the first string found that includes @args
53      * @return {str_iter}                         : find or not
54      * @Introduction: Find the first string specified by @args in @dumpInfo and store it in @result.
55      */
56     str_iter GetFirst(const string& args, const str_vec& dumpInfo, const str_iter& begin, string& result);
57 
58     /**
59      *
60      * @param  {string} args                  : specific string to find
61      * @param  {std::vector<string>} dumpInfo : dump info
62      * @param  {str_iter} begin               : where to start searching
63      * @return {str_iter}                     : iter pointing to string containing @args
64      * @Introduction: Match the first @matchStr in @dumpInfo from @begin and return the iter.
65      */
66     str_iter GetSpecific(const string& matchStr, const str_vec& dumpInfo, const str_iter& begin);
67 
68     /**
69      *
70      * @param  {str_vec} strVec_1          : the first vector of string
71      * @param  {str_vec} strVec_2          : the second vector of string
72      * @return {bool}                      : the comparison result of the the params
73      * @Introduction: Return true if each item in @strVec_1 equals the corresponding one in @strVec_2.
74      */
75     bool CompStrVec(const str_vec& strVec_1, const str_vec& strVec_2);
76 
77     /**
78      *
79      * @param  {string} key          : known type used to find info
80      * @param  {string} value        : value of the {key}
81      * @param  {string} args         : type to find
82      * @param  {str_vec} dumpInfo    : dump info
83      * @return {string}              : string found in dumpInfo
84      * @Introduction: Return the value specified by @args with @key and @value in @dumpInfo.
85      */
86     string GetBy(const string& key, const string& value, const string& args, const str_vec& dumpInfo);
87 
88     size_t GetBindingsByUri(const string& uri, const str_vec& dumpInfo, str_vec& result);
89 
90 private:
91     MTDumpUtil();
92     static std::mutex mutex_;
93     static std::shared_ptr<MTDumpUtil> instance_;
94     std::unordered_map<string, regex> findRgx_;
95 
96     inline bool MatchRegex(const regex& regex, const string& str, string& result);
97 };
98 }  // namespace MTUtil
99 }  // namespace OHOS
100 #endif  // MODULETEST_OHOS_ABILITY_RUNTIME_MODULE_TEST_DUMP_UTIL_H
101