2011-05-23 17:18:52 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
2014-07-07 15:19:01 +10:00
|
|
|
* Copyright 2010-2014 Danny Robson <danny@nerdcruft.net>
|
2011-05-23 17:18:52 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_IO_HPP
|
|
|
|
#define __UTIL_IO_HPP
|
|
|
|
|
2018-12-19 17:13:06 +11:00
|
|
|
#include "std.hpp"
|
2014-12-19 18:28:50 +11:00
|
|
|
#include "platform.hpp"
|
2016-10-02 15:50:13 +11:00
|
|
|
#include "posix/fd.hpp"
|
2011-05-25 23:02:39 +10:00
|
|
|
|
2017-09-12 14:18:34 +10:00
|
|
|
#include <cstddef>
|
2011-05-23 17:18:52 +10:00
|
|
|
#include <cstdint>
|
2017-09-12 14:18:34 +10:00
|
|
|
#include <cstdio>
|
2018-12-05 19:12:03 +11:00
|
|
|
#include <filesystem>
|
2016-11-17 18:32:08 +11:00
|
|
|
#include <vector>
|
|
|
|
#include <streambuf>
|
2018-12-17 12:42:50 +11:00
|
|
|
#include <functional>
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2014-12-19 18:28:50 +11:00
|
|
|
#ifdef PLATFORM_WIN32
|
2018-08-27 14:16:27 +10:00
|
|
|
#include "win32/windows.hpp"
|
2015-10-29 10:48:11 +11:00
|
|
|
#else
|
|
|
|
#define O_BINARY 0
|
2014-12-19 18:28:50 +11:00
|
|
|
#endif
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft {
|
2018-10-18 12:51:56 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Reads an entire file into memory in one operation.
|
|
|
|
template <typename T = std::byte>
|
|
|
|
std::vector<T>
|
2018-12-05 19:12:03 +11:00
|
|
|
slurp (std::filesystem::path const&);
|
2018-10-18 12:51:56 +11:00
|
|
|
|
|
|
|
|
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Read the entire contents of a FILE object into memory.
|
|
|
|
///
|
|
|
|
/// * This may trigger an arbitrary number of read calls.
|
|
|
|
/// * This may block indefinitely depending on the contents of the fd.
|
2017-09-12 14:18:34 +10:00
|
|
|
template <typename T = std::byte>
|
2018-10-18 12:51:56 +11:00
|
|
|
std::vector<T>
|
|
|
|
slurp (FILE *);
|
2017-09-12 14:18:34 +10:00
|
|
|
|
2018-01-30 11:32:38 +11:00
|
|
|
|
2018-10-18 12:51:56 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Read the entire contents of a file-descriptor into memory in one
|
|
|
|
/// operation.
|
|
|
|
///
|
|
|
|
/// * This may trigger an arbitrary number of read calls.
|
|
|
|
/// * This may block indefinitely depending on the contents of the fd.
|
2017-09-12 14:18:34 +10:00
|
|
|
template <typename T = std::byte>
|
2018-10-18 12:51:56 +11:00
|
|
|
std::vector<T>
|
|
|
|
slurp (cruft::posix::fd&);
|
2015-07-02 16:34:17 +10:00
|
|
|
|
2015-10-29 17:52:19 +11:00
|
|
|
|
2017-12-26 17:24:42 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-12-17 12:42:50 +11:00
|
|
|
/// Repeatedly calls FunctionT to transfer data from a supplied view to a
|
|
|
|
/// file-like-object.
|
|
|
|
///
|
|
|
|
/// FunctionT must return a view of the consumed data.
|
2017-12-26 17:24:42 +11:00
|
|
|
///
|
2018-12-17 12:42:50 +11:00
|
|
|
/// The operation will iterate until the entire view has been consumed and
|
|
|
|
/// hence may block for an indefinate period.
|
|
|
|
///
|
|
|
|
/// An exception may be throw in the event forward progress is impossible
|
|
|
|
/// (depending on the supplied FunctionT).
|
2018-12-19 17:13:06 +11:00
|
|
|
///
|
|
|
|
/// TODO: enforce constness of view iterators
|
2018-12-17 12:42:50 +11:00
|
|
|
template <
|
|
|
|
typename DstT,
|
|
|
|
typename FunctionT,
|
2018-12-19 17:13:06 +11:00
|
|
|
typename ValueT,
|
2018-12-17 12:42:50 +11:00
|
|
|
typename ...Args
|
|
|
|
>
|
|
|
|
[[nodiscard]] decltype(auto)
|
|
|
|
drain (
|
|
|
|
DstT &&dst,
|
|
|
|
FunctionT &&func,
|
2018-12-19 17:13:06 +11:00
|
|
|
cruft::view<ValueT*> const &src,
|
2018-12-17 12:42:50 +11:00
|
|
|
Args&& ...args
|
|
|
|
) {
|
2018-12-19 17:13:06 +11:00
|
|
|
auto cursor = reinterpret_cast<u08 const*> (src.data ());
|
|
|
|
auto remain = src.size () * sizeof (ValueT);
|
2018-12-17 12:42:50 +11:00
|
|
|
|
|
|
|
while (remain) {
|
|
|
|
auto count = std::invoke (func, dst, cursor, remain, args...);
|
|
|
|
cursor += count;
|
|
|
|
remain -= count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Drains all data from the supplied view `src` to the file-like-object
|
|
|
|
/// `dst` by way of repeated calls to `write`.
|
2017-12-26 17:24:42 +11:00
|
|
|
///
|
2018-12-17 12:42:50 +11:00
|
|
|
/// Returns a view over the consumed data. This SHOULD always be identical
|
|
|
|
/// to `src`.
|
2018-01-10 18:44:23 +11:00
|
|
|
template <
|
|
|
|
typename DstT,
|
2018-12-19 17:13:06 +11:00
|
|
|
typename ValueT
|
2018-01-10 18:44:23 +11:00
|
|
|
>
|
2018-12-17 12:42:50 +11:00
|
|
|
decltype(auto)
|
2018-12-19 17:13:06 +11:00
|
|
|
write (DstT &&dst, cruft::view<ValueT*> const &src)
|
2017-12-26 17:24:42 +11:00
|
|
|
{
|
2018-12-17 12:42:50 +11:00
|
|
|
constexpr auto func = &std::decay_t<DstT>::write;
|
|
|
|
return drain (std::forward<DstT> (dst), func, src);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Drain the supplied data view into the destination object by calling
|
|
|
|
/// pwrite repeatedly.
|
|
|
|
///
|
|
|
|
/// Returns a view over the consumed data. This SHOULD always be identical
|
|
|
|
/// to `src`.
|
|
|
|
template <
|
|
|
|
typename DstT,
|
2018-12-19 17:13:06 +11:00
|
|
|
typename ValueT
|
2018-12-17 12:42:50 +11:00
|
|
|
>
|
|
|
|
decltype(auto)
|
2018-12-19 17:13:06 +11:00
|
|
|
pwrite (DstT &&dst, cruft::view<ValueT*> const &src, size_t offset)
|
2018-12-17 12:42:50 +11:00
|
|
|
{
|
|
|
|
return drain (std::forward<DstT> (dst), &std::decay_t<DstT>::pwrite, src, offset);
|
2017-12-26 17:24:42 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-19 12:00:47 +10:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Read the number of bytes covered by the destination view from the
|
|
|
|
/// source descriptor.
|
|
|
|
template <typename SrcT>
|
|
|
|
decltype (auto)
|
|
|
|
read (SrcT &&src, cruft::view<u08*> dst)
|
|
|
|
{
|
|
|
|
auto remain = dst;
|
|
|
|
|
|
|
|
while (!remain.empty ()) {
|
|
|
|
auto const count = src.read (remain.begin (), remain.size () * sizeof (decltype(dst)::value_type));
|
|
|
|
remain = remain.consume (count);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-26 17:24:42 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2012-11-09 15:16:07 +11:00
|
|
|
class indenter : public std::streambuf {
|
2012-04-12 14:06:26 +10:00
|
|
|
protected:
|
|
|
|
std::streambuf* m_dest;
|
|
|
|
bool m_line_start;
|
|
|
|
std::string m_indent;
|
|
|
|
std::ostream* m_owner;
|
|
|
|
|
|
|
|
protected:
|
2015-06-30 22:02:54 +10:00
|
|
|
virtual int overflow (int ch) override;
|
2012-04-12 14:06:26 +10:00
|
|
|
|
|
|
|
public:
|
|
|
|
explicit indenter (std::streambuf* _dest, size_t _indent = 4);
|
|
|
|
explicit indenter (std::ostream& _dest, size_t _indent = 4);
|
|
|
|
|
|
|
|
virtual ~indenter ();
|
|
|
|
};
|
|
|
|
|
2014-06-29 22:49:26 +10:00
|
|
|
//-------------------------------------------------------------------------
|
2018-02-28 11:49:13 +11:00
|
|
|
// a wrapper type that implicitly indents a single value when passed to an
|
|
|
|
// ostream operator.
|
|
|
|
template <typename ValueT>
|
2014-06-29 22:49:26 +10:00
|
|
|
struct indented {
|
2018-02-28 11:49:13 +11:00
|
|
|
explicit indented (const ValueT &_data):
|
|
|
|
data (_data)
|
|
|
|
{ ; }
|
2014-06-29 22:49:26 +10:00
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
const ValueT &data;
|
|
|
|
};
|
2014-06-29 22:49:26 +10:00
|
|
|
|
2014-08-01 20:43:51 +10:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2018-02-28 11:49:13 +11:00
|
|
|
template <typename ValueT>
|
2014-08-01 20:43:51 +10:00
|
|
|
std::ostream&
|
2018-08-05 14:42:02 +10:00
|
|
|
operator<< (std::ostream &os, const cruft::indented<ValueT> &value)
|
2018-02-28 11:49:13 +11:00
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::indenter scoped_indent (os);
|
2018-02-28 11:49:13 +11:00
|
|
|
return os << value.data;
|
|
|
|
|
|
|
|
}
|
2014-08-01 20:43:51 +10:00
|
|
|
|
|
|
|
|
2014-06-29 22:49:26 +10:00
|
|
|
//-------------------------------------------------------------------------
|
2012-04-26 18:19:37 +10:00
|
|
|
class scoped_cwd {
|
|
|
|
public:
|
|
|
|
scoped_cwd ();
|
|
|
|
~scoped_cwd ();
|
|
|
|
|
|
|
|
protected:
|
2015-09-15 21:09:37 +10:00
|
|
|
std::string m_original;
|
2012-04-26 18:19:37 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-12-19 18:28:50 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2012-04-26 18:19:12 +10:00
|
|
|
class path_error : public std::runtime_error {
|
2016-02-12 13:36:03 +11:00
|
|
|
public:
|
2018-12-05 19:12:03 +11:00
|
|
|
explicit path_error (std::filesystem::path const &path);
|
2016-02-12 13:36:03 +11:00
|
|
|
|
2018-12-05 19:12:03 +11:00
|
|
|
std::filesystem::path const& path (void) const noexcept;
|
2016-02-12 13:36:03 +11:00
|
|
|
|
|
|
|
private:
|
2018-12-05 19:12:03 +11:00
|
|
|
std::filesystem::path const m_path;
|
2012-04-26 18:19:12 +10:00
|
|
|
};
|
2016-04-19 14:52:05 +10:00
|
|
|
|
|
|
|
class stream_error : public std::exception {
|
|
|
|
public:
|
|
|
|
virtual const char* what (void) const noexcept
|
|
|
|
{ return "error on C stream"; }
|
|
|
|
};
|
2011-10-01 01:58:10 +10:00
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2014-12-19 18:28:50 +11:00
|
|
|
#ifdef PLATFORM_WIN32
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "io_win32.hpp"
|
2014-12-19 18:28:50 +11:00
|
|
|
#else
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "io_posix.hpp"
|
2014-12-19 18:28:50 +11:00
|
|
|
#endif
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
#endif
|