libcruft-util/io.hpp

148 lines
4.1 KiB
C++
Raw Normal View History

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
*
* Copyright 2010-2014 Danny Robson <danny@nerdcruft.net>
2011-05-23 17:18:52 +10:00
*/
#ifndef __UTIL_IO_HPP
#define __UTIL_IO_HPP
#include "platform.hpp"
#include "posix/fd.hpp"
2011-05-25 23:02:39 +10:00
#include <cstddef>
2011-05-23 17:18:52 +10:00
#include <cstdint>
#include <cstdio>
#include <experimental/filesystem>
2016-11-17 18:32:08 +11:00
#include <vector>
#include <streambuf>
2011-05-23 17:18:52 +10:00
#ifdef PLATFORM_WIN32
#include <windows.h>
2015-10-29 10:48:11 +11:00
#else
#define O_BINARY 0
#endif
2011-05-23 17:18:52 +10:00
namespace util {
2015-07-02 16:34:17 +10:00
//-------------------------------------------------------------------------
/// Reads an entire file into memory.
template <typename T = std::byte>
std::vector<T> slurp (const std::experimental::filesystem::path&);
template <typename T = std::byte, typename A, typename B>
std::vector<T> slurp (util::view<A,B> path)
{
const std::experimental::filesystem::path src { path.begin (), path.end () };
return slurp<T> (src);
}
template <typename T = std::byte>
std::vector<T> slurp (FILE *);
2015-07-02 16:34:17 +10:00
2015-10-29 17:52:19 +11:00
///////////////////////////////////////////////////////////////////////////
/// writes all data from the provided view into the file-like-object
///
/// writing will continually iterate until the entire buffer has been
/// dispatched so as to avoid issues with partial writes. will block until
/// such time as the entire buffer has written.
///
/// an exception may be thrown in the event forward progress is impossible.
/// in this event the progress may not be reported to the caller. in the
/// future an exception member variable may expose the information.
template <
typename DstT,
typename IteratorA,
typename IteratorB
>
2017-12-28 17:49:58 +11:00
util::view<IteratorA,IteratorB>
write (DstT &&dst, const util::view<IteratorA, IteratorB> &src)
{
2017-12-28 17:49:58 +11:00
auto remain = src;
while (!remain.empty ())
remain = remain.consume (dst.write (remain));
2017-12-28 17:49:58 +11:00
return src;
}
///////////////////////////////////////////////////////////////////////////
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
//-------------------------------------------------------------------------
// 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 {
explicit indented (const ValueT &_data):
data (_data)
{ ; }
2014-06-29 22:49:26 +10:00
const ValueT &data;
};
2014-06-29 22:49:26 +10:00
//-------------------------------------------------------------------------
template <typename ValueT>
std::ostream&
operator<< (std::ostream &os, const util::indented<ValueT> &value)
{
util::indenter scoped_indent (os);
return os << value.data;
}
2014-06-29 22:49:26 +10:00
//-------------------------------------------------------------------------
class scoped_cwd {
public:
scoped_cwd ();
~scoped_cwd ();
protected:
std::string m_original;
};
//-------------------------------------------------------------------------
2012-04-26 18:19:12 +10:00
class path_error : public std::runtime_error {
2016-02-12 13:36:03 +11:00
public:
2017-05-23 12:50:51 +10:00
explicit path_error (const std::experimental::filesystem::path &path);
2016-02-12 13:36:03 +11:00
const std::experimental::filesystem::path& path (void) const noexcept;
2016-02-12 13:36:03 +11:00
private:
const std::experimental::filesystem::path 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-05-23 17:18:52 +10:00
#ifdef PLATFORM_WIN32
#include "io_win32.hpp"
#else
#include "io_posix.hpp"
#endif
2011-05-23 17:18:52 +10:00
#endif