• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   // TODO(kenton):  Overloaded versions with more variables?  Three seems
90   //   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 
101   // 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 
113   // 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.)
failed()116   bool failed() const { return failed_; }
117 
118  private:
119   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__
137