2012-04-12 16:09:02 +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/.
|
2012-04-12 16:09:02 +10:00
|
|
|
*
|
2018-01-09 16:28:25 +11:00
|
|
|
* Copyright 2010-2018 Danny Robson <danny@nerdcruft.net>
|
2012-04-12 16:09:02 +10:00
|
|
|
*/
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
#include "io.hpp"
|
|
|
|
|
|
|
|
#include "debug.hpp"
|
2015-11-17 16:19:27 +11:00
|
|
|
#include "cast.hpp"
|
2018-01-09 16:28:25 +11:00
|
|
|
#include "format.hpp"
|
2017-12-18 15:46:52 +11:00
|
|
|
#include "posix/except.hpp"
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <fcntl.h>
|
2014-07-15 19:48:34 +10:00
|
|
|
#include <sys/stat.h>
|
2011-05-23 17:18:52 +10:00
|
|
|
#include <unistd.h>
|
2014-07-15 19:48:34 +10:00
|
|
|
|
2016-10-07 19:48:42 +11:00
|
|
|
#include <experimental/filesystem>
|
2014-07-15 19:48:34 +10:00
|
|
|
#include <stdexcept>
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using namespace cruft;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2014-07-29 02:20:02 +10:00
|
|
|
|
2017-09-12 14:18:34 +10:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
|
|
|
std::vector<T>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::slurp (const std::experimental::filesystem::path &path)
|
2016-10-02 15:50:13 +11:00
|
|
|
{
|
|
|
|
posix::fd out (path, O_RDONLY | O_BINARY);
|
2014-07-29 02:20:02 +10:00
|
|
|
|
2011-05-25 23:02:39 +10:00
|
|
|
// Calculate the total file size
|
2017-12-18 15:46:52 +11:00
|
|
|
off_t size = posix::error::try_value (lseek (out, 0, SEEK_END));
|
2018-05-03 21:21:45 +10:00
|
|
|
if (size % sizeof (T))
|
|
|
|
throw std::runtime_error ("filesize not correctly rounded");
|
2011-05-25 23:02:39 +10:00
|
|
|
|
2017-12-18 15:46:52 +11:00
|
|
|
posix::error::try_value (lseek (out, 0, SEEK_SET));
|
2011-05-25 23:02:39 +10:00
|
|
|
|
2016-03-17 18:06:45 +11:00
|
|
|
// Allocate a buffer, and keep reading until it's full.
|
2017-09-12 14:18:34 +10:00
|
|
|
std::vector<T> buffer (size);
|
2011-05-25 23:02:39 +10:00
|
|
|
|
2015-01-28 14:49:34 +11:00
|
|
|
CHECK_GE (size, 0);
|
2018-08-13 14:51:33 +10:00
|
|
|
unsigned remaining = cruft::cast::lossless<unsigned> (size);
|
2017-09-12 14:18:34 +10:00
|
|
|
T *cursor = buffer.data ();
|
2011-05-25 23:02:39 +10:00
|
|
|
|
|
|
|
while (remaining) {
|
2017-12-18 15:46:52 +11:00
|
|
|
ssize_t consumed = posix::error::try_value(
|
|
|
|
::read (out, cursor, remaining)
|
|
|
|
);
|
|
|
|
|
2018-05-03 18:32:08 +10:00
|
|
|
CHECK_GT (consumed, 0);
|
2018-08-05 14:42:02 +10:00
|
|
|
CHECK_LE (cruft::cast::sign<size_t> (consumed), remaining);
|
2011-05-25 23:02:39 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
remaining -= cruft::cast::sign<size_t> (consumed);
|
2011-05-25 23:02:39 +10:00
|
|
|
cursor += consumed;
|
|
|
|
}
|
|
|
|
|
2012-04-26 18:18:09 +10:00
|
|
|
return buffer;
|
2011-05-25 23:02:39 +10:00
|
|
|
}
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2015-07-02 16:34:17 +10:00
|
|
|
|
2015-10-29 17:52:19 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2018-08-05 14:42:02 +10:00
|
|
|
template std::vector<char> cruft::slurp (const std::experimental::filesystem::path&);
|
|
|
|
template std::vector<unsigned char> cruft::slurp (const std::experimental::filesystem::path&);
|
|
|
|
template std::vector<std::byte> cruft::slurp (const std::experimental::filesystem::path&);
|
|
|
|
template std::vector<uint32_t> cruft::slurp (const std::experimental::filesystem::path&);
|
2017-09-12 14:18:34 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
|
|
|
std::vector<T>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::slurp (FILE *stream)
|
2015-10-29 17:52:19 +11:00
|
|
|
{
|
2017-09-12 14:18:34 +10:00
|
|
|
static_assert (
|
|
|
|
sizeof (T) == 1,
|
|
|
|
"slurp is designed for grabbing bytes, not complex structures"
|
|
|
|
);
|
|
|
|
|
2016-03-17 18:07:08 +11:00
|
|
|
// find how much data is in this file
|
2018-08-05 14:42:02 +10:00
|
|
|
const int desc = cruft::posix::error::try_value (fileno (stream));
|
2015-10-29 17:52:19 +11:00
|
|
|
|
2016-03-17 18:07:08 +11:00
|
|
|
struct stat meta;
|
2017-12-18 15:46:52 +11:00
|
|
|
posix::error::try_value (fstat (desc, &meta));
|
2015-10-29 17:52:19 +11:00
|
|
|
|
2017-09-12 14:18:34 +10:00
|
|
|
std::vector<T> buf;
|
2016-03-17 18:07:08 +11:00
|
|
|
|
2016-04-19 14:52:05 +10:00
|
|
|
// we think we know the size, so try to do a simple read
|
|
|
|
if (meta.st_size) {
|
|
|
|
buf.resize (meta.st_size);
|
|
|
|
|
|
|
|
// read as much as possible, then resize to the actual length
|
|
|
|
auto res = fread (buf.data (), 1, meta.st_size, stream);
|
2016-03-17 18:07:08 +11:00
|
|
|
buf.resize (res);
|
2016-04-19 14:52:05 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// try reading small chunks until we've hit the end. important for
|
|
|
|
// handling pipe streams (like from popen) which report a zero size.
|
|
|
|
constexpr size_t CHUNK_SIZE = 128;
|
|
|
|
size_t cursor = buf.size ();
|
|
|
|
|
|
|
|
while (!feof (stream) && !ferror (stream)) {
|
|
|
|
auto oldsize = buf.size ();
|
|
|
|
buf.resize (oldsize + CHUNK_SIZE);
|
|
|
|
auto res = fread (buf.data () + cursor, 1, CHUNK_SIZE, stream);
|
|
|
|
|
|
|
|
if (res != CHUNK_SIZE)
|
|
|
|
buf.resize (oldsize + res);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ferror (stream))
|
|
|
|
throw stream_error ();
|
2016-03-17 18:07:08 +11:00
|
|
|
|
|
|
|
return buf;
|
2015-10-29 17:52:19 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-02 16:34:17 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2018-08-05 14:42:02 +10:00
|
|
|
template std::vector<char> cruft::slurp (FILE*);
|
|
|
|
template std::vector<std::byte> cruft::slurp (FILE*);
|
2017-09-12 14:18:34 +10:00
|
|
|
|
|
|
|
|
2015-10-29 10:48:11 +11:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2012-04-12 14:06:26 +10:00
|
|
|
int
|
|
|
|
indenter::overflow (int ch) {
|
|
|
|
if (m_line_start && ch != '\n')
|
2018-08-05 14:42:02 +10:00
|
|
|
m_dest->sputn (m_indent.data (), cruft::cast::sign<std::streamsize> (m_indent.size ()));
|
2012-04-12 14:06:26 +10:00
|
|
|
|
|
|
|
m_line_start = ch == '\n';
|
|
|
|
return m_dest->sputc (ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-17 18:07:45 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-04-12 14:06:26 +10:00
|
|
|
indenter::indenter (std::streambuf* _dest, size_t _indent)
|
|
|
|
: m_dest (_dest)
|
|
|
|
, m_line_start (true)
|
|
|
|
, m_indent (_indent, ' ')
|
|
|
|
, m_owner (NULL)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2016-03-17 18:07:45 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-04-12 14:06:26 +10:00
|
|
|
indenter::indenter (std::ostream& _dest, size_t _indent)
|
|
|
|
: m_dest (_dest.rdbuf())
|
|
|
|
, m_line_start (true)
|
|
|
|
, m_indent (_indent, ' ')
|
|
|
|
, m_owner (&_dest)
|
|
|
|
{
|
|
|
|
m_owner->rdbuf (this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-17 18:07:45 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-04-12 14:06:26 +10:00
|
|
|
indenter::~indenter ()
|
|
|
|
{
|
|
|
|
if (m_owner != NULL)
|
|
|
|
m_owner->rdbuf (m_dest);
|
|
|
|
}
|
|
|
|
|
2016-03-17 18:07:45 +11:00
|
|
|
|
2015-09-15 21:09:37 +10:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
scoped_cwd::scoped_cwd ()
|
|
|
|
{
|
|
|
|
m_original.resize (16);
|
2018-08-13 14:51:33 +10:00
|
|
|
while (getcwd (&m_original[0], cruft::cast::lossless<int> (m_original.size ())) == nullptr && errno == ERANGE)
|
2015-09-15 21:09:37 +10:00
|
|
|
m_original.resize (m_original.size () * 2);
|
2017-12-18 15:46:52 +11:00
|
|
|
posix::error::try_code ();
|
2012-04-26 18:19:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-15 21:09:37 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
scoped_cwd::~scoped_cwd ()
|
|
|
|
{
|
|
|
|
if (!chdir (m_original.c_str ()))
|
2017-12-18 15:46:52 +11:00
|
|
|
posix::error::throw_code ();
|
2012-04-26 18:19:37 +10:00
|
|
|
}
|
2016-02-12 13:36:03 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-10-07 19:48:42 +11:00
|
|
|
path_error::path_error (const std::experimental::filesystem::path &_path):
|
2018-01-09 16:28:25 +11:00
|
|
|
runtime_error (to_string (format::printf ("Unknown path: %!", m_path))),
|
2016-02-12 13:36:03 +11:00
|
|
|
m_path (_path)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-10-08 17:28:06 +11:00
|
|
|
const std::experimental::filesystem::path&
|
2016-02-12 13:36:03 +11:00
|
|
|
path_error::path (void) const noexcept
|
|
|
|
{
|
2016-10-08 17:28:06 +11:00
|
|
|
return m_path;
|
2016-02-12 13:36:03 +11:00
|
|
|
}
|