1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc.
3
// http://code.google.com/p/protobuf/
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
9
// http://www.apache.org/licenses/LICENSE-2.0
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
17
// Author: jschorr@google.com (Joseph Schorr)
18
// Based on original Protocol Buffers design by
19
// Sanjay Ghemawat, Jeff Dean, and others.
21
// Utilities for printing and parsing protocol messages in a human-readable,
24
#ifndef GOOGLE_PROTOBUF_TEXT_FORMAT_H__
25
#define GOOGLE_PROTOBUF_TEXT_FORMAT_H__
28
#include <google/protobuf/message.h> // Message, Message::Reflection
29
#include <google/protobuf/descriptor.h>
35
class ErrorCollector; // tokenizer.h
38
// This class implements protocol buffer text format. Printing and parsing
39
// protocol messages in text format is useful for debugging and human editing
42
// This class is really a namespace that contains only static methods.
43
class LIBPROTOBUF_EXPORT TextFormat {
45
// Outputs a textual representation of the given message to the given
47
static bool Print(const Message& message, io::ZeroCopyOutputStream* output);
48
// Like Print(), but outputs directly to a string.
49
static bool PrintToString(const Message& message, string* output);
51
// Outputs a textual representation of the value of the field supplied on
52
// the message supplied. For non-repeated fields, an index of -1 must
53
// be supplied. Note that this method will print the default value for a
54
// field if it is not set.
55
static void PrintFieldValueToString(const Message& message,
56
const FieldDescriptor* field,
60
// Parses a text-format protocol message from the given input stream to
61
// the given message object. This function parses the format written
63
static bool Parse(io::ZeroCopyInputStream* input, Message* output);
64
// Like Parse(), but reads directly from a string.
65
static bool ParseFromString(const string& input, Message* output);
67
// Like Parse(), but the data is merged into the given message, as if
68
// using Message::MergeFrom().
69
static bool Merge(io::ZeroCopyInputStream* input, Message* output);
70
// Like Merge(), but reads directly from a string.
71
static bool MergeFromString(const string& input, Message* output);
73
// For more control over parsing, use this class.
74
class LIBPROTOBUF_EXPORT Parser {
79
// Like TextFormat::Parse().
80
bool Parse(io::ZeroCopyInputStream* input, Message* output);
81
// Like TextFormat::ParseFromString().
82
bool ParseFromString(const string& input, Message* output);
83
// Like TextFormat::Merge().
84
bool Merge(io::ZeroCopyInputStream* input, Message* output);
85
// Like TextFormat::MergeFromString().
86
bool MergeFromString(const string& input, Message* output);
88
// Set where to report parse errors. If NULL (the default), errors will
89
// be printed to stderr.
90
void RecordErrorsTo(io::ErrorCollector* error_collector) {
91
error_collector_ = error_collector;
94
// Normally parsing fails if, after parsing, output->IsInitialized()
95
// returns false. Call AllowPartialMessage(true) to skip this check.
96
void AllowPartialMessage(bool allow) {
97
allow_partial_ = allow;
101
io::ErrorCollector* error_collector_;
106
// Forward declaration of an internal class used to print the text
107
// output to the OutputStream (see text_format.cc for implementation).
110
// Forward declaration of an internal class used to parse text
111
// representations (see text_format.cc for implementation).
114
// Internal Print method, used for writing to the OutputStream via
115
// the TextGenerator class.
116
static void Print(const Descriptor* descriptor,
117
const Message::Reflection* message,
118
TextGenerator& generator);
120
// Print a single field.
121
static void PrintField(const FieldDescriptor* field,
122
const Message::Reflection* message,
123
TextGenerator& generator);
125
// Outputs a textual representation of the value of the field supplied on
126
// the message supplied or the default value if not set.
127
static void PrintFieldValue(const Message::Reflection* reflection,
128
const FieldDescriptor* field,
130
TextGenerator& generator);
132
// Print the fields in an UnknownFieldSet. They are printed by tag number
134
static void PrintUnknownFields(const UnknownFieldSet& unknown_fields,
135
TextGenerator& generator);
137
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextFormat);
140
} // namespace protobuf
142
} // namespace google
143
#endif // GOOGLE_PROTOBUF_TEXT_FORMAT_H__