~ubuntu-branches/ubuntu/oneiric/protobuf/oneiric

« back to all changes in this revision

Viewing changes to src/google/protobuf/text_format.h

  • Committer: Bazaar Package Importer
  • Author(s): Iustin Pop
  • Date: 2008-08-03 11:01:44 UTC
  • Revision ID: james.westby@ubuntu.com-20080803110144-uyiw41bf1m2oe17t
Tags: upstream-2.0.0~b
ImportĀ upstreamĀ versionĀ 2.0.0~b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Protocol Buffers - Google's data interchange format
 
2
// Copyright 2008 Google Inc.
 
3
// http://code.google.com/p/protobuf/
 
4
//
 
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
 
8
//
 
9
//      http://www.apache.org/licenses/LICENSE-2.0
 
10
//
 
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.
 
16
 
 
17
// Author: jschorr@google.com (Joseph Schorr)
 
18
//  Based on original Protocol Buffers design by
 
19
//  Sanjay Ghemawat, Jeff Dean, and others.
 
20
//
 
21
// Utilities for printing and parsing protocol messages in a human-readable,
 
22
// text-based format.
 
23
 
 
24
#ifndef GOOGLE_PROTOBUF_TEXT_FORMAT_H__
 
25
#define GOOGLE_PROTOBUF_TEXT_FORMAT_H__
 
26
 
 
27
#include <string>
 
28
#include <google/protobuf/message.h>  // Message, Message::Reflection
 
29
#include <google/protobuf/descriptor.h>
 
30
 
 
31
namespace google {
 
32
namespace protobuf {
 
33
 
 
34
namespace io {
 
35
  class ErrorCollector;      // tokenizer.h
 
36
}
 
37
 
 
38
// This class implements protocol buffer text format.  Printing and parsing
 
39
// protocol messages in text format is useful for debugging and human editing
 
40
// of messages.
 
41
//
 
42
// This class is really a namespace that contains only static methods.
 
43
class LIBPROTOBUF_EXPORT TextFormat {
 
44
 public:
 
45
  // Outputs a textual representation of the given message to the given
 
46
  // output stream.
 
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);
 
50
 
 
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,
 
57
                                      int index,
 
58
                                      string* output);
 
59
 
 
60
  // Parses a text-format protocol message from the given input stream to
 
61
  // the given message object.  This function parses the format written
 
62
  // by Print().
 
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);
 
66
 
 
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);
 
72
 
 
73
  // For more control over parsing, use this class.
 
74
  class LIBPROTOBUF_EXPORT Parser {
 
75
   public:
 
76
    Parser();
 
77
    ~Parser();
 
78
 
 
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);
 
87
 
 
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;
 
92
    }
 
93
 
 
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;
 
98
    }
 
99
 
 
100
   private:
 
101
    io::ErrorCollector* error_collector_;
 
102
    bool allow_partial_;
 
103
  };
 
104
 
 
105
 private:
 
106
  // Forward declaration of an internal class used to print the text
 
107
  // output to the OutputStream (see text_format.cc for implementation).
 
108
  class TextGenerator;
 
109
 
 
110
  // Forward declaration of an internal class used to parse text
 
111
  // representations (see text_format.cc for implementation).
 
112
  class ParserImpl;
 
113
 
 
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);
 
119
 
 
120
  // Print a single field.
 
121
  static void PrintField(const FieldDescriptor* field,
 
122
                         const Message::Reflection* message,
 
123
                         TextGenerator& generator);
 
124
 
 
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,
 
129
                              int index,
 
130
                              TextGenerator& generator);
 
131
 
 
132
  // Print the fields in an UnknownFieldSet.  They are printed by tag number
 
133
  // only.
 
134
  static void PrintUnknownFields(const UnknownFieldSet& unknown_fields,
 
135
                                 TextGenerator& generator);
 
136
 
 
137
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextFormat);
 
138
};
 
139
 
 
140
}  // namespace protobuf
 
141
 
 
142
}  // namespace google
 
143
#endif  // GOOGLE_PROTOBUF_TEXT_FORMAT_H__