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