blob: 5be48543ab04c5cd136ba5b0f916ffcc059892d2 [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.
temporal40ee5512008-07-10 02:12:20 +00003// http://code.google.com/p/protobuf/
4//
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);
89 // TODO(kenton): Overloaded versions with more variables? Three seems
temporal40ee5512008-07-10 02:12:20 +000090 // to be enough.
91
92 // Indent text by two spaces. After calling Indent(), two spaces will be
93 // inserted at the beginning of each line of text. Indent() may be called
94 // multiple times to produce deeper indents.
95 void Indent();
96
97 // Reduces the current indent level by two spaces, or crashes if the indent
98 // level is zero.
99 void Outdent();
100
kenton@google.comfccb1462009-12-18 02:11:36 +0000101 // Write a string to the output buffer.
102 // This method does not look for newlines to add indentation.
103 void PrintRaw(const string& data);
104
105 // Write a zero-delimited string to output buffer.
106 // This method does not look for newlines to add indentation.
107 void PrintRaw(const char* data);
108
109 // Write some bytes to the output buffer.
110 // This method does not look for newlines to add indentation.
111 void WriteRaw(const char* data, int size);
112
temporal40ee5512008-07-10 02:12:20 +0000113 // True if any write to the underlying stream failed. (We don't just
114 // crash in this case because this is an I/O failure, not a programming
115 // error.)
116 bool failed() const { return failed_; }
117
118 private:
temporal40ee5512008-07-10 02:12:20 +0000119 const char variable_delimiter_;
120
121 ZeroCopyOutputStream* const output_;
122 char* buffer_;
123 int buffer_size_;
124
125 string indent_;
126 bool at_start_of_line_;
127 bool failed_;
128
129 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Printer);
130};
131
132} // namespace io
133} // namespace protobuf
134
135} // namespace google
136#endif // GOOGLE_PROTOBUF_IO_PRINTER_H__