1 /*
2  * Copyright (c) 2023 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 "drawing_command.h"
17 #include <sstream>
18 #include <vector>
19 
20 namespace OHOS {
21 namespace Rosen {
DCLCommand(int32_t argc,char * argv[])22 DCLCommand::DCLCommand(int32_t argc, char* argv[])
23 {
24     std::vector<std::string> argvNew(argv, argv + argc);
25     ParseCommand(argvNew);
26 }
27 
ParseCommand(std::vector<std::string> argv)28 void DCLCommand::ParseCommand(std::vector<std::string> argv)
29 {
30     const size_t twoParam = 2;
31     const size_t threeParam = 3;
32     switch (argv.size()) {
33         case twoParam:
34             std::cout << "iterate frame by default, beginFrame = " << beginFrame_ << ", endFrame = " <<
35             endFrame_ << std::endl;
36             break;
37         case threeParam:
38             if (strcmp(argv.back().c_str(), "--help") != 0 || strcmp(argv.back().c_str(), "-h") != 0) {
39                 std::cout << dclMsg_ << std::endl;
40             }
41             break;
42         default:
43             for (size_t i = 2; i < argv.size(); ++i) {
44                 std::string option = argv[i];
45                 std::string augment;
46                 if (i < argv.size() - 1) {
47                     augment = argv[i + 1];
48                 }
49                 if (commandMap_.count(option) > 0) {
50                     HandleCommand(option, augment);
51                 }
52             }
53             CheckParameter();
54             break;
55     }
56 }
57 
DCLCommand(std::string commandLine)58 DCLCommand::DCLCommand(std::string commandLine)
59 {
60     std::istringstream ss(commandLine);
61     std::string param;
62     std::vector<std::string> params;
63     if (commandLine.find("drawing_ending_sample") == std::string::npos) {
64         params.emplace_back("drawing_engine_sample");
65         params.emplace_back("dcl");
66     }
67     while (ss >> param) {
68         params.emplace_back(param);
69     }
70     ParseCommand(params);
71 }
72 
HandleCommandIterateType(const std::string & inputStr)73 void DCLCommand::HandleCommandIterateType(const std::string& inputStr)
74 {
75     switch (std::stoi(inputStr.c_str())) {
76         case static_cast<int>(IterateType::ITERATE_FRAME):
77             iterateType_ = IterateType::ITERATE_FRAME;
78             break;
79         case static_cast<int>(IterateType::ITERATE_OPITEM):
80             iterateType_ = IterateType::ITERATE_OPITEM;
81             break;
82         case static_cast<int>(IterateType::ITERATE_OPITEM_MANUALLY):
83             iterateType_ = IterateType::ITERATE_OPITEM_MANUALLY;
84             break;
85         case static_cast<int>(IterateType::REPLAY_MSKP):
86             iterateType_ = IterateType::REPLAY_MSKP;
87             break;
88         case static_cast<int>(IterateType::REPLAY_SKP):
89             iterateType_ = IterateType::REPLAY_SKP;
90             break;
91         default:
92             std::cout <<"Wrong Parameter: iterateType" << std::endl;
93             return;
94     }
95 }
96 
HandleCommand(std::string option,const std::string & augment)97 void DCLCommand::HandleCommand(std::string option, const std::string& augment)
98 {
99     int inputNum = 0;
100     switch (commandMap_.at(option)) {
101         case CommandType::CT_T:
102             HandleCommandIterateType(augment);
103             break;
104         case CommandType::CT_B:
105             inputNum = std::stoi(augment.c_str());
106             beginFrame_ = inputNum > 0 ? inputNum : 0;
107             break;
108         case CommandType::CT_E:
109             inputNum = std::stoi(augment.c_str());
110             endFrame_ = inputNum > 0 ? inputNum : 0;
111             break;
112         case CommandType::CT_L:
113             inputNum = std::stoi(augment.c_str());
114             loop_ = inputNum > 0 ? inputNum : 0;
115             break;
116         case CommandType::CT_S:
117             opItemStep_ = std::stod(augment.c_str());
118             opItemStep_ = opItemStep_ > 0 ? opItemStep_ : 1;
119             break;
120         case CommandType::CT_I:
121             inputFilePath_ = augment;
122             if (inputFilePath_.back() != '/') {
123                 inputFilePath_ += '/';
124             }
125             break;
126         case CommandType::CT_O:
127             outputFilePath_ = augment;
128             if (outputFilePath_.back() != '/') {
129                 outputFilePath_ += '/';
130             }
131             break;
132         case CommandType::CT_H:
133             std::cout << dclMsg_ <<std::endl;
134             break;
135         default:
136             std::cout << "other unknown args:" <<std::endl;
137             break;
138     }
139 }
140 
CheckParameter()141 void DCLCommand::CheckParameter()
142 {
143     if (beginFrame_ > endFrame_) {
144         std::cout << "Wrong Parameter: beginFrame or endFrame!" << std::endl;
145         beginFrame_ = 0;
146         endFrame_ = 0;
147     }
148     if (opItemStep_ < 0) {
149         std::cout << "Wrong Parameter: opItemStep!" << std::endl;
150         opItemStep_ = 1;
151     }
152 }
153 }
154 }