Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame^] | 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 | // Author: kenton@google.com (Kenton Varda) |
| 32 | // Based on original Protocol Buffers design by |
| 33 | // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | // |
| 35 | // Utility class for writing text to a ZeroCopyOutputStream. |
| 36 | |
| 37 | #ifndef GOOGLE_PROTOBUF_IO_PRINTER_H__ |
| 38 | #define GOOGLE_PROTOBUF_IO_PRINTER_H__ |
| 39 | |
| 40 | #include <string> |
| 41 | #include <map> |
| 42 | #include <google/protobuf/stubs/common.h> |
| 43 | |
| 44 | namespace google { |
| 45 | namespace protobuf { |
| 46 | namespace io { |
| 47 | |
| 48 | class ZeroCopyOutputStream; // zero_copy_stream.h |
| 49 | |
| 50 | // This simple utility class assists in code generation. It basically |
| 51 | // allows the caller to define a set of variables and then output some |
| 52 | // text with variable substitutions. Example usage: |
| 53 | // |
| 54 | // Printer printer(output, '$'); |
| 55 | // map<string, string> vars; |
| 56 | // vars["name"] = "Bob"; |
| 57 | // printer.Print(vars, "My name is $name$."); |
| 58 | // |
| 59 | // The above writes "My name is Bob." to the output stream. |
| 60 | // |
| 61 | // Printer aggressively enforces correct usage, crashing (with assert failures) |
| 62 | // in the case of undefined variables in debug builds. This helps greatly in |
| 63 | // debugging code which uses it. |
| 64 | class LIBPROTOBUF_EXPORT Printer { |
| 65 | public: |
| 66 | // Create a printer that writes text to the given output stream. Use the |
| 67 | // given character as the delimiter for variables. |
| 68 | Printer(ZeroCopyOutputStream* output, char variable_delimiter); |
| 69 | ~Printer(); |
| 70 | |
| 71 | // Print some text after applying variable substitutions. If a particular |
| 72 | // variable in the text is not defined, this will crash. Variables to be |
| 73 | // substituted are identified by their names surrounded by delimiter |
| 74 | // characters (as given to the constructor). The variable bindings are |
| 75 | // defined by the given map. |
| 76 | void Print(const map<string, string>& variables, const char* text); |
| 77 | |
| 78 | // Like the first Print(), except the substitutions are given as parameters. |
| 79 | void Print(const char* text); |
| 80 | // Like the first Print(), except the substitutions are given as parameters. |
| 81 | void Print(const char* text, const char* variable, const string& value); |
| 82 | // Like the first Print(), except the substitutions are given as parameters. |
| 83 | void Print(const char* text, const char* variable1, const string& value1, |
| 84 | const char* variable2, const string& value2); |
| 85 | // Like the first Print(), except the substitutions are given as parameters. |
| 86 | void Print(const char* text, const char* variable1, const string& value1, |
| 87 | const char* variable2, const string& value2, |
| 88 | const char* variable3, const string& value3); |
| 89 | // Like the first Print(), except the substitutions are given as parameters. |
| 90 | void Print(const char* text, const char* variable1, const string& value1, |
| 91 | const char* variable2, const string& value2, |
| 92 | const char* variable3, const string& value3, |
| 93 | const char* variable4, const string& value4); |
| 94 | // Like the first Print(), except the substitutions are given as parameters. |
| 95 | void Print(const char* text, const char* variable1, const string& value1, |
| 96 | const char* variable2, const string& value2, |
| 97 | const char* variable3, const string& value3, |
| 98 | const char* variable4, const string& value4, |
| 99 | const char* variable5, const string& value5); |
| 100 | // Like the first Print(), except the substitutions are given as parameters. |
| 101 | void Print(const char* text, const char* variable1, const string& value1, |
| 102 | const char* variable2, const string& value2, |
| 103 | const char* variable3, const string& value3, |
| 104 | const char* variable4, const string& value4, |
| 105 | const char* variable5, const string& value5, |
| 106 | const char* variable6, const string& value6); |
| 107 | // Like the first Print(), except the substitutions are given as parameters. |
| 108 | void Print(const char* text, const char* variable1, const string& value1, |
| 109 | const char* variable2, const string& value2, |
| 110 | const char* variable3, const string& value3, |
| 111 | const char* variable4, const string& value4, |
| 112 | const char* variable5, const string& value5, |
| 113 | const char* variable6, const string& value6, |
| 114 | const char* variable7, const string& value7); |
| 115 | // Like the first Print(), except the substitutions are given as parameters. |
| 116 | void Print(const char* text, const char* variable1, const string& value1, |
| 117 | const char* variable2, const string& value2, |
| 118 | const char* variable3, const string& value3, |
| 119 | const char* variable4, const string& value4, |
| 120 | const char* variable5, const string& value5, |
| 121 | const char* variable6, const string& value6, |
| 122 | const char* variable7, const string& value7, |
| 123 | const char* variable8, const string& value8); |
| 124 | |
| 125 | // Indent text by two spaces. After calling Indent(), two spaces will be |
| 126 | // inserted at the beginning of each line of text. Indent() may be called |
| 127 | // multiple times to produce deeper indents. |
| 128 | void Indent(); |
| 129 | |
| 130 | // Reduces the current indent level by two spaces, or crashes if the indent |
| 131 | // level is zero. |
| 132 | void Outdent(); |
| 133 | |
| 134 | // Write a string to the output buffer. |
| 135 | // This method does not look for newlines to add indentation. |
| 136 | void PrintRaw(const string& data); |
| 137 | |
| 138 | // Write a zero-delimited string to output buffer. |
| 139 | // This method does not look for newlines to add indentation. |
| 140 | void PrintRaw(const char* data); |
| 141 | |
| 142 | // Write some bytes to the output buffer. |
| 143 | // This method does not look for newlines to add indentation. |
| 144 | void WriteRaw(const char* data, int size); |
| 145 | |
| 146 | // True if any write to the underlying stream failed. (We don't just |
| 147 | // crash in this case because this is an I/O failure, not a programming |
| 148 | // error.) |
| 149 | bool failed() const { return failed_; } |
| 150 | |
| 151 | private: |
| 152 | const char variable_delimiter_; |
| 153 | |
| 154 | ZeroCopyOutputStream* const output_; |
| 155 | char* buffer_; |
| 156 | int buffer_size_; |
| 157 | |
| 158 | string indent_; |
| 159 | bool at_start_of_line_; |
| 160 | bool failed_; |
| 161 | |
| 162 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Printer); |
| 163 | }; |
| 164 | |
| 165 | } // namespace io |
| 166 | } // namespace protobuf |
| 167 | |
| 168 | } // namespace google |
| 169 | #endif // GOOGLE_PROTOBUF_IO_PRINTER_H__ |