• 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 "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(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     try {
89         value = std::stoi(optionValue);
90         HLOGD("get int result:'%s':'%d'", optionName.c_str(), value);
91         return true;
92     } catch (...) {
93         // what can we do here ?
94     }
95     return false;
96 }
97 
GetValueFromString(const std::string & optionValue,const std::string & optionName,float & value)98 bool GetValueFromString(const std::string &optionValue, const std::string &optionName, float &value)
99 {
100     try {
101         value = std::stof(optionValue);
102         HLOGD("get float result:'%s':'%f'", optionName.c_str(), value);
103         return true;
104     } catch (...) {
105         // what can we do here ?
106     }
107     return false;
108 }
109 
GetValueFromString(const std::string & optionValue,const std::string & optionName,std::string & value)110 bool GetValueFromString(const std::string &optionValue, const std::string &optionName,
111                         std::string &value)
112 {
113     value = optionValue;
114     return true; // every thing done
115 }
116 
GetValueFromString(const std::string & optionValue,const std::string & optionName,std::vector<int> & values)117 bool GetValueFromString(const std::string &optionValue, const std::string &optionName,
118                         std::vector<int> &values)
119 {
120     std::vector<std::string> stringValues = StringSplit(optionValue, ",");
121     HLOGD("split int result:'%s':'%s'", optionName.c_str(), VectorToString(stringValues).c_str());
122     try {
123         while (!stringValues.empty()) {
124             values.push_back(std::stoi(stringValues.front()));
125             stringValues.erase(stringValues.begin()); // remove for next process
126         }
127         return values.size() > 0; // convert successed ?
128     } catch (...) {
129         // what can we do here ?
130         HLOGD("stoi failed with %s", stringValues.front().c_str());
131     }
132     return false;
133 }
134 
GetValueFromString(const std::string & optionValue,const std::string & optionName,std::vector<std::string> & values)135 bool GetValueFromString(const std::string &optionValue, const std::string &optionName,
136                         std::vector<std::string> &values)
137 {
138     values = StringSplit(optionValue, ",");
139     HLOGD("split string result:'%s':'%s' from '%s'", optionName.c_str(),
140           VectorToString(values).c_str(), optionValue.c_str());
141     return values.size() > 0; // convert successed ?
142 }
143 
GetOptionTrackedCommand(std::vector<std::string> & args,std::vector<std::string> & trackedCommand)144 bool GetOptionTrackedCommand(std::vector<std::string> &args,
145                              std::vector<std::string> &trackedCommand)
146 {
147     if (!args.empty()) {
148         trackedCommand.insert(trackedCommand.begin(), args.begin(), args.end());
149         args.clear();
150     }
151     return true;
152 }
153 
ClearMainOptions()154 void ClearMainOptions()
155 {
156     g_MainOptions.clear();
157 }
158 
RegisterMainOption(const std::string & optionName,const std::string & help,std::function<bool (std::vector<std::string> &)> callBackFunction)159 bool RegisterMainOption(const std::string &optionName, const std::string &help,
160                         std::function<bool(std::vector<std::string> &)> callBackFunction)
161 {
162     HLOGV("%s", optionName.c_str());
163     if (!CheckOptionFormat(optionName)) {
164         return false;
165     }
166 
167     if (g_MainOptions.count(optionName) == 0) {
168         g_MainOptions[optionName] = std::make_unique<MainOption>();
169         g_MainOptions[optionName].get()->help = help;
170         g_MainOptions[optionName].get()->callBackFunction = std::move(callBackFunction);
171         return true;
172     } else {
173         HLOGE("main args %s already registered!", optionName.c_str());
174         return false;
175     }
176 }
177 } // namespace Option
178 } // namespace HiPerf
179 } // namespace Developtools
180 } // namespace OHOS
181