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 "option.h"
17
18 namespace OHOS {
19 namespace Developtools {
20 namespace HiPerf {
21 namespace Option {
22 static std::map<std::string, std::unique_ptr<MainOption>> g_MainOptions;
23
CheckOptionFormat(const std::string & optionName)24 bool CheckOptionFormat(const std::string &optionName)
25 {
26 if (optionName.empty()) {
27 HLOGE("unable to use empty option name!");
28 return false;
29 }
30 if (optionName.front() != '-') {
31 HLOGE("must use '-' at the begin of option name!");
32 return false;
33 }
34
35 return true;
36 }
37
GetMainOptions()38 const std::map<std::string, std::unique_ptr<MainOption>> &GetMainOptions()
39 {
40 return g_MainOptions;
41 }
42
FindMainOption(std::string argName)43 const MainOption *FindMainOption(std::string argName)
44 {
45 HLOGV("%s", argName.c_str());
46 auto found = g_MainOptions.find(argName);
47 if (found != g_MainOptions.end()) {
48 // remove the subcmd itself
49 return found->second.get();
50 } else {
51 return nullptr;
52 }
53 }
54
FindOption(std::vector<std::string> & args,const std::string & optionName)55 std::vector<std::string>::iterator FindOption(std::vector<std::string> &args,
56 const std::string &optionName)
57 {
58 HLOGV("try find '%s' in args: %s", optionName.c_str(), VectorToString(args).c_str());
59 auto tmpit = args.begin();
60 std::string::size_type position;
61 for (; tmpit != args.end(); tmpit++) {
62 position = (*tmpit).find("hiperf");
63 if (position != (*tmpit).npos && (*tmpit)[(position + strlen("hiperf"))] == '\0') {
64 break;
65 }
66 }
67 auto it = find(args.begin(), args.end(), optionName);
68 if (it != args.end()) {
69 if (tmpit != args.end() && it > tmpit) {
70 it = args.end();
71 } else {
72 // we found it , we remove it for next process
73 HLOGD("have found '%s'", optionName.c_str());
74 }
75 }
76 return it;
77 }
78
GetValueFromString(const std::string & optionValue,const std::string & optionName,bool & value)79 bool GetValueFromString(const std::string &optionValue, const std::string &optionName, bool &value)
80 {
81 value = true;
82 HLOGD("get bool result:'%s':'%d'", optionName.c_str(), value);
83 return true;
84 }
85
GetValueFromString(const std::string & optionValue,const std::string & optionName,int & value)86 bool GetValueFromString(const std::string &optionValue, const std::string &optionName, int &value)
87 {
88 value = std::stoi(optionValue);
89 HLOGD("get int result:'%s':'%d'", optionName.c_str(), value);
90 return true;
91 }
92
GetValueFromString(const std::string & optionValue,const std::string & optionName,float & value)93 bool GetValueFromString(const std::string &optionValue, const std::string &optionName, float &value)
94 {
95 value = std::stof(optionValue);
96 HLOGD("get float result:'%s':'%f'", optionName.c_str(), value);
97 return true;
98 }
99
GetValueFromString(const std::string & optionValue,const std::string & optionName,std::string & value)100 bool GetValueFromString(const std::string &optionValue, const std::string &optionName,
101 std::string &value)
102 {
103 value = optionValue;
104 return true; // every thing done
105 }
106
GetValueFromString(const std::string & optionValue,const std::string & optionName,std::vector<int> & values)107 bool GetValueFromString(const std::string &optionValue, const std::string &optionName,
108 std::vector<int> &values)
109 {
110 std::vector<std::string> stringValues = StringSplit(optionValue, ",");
111 HLOGD("split int result:'%s':'%s'", optionName.c_str(), VectorToString(stringValues).c_str());
112 while (!stringValues.empty()) {
113 values.push_back(std::stoi(stringValues.front()));
114 stringValues.erase(stringValues.begin()); // remove for next process
115 }
116 return values.size() > 0; // convert successed ?
117 }
118
GetValueFromString(const std::string & optionValue,const std::string & optionName,std::vector<std::string> & values)119 bool GetValueFromString(const std::string &optionValue, const std::string &optionName,
120 std::vector<std::string> &values)
121 {
122 values = StringSplit(optionValue, ",");
123 HLOGD("split string result:'%s':'%s' from '%s'", optionName.c_str(),
124 VectorToString(values).c_str(), optionValue.c_str());
125 return values.size() > 0; // convert successed ?
126 }
127
GetOptionTrackedCommand(std::vector<std::string> & args,std::vector<std::string> & trackedCommand)128 bool GetOptionTrackedCommand(std::vector<std::string> &args,
129 std::vector<std::string> &trackedCommand)
130 {
131 if (!args.empty()) {
132 trackedCommand.insert(trackedCommand.begin(), args.begin(), args.end());
133 args.clear();
134 }
135 return true;
136 }
137
ClearMainOptions()138 void ClearMainOptions()
139 {
140 g_MainOptions.clear();
141 }
142
RegisterMainOption(const std::string & optionName,const std::string & help,std::function<bool (std::vector<std::string> &)> callBackFunction)143 bool RegisterMainOption(const std::string &optionName, const std::string &help,
144 std::function<bool(std::vector<std::string> &)> callBackFunction)
145 {
146 HLOGV("%s", optionName.c_str());
147 if (!CheckOptionFormat(optionName)) {
148 return false;
149 }
150
151 if (g_MainOptions.count(optionName) == 0) {
152 g_MainOptions[optionName] = std::make_unique<MainOption>();
153 g_MainOptions[optionName].get()->help = help;
154 g_MainOptions[optionName].get()->callBackFunction = std::move(callBackFunction);
155 return true;
156 } else {
157 HLOGE("main args %s already registered!", optionName.c_str());
158 return false;
159 }
160 }
161 } // namespace Option
162 } // namespace HiPerf
163 } // namespace Developtools
164 } // namespace OHOS
165