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(const std::string & argName)43 const MainOption *FindMainOption(const 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(argsVector & args,const std::string & optionName)55 std::vector<std::string>::iterator FindOption(argsVector &args, const std::string &optionName)
56 {
57 HLOGV("try find '%s' in args: %s", optionName.c_str(), VectorToString(args).c_str());
58 auto tmpit = args.begin();
59 std::string::size_type position;
60 for (; tmpit != args.end(); tmpit++) {
61 position = (*tmpit).find("hiperf");
62 if (position != (*tmpit).npos && (*tmpit)[(position + strlen("hiperf"))] == '\0') {
63 break;
64 }
65 }
66 auto it = std::find(args.begin(), args.end(), optionName);
67 if (it != args.end()) {
68 if (tmpit != args.end() && it > tmpit) {
69 it = args.end();
70 } else {
71 // we found it , we remove it for next process
72 HLOGD("have found '%s'", optionName.c_str());
73 }
74 }
75 return it;
76 }
77
GetValueFromString(const std::string & optionValue,const std::string & optionName,bool & value)78 bool GetValueFromString(const std::string &optionValue, const std::string &optionName, bool &value)
79 {
80 value = true;
81 HLOGD("get bool result:'%s':'%d'", optionName.c_str(), value);
82 return true;
83 }
84
GetValueFromString(const std::string & optionValue,const std::string & optionName,int & value)85 bool GetValueFromString(const std::string &optionValue, const std::string &optionName, int &value)
86 {
87 if (!IsStringToIntSuccess(optionValue, value)) {
88 return false;
89 }
90 HLOGD("get int result:'%s':'%d'", optionName.c_str(), value);
91 return true;
92 }
93
GetValueFromString(const std::string & optionValue,const std::string & optionName,float & value)94 bool GetValueFromString(const std::string &optionValue, const std::string &optionName, float &value)
95 {
96 char *endPtr = nullptr;
97 errno = 0;
98 value = std::strtof(optionValue.c_str(), &endPtr);
99 if (endPtr == optionValue.c_str() || *endPtr != '\0' || errno != 0) {
100 HLOGE("get float failed, optionValue: %s", optionValue.c_str());
101 return false;
102 }
103 HLOGD("get float result:'%s':'%f'", optionName.c_str(), value);
104 return true;
105 }
106
GetValueFromString(const std::string & optionValue,const std::string & optionName,uint64_t & value)107 bool GetValueFromString(const std::string& optionValue, const std::string& optionName, uint64_t& value)
108 {
109 char *endPtr = nullptr;
110 errno = 0;
111 value = std::strtoull(optionValue.c_str(), &endPtr, 10); // 10 : decimal scale
112 if (endPtr == optionValue.c_str() || *endPtr != '\0' || errno != 0) {
113 HLOGE("get uint64_t failed, optionValue: %s", optionValue.c_str());
114 return false;
115 }
116 HLOGD("get uint64_t result:'%s':'%" PRIu64 "'", optionName.c_str(), value);
117 return true;
118 }
119
GetValueFromString(const std::string & optionValue,const std::string & optionName,std::string & value)120 bool GetValueFromString(const std::string &optionValue, const std::string &optionName, std::string &value)
121 {
122 value = optionValue;
123 return true; // every thing done
124 }
125
GetValueFromString(const std::string & optionValue,const std::string & optionName,std::vector<int> & values)126 bool GetValueFromString(const std::string &optionValue, const std::string &optionName, std::vector<int> &values)
127 {
128 std::vector<std::string> stringValues = StringSplit(optionValue, ",");
129 HLOGD("split int result:'%s':'%s'", optionName.c_str(), VectorToString(stringValues).c_str());
130 while (!stringValues.empty()) {
131 int dest = 0;
132 if (!IsStringToIntSuccess(stringValues.front(), dest)) {
133 return false;
134 } else {
135 values.push_back(dest);
136 }
137 stringValues.erase(stringValues.begin()); // remove for next process
138 }
139 return values.size() > 0;
140 }
141
GetValueFromString(const std::string & optionValue,const std::string & optionName,std::vector<std::string> & values)142 bool GetValueFromString(const std::string &optionValue, const std::string &optionName, std::vector<std::string> &values)
143 {
144 values = StringSplit(optionValue, ",");
145 HLOGD("split string result:'%s':'%s' from '%s'", optionName.c_str(),
146 VectorToString(values).c_str(), optionValue.c_str());
147 return values.size() > 0; // convert successed ?
148 }
149
GetOptionTrackedCommand(argsVector & args,std::vector<std::string> & trackedCommand)150 bool GetOptionTrackedCommand(argsVector &args, std::vector<std::string> &trackedCommand)
151 {
152 if (!args.empty()) {
153 trackedCommand.insert(trackedCommand.begin(), args.begin(), args.end());
154 args.clear();
155 }
156 return true;
157 }
158
ClearMainOptions()159 void ClearMainOptions()
160 {
161 g_MainOptions.clear();
162 }
163
RegisterMainOption(const std::string & optionName,const std::string & help,std::function<bool (std::vector<std::string> &)> callBackFunction)164 bool RegisterMainOption(const std::string &optionName, const std::string &help,
165 std::function<bool(std::vector<std::string> &)> callBackFunction)
166 {
167 HLOGV("%s", optionName.c_str());
168 if (!CheckOptionFormat(optionName)) {
169 return false;
170 }
171
172 if (g_MainOptions.count(optionName) == 0) {
173 g_MainOptions[optionName] = std::make_unique<MainOption>();
174 g_MainOptions[optionName].get()->help = help;
175 g_MainOptions[optionName].get()->callBackFunction = std::move(callBackFunction);
176 return true;
177 } else {
178 HLOGE("main args %s already registered!", optionName.c_str());
179 return false;
180 }
181 }
182 } // namespace Option
183 } // namespace HiPerf
184 } // namespace Developtools
185 } // namespace OHOS
186