14#ifndef OR_TOOLS_UTIL_FILE_UTIL_H_
15#define OR_TOOLS_UTIL_FILE_UTIL_H_
20#include "absl/log/check.h"
21#include "absl/status/statusor.h"
22#include "absl/strings/string_view.h"
23#include "google/protobuf/message.h"
40 absl::string_view filename, google::protobuf::Message*
proto,
43 bool allow_partial =
false);
47 google::protobuf::Message*
proto,
48 bool allow_partial =
false);
50template <
typename Proto>
52 bool allow_partial =
false) {
55 <<
"filename=" << filename;
69 const google::protobuf::Message&
proto,
72 bool append_extension_to_file_name =
true);
78template <
typename Proto>
81 std::vector<Proto> protos;
84 while (num_read != expected_num_records &&
86 protos.push_back(
proto);
91 <<
"File '" <<
file->filename()
92 <<
"'was not fully read, or something went wrong when closing "
93 "it. Is it the right format? (RecordIO of Protocol Buffers).";
95 if (expected_num_records >= 0) {
96 CHECK_EQ(num_read, expected_num_records)
97 <<
"There were less than the expected " << expected_num_records
105template <
typename Proto>
107 int expected_num_records) {
109 expected_num_records);
116template <
typename Proto>
120template <
typename Proto>
128template <
typename Proto>
137template <
typename Proto>
139 const std::vector<Proto>& protos) {
142 for (
const Proto&
proto : protos) {
145 CHECK(writer.
Close());
#define RETURN_IF_ERROR(expr)
bool Close()
Closes the underlying file.
bool ReadProtocolMessage(P *const proto)
bool Close()
Closes the underlying file.
bool WriteProtocolMessage(const P &proto)
CpModelProto proto
The output proto.
File * OpenOrDie(absl::string_view filename, absl::string_view mode, Options options)
std::vector< Proto > ReadNumRecords(File *file, int expected_num_records)
In SWIG mode, we don't want anything besides these top-level includes.
void WriteRecordsOrDie(absl::string_view filename, const std::vector< Proto > &protos)
absl::StatusOr< std::string > ReadFileToString(absl::string_view filename)
Reads a file, optionally gzipped, to a string.
absl::Status StringToProto(absl::string_view data, google::protobuf::Message *proto, bool allow_partial)
Exactly like ReadFileToProto(), but directly from the contents.
std::vector< Proto > ReadAllRecordsOrDie(absl::string_view filename)
Proto ReadOneRecordOrDie(absl::string_view filename)
absl::Status WriteProtoToFile(absl::string_view filename, const google::protobuf::Message &proto, ProtoWriteFormat proto_write_format, bool gzipped, bool append_extension_to_file_name)
absl::Status ReadFileToProto(absl::string_view filename, google::protobuf::Message *proto, bool allow_partial)