16 #include "absl/status/statusor.h"
17 #include "absl/strings/str_cat.h"
18 #include "google/protobuf/descriptor.h"
19 #include "google/protobuf/io/zero_copy_stream_impl_lite.h"
20 #include "google/protobuf/message.h"
21 #include "google/protobuf/text_format.h"
28 using ::google::protobuf::TextFormat;
38 google::protobuf::Message*
proto) {
52 constexpr
double kMaxBinaryProtoParseShrinkFactor = 2;
53 if (
proto->ParseFromString(data)) {
59 proto->DiscardUnknownFields();
60 if (
proto->ByteSizeLong() <
61 data.size() / kMaxBinaryProtoParseShrinkFactor) {
62 VLOG(1) <<
"ReadFileToProto(): input may be a binary proto, but of a "
65 VLOG(1) <<
"ReadFileToProto(): input seems to be a binary proto";
69 if (google::protobuf::TextFormat::ParseFromString(data,
proto)) {
70 VLOG(1) <<
"ReadFileToProto(): input is a text proto";
73 LOG(
WARNING) <<
"Could not parse protocol buffer";
78 const google::protobuf::Message&
proto,
80 bool append_extension_to_file_name) {
84 std::string file_type_suffix;
85 std::string output_string;
86 google::protobuf::io::StringOutputStream stream(&output_string);
87 switch (proto_write_format) {
88 case ProtoWriteFormat::kProtoBinary:
89 if (!
proto.SerializeToZeroCopyStream(&stream)) {
93 file_type_suffix =
".bin";
95 case ProtoWriteFormat::kProtoText:
96 if (!google::protobuf::TextFormat::PrintToString(
proto, &output_string)) {
102 std::string output_filename(filename);
103 if (append_extension_to_file_name) output_filename += file_type_suffix;
104 VLOG(1) <<
"Writing " << output_string.size() <<
" bytes to "