blob: f1490bbe5b1a48206e9700f9e89ab9e9f0c61b4f [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
temporal40ee5512008-07-10 02:12:20 +00004//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * 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.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// 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.
temporal40ee5512008-07-10 02:12:20 +000030
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
44namespace google {
45namespace protobuf {
46namespace io {
47
48class 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)
kenton@google.comfccb1462009-12-18 02:11:36 +000062// in the case of undefined variables in debug builds. This helps greatly in
63// debugging code which uses it.
temporal40ee5512008-07-10 02:12:20 +000064class 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);
liujisi@google.com33165fe2010-11-02 13:14:58 +000085 // 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);
Feng Xiao6ef984a2014-11-10 17:34:54 -080089 // 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);
Bo Yang5db21732015-05-21 14:28:59 -070094 // 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);
temporal40ee5512008-07-10 02:12:20 +0000124
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
kenton@google.comfccb1462009-12-18 02:11:36 +0000134 // 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
temporal40ee5512008-07-10 02:12:20 +0000146 // 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:
temporal40ee5512008-07-10 02:12:20 +0000152 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__