~ubuntu-branches/ubuntu/raring/protobuf/raring

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Elliot Murphy
  • Date: 2010-06-08 17:29:12 UTC
  • mfrom: (2.2.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100608172912-nnd1hrkbv1u36pzl
Tags: 2.3.0-2ubuntu1
* Merge from Debian unstable.
* Remaining Ubuntu changes:
  - Don't use python2.4 (refreshed this patch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
 
55
55
string Message::DebugString() const {
56
56
  string debug_string;
57
 
  io::StringOutputStream output_stream(&debug_string);
58
57
 
59
 
  TextFormat::Print(*this, &output_stream);
 
58
  TextFormat::PrintToString(*this, &debug_string);
60
59
 
61
60
  return debug_string;
62
61
}
63
62
 
64
63
string Message::ShortDebugString() const {
65
64
  string debug_string;
66
 
  io::StringOutputStream output_stream(&debug_string);
67
65
 
68
66
  TextFormat::Printer printer;
69
67
  printer.SetSingleLineMode(true);
70
68
 
71
 
  printer.Print(*this, &output_stream);
 
69
  printer.PrintToString(*this, &debug_string);
72
70
  // Single line mode currently might have an extra space at the end.
73
71
  if (debug_string.size() > 0 &&
74
72
      debug_string[debug_string.size() - 1] == ' ') {
78
76
  return debug_string;
79
77
}
80
78
 
 
79
string Message::Utf8DebugString() const {
 
80
  string debug_string;
 
81
 
 
82
  TextFormat::Printer printer;
 
83
  printer.SetUseUtf8StringEscaping(true);
 
84
 
 
85
  printer.PrintToString(*this, &debug_string);
 
86
 
 
87
  return debug_string;
 
88
}
 
89
 
81
90
void Message::PrintDebugString() const {
82
91
  printf("%s", DebugString().c_str());
83
92
}
84
93
 
 
94
 
85
95
// ===========================================================================
86
96
// Internal class for parsing an ASCII representation of a Protocol Message.
87
97
// This class makes use of the Protocol Message compiler's tokenizer found
170
180
    }
171
181
  }
172
182
 
 
183
  void ReportWarning(int line, int col, const string& message) {
 
184
    if (error_collector_ == NULL) {
 
185
      if (line >= 0) {
 
186
        GOOGLE_LOG(WARNING) << "Warning parsing text-format "
 
187
                     << root_message_type_->full_name()
 
188
                     << ": " << (line + 1) << ":"
 
189
                     << (col + 1) << ": " << message;
 
190
      } else {
 
191
        GOOGLE_LOG(WARNING) << "Warning parsing text-format "
 
192
                     << root_message_type_->full_name()
 
193
                     << ": " << message;
 
194
      }
 
195
    } else {
 
196
      error_collector_->AddWarning(line, col, message);
 
197
    }
 
198
  }
 
199
 
173
200
 private:
174
201
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ParserImpl);
175
202
 
180
207
                message);
181
208
  }
182
209
 
 
210
  // Reports a warning with the given message with information indicating
 
211
  // the position (as derived from the current token).
 
212
  void ReportWarning(const string& message) {
 
213
    ReportWarning(tokenizer_.current().line, tokenizer_.current().column,
 
214
                  message);
 
215
  }
 
216
 
183
217
  // Consumes the specified message with the given starting delimeter.
184
218
  // This method checks to see that the end delimeter at the conclusion of
185
219
  // the consumption matches the starting delimeter passed in here.
270
304
      DO(ConsumeFieldValue(message, reflection, field));
271
305
    }
272
306
 
 
307
    if (field->options().deprecated()) {
 
308
      ReportWarning("text format contains deprecated field \""
 
309
                    + field_name + "\"");
 
310
    }
 
311
 
273
312
    return true;
274
313
  }
275
314
 
583
622
      parser_->ReportError(line, column, message);
584
623
    }
585
624
 
 
625
    virtual void AddWarning(int line, int column, const string& message) {
 
626
      parser_->ReportWarning(line, column, message);
 
627
    }
 
628
 
586
629
   private:
587
630
    GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ParserErrorCollector);
588
631
    TextFormat::Parser::ParserImpl* parser_;
644
687
 
645
688
  // Print text to the output stream.
646
689
  void Print(const string& str) {
647
 
    Print(str.c_str());
 
690
    Print(str.data(), str.size());
648
691
  }
649
692
 
650
693
  // Print text to the output stream.
651
694
  void Print(const char* text) {
652
 
    int size = strlen(text);
 
695
    Print(text, strlen(text));
 
696
  }
 
697
 
 
698
  // Print text to the output stream.
 
