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 (int i = 0; i < str.size(); ++i) { |
| 52 unsigned char ch = str[i]; |
| 53 switch (ch) { |
| 54 case '\n': dest += "\\n"; break; |
| 55 case '\r': dest += "\\r"; break; |
| 56 case '\t': dest += "\\t"; break; |
| 57 case '\"': dest += "\\\""; break; |
| 58 case '\\': dest += "\\\\"; break; |
| 59 default: |
| 60 if (AsciiIsPrint(ch)) { |
| 61 dest += ch; |
| 62 } else { |
| 63 dest += "\\"; |
| 64 dest += ToDecimalDigit(ch / 64); |
| 65 dest += ToDecimalDigit((ch % 64) / 8); |
| 66 dest += ToDecimalDigit(ch % 8); |
| 67 } |
| 68 break; |
| 69 } |
| 70 } |
| 71 |
| 72 return dest; |
| 73 } |
| 74 |
| 75 static void AddFile(const char* name, std::basic_ostream<char>* out) { |
| 76 std::ifstream in(name); |
| 77 |
| 78 if (!in.is_open()) { |
| 79 std::cerr << "Couldn't open input file: " << name << "\n"; |
| 80 std::exit(EXIT_FAILURE); |
| 81 } |
| 82 |
| 83 // Make canonical name only include the final element. |
| 84 for (const char *p = name; *p; p++) { |
| 85 if (*p == '/') { |
| 86 name = p + 1; |
| 87 } |
| 88 } |
| 89 |
| 90 *out << "{\"" << CEscape(name) << "\",\n"; |
| 91 |
| 92 for (std::string line; std::getline(in, line); ) { |
| 93 *out << " \"" << CEscape(line) << "\\n\"\n"; |
| 94 } |
| 95 |
| 96 *out << "},\n"; |
| 97 } |
| 98 |
| 99 int main(int argc, char *argv[]) { |
| 100 std::cout << "#include " |
| 101 "<google/protobuf/compiler/js/well_known_types_embed.h>\n"; |
| 102 std::cout << "struct FileToc well_known_types_js[] = {\n"; |
| 103 |
| 104 for (int i = 1; i < argc; i++) { |
| 105 AddFile(argv[i], &std::cout); |
| 106 } |
| 107 |
| 108 std::cout << " {NULL, NULL} // Terminate the list.\n"; |
| 109 std::cout << "};\n"; |
| 110 |
| 111 return EXIT_SUCCESS; |
| 112 } |
OLD | NEW |