15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Protocol Buffers - Google's data interchange format 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2008 Google Inc. All rights reserved. 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// http://code.google.com/p/protobuf/ 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Redistribution and use in source and binary forms, with or without 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// modification, are permitted provided that the following conditions are 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// met: 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Redistributions of source code must retain the above copyright 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// notice, this list of conditions and the following disclaimer. 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Redistributions in binary form must reproduce the above 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// copyright notice, this list of conditions and the following disclaimer 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in the documentation and/or other materials provided with the 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// distribution. 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Neither the name of Google Inc. nor the names of its 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// contributors may be used to endorse or promote products derived from 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this software without specific prior written permission. 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Author: kenton@google.com (Kenton Varda) 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Based on original Protocol Buffers design by 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Sanjay Ghemawat, Jeff Dean, and others. 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Utility class for writing text to a ZeroCopyOutputStream. 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef GOOGLE_PROTOBUF_IO_PRINTER_H__ 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define GOOGLE_PROTOBUF_IO_PRINTER_H__ 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map> 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <google/protobuf/stubs/common.h> 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace google { 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace protobuf { 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace io { 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ZeroCopyOutputStream; // zero_copy_stream.h 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This simple utility class assists in code generation. It basically 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// allows the caller to define a set of variables and then output some 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// text with variable substitutions. Example usage: 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Printer printer(output, '$'); 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// map<string, string> vars; 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// vars["name"] = "Bob"; 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// printer.Print(vars, "My name is $name$."); 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The above writes "My name is Bob." to the output stream. 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Printer aggressively enforces correct usage, crashing (with assert failures) 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in the case of undefined variables in debug builds. This helps greatly in 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// debugging code which uses it. 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class LIBPROTOBUF_EXPORT Printer { 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Create a printer that writes text to the given output stream. Use the 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // given character as the delimiter for variables. 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Printer(ZeroCopyOutputStream* output, char variable_delimiter); 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~Printer(); 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Print some text after applying variable substitutions. If a particular 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // variable in the text is not defined, this will crash. Variables to be 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // substituted are identified by their names surrounded by delimiter 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // characters (as given to the constructor). The variable bindings are 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // defined by the given map. 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Print(const map<string, string>& variables, const char* text); 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Like the first Print(), except the substitutions are given as parameters. 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Print(const char* text); 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Like the first Print(), except the substitutions are given as parameters. 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Print(const char* text, const char* variable, const string& value); 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Like the first Print(), except the substitutions are given as parameters. 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Print(const char* text, const char* variable1, const string& value1, 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* variable2, const string& value2); 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Like the first Print(), except the substitutions are given as parameters. 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Print(const char* text, const char* variable1, const string& value1, 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* variable2, const string& value2, 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* variable3, const string& value3); 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(kenton): Overloaded versions with more variables? Three seems 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to be enough. 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Indent text by two spaces. After calling Indent(), two spaces will be 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // inserted at the beginning of each line of text. Indent() may be called 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // multiple times to produce deeper indents. 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Indent(); 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Reduces the current indent level by two spaces, or crashes if the indent 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // level is zero. 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Outdent(); 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Write a string to the output buffer. 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This method does not look for newlines to add indentation. 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void PrintRaw(const string& data); 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Write a zero-delimited string to output buffer. 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This method does not look for newlines to add indentation. 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void PrintRaw(const char* data); 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Write some bytes to the output buffer. 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This method does not look for newlines to add indentation. 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void WriteRaw(const char* data, int size); 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // True if any write to the underlying stream failed. (We don't just 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // crash in this case because this is an I/O failure, not a programming 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // error.) 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool failed() const { return failed_; } 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char variable_delimiter_; 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ZeroCopyOutputStream* const output_; 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) char* buffer_; 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int buffer_size_; 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) string indent_; 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool at_start_of_line_; 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool failed_; 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Printer); 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace io 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace protobuf 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace google 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // GOOGLE_PROTOBUF_IO_PRINTER_H__ 137