699
  void Print(const char* text, int size) {
653
700
    int pos = 0;  // The number of bytes we've written so far.
654
701
 
655
702
    for (int i = 0; i < size; i++) {
799
846
 
800
847
TextFormat::Printer::Printer()
801
848
  : initial_indent_level_(0),
802
 
    single_line_mode_(false) {}
 
849
    single_line_mode_(false),
 
850
    use_short_repeated_primitives_(false),
 
851
    utf8_string_escaping_(false) {}
803
852
 
804
853
TextFormat::Printer::~Printer() {}
805
854
 
876
925
                                     const Reflection* reflection,
877
926
                                     const FieldDescriptor* field,
878
927
                                     TextGenerator& generator) {
 
928
  if (use_short_repeated_primitives_ &&
 
929
      field->is_repeated() &&
 
930
      field->cpp_type() != FieldDescriptor::CPPTYPE_STRING &&
 
931
      field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
 
932
    PrintShortRepeatedField(message, reflection, field, generator);
 
933
    return;
 
934
  }
 
935
 
879
936
  int count = 0;
880
937
 
881
938
  if (field->is_repeated()) {
885
942
  }
886
943
 
887
944
  for (int j = 0; j < count; ++j) {
888
 
    if (field->is_extension()) {
889
 
      generator.Print("[");
890
 
      // We special-case MessageSet elements for compatibility with proto1.
891
 
      if (field->containing_type()->options().message_set_wire_format()
892
 
          && field->type() == FieldDescriptor::TYPE_MESSAGE
893
 
          && field->is_optional()
894
 
          && field->extension_scope() == field->message_type()) {
895
 
        generator.Print(field->message_type()->full_name());
896
 
      } else {
897
 
        generator.Print(field->full_name());
898
 
      }
899
 
      generator.Print("]");
900
 
    } else {
901
 
      if (field->type() == FieldDescriptor::TYPE_GROUP) {
902
 
        // Groups must be serialized with their original capitalization.
903
 
        generator.Print(field->message_type()->name());
904
 
      } else {
905
 
        generator.Print(field->name());
906
 
      }
907
 
    }
 
945
    PrintFieldName(message, reflection, field, generator);
908
946
 
909
947
    if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
910
948
      if (single_line_mode_) {
926
964
    PrintFieldValue(message, reflection, field, field_index, generator);
927
965
 
928
966
    if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
929
 
      if (!single_line_mode_) {
 
967
      if (single_line_mode_) {
 
968
        generator.Print("} ");
 
969
      } else {
930
970
        generator.Outdent();
931
 
      }
932
 
      generator.Print("}");
933
 
    }
934
 
 
935
 
    if (single_line_mode_) {
936
 
      generator.Print(" ");
937
 
    } else {
938
 
      generator.Print("\n");
 
971
        generator.Print("}\n");
 
972
      }
 
973
    } else {
 
974
      if (single_line_mode_) {
 
975
        generator.Print(" ");
 
976
      } else {
 
977
        generator.Print("\n");
 
978
      }
 
979
    }
 
980
  }
 
981
}
 
982
 
 
983
void TextFormat::Printer::PrintShortRepeatedField(const Message& message,
 
984
                                                  const Reflection* reflection,
 
985
                                                  const FieldDescriptor* field,
 
986
                                                  TextGenerator& generator) {
 
987
  // Print primitive repeated field in short form.
 
988
  PrintFieldName(message, reflection, field, generator);
 
989
 
 
990
  int size = reflection->FieldSize(message, field);
 
991
  generator.Print(": [");
 
992
  for (int i = 0; i < size; i++) {
 
993
    if (i > 0) generator.Print(", ");
 
994
    PrintFieldValue(message, reflection, field, i, generator);
 
995
  }
 
996
  if (single_line_mode_) {
 
997
    generator.Print("] ");
 
998
  } else {
 
999
    generator.Print("]\n");
 
1000
  }
 
1001
}
 
1002
 
 
1003
void TextFormat::Printer::PrintFieldName(const Message& message,
 
1004
                                         const Reflection* reflection,
 
1005
                                         const FieldDescriptor* field,
 
1006
                                         TextGenerator& generator) {
 
1007
  if (field->is_extension()) {
 
1008
    generator.Print("[");
 
1009
    // We special-case MessageSet elements for compatibility with proto1.
 
1010
    if (field->containing_type()->options().message_set_wire_format()
 
1011
        && field->type() == FieldDescriptor::TYPE_MESSAGE
 
1012
        && field->is_optional()
 
1013
        && field->extension_scope() == field->message_type()) {
 
1014
      generator.Print(field->message_type()->full_name());
 
1015
    } else {
 
1016
      generator.Print(field->full_name());
 
1017
    }
 
1018
    generator.Print("]");
 
1019
  } else {
 
1020
    if (field->type() == FieldDescriptor::TYPE_GROUP) {
 
1021
      // Groups must be serialized with their original capitalization.
 
1022
      generator.Print(field->message_type()->name());
 
1023
    } else {
 
1024
      generator.Print(field->name());
939
1025
    }
940
1026
  }
941
1027
}
973
1059
            reflection->GetStringReference(message, field, &scratch);
974
1060
 
975
1061
        generator.Print("\"");
976
 
        generator.Print(CEscape(value));
 
1062
        if (utf8_string_escaping_) {
 
1063
          generator.Print(strings::Utf8SafeCEscape(value));
 
1064
        } else {
 
1065
          generator.Print(CEscape(value));
 
1066
        }
977
1067
        generator.Print("\"");
978
1068
 
979
1069
        break;