2012-04-12 16:09:02 +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
|
2012-04-12 16:09:02 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-04-12 16:09:02 +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.
|
2012-04-12 16:09:02 +10:00
|
|
|
*
|
2014-07-07 15:19:01 +10:00
|
|
|
* Copyright 2010-2014 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"
|
|
|
|
#include "except.hpp"
|
2012-05-08 15:04:31 +10:00
|
|
|
#include "platform.hpp"
|
2015-11-17 16:19:27 +11:00
|
|
|
#include "cast.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>
|
|
|
|
#include <sys/types.h>
|
2011-05-23 17:18:52 +10:00
|
|
|
#include <unistd.h>
|
2014-07-15 19:48:34 +10:00
|
|
|
|
2012-05-08 15:04:55 +10:00
|
|
|
#include <boost/filesystem.hpp>
|
2014-07-15 19:48:34 +10:00
|
|
|
#include <stdexcept>
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2011-10-01 01:58:10 +10:00
|
|
|
using namespace util;
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2014-07-29 02:20:02 +10:00
|
|
|
|
2011-05-25 23:02:39 +10:00
|
|
|
//----------------------------------------------------------------------------
|
2015-06-11 19:28:03 +10:00
|
|
|
std::vector<char>
|
2011-10-01 01:58:10 +10:00
|
|
|
util::slurp (const boost::filesystem::path& path) {
|
2015-10-29 10:48:11 +11:00
|
|
|
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
|
2015-06-11 19:27:42 +10:00
|
|
|
off_t size = lseek (out, 0, SEEK_END);
|
2011-05-25 23:02:39 +10:00
|
|
|
if (size == (off_t)-1)
|
|
|
|
throw errno_error();
|
|
|
|
|
2015-06-11 19:27:42 +10:00
|
|
|
if (lseek (out, 0, SEEK_SET) == (off_t)-1)
|
2011-05-25 23:02:39 +10:00
|
|
|
throw errno_error ();
|
|
|
|
|
2016-03-17 18:06:45 +11:00
|
|
|
// Allocate a buffer, and keep reading until it's full.
|
|
|
|
std::vector<char> buffer (size);
|
2011-05-25 23:02:39 +10:00
|
|
|
|
2015-01-28 14:49:34 +11:00
|
|
|
CHECK_GE (size, 0);
|
2011-05-25 23:02:39 +10:00
|
|
|
size_t remaining = (size_t)size;
|
2015-06-11 19:28:03 +10:00
|
|
|
char *cursor = buffer.data ();
|
2011-05-25 23:02:39 +10:00
|
|
|
|
|
|
|
while (remaining) {
|
2015-06-11 19:27:42 +10:00
|
|
|
ssize_t consumed = ::read (out, cursor, remaining);
|
2011-05-25 23:02:39 +10:00
|
|
|
if (consumed == -1)
|
|
|
|
throw errno_error();
|
2015-01-28 14:49:34 +11:00
|
|
|
CHECK_GT ( consumed, 0);
|
|
|
|
CHECK_LE ((size_t)consumed, remaining);
|
2011-05-25 23:02:39 +10:00
|
|
|
|
|
|
|
remaining -= (size_t)consumed;
|
|
|
|
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
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
std::vector<char>
|
|
|
|
util::slurp (FILE *stream)
|
|
|
|
{
|
2016-03-17 18:07:08 +11:00
|
|
|
// find how much data is in this file
|
|
|
|
const int desc = fileno (stream);
|
|
|
|
if (desc < 0)
|
|
|
|
errno_error::throw_code ();
|
2015-10-29 17:52:19 +11:00
|
|
|
|
2016-03-17 18:07:08 +11:00
|
|
|
struct stat meta;
|
|
|
|
if (fstat (desc, &meta) < 0)
|
2015-10-29 17:52:19 +11:00
|
|
|
errno_error::throw_code ();
|
|
|
|
|
2016-03-17 18:07:08 +11:00
|
|
|
// allocate a buffer we think is the correct size
|
|
|
|
std::vector<char> buf;
|
|
|
|
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);
|
|
|
|
if (!ferror (stream))
|
|
|
|
buf.resize (res);
|
|
|
|
|
2015-10-29 17:52:19 +11:00
|
|
|
CHECK (feof (stream));
|
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
|
|
|
//-----------------------------------------------------------------------------
|
2012-08-15 16:03:48 +10:00
|
|
|
void
|
2015-07-02 16:34:17 +10:00
|
|
|
util::write (const fd &out,
|
2015-10-29 10:48:54 +11:00
|
|
|
const void *restrict data,
|
|
|
|
size_t bytes)
|
2015-07-02 16:34:17 +10:00
|
|
|
{
|
2015-10-29 10:48:54 +11:00
|
|
|
const char *restrict cursor = reinterpret_cast<const char*> (data);
|
|
|
|
size_t remaining = bytes;
|
2012-08-15 16:03:48 +10:00
|
|
|
|
|
|
|
while (remaining) {
|
2015-06-11 19:27:42 +10:00
|
|
|
ssize_t consumed = ::write (out, cursor, remaining);
|
2012-08-15 16:03:48 +10:00
|
|
|
if (consumed < 0)
|
|
|
|
errno_error::throw_code ();
|
|
|
|
|
|
|
|
remaining -= sign_cast<size_t> (consumed);
|
|
|
|
cursor += sign_cast<size_t> (consumed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 19:40:32 +11:00
|
|
|
|
2015-10-29 10:48:11 +11:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2015-06-11 19:27:42 +10:00
|
|
|
fd::fd (int _fd):
|
|
|
|
m_fd (_fd)
|
2011-05-23 17:18:52 +10:00
|
|
|
{
|
2015-06-11 19:27:42 +10:00
|
|
|
if (_fd < 0)
|
2014-07-15 19:48:34 +10:00
|
|
|
throw std::invalid_argument ("invalid descriptor");
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-29 10:48:11 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
fd::fd (const char *path, int flags, mode_t mode):
|
|
|
|
m_fd (open (path, flags, mode))
|
2012-04-26 18:19:12 +10:00
|
|
|
{
|
2015-06-11 19:27:42 +10:00
|
|
|
if (m_fd < 0)
|
2015-10-29 10:48:11 +11:00
|
|
|
errno_error::throw_code ();
|
2012-04-26 18:19:12 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-29 10:48:11 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
fd::fd (const boost::filesystem::path &path, int flags):
|
|
|
|
fd (path.string ().c_str (), flags)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-06-11 19:27:42 +10:00
|
|
|
fd::~fd () {
|
|
|
|
CHECK (m_fd >= 0);
|
|
|
|
close (m_fd);
|
2011-05-23 17:18:52 +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')
|
|
|
|
m_dest->sputn (m_indent.data (), sign_cast<std::streamsize> (m_indent.size ()));
|
|
|
|
|
|
|
|
m_line_start = ch == '\n';
|
|
|
|
return m_dest->sputc (ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
indenter::indenter (std::streambuf* _dest, size_t _indent)
|
|
|
|
: m_dest (_dest)
|
|
|
|
, m_line_start (true)
|
|
|
|
, m_indent (_indent, ' ')
|
|
|
|
, m_owner (NULL)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
indenter::~indenter ()
|
|
|
|
{
|
|
|
|
if (m_owner != NULL)
|
|
|
|
m_owner->rdbuf (m_dest);
|
|
|
|
}
|
|
|
|
|
2015-09-15 21:09:37 +10:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
scoped_cwd::scoped_cwd ()
|
|
|
|
{
|
|
|
|
m_original.resize (16);
|
|
|
|
while (getcwd (&m_original[0], m_original.size ()) == nullptr && errno == ERANGE)
|
|
|
|
m_original.resize (m_original.size () * 2);
|
|
|
|
errno_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 ()))
|
|
|
|
errno_error::throw_code ();
|
2012-04-26 18:19:37 +10:00
|
|
|
}
|
2016-02-12 13:36:03 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
path_error::path_error (const boost::filesystem::path &_path):
|
|
|
|
runtime_error (format::render ("Unknown path: %!", m_path)),
|
|
|
|
m_path (_path)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
const char*
|
|
|
|
path_error::path (void) const noexcept
|
|
|
|
{
|
|
|
|
return m_path.c_str ();
|
|
|
|
}
|