OR-Tools  8.0
file_util.cc
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #include "ortools/util/file_util.h"
15 
16 #include "absl/strings/str_cat.h"
17 #include "google/protobuf/descriptor.h"
18 #include "google/protobuf/io/zero_copy_stream_impl_lite.h"
19 #include "google/protobuf/message.h"
20 #include "google/protobuf/text_format.h"
21 #include "ortools/base/file.h"
22 #include "ortools/base/logging.h"
24 
25 namespace operations_research {
26 
27 using ::google::protobuf::TextFormat;
28 
29 absl::StatusOr<std::string> ReadFileToString(absl::string_view filename) {
30  std::string contents;
31  RETURN_IF_ERROR(file::GetContents(filename, &contents, file::Defaults()));
32  // Note that gzipped files are currently not supported.
33  return contents;
34 }
35 
36 bool ReadFileToProto(absl::string_view filename,
37  google::protobuf::Message* proto) {
38  std::string data;
39  CHECK_OK(file::GetContents(filename, &data, file::Defaults()));
40  // Note that gzipped files are currently not supported.
41  // Try binary format first, then text format, then JSON, then proto3 JSON,
42  // then give up.
43  // For some of those, like binary format and proto3 JSON, we perform
44  // additional checks to verify that we have the right proto: it can happen
45  // to try to read a proto of type Foo as a proto of type Bar, by mistake, and
46  // we'd rather have this function fail rather than silently accept it, because
47  // the proto parser is too lenient with unknown fields.
48  // We don't require ByteSizeLong(parsed) == input.size(), because it may be
49  // the case that the proto version changed and some fields are dropped.
50  // We just fail when the difference is too large.
51  constexpr double kMaxBinaryProtoParseShrinkFactor = 2;
52  if (proto->ParseFromString(data)) {
53  // NOTE(user): When using ParseFromString() from a generic
54  // google::protobuf::Message, like we do here, all fields are stored, even
55  // if their are unknown in the underlying proto type. Unless we explicitly
56  // discard those 'unknown fields' here, our call to ByteSizeLong() will
57  // still count the unknown payload.
58  proto->DiscardUnknownFields();
59  if (proto->ByteSizeLong() <
60  data.size() / kMaxBinaryProtoParseShrinkFactor) {
61  VLOG(1) << "ReadFileToProto(): input may be a binary proto, but of a "
62  "different proto";
63  } else {
64  VLOG(1) << "ReadFileToProto(): input seems to be a binary proto";
65  return true;
66  }
67  }
68  if (google::protobuf::TextFormat::ParseFromString(data, proto)) {
69  VLOG(1) << "ReadFileToProto(): input is a text proto";
70  return true;
71  }
72  LOG(WARNING) << "Could not parse protocol buffer";
73  return false;
74 }
75 
76 bool WriteProtoToFile(absl::string_view filename,
77  const google::protobuf::Message& proto,
78  ProtoWriteFormat proto_write_format, bool gzipped,
79  bool append_extension_to_file_name) {
80  // Note that gzipped files are currently not supported.
81  gzipped = false;
82 
83  std::string file_type_suffix;
84  std::string output_string;
85  google::protobuf::io::StringOutputStream stream(&output_string);
86  switch (proto_write_format) {
88  if (!proto.SerializeToZeroCopyStream(&stream)) {
89  LOG(WARNING) << "Serialize to stream failed.";
90  return false;
91  }
92  file_type_suffix = ".bin";
93  break;
95  if (!google::protobuf::TextFormat::PrintToString(proto, &output_string)) {
96  LOG(WARNING) << "Printing to string failed.";
97  return false;
98  }
99  break;
100  }
101  std::string output_filename(filename);
102  if (append_extension_to_file_name) output_filename += file_type_suffix;
103  VLOG(1) << "Writing " << output_string.size() << " bytes to "
104  << output_filename;
105  if (!file::SetContents(output_filename, output_string, file::Defaults())
106  .ok()) {
107  LOG(WARNING) << "Writing to file failed.";
108  return false;
109  }
110  return true;
111 }
112 
113 } // namespace operations_research
operations_research::ReadFileToString
absl::StatusOr< std::string > ReadFileToString(absl::string_view filename)
Definition: file_util.cc:29
file::SetContents
absl::Status SetContents(const absl::string_view &filename, const absl::string_view &contents, int flags)
Definition: file.cc:188
CHECK_OK
#define CHECK_OK(x)
Definition: base/logging.h:29
file::GetContents
absl::Status GetContents(const absl::string_view &filename, std::string *output, int flags)
Definition: file.cc:163
file_util.h
operations_research::ProtoWriteFormat::kProtoText
@ kProtoText
logging.h
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
file.h
operations_research::ProtoWriteFormat::kProtoBinary
@ kProtoBinary
operations_research::WriteProtoToFile
bool WriteProtoToFile(absl::string_view filename, const google::protobuf::Message &proto, ProtoWriteFormat proto_write_format, bool gzipped, bool append_extension_to_file_name)
Definition: file_util.cc:76
operations_research::ProtoWriteFormat
ProtoWriteFormat
Definition: file_util.h:47
status_macros.h
operations_research::ReadFileToProto
bool ReadFileToProto(absl::string_view filename, google::protobuf::Message *proto)
Definition: file_util.cc:36
file::Defaults
int Defaults()
Definition: base/file.h:119
proto
CpModelProto proto
Definition: cp_model_fz_solver.cc:106
RETURN_IF_ERROR
#define RETURN_IF_ERROR(expr)
Definition: status_macros.h:27
absl::StatusOr
Definition: statusor.h:24