1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // http://code.google.com/p/protobuf/ 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 // TODO(kenton): Overloaded versions with more variables? Two seems 86 // to be enough. 87 88 // Indent text by two spaces. After calling Indent(), two spaces will be 89 // inserted at the beginning of each line of text. Indent() may be called 90 // multiple times to produce deeper indents. 91 void Indent(); 92 93 // Reduces the current indent level by two spaces, or crashes if the indent 94 // level is zero. 95 void Outdent(); 96 97 // Write a string to the output buffer. 98 // This method does not look for newlines to add indentation. 99 void PrintRaw(const string& data); 100 101 // Write a zero-delimited string to output buffer. 102 // This method does not look for newlines to add indentation. 103 void PrintRaw(const char* data); 104 105 // Write some bytes to the output buffer. 106 // This method does not look for newlines to add indentation. 107 void WriteRaw(const char* data, int size); 108 109 // True if any write to the underlying stream failed. (We don't just 110 // crash in this case because this is an I/O failure, not a programming 111 // error.) failed()112 bool failed() const { return failed_; } 113 114 private: 115 const char variable_delimiter_; 116 117 ZeroCopyOutputStream* const output_; 118 char* buffer_; 119 int buffer_size_; 120 121 string indent_; 122 bool at_start_of_line_; 123 bool failed_; 124 125 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Printer); 126 }; 127 128 } // namespace io 129 } // namespace protobuf 130 131 } // namespace google 132 #endif // GOOGLE_PROTOBUF_IO_PRINTER_H__ 133