17#include "absl/strings/str_cat.h"
33File::File(FILE*
const f_des,
const absl::string_view&
name)
34 : f_(f_des), name_(
name) {}
42 stat(name_.data(), &f_stat);
43 return f_stat.st_size;
49 if (fclose(f_) == 0) {
59 return absl::Status(absl::StatusCode::kInvalidArgument,
"Wrong flags");
62 : absl::Status(absl::StatusCode::kInvalidArgument,
63 absl::StrCat(
"Could not close file '", name_,
"'"));
67 CHECK_EQ(fread(buf, 1, size, f_), size);
71 return fread(buf, 1, size, f_);
75 CHECK_EQ(fwrite(buf, 1, size, f_), size);
78 return fwrite(buf, 1, size, f_);
82 FILE*
const f_des = fopen(
name, flag);
84 std::cerr <<
"Cannot open " <<
name;
92 FILE*
const f_des = fopen(
name, flag);
93 if (f_des == NULL)
return NULL;
99 return fgets(output, max_length, f_);
103 CHECK(output !=
nullptr);
106 if (max_length == 0)
return 0;
108 int64 needed = max_length;
109 int bufsize = (needed < (2 << 20) ? needed : (2 << 20));
111 std::unique_ptr<char[]> buf(
new char[bufsize]);
115 nread =
Read(buf.get(), (bufsize < needed ? bufsize : needed));
117 output->append(buf.get(), nread);
123 return (nread >= 0 ?
static_cast<int64>(output->size()) : -1);
127 return Write(line.c_str(), line.size());
131 if (
Write(line.c_str(), line.size()) != line.size())
return false;
132 return Write(
"\n", 1) == 1;
142absl::Status
Open(
const absl::string_view& filename,
143 const absl::string_view& mode,
File** f,
int flags) {
147 return absl::OkStatus();
150 return absl::Status(absl::StatusCode::kInvalidArgument,
151 absl::StrCat(
"Could not open '", filename,
"'"));
155 const absl::string_view& mode,
int flags) {
159 CHECK(f !=
nullptr) << absl::StrCat(
"Could not open '", filename,
"'");
163absl::Status
GetContents(
const absl::string_view& filename, std::string* output,
169 if (
file->ReadToString(output, size) == size)
return absl::OkStatus();
176 if (
file->ReadToString(output, b_size) == b_size)
return absl::OkStatus();
180 return absl::Status(absl::StatusCode::kInvalidArgument,
181 absl::StrCat(
"Could not read '", filename,
"'"));
187 file->Write(contents.data(), contents.size()) == contents.size() &&
189 return absl::OkStatus();
192 absl::StatusCode::kInvalidArgument,
193 absl::StrCat(
"Could not write ", contents.size(),
" bytes"));
197 const absl::string_view& contents,
int flags) {
206 const absl::string_view& file_name) {
211class NoOpErrorCollector :
public google::protobuf::io::ErrorCollector {
213 virtual void AddError(
int line,
int column,
const std::string&
message) {}
218 google::protobuf::Message*
proto) {
221 LOG(
INFO) <<
"Could not read " << file_name;
230 NoOpErrorCollector error_collector;
231 google::protobuf::TextFormat::Parser parser;
232 parser.RecordErrorsTo(&error_collector);
233 if (parser.ParseFromString(str,
proto)) {
236 if (
proto->ParseFromString(str)) {
241 google::protobuf::TextFormat::ParseFromString(str,
proto);
242 LOG(
INFO) <<
"Could not parse contents of " << file_name;
247 google::protobuf::Message*
proto) {
252 const absl::string_view& file_name) {
253 std::string proto_string;
254 return google::protobuf::TextFormat::PrintToString(
proto, &proto_string) &&
259 const absl::string_view& file_name) {
264 const absl::string_view& file_name) {
265 std::string proto_string;
266 return proto.AppendToString(&proto_string) &&
271 const absl::string_view& file_name) {
276 google::protobuf::Message*
proto,
int flags) {
281 absl::StatusCode::kInvalidArgument,
282 absl::StrCat(
"Could not read proto from '", filename,
"'."));
286 const google::protobuf::Message&
proto,
int flags) {
291 absl::StatusCode::kInvalidArgument,
292 absl::StrCat(
"Could not write proto to '", filename,
"'."));
296 const google::protobuf::Message&
proto,
int flags) {
301 absl::StatusCode::kInvalidArgument,
302 absl::StrCat(
"Could not write proto to '", filename,
"'."));
305absl::Status
Delete(
const absl::string_view& path,
int flags) {
307 if (remove(path.data()) == 0)
return absl::OkStatus();
309 return absl::Status(absl::StatusCode::kInvalidArgument,
310 absl::StrCat(
"Could not delete '", path,
"'."));
313absl::Status
Exists(
const absl::string_view& path,
int flags) {
315 if (access(path.data(), F_OK) == 0)
return absl::OkStatus();
317 return absl::Status(absl::StatusCode::kInvalidArgument,
318 absl::StrCat(
"File '", path,
"' does not exist."));
#define CHECK_EQ(val1, val2)
static File * OpenOrDie(const char *const name, const char *const flag)
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)
char * ReadLine(char *const output, uint64 max_length)
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
int64 ReadToString(std::string *const line, uint64 max_length)
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)
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)