OLD | NEW |
| (Empty) |
1 // Protocol Buffers - Google's data interchange format | |
2 // Copyright 2008 Google Inc. All rights reserved. | |
3 // https://developers.google.com/protocol-buffers/ | |
4 // | |
5 // Redistribution and use in source and binary forms, with or without | |
6 // modification, are permitted provided that the following conditions are | |
7 // met: | |
8 // | |
9 // * Redistributions of source code must retain the above copyright | |
10 // notice, this list of conditions and the following disclaimer. | |
11 // * Redistributions in binary form must reproduce the above | |
12 // copyright notice, this list of conditions and the following disclaimer | |
13 // in the documentation and/or other materials provided with the | |
14 // distribution. | |
15 // * Neither the name of Google Inc. nor the names of its | |
16 // contributors may be used to endorse or promote products derived from | |
17 // this software without specific prior written permission. | |
18 // | |
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
30 | |
31 #include <cassert> | |
32 #include <cstdlib> | |
33 #include <fstream> | |
34 #include <iostream> | |
35 #include <string> | |
36 | |
37 const char output_file[] = "well_known_types_embed.cc"; | |
38 | |
39 static bool AsciiIsPrint(unsigned char c) { | |
40 return c >= 32 && c < 127; | |
41 } | |
42 | |
43 static char ToDecimalDigit(int num) { | |
44 assert(num < 10); | |
45 return '0' + num; | |
46 } | |
47 | |
48 static std::string CEscape(const std::string& str) { | |
49 std::string dest; | |
50 | |
51 for (unsigned char ch : str) { | |
52 switch (ch) { | |
53 case '\n': dest += "\\n"; break; | |
54 case '\r': dest += "\\r"; break; | |
55 case '\t': dest += "\\t"; break; | |
56 case '\"': dest += "\\\""; break; | |
57 case '\\': dest += "\\\\"; break; | |
58 default: | |
59 if (AsciiIsPrint(ch)) { | |
60 dest += ch; | |
61 } else { | |
62 dest += "\\"; | |
63 dest += ToDecimalDigit(ch / 64); | |
64 dest += ToDecimalDigit((ch % 64) / 8); | |
65 dest += ToDecimalDigit(ch % 8); | |
66 } | |
67 break; | |
68 } | |
69 } | |
70 | |
71 return dest; | |
72 } | |
73 | |
74 static void AddFile(const char* name, std::basic_ostream<char>* out) { | |
75 std::ifstream in(name); | |
76 | |
77 if (!in.is_open()) { | |
78 std::cerr << "Couldn't open input file: " << name << "\n"; | |
79 std::exit(EXIT_FAILURE); | |
80 } | |
81 | |
82 // Make canonical name only include the final element. | |
83 for (const char *p = name; *p; p++) { | |
84 if (*p == '/') { | |
85 name = p + 1; | |
86 } | |
87 } | |
88 | |
89 *out << "{\"" << CEscape(name) << "\",\n"; | |
90 | |
91 for (std::string line; std::getline(in, line); ) { | |
92 *out << " \"" << CEscape(line) << "\\n\"\n"; | |
93 } | |
94 | |
95 *out << "},\n"; | |
96 } | |
97 | |
98 int main(int argc, char *argv[]) { | |
99 auto& out = std::cout; | |
100 | |
101 out << "#include " | |
102 "\"google/protobuf/compiler/js/well_known_types_embed.h\"\n"; | |
103 out << "struct FileToc well_known_types_js[] = {\n"; | |
104 | |
105 for (int i = 1; i < argc; i++) { | |
106 AddFile(argv[i], &out); | |
107 } | |
108 | |
109 out << " {NULL, NULL} // Terminate the list.\n"; | |
110 out << "};\n"; | |
111 | |
112 return EXIT_SUCCESS; | |
113 } | |
OLD | NEW |