Google OR-Tools: ortools/util/file_util.h Source File

1

2

3

4

5

6

7

8

9

10

11

12

13

14#ifndef ORTOOLS_UTIL_FILE_UTIL_H_

15#define ORTOOLS_UTIL_FILE_UTIL_H_

16

17#include <string>

18#include <vector>

19

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"

28

30

31

32absl::StatusOr<std::string> ReadFileToString(absl::string_view filename);

33

34

35

36

37

38

40 absl::string_view filename, google::protobuf::Message* proto,

41

42

43 bool allow_partial = false);

44

45

47 google::protobuf::Message* proto,

48 bool allow_partial = false);

49

50template <typename Proto>

52 bool allow_partial = false) {

53 Proto proto;

55 << "filename=" << filename;

56 return proto;

57}

58

59

60

62

63

64

65

66

67

69 const google::protobuf::Message& proto,

71 bool gzipped = false,

72 bool append_extension_to_file_name = true);

73

75

76

77

78template <typename Proto>

81 std::vector<Proto> protos;

82 Proto proto;

83 int num_read = 0;

84 while (num_read != expected_num_records &&

86 protos.push_back(proto);

87 ++num_read;

88 }

89

90 CHECK(reader.Close())

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).";

94

95 if (expected_num_records >= 0) {

96 CHECK_EQ(num_read, expected_num_records)

97 << "There were less than the expected " << expected_num_records

98 << " in the file.";

99 }

100

101 return protos;

102}

103

104

105template <typename Proto>

107 int expected_num_records) {

109 expected_num_records);

110}

111}

112

113

114

115

116template <typename Proto>

120template <typename Proto>

124

125

126

127

128template <typename Proto>

130 Proto p;

132 return p;

133}

134

135

136

137template <typename Proto>

139 const std::vector<Proto>& protos) {

142 for (const Proto& proto : protos) {

144 }

145 CHECK(writer.Close());

146}

147

148}

149

150#endif

#define RETURN_IF_ERROR(expr)

bool ReadProtocolMessage(P *const proto)

bool WriteProtocolMessage(const P &proto)

File * OpenOrDie(absl::string_view file_name, absl::string_view mode, Options options)

std::vector< Proto > ReadNumRecords(File *file, int expected_num_records)

Definition file_util.h:79

void WriteRecordsOrDie(absl::string_view filename, const std::vector< Proto > &protos)

Definition file_util.h:138

absl::StatusOr< std::string > ReadFileToString(absl::string_view filename)

ProtoWriteFormat

Definition file_util.h:61

@ kJson

Definition file_util.h:61

@ kProtoBinary

Definition file_util.h:61

@ kCanonicalJson

Definition file_util.h:61

@ kProtoText

Definition file_util.h:61

std::vector< Proto > ReadAllRecordsOrDie(absl::string_view filename)

Definition file_util.h:117

absl::Status StringToProto(absl::string_view data, google::protobuf::Message *proto, bool allow_partial)

Proto ReadOneRecordOrDie(absl::string_view filename)

Definition file_util.h:129

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)