OR-Tools  8.1
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/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 "ortools/base/file.h"
23 #include "ortools/base/logging.h"
25 
26 namespace operations_research {
27 
28 using ::google::protobuf::TextFormat;
29 
30 absl::StatusOr<std::string> ReadFileToString(absl::string_view filename) {
31  std::string contents;
32  RETURN_IF_ERROR(file::GetContents(filename, &contents, file::Defaults()));
33  // Note that gzipped files are currently not supported.
34  return contents;
35 }
36 
37 bool ReadFileToProto(absl::string_view filename,
38  google::protobuf::Message* proto) {
39  std::string data;
40  CHECK_OK(file::GetContents(filename, &data, file::Defaults()));
41  // Note that gzipped files are currently not supported.
42  // Try binary format first, then text format, then JSON, then proto3 JSON,
43  // then give up.
44  // For some of those, like binary format and proto3 JSON, we perform
45  // additional checks to verify that we have the right proto: it can happen
46  // to try to read a proto of type Foo as a proto of type Bar, by mistake, and
47  // we'd rather have this function fail rather than silently accept it, because
48  // the proto parser is too lenient with unknown fields.
49  // We don't require ByteSizeLong(parsed) == input.size(), because it may be
50  // the case that the proto version changed and some fields are dropped.
51  // We just fail when the difference is too large.
52  constexpr double kMaxBinaryProtoParseShrinkFactor = 2;
53  if (proto->ParseFromString(data)) {
54  // NOTE(user): When using ParseFromString() from a generic
55  // google::protobuf::Message, like we do here, all fields are stored, even
56  // if their are unknown in the underlying proto type. Unless we explicitly
57  // discard those 'unknown fields' here, our call to ByteSizeLong() will
58  // still count the unknown payload.
59  proto->DiscardUnknownFields();
60  if (proto->ByteSizeLong() <
61  data.size() / kMaxBinaryProtoParseShrinkFactor) {
62  VLOG(1) << "ReadFileToProto(): input may be a binary proto, but of a "
63  "different proto";
64  } else {
65  VLOG(1) << "ReadFileToProto(): input seems to be a binary proto";
66  return true;
67  }
68  }
69  if (google::protobuf::TextFormat::ParseFromString(data, proto)) {
70  VLOG(1) << "ReadFileToProto(): input is a text proto";
71  return true;
72  }
73  LOG(WARNING) << "Could not parse protocol buffer";
74  return false;
75 }
76 
77 bool WriteProtoToFile(absl::string_view filename,
78  const google::protobuf::Message& proto,
79  ProtoWriteFormat proto_write_format, bool gzipped,
80  bool append_extension_to_file_name) {
81  // Note that gzipped files are currently not supported.
82  gzipped = false;
83 
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)) {
90  LOG(WARNING) << "Serialize to stream failed.";
91  return false;
92  }
93  file_type_suffix = ".bin";
94  break;
95  case ProtoWriteFormat::kProtoText:
96  if (!google::protobuf::TextFormat::PrintToString(proto, &output_string)) {
97  LOG(WARNING) << "Printing to string failed.";
98  return false;
99  }
100  break;
101  }
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 "
105  << output_filename;
106  if (!file::SetContents(output_filename, output_string, file::Defaults())
107  .ok()) {
108  LOG(WARNING) << "Writing to file failed.";
109  return false;
110  }
111  return true;
112 }
113 
114 } // namespace operations_research
operations_research::ReadFileToString
absl::StatusOr< std::string > ReadFileToString(absl::string_view filename)
Definition: file_util.cc:30
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:40
VLOG
#define VLOG(verboselevel)
Definition: base/logging.h:978
file::GetContents
absl::Status GetContents(const absl::string_view &filename, std::string *output, int flags)
Definition: file.cc:163
LOG
#define LOG(severity)
Definition: base/logging.h:420
file_util.h
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
WARNING
const int WARNING
Definition: log_severity.h:31
file.h
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:77
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:37
file::Defaults
int Defaults()
Definition: base/file.h:119
proto
CpModelProto proto
Definition: cp_model_fz_solver.cc:107
RETURN_IF_ERROR
#define RETURN_IF_ERROR(expr)
Definition: status_macros.h:27