BWAPI
Undermind/proxy/cpp/include/google/protobuf/text_format.h
Go to the documentation of this file.
00001 // Protocol Buffers - Google's data interchange format
00002 // Copyright 2008 Google Inc.  All rights reserved.
00003 // http://code.google.com/p/protobuf/
00004 //
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are
00007 // met:
00008 //
00009 //     * Redistributions of source code must retain the above copyright
00010 // notice, this list of conditions and the following disclaimer.
00011 //     * Redistributions in binary form must reproduce the above
00012 // copyright notice, this list of conditions and the following disclaimer
00013 // in the documentation and/or other materials provided with the
00014 // distribution.
00015 //     * Neither the name of Google Inc. nor the names of its
00016 // contributors may be used to endorse or promote products derived from
00017 // this software without specific prior written permission.
00018 //
00019 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00023 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00025 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00026 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00027 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00029 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 
00031 // Author: jschorr@google.com (Joseph Schorr)
00032 //  Based on original Protocol Buffers design by
00033 //  Sanjay Ghemawat, Jeff Dean, and others.
00034 //
00035 // Utilities for printing and parsing protocol messages in a human-readable,
00036 // text-based format.
00037 
00038 #ifndef GOOGLE_PROTOBUF_TEXT_FORMAT_H__
00039 #define GOOGLE_PROTOBUF_TEXT_FORMAT_H__
00040 
00041 #include <string>
00042 #include <google/protobuf/message.h>
00043 #include <google/protobuf/descriptor.h>
00044 
00045 namespace google {
00046 namespace protobuf {
00047 
00048 namespace io {
00049   class ErrorCollector;      // tokenizer.h
00050 }
00051 
00052 // This class implements protocol buffer text format.  Printing and parsing
00053 // protocol messages in text format is useful for debugging and human editing
00054 // of messages.
00055 //
00056 // This class is really a namespace that contains only static methods.
00057 class LIBPROTOBUF_EXPORT TextFormat {
00058  public:
00059   // Outputs a textual representation of the given message to the given
00060   // output stream.
00061   static bool Print(const Message& message, io::ZeroCopyOutputStream* output);
00062 
00063   // Print the fields in an UnknownFieldSet.  They are printed by tag number
00064   // only.  Embedded messages are heuristically identified by attempting to
00065   // parse them.
00066   static bool PrintUnknownFields(const UnknownFieldSet& unknown_fields,
00067                                  io::ZeroCopyOutputStream* output);
00068 
00069   // Like Print(), but outputs directly to a string.
00070   static bool PrintToString(const Message& message, string* output);
00071 
00072   // Like PrintUnknownFields(), but outputs directly to a string.
00073   static bool PrintUnknownFieldsToString(const UnknownFieldSet& unknown_fields,
00074                                          string* output);
00075 
00076   // Outputs a textual representation of the value of the field supplied on
00077   // the message supplied. For non-repeated fields, an index of -1 must
00078   // be supplied. Note that this method will print the default value for a
00079   // field if it is not set.
00080   static void PrintFieldValueToString(const Message& message,
00081                                       const FieldDescriptor* field,
00082                                       int index,
00083                                       string* output);
00084 
00085   // Class for those users which require more fine-grained control over how
00086   // a protobuffer message is printed out.
00087   class LIBPROTOBUF_EXPORT Printer {
00088    public:
00089     Printer();
00090     ~Printer();
00091 
00092     // Like TextFormat::Print
00093     bool Print(const Message& message, io::ZeroCopyOutputStream* output);
00094     // Like TextFormat::PrintUnknownFields
00095     bool PrintUnknownFields(const UnknownFieldSet& unknown_fields,
00096                             io::ZeroCopyOutputStream* output);
00097     // Like TextFormat::PrintToString
00098     bool PrintToString(const Message& message, string* output);
00099     // Like TextFormat::PrintUnknownFieldsToString
00100     bool PrintUnknownFieldsToString(const UnknownFieldSet& unknown_fields,
00101                                     string* output);
00102     // Like TextFormat::PrintFieldValueToString
00103     void PrintFieldValueToString(const Message& message,
00104                                  const FieldDescriptor* field,
00105                                  int index,
00106                                  string* output);
00107 
00108     // Adjust the initial indent level of all output.  Each indent level is
00109     // equal to two spaces.
00110     void SetInitialIndentLevel(int indent_level) {
00111       initial_indent_level_ = indent_level;
00112     }
00113 
00114     // If printing in single line mode, then the entire message will be output
00115     // on a single line with no line breaks.
00116     void SetSingleLineMode(bool single_line_mode) {
00117       single_line_mode_ = single_line_mode;
00118     }
00119 
00120     // Set true to print repeated primitives in a format like:
00121     //   field_name: [1, 2, 3, 4]
00122     // instead of printing each value on its own line.  Short format applies
00123     // only to primitive values -- i.e. everything except strings and
00124     // sub-messages/groups.  Note that at present this format is not recognized
00125     // by the parser.
00126     void SetUseShortRepeatedPrimitives(bool use_short_repeated_primitives) {
00127       use_short_repeated_primitives_ = use_short_repeated_primitives;
00128     }
00129 
00130     // Set true to output UTF-8 instead of ASCII.  The only difference
00131     // is that bytes >= 0x80 in string fields will not be escaped,
00132     // because they are assumed to be part of UTF-8 multi-byte
00133     // sequences.
00134     void SetUseUtf8StringEscaping(bool as_utf8) {
00135       utf8_string_escaping_ = as_utf8;
00136     }
00137 
00138    private:
00139     // Forward declaration of an internal class used to print the text
00140     // output to the OutputStream (see text_format.cc for implementation).
00141     class TextGenerator;
00142 
00143     // Internal Print method, used for writing to the OutputStream via
00144     // the TextGenerator class.
00145     void Print(const Message& message,
00146                TextGenerator& generator);
00147 
00148     // Print a single field.
00149     void PrintField(const Message& message,
00150                     const Reflection* reflection,
00151                     const FieldDescriptor* field,
00152                     TextGenerator& generator);
00153 
00154     // Print a repeated primitive field in short form.
00155     void PrintShortRepeatedField(const Message& message,
00156                                  const Reflection* reflection,
00157                                  const FieldDescriptor* field,
00158                                  TextGenerator& generator);
00159 
00160     // Print the name of a field -- i.e. everything that comes before the
00161     // ':' for a single name/value pair.
00162     void PrintFieldName(const Message& message,
00163                         const Reflection* reflection,
00164                         const FieldDescriptor* field,
00165                         TextGenerator& generator);
00166 
00167     // Outputs a textual representation of the value of the field supplied on
00168     // the message supplied or the default value if not set.
00169     void PrintFieldValue(const Message& message,
00170                          const Reflection* reflection,
00171                          const FieldDescriptor* field,
00172                          int index,
00173                          TextGenerator& generator);
00174 
00175     // Print the fields in an UnknownFieldSet.  They are printed by tag number
00176     // only.  Embedded messages are heuristically identified by attempting to
00177     // parse them.
00178     void PrintUnknownFields(const UnknownFieldSet& unknown_fields,
00179                             TextGenerator& generator);
00180 
00181     int initial_indent_level_;
00182 
00183     bool single_line_mode_;
00184 
00185     bool use_short_repeated_primitives_;
00186 
00187     bool utf8_string_escaping_;
00188   };
00189 
00190   // Parses a text-format protocol message from the given input stream to
00191   // the given message object.  This function parses the format written
00192   // by Print().
00193   static bool Parse(io::ZeroCopyInputStream* input, Message* output);
00194   // Like Parse(), but reads directly from a string.
00195   static bool ParseFromString(const string& input, Message* output);
00196 
00197   // Like Parse(), but the data is merged into the given message, as if
00198   // using Message::MergeFrom().
00199   static bool Merge(io::ZeroCopyInputStream* input, Message* output);
00200   // Like Merge(), but reads directly from a string.
00201   static bool MergeFromString(const string& input, Message* output);
00202 
00203   // Parse the given text as a single field value and store it into the
00204   // given field of the given message. If the field is a repeated field,
00205   // the new value will be added to the end
00206   static bool ParseFieldValueFromString(const string& input,
00207                                         const FieldDescriptor* field,
00208                                         Message* message);
00209 
00210   // For more control over parsing, use this class.
00211   class LIBPROTOBUF_EXPORT Parser {
00212    public:
00213     Parser();
00214     ~Parser();
00215 
00216     // Like TextFormat::Parse().
00217     bool Parse(io::ZeroCopyInputStream* input, Message* output);
00218     // Like TextFormat::ParseFromString().
00219     bool ParseFromString(const string& input, Message* output);
00220     // Like TextFormat::Merge().
00221     bool Merge(io::ZeroCopyInputStream* input, Message* output);
00222     // Like TextFormat::MergeFromString().
00223     bool MergeFromString(const string& input, Message* output);
00224 
00225     // Set where to report parse errors.  If NULL (the default), errors will
00226     // be printed to stderr.
00227     void RecordErrorsTo(io::ErrorCollector* error_collector) {
00228       error_collector_ = error_collector;
00229     }
00230 
00231     // Normally parsing fails if, after parsing, output->IsInitialized()
00232     // returns false.  Call AllowPartialMessage(true) to skip this check.
00233     void AllowPartialMessage(bool allow) {
00234       allow_partial_ = allow;
00235     }
00236 
00237     // Like TextFormat::ParseFieldValueFromString
00238     bool ParseFieldValueFromString(const string& input,
00239                                    const FieldDescriptor* field,
00240                                    Message* output);
00241 
00242    private:
00243     // Forward declaration of an internal class used to parse text
00244     // representations (see text_format.cc for implementation).
00245     class ParserImpl;
00246 
00247     // Like TextFormat::Merge().  The provided implementation is used
00248     // to do the parsing.
00249     bool MergeUsingImpl(io::ZeroCopyInputStream* input,
00250                         Message* output,
00251                         ParserImpl* parser_impl);
00252 
00253     io::ErrorCollector* error_collector_;
00254     bool allow_partial_;
00255   };
00256 
00257  private:
00258   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextFormat);
00259 };
00260 
00261 }  // namespace protobuf
00262 
00263 }  // namespace google
00264 #endif  // GOOGLE_PROTOBUF_TEXT_FORMAT_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines