1#ifndef SOURCEMETA_CORE_IO_H_
2#define SOURCEMETA_CORE_IO_H_
4#ifndef SOURCEMETA_CORE_IO_EXPORT
5#include <sourcemeta/core/io_export.h>
9#include <sourcemeta/core/io_atomic.h>
10#include <sourcemeta/core/io_binary.h>
11#include <sourcemeta/core/io_bytestream.h>
12#include <sourcemeta/core/io_error.h>
13#include <sourcemeta/core/io_fileview.h>
14#include <sourcemeta/core/io_temporary.h>
30#include <system_error>
41namespace sourcemeta::core {
55SOURCEMETA_CORE_IO_EXPORT
56auto canonical(
const std::filesystem::path &path) -> std::filesystem::path;
71SOURCEMETA_CORE_IO_EXPORT
73 -> std::filesystem::path;
86SOURCEMETA_CORE_IO_EXPORT
88 const std::filesystem::path &prefix) -> bool;
101SOURCEMETA_CORE_IO_EXPORT
103 const std::filesystem::path &prefix) -> bool;
117SOURCEMETA_CORE_IO_EXPORT
119 const std::filesystem::path &prefix)
120 -> std::filesystem::path;
134template <
typename CharT =
char,
typename Traits = std::
char_traits<CharT>>
136 -> std::basic_ifstream<CharT, Traits> {
137 if (std::filesystem::is_directory(path)) {
145 std::basic_ifstream<CharT, Traits> stream{canonical_path, std::ios::binary};
146 if (!stream.is_open()) {
150 stream.exceptions(std::basic_ifstream<CharT, Traits>::badbit);
167template <
typename CharT =
char,
typename Traits = std::
char_traits<CharT>>
169 -> std::basic_string<CharT, Traits> {
170 const auto start{stream.tellg()};
171 if (start !=
static_cast<std::streampos
>(-1)) {
172 stream.seekg(0, std::ios::end);
173 const auto end{stream.tellg()};
176 std::basic_string<CharT, Traits> result;
177 result.resize(
static_cast<std::size_t
>(end - start));
178 stream.read(result.data(),
static_cast<std::streamsize
>(result.size()));
181 result.resize(
static_cast<std::size_t
>(stream.gcount()));
186 std::basic_ostringstream<CharT, Traits> buffer;
187 buffer << stream.rdbuf();
208template <
typename CharT =
char,
typename Traits = std::
char_traits<CharT>>
210 const std::streampos start,
const std::streamsize count)
212 if (start ==
static_cast<std::streampos
>(-1)) {
217 stream.seekg(start +
static_cast<std::streamoff
>(count));
231template <
typename CharT =
char,
typename Traits = std::
char_traits<CharT>>
233 -> std::basic_string<CharT, Traits> {
234 if (std::filesystem::is_directory(path)) {
242 std::basic_ifstream<CharT, Traits> stream{canonical_path, std::ios::binary};
243 if (!stream.is_open()) {
247 stream.exceptions(std::basic_ifstream<CharT, Traits>::badbit);
252 std::error_code file_size_error;
253 const auto size{std::filesystem::file_size(canonical_path, file_size_error)};
254 if (file_size_error) {
260 if constexpr (std::numeric_limits<
decltype(size)>::max() >
261 std::numeric_limits<std::size_t>::max()) {
262 if (size > std::numeric_limits<std::size_t>::max()) {
267 std::basic_string<CharT, Traits> result;
268 result.resize(
static_cast<std::size_t
>(size));
269 stream.read(result.data(),
static_cast<std::streamsize
>(result.size()));
272 result.resize(
static_cast<std::size_t
>(stream.gcount()));
305template <
typename Callback>
308 while (std::getline(stream, line)) {
309 std::string_view view{line};
310 if (!view.empty() && view.back() ==
'\r') {
311 view.remove_suffix(1);
330SOURCEMETA_CORE_IO_EXPORT
332 const std::filesystem::path &destination) -> void;
343SOURCEMETA_CORE_IO_EXPORT
345 const std::string_view contents) -> void;
359SOURCEMETA_CORE_IO_EXPORT
361 const std::span<const std::byte> contents) -> void;
377SOURCEMETA_CORE_IO_EXPORT
379 const std::function<
void(std::ostream &)> &writer) -> void;
391SOURCEMETA_CORE_IO_EXPORT
392auto flush(
const std::filesystem::path &path) -> void;
SOURCEMETA_CORE_IO_EXPORT auto weakly_canonical(const std::filesystem::path &path) -> std::filesystem::path
SOURCEMETA_CORE_IO_EXPORT auto is_under_path(const std::filesystem::path &path, const std::filesystem::path &prefix) -> bool
auto read_file(const std::filesystem::path &path) -> std::basic_ifstream< CharT, Traits >
Definition io.h:135
auto resume_stream(std::basic_istream< CharT, Traits > &stream, const std::streampos start, const std::streamsize count) -> void
Definition io.h:209
auto read_to_string(std::basic_istream< CharT, Traits > &stream) -> std::basic_string< CharT, Traits >
Definition io.h:168
auto for_each_line(std::istream &stream, Callback callback) -> void
Definition io.h:306
auto read_file_to_string(const std::filesystem::path &path) -> std::basic_string< CharT, Traits >
Definition io.h:232
SOURCEMETA_CORE_IO_EXPORT auto is_lexically_under_path(const std::filesystem::path &path, const std::filesystem::path &prefix) -> bool
SOURCEMETA_CORE_IO_EXPORT auto canonical(const std::filesystem::path &path) -> std::filesystem::path
SOURCEMETA_CORE_IO_EXPORT auto flush(const std::filesystem::path &path) -> void
SOURCEMETA_CORE_IO_EXPORT auto strip_path_prefix(const std::filesystem::path &path, const std::filesystem::path &prefix) -> std::filesystem::path
SOURCEMETA_CORE_IO_EXPORT auto hardlink_directory(const std::filesystem::path &source, const std::filesystem::path &destination) -> void
auto read_stdin() -> std::string
Definition io.h:285
SOURCEMETA_CORE_IO_EXPORT auto write_file(const std::filesystem::path &path, const std::string_view contents) -> void