15 #include <sys/types.h>
17 #include "absl/strings/str_cat.h"
20 #define access _access
30 #include "absl/log/check.h"
34 File::File(FILE*
const f_des,
const absl::string_view&
name)
35 : f_(f_des), name_(
name) {}
43 stat(name_.data(), &f_stat);
44 return f_stat.st_size;
50 if (fclose(f_) == 0) {
60 return absl::Status(absl::StatusCode::kInvalidArgument,
"Wrong flags");
63 : absl::Status(absl::StatusCode::kInvalidArgument,
64 absl::StrCat(
"Could not close file '", name_,
"'"));
68 CHECK_EQ(fread(buf, 1, size, f_), size);
72 return fread(buf, 1, size, f_);
76 CHECK_EQ(fwrite(buf, 1, size, f_), size);
79 return fwrite(buf, 1, size, f_);
83 FILE*
const f_des = fopen(
name, flag);
84 if (f_des ==
nullptr) {
85 std::cerr <<
"Cannot open " <<
name;
93 FILE*
const f_des = fopen(
name, flag);
94 if (f_des ==
nullptr)
return nullptr;
100 return fgets(output, max_length, f_);
104 CHECK(output !=
nullptr);
107 if (max_length == 0)
return 0;
109 int64_t needed = max_length;
110 int bufsize = (needed < (2 << 20) ? needed : (2 << 20));
112 std::unique_ptr<char[]> buf(
new char[bufsize]);
116 nread =
Read(buf.get(), (bufsize < needed ? bufsize : needed));
118 output->append(buf.get(), nread);
124 return (nread >= 0 ?
static_cast<int64_t
>(output->size()) : -1);
133 return Write(
"\n", 1) == 1;
143 absl::Status
Open(
const absl::string_view& filename,
144 const absl::string_view& mode,
File** f,
int flags) {
148 return absl::OkStatus();
151 return absl::Status(absl::StatusCode::kInvalidArgument,
152 absl::StrCat(
"Could not open '", filename,
"'"));
156 const absl::string_view& mode,
int flags) {
160 CHECK(f !=
nullptr) << absl::StrCat(
"Could not open '", filename,
"'");
164 absl::Status
GetContents(
const absl::string_view& filename, std::string* output,
170 const int64_t size =
file->Size();
171 if (
file->ReadToString(output, size) == size) {
175 #if defined(_MSC_VER)
182 const int64_t b_size =
file->Size();
183 if (
file->ReadToString(output, b_size) == b_size) {
190 return absl::Status(absl::StatusCode::kInvalidArgument,
191 absl::StrCat(
"Could not read from '", filename,
"'."));
197 file->Write(contents.data(), contents.size()) == contents.size()) {
198 return absl::OkStatus();
201 absl::StatusCode::kInvalidArgument,
202 absl::StrCat(
"Could not write ", contents.size(),
" bytes"));
206 const absl::string_view& contents,
int flags) {
220 const absl::string_view& file_name) {
225 class NoOpErrorCollector :
public google::protobuf::io::ErrorCollector {
232 google::protobuf::Message*
proto) {
235 LOG(INFO) <<
"Could not read " << file_name;
244 NoOpErrorCollector error_collector;
245 google::protobuf::TextFormat::Parser parser;
246 parser.RecordErrorsTo(&error_collector);
247 if (parser.ParseFromString(str,
proto)) {
250 if (
proto->ParseFromString(str)) {
255 google::protobuf::TextFormat::ParseFromString(str,
proto);
256 LOG(INFO) <<
"Could not parse contents of " << file_name;
261 google::protobuf::Message*
proto) {
266 const absl::string_view& file_name) {
267 std::string proto_string;
268 return google::protobuf::TextFormat::PrintToString(
proto, &proto_string) &&
273 const absl::string_view& file_name) {
278 const absl::string_view& file_name) {
279 std::string proto_string;
280 return proto.AppendToString(&proto_string) &&
285 const absl::string_view& file_name) {
290 google::protobuf::Message*
proto,
int flags) {
295 absl::StatusCode::kInvalidArgument,
296 absl::StrCat(
"Could not read proto from '", filename,
"'."));
300 const google::protobuf::Message&
proto,
int flags) {
305 absl::StatusCode::kInvalidArgument,
306 absl::StrCat(
"Could not write proto to '", filename,
"'."));
310 google::protobuf::Message*
const proto,
314 proto->ParseFromString(str)) {
315 return absl::OkStatus();
318 absl::StatusCode::kInvalidArgument,
319 absl::StrCat(
"Could not read proto from '", filename,
"'."));
323 const google::protobuf::Message&
proto,
int flags) {
328 absl::StatusCode::kInvalidArgument,
329 absl::StrCat(
"Could not write proto to '", filename,
"'."));
332 absl::Status
Delete(
const absl::string_view& path,
int flags) {
334 if (remove(path.data()) == 0)
return absl::OkStatus();
336 return absl::Status(absl::StatusCode::kInvalidArgument,
337 absl::StrCat(
"Could not delete '", path,
"'."));
340 absl::Status
Exists(
const absl::string_view& path,
int flags) {
342 if (access(path.data(), F_OK) == 0)
return absl::OkStatus();
344 return absl::Status(absl::StatusCode::kInvalidArgument,
345 absl::StrCat(
"File '", path,
"' does not exist."));
static File * OpenOrDie(const char *const name, const char *const flag)
int64_t ReadToString(std::string *const line, uint64_t max_length)
char * ReadLine(char *const output, uint64_t max_length)
static bool Exists(const char *const name)
void WriteOrDie(const void *const buff, size_t size)
void ReadOrDie(void *const buff, size_t size)
size_t Write(const void *const buff, size_t size)
size_t Read(void *const buff, size_t size)
bool WriteLine(const std::string &line)
static bool Delete(const char *const name)
absl::string_view filename() const
size_t WriteString(const std::string &line)
absl::Status WriteString(File *file, const absl::string_view &contents, int flags)
absl::Status GetContents(const absl::string_view &filename, std::string *output, int flags)
absl::Status GetBinaryProto(const absl::string_view filename, google::protobuf::Message *const proto, const int flags)
bool ReadFileToString(const absl::string_view &file_name, std::string *output)
absl::Status Delete(const absl::string_view &path, int flags)
File * OpenOrDie(const absl::string_view &filename, const absl::string_view &mode, int flags)
void WriteProtoToASCIIFileOrDie(const google::protobuf::Message &proto, const absl::string_view &file_name)
bool WriteProtoToFile(const google::protobuf::Message &proto, const absl::string_view &file_name)
absl::Status GetTextProto(const absl::string_view &filename, google::protobuf::Message *proto, int flags)
void WriteProtoToFileOrDie(const google::protobuf::Message &proto, const absl::string_view &file_name)
bool WriteProtoToASCIIFile(const google::protobuf::Message &proto, const absl::string_view &file_name)
absl::Status SetTextProto(const absl::string_view &filename, const google::protobuf::Message &proto, int flags)
bool WriteStringToFile(const std::string &data, const absl::string_view &file_name)
absl::Status SetBinaryProto(const absl::string_view &filename, const google::protobuf::Message &proto, int flags)
bool ReadFileToProto(const absl::string_view &file_name, google::protobuf::Message *proto)
absl::Status Exists(const absl::string_view &path, int flags)
absl::Status Open(const absl::string_view &filename, const absl::string_view &mode, File **f, int flags)
absl::Status SetContents(const absl::string_view &filename, const absl::string_view &contents, int flags)
void ReadFileToProtoOrDie(const absl::string_view &file_name, google::protobuf::Message *proto)