1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <fstream>
18 #include <iostream>
19 #include <memory>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23 
24 #include "idmap2/CommandLineOptions.h"
25 #include "idmap2/Idmap.h"
26 #include "idmap2/PrettyPrintVisitor.h"
27 #include "idmap2/RawPrintVisitor.h"
28 #include "idmap2/Result.h"
29 #include "idmap2/SysTrace.h"
30 
31 using android::idmap2::CommandLineOptions;
32 using android::idmap2::Error;
33 using android::idmap2::Idmap;
34 using android::idmap2::PrettyPrintVisitor;
35 using android::idmap2::RawPrintVisitor;
36 using android::idmap2::Result;
37 using android::idmap2::Unit;
38 
Dump(const std::vector<std::string> & args)39 Result<Unit> Dump(const std::vector<std::string>& args) {
40   SYSTRACE << "Dump " << args;
41   std::string idmap_path;
42   bool verbose = false;
43 
44   const CommandLineOptions opts =
45       CommandLineOptions("idmap2 dump")
46           .MandatoryOption("--idmap-path", "input: path to idmap file to pretty-print", &idmap_path)
47           .OptionalFlag("--verbose", "annotate every byte of the idmap", &verbose);
48   const auto opts_ok = opts.Parse(args);
49   if (!opts_ok) {
50     return opts_ok.GetError();
51   }
52   std::ifstream fin(idmap_path);
53   const auto idmap = Idmap::FromBinaryStream(fin);
54   fin.close();
55   if (!idmap) {
56     return Error(idmap.GetError(), "failed to load idmap");
57   }
58 
59   if (verbose) {
60     RawPrintVisitor visitor(std::cout);
61     (*idmap)->accept(&visitor);
62   } else {
63     PrettyPrintVisitor visitor(std::cout);
64     (*idmap)->accept(&visitor);
65   }
66 
67   return Unit{};
68 }
69