35#include "absl/log/check.h"
36#include "absl/status/status.h"
37#include "absl/strings/str_cat.h"
38#include "absl/strings/string_view.h"
39#include "google/protobuf/io/tokenizer.h"
40#include "google/protobuf/message.h"
41#include "google/protobuf/text_format.h"
44File::File(FILE* descriptor, absl::string_view
name)
45 : f_(descriptor), name_(
name) {}
48 std::string null_terminated_name = std::string(
filename);
49 return remove(null_terminated_name.c_str()) == 0;
53 std::string null_terminated_name = std::string(
filename);
54 return access(null_terminated_name.c_str(), F_OK) == 0;
59 stat(name_.c_str(), &f_stat);
60 return f_stat.st_size;
71 if (fclose(f_) == 0) {
86 if (fclose(f_) == 0) {
90 absl::Status(absl::StatusCode::kInvalidArgument,
91 absl::StrCat(
"Could not close file '", name_,
"'")));
98 CHECK_EQ(fread(buf, 1,
size, f_),
size);
104 CHECK_EQ(fwrite(buf, 1,
size, f_),
size);
107 return fwrite(buf, 1,
size, f_);
112 CHECK(f !=
nullptr) << absl::StrCat(
"Could not open '",
filename,
"'");
117 std::string null_terminated_name = std::string(
filename);
118 std::string null_terminated_mode = std::string(mode);
120 fopen(null_terminated_name.c_str(), null_terminated_mode.c_str());
121 if (f_des ==
nullptr)
return nullptr;
127 return fgets(output, max_length, f_);
131 CHECK(
line !=
nullptr);
134 if (max_length == 0)
return 0;
136 int64_t needed = max_length;
137 int bufsize = (needed < (2 << 20) ? needed : (2 << 20));
139 std::unique_ptr<char[]> buf(
new char[bufsize]);
143 nread =
Read(buf.get(), (bufsize < needed ? bufsize : needed));
145 line->append(buf.get(), nread);
151 return (nread >= 0 ?
static_cast<int64_t
>(
line->size()) : -1);
155 return Write(str.data(), str.size());
160 return Write(
"\n", 1) == 1;
170absl::Status
Open(absl::string_view filename, absl::string_view mode,
File** f,
175 return absl::OkStatus();
178 return absl::Status(absl::StatusCode::kInvalidArgument,
179 absl::StrCat(
"Could not open '", filename,
"'"));
187 CHECK(f !=
nullptr) << absl::StrCat(
"Could not open '", filename,
"'");
193 std::string contents;
201absl::Status
GetContents(absl::string_view filename, std::string* output,
219 const int64_t b_size =
file->Size();
220 if (
file->ReadToString(output, b_size) == b_size) {
226 file->Close(options).IgnoreError();
227 return absl::Status(absl::StatusCode::kInvalidArgument,
228 absl::StrCat(
"Could not read from '", filename,
"'."));
234 file->Write(contents.data(), contents.size()) == contents.size()) {
235 return absl::OkStatus();
238 absl::StatusCode::kInvalidArgument,
239 absl::StrCat(
"Could not write ", contents.size(),
" bytes"));
242absl::Status
SetContents(absl::string_view filename, absl::string_view contents,
261class NoOpErrorCollector :
public google::protobuf::io::ErrorCollector {
263 ~NoOpErrorCollector()
override =
default;
264 void RecordError(
int ,
int ,
265 absl::string_view )
override {}
270 google::protobuf::Message*
proto) {
273 LOG(INFO) <<
"Could not read " << file_name;
282 NoOpErrorCollector error_collector;
283 google::protobuf::TextFormat::Parser parser;
284 parser.RecordErrorsTo(&error_collector);
285 if (parser.ParseFromString(str,
proto)) {
288 if (
proto->ParseFromString(str)) {
293 google::protobuf::TextFormat::ParseFromString(str,
proto);
294 LOG(INFO) <<
"Could not parse contents of " << file_name;
299 google::protobuf::Message*
proto) {
304 absl::string_view file_name) {
305 std::string proto_string;
306 return google::protobuf::TextFormat::PrintToString(
proto, &proto_string) &&
311 absl::string_view file_name) {
316 absl::string_view file_name) {
317 std::string proto_string;
318 return proto.AppendToString(&proto_string) &&
323 absl::string_view file_name) {
333 absl::StatusCode::kInvalidArgument,
334 absl::StrCat(
"Could not read proto from '", filename,
"'."));
338 const google::protobuf::Message&
proto,
344 absl::StatusCode::kInvalidArgument,
345 absl::StrCat(
"Could not write proto to '", filename,
"'."));
352 proto->ParseFromString(str)) {
353 return absl::OkStatus();
356 absl::StatusCode::kInvalidArgument,
357 absl::StrCat(
"Could not read proto from '", filename,
"'."));
361 const google::protobuf::Message&
proto,
367 absl::StatusCode::kInvalidArgument,
368 absl::StrCat(
"Could not write proto to '", filename,
"'."));
373 std::string null_terminated_path = std::string(path);
374 if (remove(null_terminated_path.c_str()) == 0)
return absl::OkStatus();
376 return absl::Status(absl::StatusCode::kInvalidArgument,
377 absl::StrCat(
"Could not delete '", path,
"'."));
382 std::string null_terminated_path = std::string(path);
383 if (access(null_terminated_path.c_str(), F_OK) == 0) {
384 return absl::OkStatus();
387 return absl::Status(absl::StatusCode::kInvalidArgument,
388 absl::StrCat(
"File '", path,
"' does not exist."));
absl::string_view filename() const
Returns the file name.
static bool Delete(absl::string_view filename)
Deletes a file.
bool WriteLine(absl::string_view line)
Writes a string to file and append a "\n".
size_t Write(const void *buff, size_t size)
Writes "size" bytes of buff to file, buff should be pre-allocated.
size_t Read(void *buff, size_t size)
Reads "size" bytes to buff from file, buff should be pre-allocated.
static File * OpenOrDie(absl::string_view filename, absl::string_view mode)
void WriteOrDie(const void *buff, size_t size)
static void Init()
Inits internal data structures.
size_t Size()
Returns file size.
char * ReadLine(char *output, uint64_t max_length)
static bool Exists(absl::string_view filename)
Tests if a file exists.
size_t WriteString(absl::string_view str)
Writes a string to file.
bool Flush()
Flushes buffer.
bool Close()
Closes the file.
int64_t ReadToString(std::string *line, uint64_t max_length)
void ReadOrDie(void *buff, size_t size)
CpModelProto proto
The output proto.
const std::string name
A name for logging purposes.
absl::Status SetTextProto(absl::string_view filename, const google::protobuf::Message &proto, Options options)
absl::StatusOr< std::string > GetContents(absl::string_view path, Options options)
bool WriteProtoToASCIIFile(const google::protobuf::Message &proto, absl::string_view file_name)
absl::Status Exists(absl::string_view path, Options options)
absl::Status Open(absl::string_view filename, absl::string_view mode, File **f, Options options)
As of 2016-01, these methods can only be used with flags = file::Defaults().
absl::Status SetBinaryProto(absl::string_view filename, const google::protobuf::Message &proto, Options options)
bool ReadFileToString(absl::string_view file_name, std::string *output)
bool ReadFileToProto(absl::string_view file_name, google::protobuf::Message *proto)
absl::Status SetContents(absl::string_view filename, absl::string_view contents, Options options)
bool WriteStringToFile(absl::string_view data, absl::string_view file_name)
absl::Status WriteString(File *file, absl::string_view contents, Options options)
void WriteProtoToASCIIFileOrDie(const google::protobuf::Message &proto, absl::string_view file_name)
bool WriteProtoToFile(const google::protobuf::Message &proto, absl::string_view file_name)
void ReadFileToProtoOrDie(absl::string_view file_name, google::protobuf::Message *proto)
void WriteProtoToFileOrDie(const google::protobuf::Message &proto, absl::string_view file_name)
absl::Status Delete(absl::string_view path, Options options)
absl::Status GetTextProto(absl::string_view filename, google::protobuf::Message *proto, Options options)
absl::Status GetBinaryProto(const absl::string_view filename, google::protobuf::Message *proto, Options options)
File * OpenOrDie(absl::string_view filename, absl::string_view mode, Options options)