1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
35 #include <google/protobuf/io/printer.h>
36 #include <google/protobuf/io/zero_copy_stream.h>
37 #include <google/protobuf/stubs/common.h>
43 Printer::Printer(ZeroCopyOutputStream* output, char variable_delimiter)
44 : variable_delimiter_(variable_delimiter),
48 at_start_of_line_(true),
53 // Only BackUp() if we have called Next() at least once and never failed.
54 if (buffer_size_ > 0 && !failed_) {
55 output_->BackUp(buffer_size_);
59 void Printer::Print(const map<string, string>& variables, const char* text) {
60 int size = strlen(text);
61 int pos = 0; // The number of bytes we've written so far.
63 for (int i = 0; i < size; i++) {
64 if (text[i] == '\n') {
65 // Saw newline. If there is more text, we may need to insert an indent
66 // here. So, write what we have so far, including the '\n'.
67 WriteRaw(text + pos, i - pos + 1);
70 // Setting this true will cause the next WriteRaw() to insert an indent
72 at_start_of_line_ = true;
74 } else if (text[i] == variable_delimiter_) {
75 // Saw the start of a variable name.
77 // Write what we have so far.
78 WriteRaw(text + pos, i - pos);
81 // Find closing delimiter.
82 const char* end = strchr(text + pos, variable_delimiter_);
84 GOOGLE_LOG(DFATAL) << " Unclosed variable name.";
87 int endpos = end - text;
89 string varname(text + pos, endpos - pos);
90 if (varname.empty()) {
91 // Two delimiters in a row reduce to a literal delimiter character.
92 WriteRaw(&variable_delimiter_, 1);
94 // Replace with the variable's value.
95 map<string, string>::const_iterator iter = variables.find(varname);
96 if (iter == variables.end()) {
97 GOOGLE_LOG(DFATAL) << " Undefined variable: " << varname;
99 WriteRaw(iter->second.data(), iter->second.size());
103 // Advance past this variable.
110 WriteRaw(text + pos, size - pos);
113 void Printer::Print(const char* text) {
114 static map<string, string> empty;
118 void Printer::Print(const char* text,
119 const char* variable, const string& value) {
120 map<string, string> vars;
121 vars[variable] = value;
125 void Printer::Print(const char* text,
126 const char* variable1, const string& value1,
127 const char* variable2, const string& value2) {
128 map<string, string> vars;
129 vars[variable1] = value1;
130 vars[variable2] = value2;
134 void Printer::Print(const char* text,
135 const char* variable1, const string& value1,
136 const char* variable2, const string& value2,
137 const char* variable3, const string& value3) {
138 map<string, string> vars;
139 vars[variable1] = value1;
140 vars[variable2] = value2;
141 vars[variable3] = value3;
145 void Printer::Indent() {
149 void Printer::Outdent() {
150 if (indent_.empty()) {
151 GOOGLE_LOG(DFATAL) << " Outdent() without matching Indent().";
155 indent_.resize(indent_.size() - 2);
158 void Printer::PrintRaw(const string& data) {
159 WriteRaw(data.data(), data.size());
162 void Printer::PrintRaw(const char* data) {
164 WriteRaw(data, strlen(data));
167 void Printer::WriteRaw(const char* data, int size) {
169 if (size == 0) return;
171 if (at_start_of_line_ && (size > 0) && (data[0] != '\n')) {
173 at_start_of_line_ = false;
174 WriteRaw(indent_.data(), indent_.size());
178 while (size > buffer_size_) {
179 // Data exceeds space in the buffer. Copy what we can and request a
181 memcpy(buffer_, data, buffer_size_);
182 data += buffer_size_;
183 size -= buffer_size_;
185 failed_ = !output_->Next(&void_buffer, &buffer_size_);
187 buffer_ = reinterpret_cast<char*>(void_buffer);
190 // Buffer is big enough to receive the data; copy it.
191 memcpy(buffer_, data, size);
193 buffer_size_ -= size;
197 } // namespace protobuf
198 } // namespace google