2011-05-23 17:18:52 +10:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
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
|
|
|
|
|
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
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdint>
|
2016-10-07 19:48:42 +11:00
|
|
|
#include <experimental/filesystem>
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2014-12-19 18:28:50 +11:00
|
|
|
#ifdef PLATFORM_WIN32
|
|
|
|
#include <windows.h>
|
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
|
|
|
|
2011-10-01 01:58:10 +10:00
|
|
|
namespace util {
|
2015-07-02 16:34:17 +10:00
|
|
|
//-------------------------------------------------------------------------
|
2016-03-17 18:06:45 +11:00
|
|
|
/// Reads an entire file into memory.
|
2016-10-08 17:18:04 +11:00
|
|
|
std::vector<char> slurp (const std::experimental::filesystem::path&);
|
2015-10-29 17:52:19 +11:00
|
|
|
std::vector<char> slurp (FILE *);
|
2015-07-02 16:34:17 +10:00
|
|
|
|
2015-10-29 17:52:19 +11:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2016-10-02 15:50:13 +11:00
|
|
|
void write (const posix::fd&, const void *restrict data, size_t bytes);
|
2015-07-02 16:34:17 +10:00
|
|
|
|
2015-10-20 21:04:14 +11:00
|
|
|
template <typename T>
|
2016-10-02 15:50:13 +11:00
|
|
|
void write (const posix::fd&, const T &data);
|
2015-10-20 21:04:14 +11:00
|
|
|
|
2015-07-02 16:34:17 +10:00
|
|
|
template <typename T>
|
2016-10-02 15:50:13 +11:00
|
|
|
void write (const posix::fd&, const T *restrict first, const T *restrict last);
|
2015-07-02 16:34:17 +10:00
|
|
|
|
2014-06-29 22:49:26 +10: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
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
struct indented {
|
|
|
|
indented (const T &_data);
|
|
|
|
const T &data;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-08-01 20:43:51 +10:00
|
|
|
//-------------------------------------------------------------------------
|
2014-06-29 22:49:26 +10:00
|
|
|
template <typename T>
|
|
|
|
indented<T>
|
|
|
|
make_indented (const T &_data);
|
|
|
|
|
2014-08-01 20:43:51 +10:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
std::ostream&
|
|
|
|
operator<< (std::ostream &os, const util::indented<T> &v);
|
|
|
|
|
|
|
|
|
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:
|
2016-10-07 19:48:42 +11:00
|
|
|
path_error (const std::experimental::filesystem::path &path);
|
2016-02-12 13:36:03 +11:00
|
|
|
|
2016-10-08 17:28:06 +11:00
|
|
|
const std::experimental::filesystem::path& path (void) const noexcept;
|
2016-02-12 13:36:03 +11:00
|
|
|
|
|
|
|
private:
|
2016-10-07 19:48:42 +11:00
|
|
|
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-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
|
2015-10-29 10:48:11 +11:00
|
|
|
#include "./io_win32.hpp"
|
2014-12-19 18:28:50 +11:00
|
|
|
#else
|
2015-10-29 10:48:11 +11:00
|
|
|
#include "./io_posix.hpp"
|
2014-12-19 18:28:50 +11:00
|
|
|
#endif
|
|
|
|
|
2015-10-29 10:48:11 +11:00
|
|
|
#include "./io.ipp"
|
2014-06-29 22:49:26 +10:00
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
#endif
|