1cf69771bSopenharmony_ci/* 2cf69771bSopenharmony_ci * Copyright (c) 2022-2022 Huawei Device Co., Ltd. 3cf69771bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4cf69771bSopenharmony_ci * you may not use this file except in compliance with the License. 5cf69771bSopenharmony_ci * You may obtain a copy of the License at 6cf69771bSopenharmony_ci * 7cf69771bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8cf69771bSopenharmony_ci * 9cf69771bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10cf69771bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11cf69771bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12cf69771bSopenharmony_ci * See the License for the specific language governing permissions and 13cf69771bSopenharmony_ci * limitations under the License. 14cf69771bSopenharmony_ci */ 15cf69771bSopenharmony_ci 16cf69771bSopenharmony_ci#include "time_cmd_dispatcher.h" 17cf69771bSopenharmony_ci 18cf69771bSopenharmony_ci#include <cstdio> 19cf69771bSopenharmony_ci#include <iostream> 20cf69771bSopenharmony_ci#include "time_hilog.h" 21cf69771bSopenharmony_ci 22cf69771bSopenharmony_cinamespace OHOS { 23cf69771bSopenharmony_cinamespace MiscServices { 24cf69771bSopenharmony_cibool TimeCmdDispatcher::Dispatch(int fd, const std::vector<std::string> &args) 25cf69771bSopenharmony_ci{ 26cf69771bSopenharmony_ci if (args.empty() || args.at(0) == "-h") { 27cf69771bSopenharmony_ci dprintf(fd, "\n%-15s: %-20s", "Option", "Description"); 28cf69771bSopenharmony_ci for (auto &[key, handler] : cmdHandler) { 29cf69771bSopenharmony_ci dprintf(fd, "\n%-15s: %-20s", handler->GetFormat().c_str(), handler->ShowHelp().c_str()); 30cf69771bSopenharmony_ci } 31cf69771bSopenharmony_ci return false; 32cf69771bSopenharmony_ci } 33cf69771bSopenharmony_ci std::string cmdTitle = getCmdTitle(fd, args); 34cf69771bSopenharmony_ci auto handler = cmdHandler.find(cmdTitle); 35cf69771bSopenharmony_ci if (handler != cmdHandler.end()) { 36cf69771bSopenharmony_ci handler->second->DoAction(fd, args); 37cf69771bSopenharmony_ci return true; 38cf69771bSopenharmony_ci } 39cf69771bSopenharmony_ci return false; 40cf69771bSopenharmony_ci} 41cf69771bSopenharmony_ci 42cf69771bSopenharmony_civoid TimeCmdDispatcher::RegisterCommand(std::shared_ptr<TimeCmdParse> &cmd) 43cf69771bSopenharmony_ci{ 44cf69771bSopenharmony_ci cmdHandler.insert(std::make_pair(cmd->GetOption(), cmd)); 45cf69771bSopenharmony_ci} 46cf69771bSopenharmony_ci 47cf69771bSopenharmony_cistd::string TimeCmdDispatcher::getCmdTitle(int fd, const std::vector<std::string> &args) 48cf69771bSopenharmony_ci{ 49cf69771bSopenharmony_ci std::string formatTitle; 50cf69771bSopenharmony_ci if (args.size() == 0) { 51cf69771bSopenharmony_ci return formatTitle; 52cf69771bSopenharmony_ci } 53cf69771bSopenharmony_ci for (unsigned long i = 0; i < args.size() - 1; i++) { 54cf69771bSopenharmony_ci formatTitle += args.at(i); 55cf69771bSopenharmony_ci formatTitle += " "; 56cf69771bSopenharmony_ci } 57cf69771bSopenharmony_ci return formatTitle.substr(0, formatTitle.length() - 1); 58cf69771bSopenharmony_ci dprintf(fd, "TimeCmdDispatcher::getCmdTitle formatTitle:%s.\n", formatTitle.c_str()); 59cf69771bSopenharmony_ci} 60cf69771bSopenharmony_ci 61cf69771bSopenharmony_ciTimeCmdDispatcher &TimeCmdDispatcher::GetInstance() 62cf69771bSopenharmony_ci{ 63cf69771bSopenharmony_ci static TimeCmdDispatcher instance; 64cf69771bSopenharmony_ci return instance; 65cf69771bSopenharmony_ci} 66cf69771bSopenharmony_ci} // namespace MiscServices 67cf69771bSopenharmony_ci} // namespace OHOS