诸暨麻将添加redis
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

636 righe
28 KiB

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  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
  14. // distribution.
  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.
  18. //
  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.
  30. // Author: jschorr@google.com (Joseph Schorr)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // Utilities for printing and parsing protocol messages in a human-readable,
  35. // text-based format.
  36. #ifndef GOOGLE_PROTOBUF_TEXT_FORMAT_H__
  37. #define GOOGLE_PROTOBUF_TEXT_FORMAT_H__
  38. #include <map>
  39. #include <memory>
  40. #include <string>
  41. #include <vector>
  42. #include <google/protobuf/stubs/common.h>
  43. #include <google/protobuf/descriptor.h>
  44. #include <google/protobuf/message.h>
  45. #include <google/protobuf/message_lite.h>
  46. #include <google/protobuf/port.h>
  47. #include <google/protobuf/port_def.inc>
  48. #ifdef SWIG
  49. #error "You cannot SWIG proto headers"
  50. #endif
  51. namespace google {
  52. namespace protobuf {
  53. namespace io {
  54. class ErrorCollector; // tokenizer.h
  55. }
  56. // This class implements protocol buffer text format. Printing and parsing
  57. // protocol messages in text format is useful for debugging and human editing
  58. // of messages.
  59. //
  60. // This class is really a namespace that contains only static methods.
  61. class PROTOBUF_EXPORT TextFormat {
  62. public:
  63. // Outputs a textual representation of the given message to the given
  64. // output stream. Returns false if printing fails.
  65. static bool Print(const Message& message, io::ZeroCopyOutputStream* output);
  66. // Print the fields in an UnknownFieldSet. They are printed by tag number
  67. // only. Embedded messages are heuristically identified by attempting to
  68. // parse them. Returns false if printing fails.
  69. static bool PrintUnknownFields(const UnknownFieldSet& unknown_fields,
  70. io::ZeroCopyOutputStream* output);
  71. // Like Print(), but outputs directly to a string.
  72. // Note: output will be cleared prior to printing, and will be left empty
  73. // even if printing fails. Returns false if printing fails.
  74. static bool PrintToString(const Message& message, std::string* output);
  75. // Like PrintUnknownFields(), but outputs directly to a string. Returns
  76. // false if printing fails.
  77. static bool PrintUnknownFieldsToString(const UnknownFieldSet& unknown_fields,
  78. std::string* output);
  79. // Outputs a textual representation of the value of the field supplied on
  80. // the message supplied. For non-repeated fields, an index of -1 must
  81. // be supplied. Note that this method will print the default value for a
  82. // field if it is not set.
  83. static void PrintFieldValueToString(const Message& message,
  84. const FieldDescriptor* field, int index,
  85. std::string* output);
  86. class PROTOBUF_EXPORT BaseTextGenerator {
  87. public:
  88. virtual ~BaseTextGenerator();
  89. virtual void Indent() {}
  90. virtual void Outdent() {}
  91. // Returns the current indentation size in characters.
  92. virtual size_t GetCurrentIndentationSize() const { return 0; }
  93. // Print text to the output stream.
  94. virtual void Print(const char* text, size_t size) = 0;
  95. void PrintString(const std::string& str) { Print(str.data(), str.size()); }
  96. template <size_t n>
  97. void PrintLiteral(const char (&text)[n]) {
  98. Print(text, n - 1); // n includes the terminating zero character.
  99. }
  100. };
  101. // The default printer that converts scalar values from fields into their
  102. // string representation.
  103. // You can derive from this FastFieldValuePrinter if you want to have fields
  104. // to be printed in a different way and register it at the Printer.
  105. class PROTOBUF_EXPORT FastFieldValuePrinter {
  106. public:
  107. FastFieldValuePrinter();
  108. virtual ~FastFieldValuePrinter();
  109. virtual void PrintBool(bool val, BaseTextGenerator* generator) const;
  110. virtual void PrintInt32(int32 val, BaseTextGenerator* generator) const;
  111. virtual void PrintUInt32(uint32 val, BaseTextGenerator* generator) const;
  112. virtual void PrintInt64(int64 val, BaseTextGenerator* generator) const;
  113. virtual void PrintUInt64(uint64 val, BaseTextGenerator* generator) const;
  114. virtual void PrintFloat(float val, BaseTextGenerator* generator) const;
  115. virtual void PrintDouble(double val, BaseTextGenerator* generator) const;
  116. virtual void PrintString(const std::string& val,
  117. BaseTextGenerator* generator) const;
  118. virtual void PrintBytes(const std::string& val,
  119. BaseTextGenerator* generator) const;
  120. virtual void PrintEnum(int32 val, const std::string& name,
  121. BaseTextGenerator* generator) const;
  122. virtual void PrintFieldName(const Message& message, int field_index,
  123. int field_count, const Reflection* reflection,
  124. const FieldDescriptor* field,
  125. BaseTextGenerator* generator) const;
  126. virtual void PrintFieldName(const Message& message,
  127. const Reflection* reflection,
  128. const FieldDescriptor* field,
  129. BaseTextGenerator* generator) const;
  130. virtual void PrintMessageStart(const Message& message, int field_index,
  131. int field_count, bool single_line_mode,
  132. BaseTextGenerator* generator) const;
  133. virtual void PrintMessageEnd(const Message& message, int field_index,
  134. int field_count, bool single_line_mode,
  135. BaseTextGenerator* generator) const;
  136. private:
  137. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastFieldValuePrinter);
  138. };
  139. // Deprecated: please use FastFieldValuePrinter instead.
  140. class PROTOBUF_EXPORT FieldValuePrinter {
  141. public:
  142. FieldValuePrinter();
  143. virtual ~FieldValuePrinter();
  144. virtual std::string PrintBool(bool val) const;
  145. virtual std::string PrintInt32(int32 val) const;
  146. virtual std::string PrintUInt32(uint32 val) const;
  147. virtual std::string PrintInt64(int64 val) const;
  148. virtual std::string PrintUInt64(uint64 val) const;
  149. virtual std::string PrintFloat(float val) const;
  150. virtual std::string PrintDouble(double val) const;
  151. virtual std::string PrintString(const std::string& val) const;
  152. virtual std::string PrintBytes(const std::string& val) const;
  153. virtual std::string PrintEnum(int32 val, const std::string& name) const;
  154. virtual std::string PrintFieldName(const Message& message,
  155. const Reflection* reflection,
  156. const FieldDescriptor* field) const;
  157. virtual std::string PrintMessageStart(const Message& message,
  158. int field_index, int field_count,
  159. bool single_line_mode) const;
  160. virtual std::string PrintMessageEnd(const Message& message, int field_index,
  161. int field_count,
  162. bool single_line_mode) const;
  163. private:
  164. FastFieldValuePrinter delegate_;
  165. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldValuePrinter);
  166. };
  167. class PROTOBUF_EXPORT MessagePrinter {
  168. public:
  169. MessagePrinter() {}
  170. virtual ~MessagePrinter() {}
  171. virtual void Print(const Message& message, bool single_line_mode,
  172. BaseTextGenerator* generator) const = 0;
  173. private:
  174. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessagePrinter);
  175. };
  176. // Interface that Printers or Parsers can use to find extensions, or types
  177. // referenced in Any messages.
  178. class PROTOBUF_EXPORT Finder {
  179. public:
  180. virtual ~Finder();
  181. // Try to find an extension of *message by fully-qualified field
  182. // name. Returns NULL if no extension is known for this name or number.
  183. // The base implementation uses the extensions already known by the message.
  184. virtual const FieldDescriptor* FindExtension(Message* message,
  185. const std::string& name) const;
  186. // Similar to FindExtension, but uses a Descriptor and the extension number
  187. // instead of using a Message and the name when doing the look up.
  188. virtual const FieldDescriptor* FindExtensionByNumber(
  189. const Descriptor* descriptor, int number) const;
  190. // Find the message type for an Any proto.
  191. // Returns NULL if no message is known for this name.
  192. // The base implementation only accepts prefixes of type.googleprod.com/ or
  193. // type.googleapis.com/, and searches the DescriptorPool of the parent
  194. // message.
  195. virtual const Descriptor* FindAnyType(const Message& message,
  196. const std::string& prefix,
  197. const std::string& name) const;
  198. // Find the message factory for the given extension field. This can be used
  199. // to generalize the Parser to add extension fields to a message in the same
  200. // way as the "input" message for the Parser.
  201. virtual MessageFactory* FindExtensionFactory(
  202. const FieldDescriptor* field) const;
  203. };
  204. // Class for those users which require more fine-grained control over how
  205. // a protobuffer message is printed out.
  206. class PROTOBUF_EXPORT Printer {
  207. public:
  208. Printer();
  209. // Like TextFormat::Print
  210. bool Print(const Message& message, io::ZeroCopyOutputStream* output) const;
  211. // Like TextFormat::PrintUnknownFields
  212. bool PrintUnknownFields(const UnknownFieldSet& unknown_fields,
  213. io::ZeroCopyOutputStream* output) const;
  214. // Like TextFormat::PrintToString
  215. bool PrintToString(const Message& message, std::string* output) const;
  216. // Like TextFormat::PrintUnknownFieldsToString
  217. bool PrintUnknownFieldsToString(const UnknownFieldSet& unknown_fields,
  218. std::string* output) const;
  219. // Like TextFormat::PrintFieldValueToString
  220. void PrintFieldValueToString(const Message& message,
  221. const FieldDescriptor* field, int index,
  222. std::string* output) const;
  223. // Adjust the initial indent level of all output. Each indent level is
  224. // equal to two spaces.
  225. void SetInitialIndentLevel(int indent_level) {
  226. initial_indent_level_ = indent_level;
  227. }
  228. // If printing in single line mode, then the entire message will be output
  229. // on a single line with no line breaks.
  230. void SetSingleLineMode(bool single_line_mode) {
  231. single_line_mode_ = single_line_mode;
  232. }
  233. bool IsInSingleLineMode() const { return single_line_mode_; }
  234. // If use_field_number is true, uses field number instead of field name.
  235. void SetUseFieldNumber(bool use_field_number) {
  236. use_field_number_ = use_field_number;
  237. }
  238. // Set true to print repeated primitives in a format like:
  239. // field_name: [1, 2, 3, 4]
  240. // instead of printing each value on its own line. Short format applies
  241. // only to primitive values -- i.e. everything except strings and
  242. // sub-messages/groups.
  243. void SetUseShortRepeatedPrimitives(bool use_short_repeated_primitives) {
  244. use_short_repeated_primitives_ = use_short_repeated_primitives;
  245. }
  246. // Set true to output UTF-8 instead of ASCII. The only difference
  247. // is that bytes >= 0x80 in string fields will not be escaped,
  248. // because they are assumed to be part of UTF-8 multi-byte
  249. // sequences. This will change the default FastFieldValuePrinter.
  250. void SetUseUtf8StringEscaping(bool as_utf8);
  251. // Set the default FastFieldValuePrinter that is used for all fields that
  252. // don't have a field-specific printer registered.
  253. // Takes ownership of the printer.
  254. void SetDefaultFieldValuePrinter(const FastFieldValuePrinter* printer);
  255. PROTOBUF_DEPRECATED_MSG("Please use FastFieldValuePrinter")
  256. void SetDefaultFieldValuePrinter(const FieldValuePrinter* printer);
  257. // Sets whether we want to hide unknown fields or not.
  258. // Usually unknown fields are printed in a generic way that includes the
  259. // tag number of the field instead of field name. However, sometimes it
  260. // is useful to be able to print the message without unknown fields (e.g.
  261. // for the python protobuf version to maintain consistency between its pure
  262. // python and c++ implementations).
  263. void SetHideUnknownFields(bool hide) { hide_unknown_fields_ = hide; }
  264. // If print_message_fields_in_index_order is true, fields of a proto message
  265. // will be printed using the order defined in source code instead of the
  266. // field number, extensions will be printed at the end of the message
  267. // and their relative order is determined by the extension number.
  268. // By default, use the field number order.
  269. void SetPrintMessageFieldsInIndexOrder(
  270. bool print_message_fields_in_index_order) {
  271. print_message_fields_in_index_order_ =
  272. print_message_fields_in_index_order;
  273. }
  274. // If expand==true, expand google.protobuf.Any payloads. The output
  275. // will be of form
  276. // [type_url] { <value_printed_in_text> }
  277. //
  278. // If expand==false, print Any using the default printer. The output will
  279. // look like
  280. // type_url: "<type_url>" value: "serialized_content"
  281. void SetExpandAny(bool expand) { expand_any_ = expand; }
  282. // Set how parser finds message for Any payloads.
  283. void SetFinder(const Finder* finder) { finder_ = finder; }
  284. // If non-zero, we truncate all string fields that are longer than
  285. // this threshold. This is useful when the proto message has very long
  286. // strings, e.g., dump of encoded image file.
  287. //
  288. // NOTE(hfgong): Setting a non-zero value breaks round-trip safe
  289. // property of TextFormat::Printer. That is, from the printed message, we
  290. // cannot fully recover the original string field any more.
  291. void SetTruncateStringFieldLongerThan(
  292. const int64 truncate_string_field_longer_than) {
  293. truncate_string_field_longer_than_ = truncate_string_field_longer_than;
  294. }
  295. // Register a custom field-specific FastFieldValuePrinter for fields
  296. // with a particular FieldDescriptor.
  297. // Returns "true" if the registration succeeded, or "false", if there is
  298. // already a printer for that FieldDescriptor.
  299. // Takes ownership of the printer on successful registration.
  300. bool RegisterFieldValuePrinter(const FieldDescriptor* field,
  301. const FastFieldValuePrinter* printer);
  302. PROTOBUF_DEPRECATED_MSG("Please use FastFieldValuePrinter")
  303. bool RegisterFieldValuePrinter(const FieldDescriptor* field,
  304. const FieldValuePrinter* printer);
  305. // Register a custom message-specific MessagePrinter for messages with a
  306. // particular Descriptor.
  307. // Returns "true" if the registration succeeded, or "false" if there is
  308. // already a printer for that Descriptor.
  309. bool RegisterMessagePrinter(const Descriptor* descriptor,
  310. const MessagePrinter* printer);
  311. private:
  312. // Forward declaration of an internal class used to print the text
  313. // output to the OutputStream (see text_format.cc for implementation).
  314. class TextGenerator;
  315. // Internal Print method, used for writing to the OutputStream via
  316. // the TextGenerator class.
  317. void Print(const Message& message, TextGenerator* generator) const;
  318. // Print a single field.
  319. void PrintField(const Message& message, const Reflection* reflection,
  320. const FieldDescriptor* field,
  321. TextGenerator* generator) const;
  322. // Print a repeated primitive field in short form.
  323. void PrintShortRepeatedField(const Message& message,
  324. const Reflection* reflection,
  325. const FieldDescriptor* field,
  326. TextGenerator* generator) const;
  327. // Print the name of a field -- i.e. everything that comes before the
  328. // ':' for a single name/value pair.
  329. void PrintFieldName(const Message& message, int field_index,
  330. int field_count, const Reflection* reflection,
  331. const FieldDescriptor* field,
  332. TextGenerator* generator) const;
  333. // Outputs a textual representation of the value of the field supplied on
  334. // the message supplied or the default value if not set.
  335. void PrintFieldValue(const Message& message, const Reflection* reflection,
  336. const FieldDescriptor* field, int index,
  337. TextGenerator* generator) const;
  338. // Print the fields in an UnknownFieldSet. They are printed by tag number
  339. // only. Embedded messages are heuristically identified by attempting to
  340. // parse them.
  341. void PrintUnknownFields(const UnknownFieldSet& unknown_fields,
  342. TextGenerator* generator) const;
  343. bool PrintAny(const Message& message, TextGenerator* generator) const;
  344. const FastFieldValuePrinter* GetFieldPrinter(
  345. const FieldDescriptor* field) const {
  346. auto it = custom_printers_.find(field);
  347. return it == custom_printers_.end() ? default_field_value_printer_.get()
  348. : it->second.get();
  349. }
  350. int initial_indent_level_;
  351. bool single_line_mode_;
  352. bool use_field_number_;
  353. bool use_short_repeated_primitives_;
  354. bool hide_unknown_fields_;
  355. bool print_message_fields_in_index_order_;
  356. bool expand_any_;
  357. int64 truncate_string_field_longer_than_;
  358. std::unique_ptr<const FastFieldValuePrinter> default_field_value_printer_;
  359. typedef std::map<const FieldDescriptor*,
  360. std::unique_ptr<const FastFieldValuePrinter>>
  361. CustomPrinterMap;
  362. CustomPrinterMap custom_printers_;
  363. typedef std::map<const Descriptor*, std::unique_ptr<const MessagePrinter>>
  364. CustomMessagePrinterMap;
  365. CustomMessagePrinterMap custom_message_printers_;
  366. const Finder* finder_;
  367. };
  368. // Parses a text-format protocol message from the given input stream to
  369. // the given message object. This function parses the human-readable format
  370. // written by Print(). Returns true on success. The message is cleared first,
  371. // even if the function fails -- See Merge() to avoid this behavior.
  372. //
  373. // Example input: "user {\n id: 123 extra { gender: MALE language: 'en' }\n}"
  374. //
  375. // One use for this function is parsing handwritten strings in test code.
  376. // Another use is to parse the output from google::protobuf::Message::DebugString()
  377. // (or ShortDebugString()), because these functions output using
  378. // google::protobuf::TextFormat::Print().
  379. //
  380. // If you would like to read a protocol buffer serialized in the
  381. // (non-human-readable) binary wire format, see
  382. // google::protobuf::MessageLite::ParseFromString().
  383. static bool Parse(io::ZeroCopyInputStream* input, Message* output);
  384. // Like Parse(), but reads directly from a string.
  385. static bool ParseFromString(const std::string& input, Message* output);
  386. // Like Parse(), but the data is merged into the given message, as if
  387. // using Message::MergeFrom().
  388. static bool Merge(io::ZeroCopyInputStream* input, Message* output);
  389. // Like Merge(), but reads directly from a string.
  390. static bool MergeFromString(const std::string& input, Message* output);
  391. // Parse the given text as a single field value and store it into the
  392. // given field of the given message. If the field is a repeated field,
  393. // the new value will be added to the end
  394. static bool ParseFieldValueFromString(const std::string& input,
  395. const FieldDescriptor* field,
  396. Message* message);
  397. // A location in the parsed text.
  398. struct ParseLocation {
  399. int line;
  400. int column;
  401. ParseLocation() : line(-1), column(-1) {}
  402. ParseLocation(int line_param, int column_param)
  403. : line(line_param), column(column_param) {}
  404. };
  405. // Data structure which is populated with the locations of each field
  406. // value parsed from the text.
  407. class PROTOBUF_EXPORT ParseInfoTree {
  408. public:
  409. ParseInfoTree() = default;
  410. ParseInfoTree(const ParseInfoTree&) = delete;
  411. ParseInfoTree& operator=(const ParseInfoTree&) = delete;
  412. // Returns the parse location for index-th value of the field in the parsed
  413. // text. If none exists, returns a location with line = -1. Index should be
  414. // -1 for not-repeated fields.
  415. ParseLocation GetLocation(const FieldDescriptor* field, int index) const;
  416. // Returns the parse info tree for the given field, which must be a message
  417. // type. The nested information tree is owned by the root tree and will be
  418. // deleted when it is deleted.
  419. ParseInfoTree* GetTreeForNested(const FieldDescriptor* field,
  420. int index) const;
  421. private:
  422. // Allow the text format parser to record information into the tree.
  423. friend class TextFormat;
  424. // Records the starting location of a single value for a field.
  425. void RecordLocation(const FieldDescriptor* field, ParseLocation location);
  426. // Create and records a nested tree for a nested message field.
  427. ParseInfoTree* CreateNested(const FieldDescriptor* field);
  428. // Defines the map from the index-th field descriptor to its parse location.
  429. typedef std::map<const FieldDescriptor*, std::vector<ParseLocation> >
  430. LocationMap;
  431. // Defines the map from the index-th field descriptor to the nested parse
  432. // info tree.
  433. typedef std::map<const FieldDescriptor*,
  434. std::vector<std::unique_ptr<ParseInfoTree>>>
  435. NestedMap;
  436. LocationMap locations_;
  437. NestedMap nested_;
  438. };
  439. // For more control over parsing, use this class.
  440. class PROTOBUF_EXPORT Parser {
  441. public:
  442. Parser();
  443. ~Parser();
  444. // Like TextFormat::Parse().
  445. bool Parse(io::ZeroCopyInputStream* input, Message* output);
  446. // Like TextFormat::ParseFromString().
  447. bool ParseFromString(const std::string& input, Message* output);
  448. // Like TextFormat::Merge().
  449. bool Merge(io::ZeroCopyInputStream* input, Message* output);
  450. // Like TextFormat::MergeFromString().
  451. bool MergeFromString(const std::string& input, Message* output);
  452. // Set where to report parse errors. If NULL (the default), errors will
  453. // be printed to stderr.
  454. void RecordErrorsTo(io::ErrorCollector* error_collector) {
  455. error_collector_ = error_collector;
  456. }
  457. // Set how parser finds extensions. If NULL (the default), the
  458. // parser will use the standard Reflection object associated with
  459. // the message being parsed.
  460. void SetFinder(const Finder* finder) { finder_ = finder; }
  461. // Sets where location information about the parse will be written. If NULL
  462. // (the default), then no location will be written.
  463. void WriteLocationsTo(ParseInfoTree* tree) { parse_info_tree_ = tree; }
  464. // Normally parsing fails if, after parsing, output->IsInitialized()
  465. // returns false. Call AllowPartialMessage(true) to skip this check.
  466. void AllowPartialMessage(bool allow) { allow_partial_ = allow; }
  467. // Allow field names to be matched case-insensitively.
  468. // This is not advisable if there are fields that only differ in case, or
  469. // if you want to enforce writing in the canonical form.
  470. // This is 'false' by default.
  471. void AllowCaseInsensitiveField(bool allow) {
  472. allow_case_insensitive_field_ = allow;
  473. }
  474. // Like TextFormat::ParseFieldValueFromString
  475. bool ParseFieldValueFromString(const std::string& input,
  476. const FieldDescriptor* field,
  477. Message* output);
  478. // When an unknown extension is met, parsing will fail if this option is set
  479. // to false (the default). If true, unknown extensions will be ignored and
  480. // a warning message will be generated.
  481. void AllowUnknownExtension(bool allow) { allow_unknown_extension_ = allow; }
  482. // When an unknown field is met, parsing will fail if this option is set
  483. // to false(the default). If true, unknown fields will be ignored and
  484. // a warning message will be generated.
  485. // Please aware that set this option true may hide some errors (e.g.
  486. // spelling error on field name). Avoid to use this option if possible.
  487. void AllowUnknownField(bool allow) { allow_unknown_field_ = allow; }
  488. void AllowFieldNumber(bool allow) { allow_field_number_ = allow; }
  489. // Sets maximum recursion depth which parser can use. This is effectively
  490. // the maximum allowed nesting of proto messages.
  491. void SetRecursionLimit(int limit) { recursion_limit_ = limit; }
  492. private:
  493. // Forward declaration of an internal class used to parse text
  494. // representations (see text_format.cc for implementation).
  495. class ParserImpl;
  496. // Like TextFormat::Merge(). The provided implementation is used
  497. // to do the parsing.
  498. bool MergeUsingImpl(io::ZeroCopyInputStream* input, Message* output,
  499. ParserImpl* parser_impl);
  500. io::ErrorCollector* error_collector_;
  501. const Finder* finder_;
  502. ParseInfoTree* parse_info_tree_;
  503. bool allow_partial_;
  504. bool allow_case_insensitive_field_;
  505. bool allow_unknown_field_;
  506. bool allow_unknown_extension_;
  507. bool allow_unknown_enum_;
  508. bool allow_field_number_;
  509. bool allow_relaxed_whitespace_;
  510. bool allow_singular_overwrites_;
  511. int recursion_limit_;
  512. };
  513. private:
  514. // Hack: ParseInfoTree declares TextFormat as a friend which should extend
  515. // the friendship to TextFormat::Parser::ParserImpl, but unfortunately some
  516. // old compilers (e.g. GCC 3.4.6) don't implement this correctly. We provide
  517. // helpers for ParserImpl to call methods of ParseInfoTree.
  518. static inline void RecordLocation(ParseInfoTree* info_tree,
  519. const FieldDescriptor* field,
  520. ParseLocation location);
  521. static inline ParseInfoTree* CreateNested(ParseInfoTree* info_tree,
  522. const FieldDescriptor* field);
  523. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextFormat);
  524. };
  525. inline void TextFormat::RecordLocation(ParseInfoTree* info_tree,
  526. const FieldDescriptor* field,
  527. ParseLocation location) {
  528. info_tree->RecordLocation(field, location);
  529. }
  530. inline TextFormat::ParseInfoTree* TextFormat::CreateNested(
  531. ParseInfoTree* info_tree, const FieldDescriptor* field) {
  532. return info_tree->CreateNested(field);
  533. }
  534. } // namespace protobuf
  535. } // namespace google
  536. #include <google/protobuf/port_undef.inc>
  537. #endif // GOOGLE_PROTOBUF_TEXT_FORMAT_H__