4#ifndef GREM_CORE_SYSTEM_FILE_HPP
5#define GREM_CORE_SYSTEM_FILE_HPP
44 enum class Kind : uint8_t {
66 static constexpr size_t npos =
static_cast<size_t>(-1);
95 explicit operator
bool() const noexcept {
117 [[nodiscard]]
virtual bool isOpen() const noexcept = 0;
142 [[nodiscard]]
virtual bool eof() const noexcept = 0;
150 [[nodiscard]] virtual
size_t size() const noexcept = 0;
158 [[nodiscard]] virtual
size_t tellg() const noexcept = 0;
171 virtual
void seekg(
size_t position) = 0;
184 virtual
void skipg(ptrdiff_t offset) = 0;
377 template <
typename Implementation,
typename... Args>
453 return file && file->isOpen();
458 return !file || file->eof();
463 return (file) ? file->size() : 0;
468 return (file) ? file->tellg() : 0;
476 file->seekg(position);
492 return file->readBytesIntoAllocation(maxLength);
500 return file->readBytesIntoArrayList(maxLength);
508 return file->readBytesIntoBuffer(maxLength);
516 return file->readBytesIntoString(maxLength);
524 return file->readBytesIntoCString(maxLength);
532 return file->readSome(data);
540 return file->readUntilEOF(data);
556 return file->tryRead(data);
561 : file(std::move(file)) {}
563 UniquePointer<InputFile> file{};
584 const size_t bytesWritten = output.
writeSome(data);
599 [[nodiscard]]
virtual size_t tellp() const noexcept = 0;
612 virtual
void seekp(
size_t position) = 0;
625 virtual
void skipp(ptrdiff_t offset) = 0;
655 while (!data.empty()) {
656 data = data.subspan(writeSome(data));
700 template <
typename Implementation,
typename... Args>
776 return file && file->isOpen();
781 return (file) ? file->tellp() : 0;
789 file->seekp(position);
805 return file->writeSome(data);
834 : file(std::move(file)) {}
836 UniquePointer<OutputFile> file{};
#define GREM_ALWAYS_INLINE
Definition attributes.hpp:10
Definition Allocation.hpp:25
Definition ArrayList.hpp:27
Base exception type for runtime errors.
Definition Error.hpp:38
Error(CStringView message)
Definition Error.hpp:55
static constexpr size_t npos
Invalid value for a file offset, used as an end-of-file marker.
Definition File.hpp:66
virtual bool isOpen() const noexcept=0
Check if the file handle has an open file associated with it.
Kind
File entry type.
Definition File.hpp:44
@ OTHER
Something else, such as a network socket or a device.
Definition File.hpp:48
@ SYMLINK
Symbolic link.
Definition File.hpp:47
@ DIRECTORY
Directory/folder.
Definition File.hpp:46
@ REGULAR
Regular file.
Definition File.hpp:45
virtual void close()=0
Close the associated file so that it can no longer be accessed through this handle,...
File() noexcept=default
Default constructor.
Generic boxed unique handle to a writable file in any filesystem.
Definition File.hpp:686
GREM_ALWAYS_INLINE void seekp(size_t position)
Set the file writing position to an absolute offset from the beginning of the file.
Definition File.hpp:785
GREM_ALWAYS_INLINE bool isOpen() const noexcept
Check if the file handle has an open file associated with it.
Definition File.hpp:775
static GREM_ALWAYS_INLINE OutputFileHandle create(Args &&... args)
Create a boxed file with a specific implementation.
Definition File.hpp:701
GREM_ALWAYS_INLINE const OutputFile * get() const noexcept
Get a pointer to the underlying file interface.
Definition File.hpp:735
GREM_ALWAYS_INLINE size_t writeSome(Span< const byte > data)
Write data to the end of an open file from a buffer.
Definition File.hpp:801
GREM_ALWAYS_INLINE void skipp(ptrdiff_t offset)
Advance the file writing position by a relative offset, which may be negative in order to go backward...
Definition File.hpp:793
GREM_ALWAYS_INLINE void close()
Close the associated file so that it can no longer be accessed through this handle,...
Definition File.hpp:767
GREM_ALWAYS_INLINE void write(StringView data)
Write data to the end of an open file.
Definition File.hpp:817
GREM_ALWAYS_INLINE size_t tellp() const noexcept
Get the current writing position of the file.
Definition File.hpp:780
GREM_ALWAYS_INLINE OutputFile * get() noexcept
Get a pointer to the underlying file interface.
Definition File.hpp:726
GREM_ALWAYS_INLINE void flush()
Synchronize with the underlying file to make sure that all buffered data that has been written so far...
Definition File.hpp:825
GREM_ALWAYS_INLINE void write(Span< const byte > data)
Write data to the end of an open file.
Definition File.hpp:809
OutputFileHandle() noexcept=default
Construct a null file handle without an associated file.
Base interface for a writable file in a Filesystem.
Definition File.hpp:569
virtual void seekp(size_t position)=0
Set the file writing position to an absolute offset from the beginning of the file.
void write(Span< const byte > data)
Write data to the end of an open file.
Definition File.hpp:654
virtual void skipp(ptrdiff_t offset)=0
Advance the file writing position by a relative offset, which may be negative in order to go backward...
virtual void flush()=0
Synchronize with the underlying file to make sure that all buffered data that has been written so far...
virtual size_t tellp() const noexcept=0
Get the current writing position of the file.
OutputFile() noexcept=default
Default constructor.
virtual size_t writeSome(Span< const byte > data)=0
Write data to the end of an open file from a buffer.
void write(StringView string)
Write data from a string to the end of an open file.
Definition File.hpp:670
Definition Reader.hpp:658
constexpr size_type size() const noexcept
Definition Span.hpp:207
Nullable RAII data pointer with exclusive ownership of an object that is automatically destroyed on p...
Definition UniquePointer.hpp:77
static constexpr UniquePointer create(Args &&... args)
Allocate and construct an object of the associated type and wrap it in a pointer with unique ownershi...
Definition UniquePointer.hpp:101
Definition Writer.hpp:217
StringBase< char, std::char_traits< char >, std::allocator< char > > String
Definition String.hpp:17
StringViewBase< char, std::char_traits< char > > StringView
Definition StringView.hpp:16
auto asBytes(Span< T, N > s) noexcept
Definition Span.hpp:359
Exception type for errors that may arise when attempting to access files through the File API.
Definition File.hpp:37
Error(CStringView message)
Definition Error.hpp:55
Definition fundamentals.hpp:27
static constexpr T MAX
Definition fundamentals.hpp:29