9#ifndef CPPCOMMON_FILESYSTEM_FILE_H
10#define CPPCOMMON_FILESYSTEM_FILE_H
49 {
Assign(path);
return *
this; }
59 uint64_t
size()
const;
65 {
return (
size() == 0); }
118 size_t Read(
void* buffer,
size_t size)
override;
133 size_t Write(
const void* buffer,
size_t size)
override;
164 void Flush()
override;
213 static size_t WriteAllLines(
const Path& path,
const std::vector<std::string>& lines);
228 Impl& impl() noexcept {
return reinterpret_cast<Impl&
>(_storage); }
229 const Impl& impl() const noexcept {
return reinterpret_cast<Impl const&
>(_storage); }
231 static const size_t StorageSize = 128;
232 static const size_t StorageAlign = 8;
233 alignas(StorageAlign) std::byte _storage[StorageSize];
void Close()
Close the file.
size_t Read(void *buffer, size_t size) override
Read a bytes buffer from the opened file.
static const Flags< FilePermissions > DEFAULT_PERMISSIONS
Default file permissions (IRUSR | IWUSR | IRGRP | IROTH).
void Resize(uint64_t size)
Resize the current file.
static size_t WriteAllLines(const Path &path, const std::vector< std::string > &lines)
Write text lines into the given file.
static size_t WriteAllText(const Path &path, const std::string &text)
Write a text string into the given file.
File()
Initialize file with an empty path.
void OpenOrCreate(bool read, bool write, bool truncate=false, const Flags< FileAttributes > &attributes=File::DEFAULT_ATTRIBUTES, const Flags< FilePermissions > &permissions=File::DEFAULT_PERMISSIONS, size_t buffer=File::DEFAULT_BUFFER)
Open or create file.
uint64_t size() const
Get the current file size.
bool IsFileReadOpened() const
Is the file opened for reading?
bool IsFileEmpty() const
Is the file empty?
bool IsFileOpened() const
Is the file opened?
static const size_t DEFAULT_BUFFER
Default file buffer size (8192).
void Flush() override
Flush the file.
bool IsFileWriteOpened() const
Is the file opened for writing?
void Create(bool read, bool write, const Flags< FileAttributes > &attributes=File::DEFAULT_ATTRIBUTES, const Flags< FilePermissions > &permissions=File::DEFAULT_PERMISSIONS, size_t buffer=File::DEFAULT_BUFFER)
Create a new file.
bool IsFileExists() const
Is the file exists?
void Open(bool read, bool write, bool truncate=false, const Flags< FileAttributes > &attributes=File::DEFAULT_ATTRIBUTES, const Flags< FilePermissions > &permissions=File::DEFAULT_PERMISSIONS, size_t buffer=File::DEFAULT_BUFFER)
Open an existing file.
std::vector< std::string > ReadAllLines()
Read all text lines.
void Seek(uint64_t offset)
Seek into the opened file.
size_t Write(const void *buffer, size_t size) override
Write a byte buffer into the opened file.
static const Flags< FileAttributes > DEFAULT_ATTRIBUTES
Default file attributes (Normal).
File & operator=(const Path &path)
std::vector< uint8_t > ReadAllBytes()
Read all bytes.
std::string ReadAllText()
Read all text.
void swap(File &file) noexcept
Swap two instances.
static size_t WriteAllBytes(const Path &path, const void *buffer, size_t size)
Write a bytes buffer into the given file.
static void WriteEmpty(const Path &path)
Write an empty file.
uint64_t offset() const
Get the current read/write offset of the opened file.
Path & Assign(const Path &path)
Assign the given path to the current one.
Flags< FileAttributes > attributes() const
Get the path file attributes.
Path()
Initialize path with an empty value.
Flags< FilePermissions > permissions() const
Get the path file permissions.
Reader() noexcept=default
std::vector< std::string > ReadAllLines()
Read all text lines.
std::vector< uint8_t > ReadAllBytes()
Read all bytes.
std::string ReadAllText()
Read all text.
virtual size_t Write(const void *buffer, size_t size)=0
Write a byte buffer base method.
Writer() noexcept=default
Filesystem file inline implementation.
C++ Common project definitions.
Filesystem path definition.
Reader interface definition.
Writer interface definition.