• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2022 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 "time_cmd_dispatcher.h"
17 
18 #include <cstdio>
19 #include <iostream>
20 #include "time_hilog.h"
21 
22 namespace OHOS {
23 namespace MiscServices {
Dispatch(int fd,const std::vector<std::string> & args)24 bool TimeCmdDispatcher::Dispatch(int fd, const std::vector<std::string> &args)
25 {
26     if (args.empty() || args.at(0) == "-h") {
27         dprintf(fd, "\n%-15s: %-20s", "Option", "Description");
28         for (auto &[key, handler] : cmdHandler) {
29             dprintf(fd, "\n%-15s: %-20s", handler->GetFormat().c_str(), handler->ShowHelp().c_str());
30         }
31         return false;
32     }
33     std::string cmdTitle = getCmdTitle(fd, args);
34     auto handler = cmdHandler.find(cmdTitle);
35     if (handler != cmdHandler.end()) {
36         handler->second->DoAction(fd, args);
37         return true;
38     }
39     return false;
40 }
41 
RegisterCommand(std::shared_ptr<TimeCmdParse> & cmd)42 void TimeCmdDispatcher::RegisterCommand(std::shared_ptr<TimeCmdParse> &cmd)
43 {
44     cmdHandler.insert(std::make_pair(cmd->GetOption(), cmd));
45 }
46 
getCmdTitle(int fd,const std::vector<std::string> & args)47 std::string TimeCmdDispatcher::getCmdTitle(int fd, const std::vector<std::string> &args)
48 {
49     std::string formatTitle;
50     for (unsigned long i = 0; i < args.size() - 1; i++) {
51         formatTitle += args.at(i);
52         formatTitle += " ";
53     }
54     return formatTitle.substr(0, formatTitle.length() - 1);
55     dprintf(fd, "TimeCmdDispatcher::getCmdTitle formatTitle:%s.\n", formatTitle.c_str());
56 }
57 
GetInstance()58 TimeCmdDispatcher &TimeCmdDispatcher::GetInstance()
59 {
60     static TimeCmdDispatcher instance;
61     return instance;
62 }
63 } // namespace MiscServices
64 } // namespace OHOS