Skip to content

Commit 65125e2

Browse files
committed
reduce FILE API
1 parent d95ee9b commit 65125e2

File tree

3 files changed

+32
-113
lines changed

3 files changed

+32
-113
lines changed

ortools/base/file.cc

+3-63
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,6 @@
4444
File::File(FILE* descriptor, absl::string_view name)
4545
: f_(descriptor), name_(name) {}
4646

47-
bool File::Delete(absl::string_view filename) {
48-
std::string null_terminated_name = std::string(filename);
49-
return remove(null_terminated_name.c_str()) == 0;
50-
}
51-
52-
bool File::Exists(absl::string_view filename) {
53-
std::string null_terminated_name = std::string(filename);
54-
return access(null_terminated_name.c_str(), F_OK) == 0;
55-
}
56-
5747
size_t File::Size() {
5848
struct stat f_stat;
5949
stat(name_.c_str(), &f_stat);
@@ -62,21 +52,6 @@ size_t File::Size() {
6252

6353
bool File::Flush() { return fflush(f_) == 0; }
6454

65-
// Deletes "this" on closing.
66-
bool File::Close() {
67-
bool ok = true;
68-
if (f_ == nullptr) {
69-
return ok;
70-
}
71-
if (fclose(f_) == 0) {
72-
f_ = nullptr;
73-
} else {
74-
ok = false;
75-
}
76-
delete this;
77-
return ok;
78-
}
79-
8055
// Deletes "this" on closing.
8156
absl::Status File::Close(int /*flags*/) {
8257
absl::Status status;
@@ -94,15 +69,8 @@ absl::Status File::Close(int /*flags*/) {
9469
return status;
9570
}
9671

97-
void File::ReadOrDie(void* buf, size_t size) {
98-
CHECK_EQ(fread(buf, 1, size, f_), size);
99-
}
100-
10172
size_t File::Read(void* buf, size_t size) { return fread(buf, 1, size, f_); }
10273

103-
void File::WriteOrDie(const void* buf, size_t size) {
104-
CHECK_EQ(fwrite(buf, 1, size, f_), size);
105-
}
10674
size_t File::Write(const void* buf, size_t size) {
10775
return fwrite(buf, 1, size, f_);
10876
}
@@ -123,10 +91,6 @@ File* File::Open(absl::string_view filename, absl::string_view mode) {
12391
return f;
12492
}
12593

126-
char* File::ReadLine(char* output, uint64_t max_length) {
127-
return fgets(output, max_length, f_);
128-
}
129-
13094
int64_t File::ReadToString(std::string* line, uint64_t max_length) {
13195
CHECK(line != nullptr);
13296
line->clear();
@@ -155,11 +119,6 @@ size_t File::WriteString(absl::string_view str) {
155119
return Write(str.data(), str.size());
156120
}
157121

158-
bool File::WriteLine(absl::string_view line) {
159-
if (Write(line.data(), line.size()) != line.size()) return false;
160-
return Write("\n", 1) == 1;
161-
}
162-
163122
absl::string_view File::filename() const { return name_; }
164123

165124
bool File::Open() const { return f_ != nullptr; }
@@ -211,7 +170,7 @@ absl::Status GetContents(absl::string_view filename, std::string* output,
211170
}
212171
#if defined(_MSC_VER)
213172
// On windows, binary files needs to be opened with the "rb" flags.
214-
file->Close();
173+
file->Close(options);
215174
// Retry in binary mode.
216175
status = file::Open(filename, "rb", &file, options);
217176
if (!status.ok()) return status;
@@ -257,10 +216,6 @@ bool ReadFileToString(absl::string_view file_name, std::string* output) {
257216
return GetContents(file_name, output, file::Defaults()).ok();
258217
}
259218

260-
bool WriteStringToFile(absl::string_view data, absl::string_view file_name) {
261-
return SetContents(file_name, data, file::Defaults()).ok();
262-
}
263-
264219
namespace {
265220
class NoOpErrorCollector : public google::protobuf::io::ErrorCollector {
266221
public:
@@ -299,33 +254,18 @@ bool ReadFileToProto(absl::string_view file_name,
299254
return false;
300255
}
301256

302-
void ReadFileToProtoOrDie(absl::string_view file_name,
303-
google::protobuf::Message* proto) {
304-
CHECK(ReadFileToProto(file_name, proto)) << "file_name: " << file_name;
305-
}
306-
307257
bool WriteProtoToASCIIFile(const google::protobuf::Message& proto,
308258
absl::string_view file_name) {
309259
std::string proto_string;
310260
return google::protobuf::TextFormat::PrintToString(proto, &proto_string) &&
311-
WriteStringToFile(proto_string, file_name);
312-
}
313-
314-
void WriteProtoToASCIIFileOrDie(const google::protobuf::Message& proto,
315-
absl::string_view file_name) {
316-
CHECK(WriteProtoToASCIIFile(proto, file_name)) << "file_name: " << file_name;
261+
file::SetContents(file_name, proto_string, file::Defaults()).ok();
317262
}
318263

319264
bool WriteProtoToFile(const google::protobuf::Message& proto,
320265
absl::string_view file_name) {
321266
std::string proto_string;
322267
return proto.AppendToString(&proto_string) &&
323-
WriteStringToFile(proto_string, file_name);
324-
}
325-
326-
void WriteProtoToFileOrDie(const google::protobuf::Message& proto,
327-
absl::string_view file_name) {
328-
CHECK(WriteProtoToFile(proto, file_name)) << "file_name: " << file_name;
268+
file::SetContents(file_name, proto_string, file::Defaults()).ok();
329269
}
330270

331271
absl::Status GetTextProto(absl::string_view filename,

ortools/base/file.h

+27-48
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,17 @@ class File {
4444
// Reads "size" bytes to buff from file, buff should be pre-allocated.
4545
size_t Read(void* buff, size_t size);
4646

47-
// Reads "size" bytes to buff from file, buff should be pre-allocated.
48-
// If read failed, program will exit.
49-
void ReadOrDie(void* buff, size_t size);
50-
51-
// Reads a line from file to a string.
52-
// Each line must be no more than max_length bytes.
53-
char* ReadLine(char* output, uint64_t max_length);
54-
5547
// Reads the whole file to a string, with a maximum length of 'max_length'.
5648
// Returns the number of bytes read.
5749
int64_t ReadToString(std::string* line, uint64_t max_length);
5850

5951
// Writes "size" bytes of buff to file, buff should be pre-allocated.
6052
size_t Write(const void* buff, size_t size);
6153

62-
// Writes "size" bytes of buff to file, buff should be pre-allocated.
63-
// If write failed, program will exit.
64-
void WriteOrDie(const void* buff, size_t size);
65-
6654
// Writes a string to file.
6755
size_t WriteString(absl::string_view str);
6856

69-
// Writes a string to file and append a "\n".
70-
bool WriteLine(absl::string_view line);
71-
7257
// Closes the file and delete the underlying FILE* descriptor.
73-
bool Close();
7458
absl::Status Close(int flags);
7559

7660
// Flushes buffer.
@@ -85,12 +69,6 @@ class File {
8569
// Returns the file name.
8670
absl::string_view filename() const;
8771

88-
// Deletes a file.
89-
static bool Delete(absl::string_view filename);
90-
91-
// Tests if a file exists.
92-
static bool Exists(absl::string_view filename);
93-
9472
bool Open() const;
9573

9674
private:
@@ -108,61 +86,62 @@ inline Options Defaults() { return 0xBABA; }
10886

10987
// As of 2016-01, these methods can only be used with flags = file::Defaults().
11088

89+
// ---- File API ----
90+
11191
// The caller should free the File after closing it by passing *f to delete.
11292
absl::Status Open(absl::string_view filename, absl::string_view mode, File** f,
11393
Options options);
11494
// The caller should free the File after closing it by passing the returned
11595
// pointer to delete.
11696
File* OpenOrDie(absl::string_view filename, absl::string_view mode,
11797
Options options);
98+
99+
absl::Status Delete(absl::string_view path, Options options);
100+
absl::Status Exists(absl::string_view path, Options options);
101+
102+
// ---- Content API ----
103+
104+
absl::StatusOr<std::string> GetContents(absl::string_view path,
105+
Options options);
106+
107+
absl::Status GetContents(absl::string_view filename, std::string* output,
108+
Options options);
109+
110+
absl::Status SetContents(absl::string_view filename, absl::string_view contents,
111+
Options options);
112+
113+
absl::Status WriteString(File* file, absl::string_view contents,
114+
Options options);
115+
116+
// ---- Protobuf API ----
117+
118118
absl::Status GetTextProto(absl::string_view filename,
119119
google::protobuf::Message* proto, Options options);
120+
120121
template <typename T>
121122
absl::StatusOr<T> GetTextProto(absl::string_view filename, Options options) {
122123
T proto;
123124
RETURN_IF_ERROR(GetTextProto(filename, &proto, options));
124125
return proto;
125126
}
127+
126128
absl::Status SetTextProto(absl::string_view filename,
127129
const google::protobuf::Message& proto,
128130
Options options);
131+
129132
absl::Status GetBinaryProto(absl::string_view filename,
130133
google::protobuf::Message* proto, Options options);
131134
template <typename T>
135+
132136
absl::StatusOr<T> GetBinaryProto(absl::string_view filename, Options options) {
133137
T proto;
134138
RETURN_IF_ERROR(GetBinaryProto(filename, &proto, options));
135139
return proto;
136140
}
141+
137142
absl::Status SetBinaryProto(absl::string_view filename,
138143
const google::protobuf::Message& proto,
139144
Options options);
140-
absl::Status SetContents(absl::string_view filename, absl::string_view contents,
141-
Options options);
142-
absl::StatusOr<std::string> GetContents(absl::string_view path,
143-
Options options);
144-
absl::Status GetContents(absl::string_view filename, std::string* output,
145-
Options options);
146-
absl::Status WriteString(File* file, absl::string_view contents,
147-
Options options);
148-
149-
bool ReadFileToString(absl::string_view file_name, std::string* output);
150-
bool WriteStringToFile(absl::string_view data, absl::string_view file_name);
151-
bool ReadFileToProto(absl::string_view file_name,
152-
google::protobuf::Message* proto);
153-
void ReadFileToProtoOrDie(absl::string_view file_name,
154-
google::protobuf::Message* proto);
155-
bool WriteProtoToASCIIFile(const google::protobuf::Message& proto,
156-
absl::string_view file_name);
157-
void WriteProtoToASCIIFileOrDie(const google::protobuf::Message& proto,
158-
absl::string_view file_name);
159-
bool WriteProtoToFile(const google::protobuf::Message& proto,
160-
absl::string_view file_name);
161-
void WriteProtoToFileOrDie(const google::protobuf::Message& proto,
162-
absl::string_view file_name);
163-
164-
absl::Status Delete(absl::string_view path, Options options);
165-
absl::Status Exists(absl::string_view path, Options options);
166145

167146
} // namespace file
168147

ortools/base/recordio.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const int RecordWriter::kMagicNumber = 0x3ed7230a;
2727

2828
RecordWriter::RecordWriter(File* file) : file_(file), use_compression_(true) {}
2929

30-
bool RecordWriter::Close() { return file_->Close(); }
30+
bool RecordWriter::Close() { return file_->Close(file::Defaults()).ok(); }
3131

3232
void RecordWriter::set_use_compression(bool use_compression) {
3333
use_compression_ = use_compression;
@@ -52,7 +52,7 @@ std::string RecordWriter::Compress(std::string const& s) const {
5252

5353
RecordReader::RecordReader(File* const file) : file_(file) {}
5454

55-
bool RecordReader::Close() { return file_->Close(); }
55+
bool RecordReader::Close() { return file_->Close(file::Defaults()).ok(); }
5656

5757
void RecordReader::Uncompress(const char* const source, uint64_t source_size,
5858
char* const output_buffer,

0 commit comments

Comments
 (0)