A growing collection of I/O utilities. More...
Functions | |
| SOURCEMETA_CORE_IO_EXPORT auto | sourcemeta::core::canonical (const std::filesystem::path &path) -> std::filesystem::path |
| SOURCEMETA_CORE_IO_EXPORT auto | sourcemeta::core::weakly_canonical (const std::filesystem::path &path) -> std::filesystem::path |
| SOURCEMETA_CORE_IO_EXPORT auto | sourcemeta::core::is_under_path (const std::filesystem::path &path, const std::filesystem::path &prefix) -> bool |
| SOURCEMETA_CORE_IO_EXPORT auto | sourcemeta::core::is_lexically_under_path (const std::filesystem::path &path, const std::filesystem::path &prefix) -> bool |
| SOURCEMETA_CORE_IO_EXPORT auto | sourcemeta::core::strip_path_prefix (const std::filesystem::path &path, const std::filesystem::path &prefix) -> std::filesystem::path |
| template<typename CharT = char, typename Traits = std::char_traits<CharT>> | |
| auto | sourcemeta::core::read_file (const std::filesystem::path &path) -> std::basic_ifstream< CharT, Traits > |
| template<typename CharT = char, typename Traits = std::char_traits<CharT>> | |
| auto | sourcemeta::core::read_to_string (std::basic_istream< CharT, Traits > &stream) -> std::basic_string< CharT, Traits > |
| template<typename CharT = char, typename Traits = std::char_traits<CharT>> | |
| auto | sourcemeta::core::resume_stream (std::basic_istream< CharT, Traits > &stream, const std::streampos start, const std::streamsize count) -> void |
| template<typename CharT = char, typename Traits = std::char_traits<CharT>> | |
| auto | sourcemeta::core::read_file_to_string (const std::filesystem::path &path) -> std::basic_string< CharT, Traits > |
| auto | sourcemeta::core::read_stdin () -> std::string |
| template<typename Callback> | |
| auto | sourcemeta::core::for_each_line (std::istream &stream, Callback callback) -> void |
| SOURCEMETA_CORE_IO_EXPORT auto | sourcemeta::core::hardlink_directory (const std::filesystem::path &source, const std::filesystem::path &destination) -> void |
| SOURCEMETA_CORE_IO_EXPORT auto | sourcemeta::core::write_file (const std::filesystem::path &path, const std::string_view contents) -> void |
| SOURCEMETA_CORE_IO_EXPORT auto | sourcemeta::core::write_file (const std::filesystem::path &path, const std::span< const std::byte > contents) -> void |
| SOURCEMETA_CORE_IO_EXPORT auto | sourcemeta::core::write_file (const std::filesystem::path &path, const std::function< void(std::ostream &)> &writer) -> void |
| SOURCEMETA_CORE_IO_EXPORT auto | sourcemeta::core::flush (const std::filesystem::path &path) -> void |
| SOURCEMETA_CORE_IO_EXPORT auto | sourcemeta::core::atomic_write_file (const std::filesystem::path &path, const std::string_view contents) -> void |
| SOURCEMETA_CORE_IO_EXPORT auto | sourcemeta::core::atomic_write_file (const std::filesystem::path &path, const std::span< const std::byte > contents) -> void |
| SOURCEMETA_CORE_IO_EXPORT auto | sourcemeta::core::atomic_write_file (const std::filesystem::path &path, const std::function< void(std::ostream &)> &writer) -> void |
| SOURCEMETA_CORE_IO_EXPORT auto | sourcemeta::core::atomic_directory_swap (const std::filesystem::path &original, const std::filesystem::path &replacement) -> void |
A growing collection of I/O utilities.
This functionality is included as follows:
| class sourcemeta::core::BinaryWriter |
Typed wrapper over an output stream.
| class sourcemeta::core::BinaryReader |
Cursor-tracking reader over a FileView or an std::istream.
Public Member Functions | |
| BinaryReader (const FileView &view) noexcept | |
| Construct a reader over the given file view. | |
| BinaryReader (std::istream &stream) noexcept | |
| Construct a reader over the given input stream. | |
| BinaryReader (const BinaryReader &)=delete | |
| BinaryReader (BinaryReader &&)=delete | |
| auto | get_byte () -> std::uint8_t |
| Read a single byte. | |
| auto | get_word () -> std::uint16_t |
| Read a 16-bit unsigned integer. | |
| auto | get_dword () -> std::uint32_t |
| Read a 32-bit unsigned integer. | |
| auto | get_qword () -> std::uint64_t |
| Read a 64-bit unsigned integer. | |
| auto | get_bytes (std::byte *destination, const std::size_t size) -> void |
| Read a raw sequence of bytes. | |
| auto | position () const -> std::size_t |
| The current cursor position in bytes. | |
| auto | seek (const std::size_t position) -> void |
| Move the cursor to position. | |
| auto | has_more_data () const -> bool |
| Whether the source has unconsumed bytes at the current cursor. | |
| class sourcemeta::core::InputByteStream |
An input stream constructed from an inline list of byte values. For example:
Public Member Functions | |
| SOURCEMETA_CORE_IO_EXPORT | InputByteStream (std::initializer_list< std::uint8_t > bytes) |
| Construct the stream from the given byte values. | |
| class sourcemeta::core::OutputByteStream |
An output stream that exposes its accumulated bytes as a byte vector. For example:
Public Member Functions | |
| SOURCEMETA_CORE_IO_EXPORT auto | bytes () const -> std::vector< std::byte > |
| The accumulated bytes. | |
| class sourcemeta::core::FileViewError |
An error that represents a failure to memory-map a file
Public Member Functions | |
| FileViewError (std::filesystem::path path, const char *message) | |
| Construct the error given the offending path and a message. | |
| FileViewError (std::filesystem::path path, std::string message)=delete | |
| FileViewError (std::filesystem::path path, std::string &&message)=delete | |
| FileViewError (std::filesystem::path path, std::string_view message)=delete | |
| auto | path () const noexcept -> const std::filesystem::path & |
| The offending path. | |
| class sourcemeta::core::IOFileNotFoundError |
The requested file does not exist.
Public Member Functions | |
| IOFileNotFoundError (std::filesystem::path path) | |
| Construct the error given the offending path. | |
| auto | path () const noexcept -> const std::filesystem::path & |
| The offending path. | |
| class sourcemeta::core::IOFilePermissionError |
The current process lacks permission to access the requested path.
Public Member Functions | |
| IOFilePermissionError (std::filesystem::path path) | |
| Construct the error given the offending path. | |
| auto | path () const noexcept -> const std::filesystem::path & |
| The offending path. | |
| class sourcemeta::core::IOIsADirectoryError |
The path resolves to a directory where a regular file was expected.
Public Member Functions | |
| IOIsADirectoryError (std::filesystem::path path) | |
| Construct the error given the offending path. | |
| auto | path () const noexcept -> const std::filesystem::path & |
| The offending path. | |
| class sourcemeta::core::IONotADirectoryError |
The path resolves to a regular file where a directory was expected.
Public Member Functions | |
| IONotADirectoryError (std::filesystem::path path) | |
| Construct the error given the offending path. | |
| auto | path () const noexcept -> const std::filesystem::path & |
| The offending path. | |
| class sourcemeta::core::IOFileAlreadyExistsError |
The destination path already exists and cannot be replaced.
Public Member Functions | |
| IOFileAlreadyExistsError (std::filesystem::path path) | |
| Construct the error given the offending path. | |
| auto | path () const noexcept -> const std::filesystem::path & |
| The offending path. | |
| class sourcemeta::core::IOReadOutOfBoundsError |
A read attempted to access bytes outside the bounds of the underlying data.
| class sourcemeta::core::IOStreamWriteError |
A write to the underlying stream failed.
| class sourcemeta::core::FileView |
A read-only memory-mapped file. For example:
Public Member Functions | |
| FileView (const std::filesystem::path &path) | |
| Memory-map the file at the given path. | |
| FileView (const FileView &)=delete | |
| FileView (FileView &&)=delete | |
| auto | size () const noexcept -> std::size_t |
| The size of the memory-mapped data in bytes. | |
| template<typename T> | |
| auto | as (const std::size_t offset=0) const noexcept -> const T * |
|
inlinenodiscardnoexcept |
Interpret the memory-mapped data as a pointer to T at the given offset. The caller must ensure that the offset yields a pointer suitably aligned for T, as dereferencing a misaligned pointer is undefined behavior.
| class sourcemeta::core::TemporaryDirectory |
An RAII class that creates a uniquely-named temporary directory on construction and removes it on destruction.
Public Member Functions | |
| TemporaryDirectory (const std::filesystem::path &parent, const std::string_view prefix) | |
| TemporaryDirectory (const TemporaryDirectory &)=delete | |
| TemporaryDirectory (TemporaryDirectory &&)=delete | |
| auto | path () const noexcept -> const std::filesystem::path & |
| The path to the temporary directory. | |
| sourcemeta::core::TemporaryDirectory::TemporaryDirectory | ( | const std::filesystem::path & | parent, |
| const std::string_view | prefix ) |
Create a uniquely-named temporary directory under the given parent using the given name prefix.
| SOURCEMETA_CORE_IO_EXPORT auto sourcemeta::core::atomic_directory_swap | ( | const std::filesystem::path & | original, |
| const std::filesystem::path & | replacement ) -> void |
Atomically swap two directories. Both directories must reside on the same filesystem and the original path must not be a bare filename (it must have a parent component). After the call, the original path holds the contents of the replacement and the replacement path holds the former contents of the original. If the original does not exist, the replacement is simply renamed into place and the replacement path will no longer exist.
| SOURCEMETA_CORE_IO_EXPORT auto sourcemeta::core::atomic_write_file | ( | const std::filesystem::path & | path, |
| const std::function< void(std::ostream &)> & | writer ) -> void |
Callback variant of atomic_write_file. For example:
| SOURCEMETA_CORE_IO_EXPORT auto sourcemeta::core::atomic_write_file | ( | const std::filesystem::path & | path, |
| const std::span< const std::byte > | contents ) -> void |
Atomically write a byte span to path. For example:
| SOURCEMETA_CORE_IO_EXPORT auto sourcemeta::core::atomic_write_file | ( | const std::filesystem::path & | path, |
| const std::string_view | contents ) -> void |
Atomically write contents to path. For example:
| SOURCEMETA_CORE_IO_EXPORT auto sourcemeta::core::canonical | ( | const std::filesystem::path & | path | ) | -> std::filesystem::path |
A safe variant of std::filesystem::canonical that takes into account platform-specific oddities like FIFO on GNU/Linux. For example:
| SOURCEMETA_CORE_IO_EXPORT auto sourcemeta::core::flush | ( | const std::filesystem::path & | path | ) | -> void |
Flush an existing file to disk, beyond just to the operating system. For example:
| auto sourcemeta::core::for_each_line | ( | std::istream & | stream, |
| Callback | callback ) -> void |
Iterate the lines of stream, invoking callback with each line. A trailing carriage return is dropped so that Windows line endings produce the same line as Unix ones. The line view is only valid for the duration of the callback. For example:
| SOURCEMETA_CORE_IO_EXPORT auto sourcemeta::core::hardlink_directory | ( | const std::filesystem::path & | source, |
| const std::filesystem::path & | destination ) -> void |
Recursively mirror a directory tree using hard links for regular files. Directories are created, regular files are hard-linked. Both paths must reside on the same filesystem. The destination must not be inside the source tree, as that would cause infinite recursion.
| SOURCEMETA_CORE_IO_EXPORT auto sourcemeta::core::is_lexically_under_path | ( | const std::filesystem::path & | path, |
| const std::filesystem::path & | prefix ) -> bool |
Check whether a path lies under another path lexically, comparing component by component without resolving against the filesystem. For example:
| SOURCEMETA_CORE_IO_EXPORT auto sourcemeta::core::is_under_path | ( | const std::filesystem::path & | path, |
| const std::filesystem::path & | prefix ) -> bool |
Check whether a path lies under another path, comparing component by component after weak canonicalisation. For example:
| auto sourcemeta::core::read_file | ( | const std::filesystem::path & | path | ) | -> std::basic_ifstream< CharT, Traits > |
A convenience function to open a stream from a file in binary mode, so that its positions are byte offsets on every platform. For example:
| auto sourcemeta::core::read_file_to_string | ( | const std::filesystem::path & | path | ) | -> std::basic_string< CharT, Traits > |
Read an entire file into a string. For example:
|
inline |
Drain std::cin fully into a string. For example:
| auto sourcemeta::core::read_to_string | ( | std::basic_istream< CharT, Traits > & | stream | ) | -> std::basic_string< CharT, Traits > |
Drain an input stream into a string. For example:
| auto sourcemeta::core::resume_stream | ( | std::basic_istream< CharT, Traits > & | stream, |
| const std::streampos | start, | ||
| const std::streamsize | count ) -> void |
Position an input stream a given number of characters after a position it previously reported, leaving a stream that could not report one untouched. The stream must address its contents in bytes, as one opened in binary mode does. For example:
| SOURCEMETA_CORE_IO_EXPORT auto sourcemeta::core::strip_path_prefix | ( | const std::filesystem::path & | path, |
| const std::filesystem::path & | prefix ) -> std::filesystem::path |
Return the portion of a path that follows a given prefix, or the path unchanged if it does not lie under the prefix. For example:
| SOURCEMETA_CORE_IO_EXPORT auto sourcemeta::core::weakly_canonical | ( | const std::filesystem::path & | path | ) | -> std::filesystem::path |
A safe variant of std::filesystem::weakly_canonical that takes into account platform-specific oddities like FIFO on GNU/Linux, always resolving relative paths against the current working directory. For example:
| SOURCEMETA_CORE_IO_EXPORT auto sourcemeta::core::write_file | ( | const std::filesystem::path & | path, |
| const std::function< void(std::ostream &)> & | writer ) -> void |
Callback variant of write_file. For example:
| SOURCEMETA_CORE_IO_EXPORT auto sourcemeta::core::write_file | ( | const std::filesystem::path & | path, |
| const std::span< const std::byte > | contents ) -> void |
Non-atomically write a byte span to path. For example:
| SOURCEMETA_CORE_IO_EXPORT auto sourcemeta::core::write_file | ( | const std::filesystem::path & | path, |
| const std::string_view | contents ) -> void |
Non-atomically write contents to path. For example: