1 /**
2 * Copyright (c) 2021-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 <iostream>
17
18 #include "quick.h"
19
20 #include "libpandabase/utils/logger.h"
21 #include "libpandabase/utils/pandargs.h"
22 #include "libpandafile/file_writer.h"
23
24 namespace panda {
PrintHelp(panda::PandArgParser & pa_parser)25 void PrintHelp(panda::PandArgParser &pa_parser)
26 {
27 std::cerr << "Usage:" << std::endl;
28 std::cerr << "arkquicker [options] INPUT_FILE OUTPUT_FILE" << std::endl << std::endl;
29 std::cerr << "Supported options:" << std::endl << std::endl;
30 std::cerr << pa_parser.GetHelpString() << std::endl;
31 }
32
ProcessArgs(panda::PandArgParser & pa_parser,const panda::PandArg<std::string> & input,const panda::PandArg<std::string> & output,const panda::PandArg<bool> & help,int argc,const char ** argv)33 bool ProcessArgs(panda::PandArgParser &pa_parser, const panda::PandArg<std::string> &input,
34 const panda::PandArg<std::string> &output, const panda::PandArg<bool> &help, int argc,
35 const char **argv)
36 {
37 if (!pa_parser.Parse(argc, argv)) {
38 PrintHelp(pa_parser);
39 return false;
40 }
41
42 if (input.GetValue().empty() || output.GetValue().empty() || help.GetValue()) {
43 PrintHelp(pa_parser);
44 return false;
45 }
46
47 panda::Logger::InitializeStdLogging(panda::Logger::Level::ERROR, panda::Logger::ComponentMask()
48 .set(panda::Logger::Component::QUICKENER)
49 .set(panda::Logger::Component::PANDAFILE));
50
51 return true;
52 }
53
main(int argc,const char ** argv)54 int main(int argc, const char **argv)
55 {
56 panda::PandArg<bool> help("help", false, "Print this message and exit");
57 panda::PandArg<std::string> input("INPUT", "", "Path to the input binary file");
58 panda::PandArg<std::string> output("OUTPUT", "", "Path to the output binary file");
59
60 panda::PandArgParser pa_parser;
61
62 pa_parser.Add(&help);
63 pa_parser.PushBackTail(&input);
64 pa_parser.PushBackTail(&output);
65 pa_parser.EnableTail();
66
67 if (!ProcessArgs(pa_parser, input, output, help, argc, argv)) {
68 return 1;
69 }
70
71 auto input_file = panda::panda_file::File::Open(input.GetValue());
72 if (!input_file) {
73 LOG(ERROR, QUICKENER) << "Cannot open file '" << input.GetValue() << "'";
74 return 1;
75 }
76 panda::panda_file::FileReader reader(std::move(input_file));
77 if (!reader.ReadContainer()) {
78 LOG(ERROR, QUICKENER) << "Cannot read container";
79 return 1;
80 }
81
82 panda::panda_file::ItemContainer *container = reader.GetContainerPtr();
83 auto writer = panda::panda_file::FileWriter(output.GetValue());
84 if (!writer) {
85 PLOG(ERROR, QUICKENER) << "Cannot create file writer with path '" << output.GetValue() << "'";
86 return 1;
87 }
88
89 if (!container->Write(&writer, false)) {
90 PLOG(ERROR, QUICKENER) << "Cannot write panda file '" << output.GetValue() << "'";
91 return 1;
92 }
93
94 return 0;
95 }
96 } // namespace panda
97