14#ifndef OR_TOOLS_BASE_FILE_H_
15#define OR_TOOLS_BASE_FILE_H_
21#include "absl/status/status.h"
22#include "absl/status/statusor.h"
23#include "absl/strings/string_view.h"
24#include "google/protobuf/message.h"
35 static File*
Open(absl::string_view file_name, absl::string_view mode);
40 static File*
OpenOrDie(absl::string_view file_name, absl::string_view mode);
43 explicit File(absl::string_view name);
47 virtual size_t Read(
void* buf,
size_t size) = 0;
50 virtual size_t Write(
const void* buf,
size_t size) = 0;
53 virtual absl::Status
Close(
int flags) = 0;
62 virtual bool Open()
const = 0;
66 int64_t
ReadToString(std::string* line, uint64_t max_length);
92absl::Status
Open(absl::string_view file_name, absl::string_view mode,
File** f,
96File*
OpenOrDie(absl::string_view file_name, absl::string_view mode,
104absl::StatusOr<std::string>
GetContents(absl::string_view path,
107absl::Status
GetContents(absl::string_view file_name, std::string* output,
110absl::Status
SetContents(absl::string_view file_name,
111 absl::string_view contents,
Options options);
119 google::protobuf::Message* proto,
Options options);
129 const google::protobuf::Message& proto,
133 google::protobuf::Message* proto,
Options options);
143 const google::protobuf::Message& proto,
#define RETURN_IF_ERROR(expr)
absl::string_view filename() const
Returns the file name.
virtual bool Flush()=0
Flushes buffer.
virtual absl::Status Close(int flags)=0
Closes the file and delete the underlying FILE* descriptor.
virtual size_t Write(const void *buf, size_t size)=0
Writes "size" bytes of buf to file, buff should be pre-allocated.
virtual size_t Read(void *buf, size_t size)=0
Reads "size" bytes to buf from file, buff should be pre-allocated.
static void Init()
Inits internal data structures.
static File * OpenOrDie(absl::string_view file_name, absl::string_view mode)
size_t WriteString(absl::string_view str)
Writes a string to file.
File(absl::string_view name)
virtual bool Open() const =0
Returns whether the file is currently open.
int64_t ReadToString(std::string *line, uint64_t max_length)
virtual size_t Size()=0
Returns file size.
absl::StatusOr< std::string > GetContents(absl::string_view path, Options options)
-— Content API -—
absl::Status Exists(absl::string_view path, Options options)
absl::Status SetBinaryProto(absl::string_view file_name, const google::protobuf::Message &proto, Options options)
absl::Status SetTextProto(absl::string_view file_name, const google::protobuf::Message &proto, Options options)
absl::Status GetTextProto(absl::string_view file_name, google::protobuf::Message *proto, Options options)
-— Protobuf API -—
File * OpenOrDie(absl::string_view file_name, absl::string_view mode, Options options)
absl::Status WriteString(File *file, absl::string_view contents, Options options)
absl::Status GetBinaryProto(const absl::string_view file_name, google::protobuf::Message *proto, Options options)
absl::Status Delete(absl::string_view path, Options options)
absl::Status Open(absl::string_view file_name, absl::string_view mode, File **f, Options options)
As of 2016-01, these methods can only be used with flags = file::Defaults().
absl::Status SetContents(absl::string_view file_name, absl::string_view contents, Options options)