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"
22#include "google/protobuf/util/json_util.h"
30using ::google::protobuf::TextFormat;
31using google::protobuf::util::JsonParseOptions;
32using google::protobuf::util::JsonStringToMessage;
39 std::string uncompressed;
40 if (
GunzipString(contents, &uncompressed)) contents.swap(uncompressed);
46 google::protobuf::Message*
proto) {
51 std::string uncompressed;
53 VLOG(1) <<
"ReadFileToProto(): input is gzipped";
54 data.swap(uncompressed);
67 constexpr double kMaxBinaryProtoParseShrinkFactor = 2;
68 if (
proto->ParseFromString(data)) {
74 proto->DiscardUnknownFields();
75 if (
proto->ByteSizeLong() <
76 data.size() / kMaxBinaryProtoParseShrinkFactor) {
77 VLOG(1) <<
"ReadFileToProto(): input may be a binary proto, but of a "
80 VLOG(1) <<
"ReadFileToProto(): input seems to be a binary proto";
84 if (google::protobuf::TextFormat::ParseFromString(data,
proto)) {
85 VLOG(1) <<
"ReadFileToProto(): input is a text proto";
88 if (JsonStringToMessage(data,
proto, JsonParseOptions()).ok()) {
93 constexpr int kMaxJsonToBinaryShrinkFactor = 30;
94 if (
proto->ByteSizeLong() < data.size() / kMaxJsonToBinaryShrinkFactor) {
95 VLOG(1) <<
"ReadFileToProto(): input is probably JSON, but probably not"
96 " of the right proto";
98 VLOG(1) <<
"ReadFileToProto(): input is a proto JSON";
102 LOG(
WARNING) <<
"Could not parse protocol buffer";
107 const google::protobuf::Message&
proto,
109 bool append_extension_to_file_name) {
110 std::string file_type_suffix;
111 std::string output_string;
112 google::protobuf::io::StringOutputStream stream(&output_string);
113 switch (proto_write_format) {
115 if (!
proto.SerializeToZeroCopyStream(&stream)) {
116 LOG(
WARNING) <<
"Serialize to stream failed.";
119 file_type_suffix =
".bin";
122 if (!google::protobuf::TextFormat::PrintToString(
proto, &output_string)) {
128 google::protobuf::util::JsonPrintOptions options;
129 options.add_whitespace =
true;
130 options.always_print_primitive_fields =
true;
131 options.preserve_proto_field_names =
true;
132 if (!google::protobuf::util::MessageToJsonString(
proto, &output_string,
138 file_type_suffix =
".json";
142 google::protobuf::util::JsonPrintOptions options;
143 options.add_whitespace =
true;
144 if (!google::protobuf::util::MessageToJsonString(
proto, &output_string,
150 file_type_suffix =
".json";
154 std::string gzip_string;
156 output_string.swap(gzip_string);
157 file_type_suffix +=
".gz";
159 std::string output_filename(filename);
160 if (append_extension_to_file_name) output_filename += file_type_suffix;
161 VLOG(1) <<
"Writing " << output_string.size() <<
" bytes to "
#define VLOG(verboselevel)
void GzipString(absl::string_view uncompressed, std::string *compressed)
bool GunzipString(const std::string &str, std::string *out)
absl::Status GetContents(const absl::string_view &filename, std::string *output, int flags)
absl::Status SetContents(const absl::string_view &filename, const absl::string_view &contents, int flags)
Collection of objects used to extend the Constraint Solver library.
absl::StatusOr< std::string > ReadFileToString(absl::string_view filename)
bool WriteProtoToFile(absl::string_view filename, const google::protobuf::Message &proto, ProtoWriteFormat proto_write_format, bool gzipped, bool append_extension_to_file_name)
bool ReadFileToProto(absl::string_view filename, google::protobuf::Message *proto)
#define RETURN_IF_ERROR(expr)