1 /*
2  * Copyright (C) 2020 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 
20 #include <json/json.h>
21 
main(int argc,char ** argv)22 int main(int argc, char** argv) {
23   if (argc != 2) {
24     std::cerr << "Usage: aidl_metadata_parser *.json" << std::endl;
25     return EXIT_FAILURE;
26   }
27   const std::string path = argv[1];
28 
29   Json::Value root;
30   Json::CharReaderBuilder builder;
31 
32   std::ifstream stream(path);
33   std::string errorMessage;
34   if (!Json::parseFromStream(builder, stream, &root, &errorMessage)) {
35     std::cerr << "Failed to read interface metadata file: " << path << std::endl
36               << errorMessage << std::endl;
37     return EXIT_FAILURE;
38   }
39 
40   std::cout << "#include <aidl/metadata.h>" << std::endl;
41   std::cout << "namespace android {" << std::endl;
42   std::cout << "std::vector<AidlInterfaceMetadata> AidlInterfaceMetadata::all() {" << std::endl;
43   std::cout << "return std::vector<AidlInterfaceMetadata>{" << std::endl;
44   for (const Json::Value& entry : root) {
45     std::cout << "AidlInterfaceMetadata{" << std::endl;
46     // AIDL interface characters guaranteed to be accepted in C++ string
47     std::cout << "std::string(\"" << entry["name"].asString() << "\")," << std::endl;
48     std::cout << "std::string(\"" << entry["stability"].asString() << "\")," << std::endl;
49     std::cout << "std::vector<std::string>{" << std::endl;
50     for (const Json::Value& intf : entry["types"]) {
51       std::cout << "std::string(\"" << intf.asString() << "\")," << std::endl;
52     }
53     std::cout << "}," << std::endl;
54     std::cout << "std::vector<std::string>{" << std::endl;
55     for (const Json::Value& intf : entry["hashes"]) {
56       std::cout << "std::string(\"" << intf.asString() << "\")," << std::endl;
57     }
58     std::cout << "}," << std::endl;
59     std::cout << "}," << std::endl;
60   }
61   std::cout << "};" << std::endl;
62   std::cout << "}" << std::endl;
63   std::cout << "}  // namespace android" << std::endl;
64   return EXIT_SUCCESS;
65 }
66