2012-04-12 16:09:02 +10:00
|
|
|
/*
|
|
|
|
* This file is part of libgim.
|
|
|
|
*
|
|
|
|
* libgim is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
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"
|
2012-05-25 15:19:07 +10:00
|
|
|
#include "types/casts.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
|
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
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
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
static int
|
|
|
|
access_to_cflags (access_t a) {
|
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
if ((a & ACCESS_READWRITE) == ACCESS_READWRITE) {
|
2014-12-30 18:37:04 +11:00
|
|
|
flags = O_RDWR | O_CREAT;
|
2014-07-29 02:20:02 +10:00
|
|
|
} else if (a & ACCESS_READ) {
|
|
|
|
flags = O_RDONLY;
|
|
|
|
} else if (a & ACCESS_WRITE) {
|
2014-12-30 18:37:04 +11:00
|
|
|
flags = O_WRONLY | O_CREAT;
|
2014-07-29 02:20:02 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
a = static_cast<access_t> (a & ~ACCESS_READWRITE);
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2011-05-25 23:02:39 +10:00
|
|
|
//----------------------------------------------------------------------------
|
2012-04-26 18:18:09 +10:00
|
|
|
std::unique_ptr<char []>
|
2011-10-01 01:58:10 +10:00
|
|
|
util::slurp (const boost::filesystem::path& path) {
|
2014-07-29 02:20:02 +10:00
|
|
|
fd_ref fd(path, ACCESS_READ);
|
|
|
|
|
2011-05-25 23:02:39 +10:00
|
|
|
// Calculate the total file size
|
|
|
|
off_t size = lseek (fd, 0, SEEK_END);
|
|
|
|
if (size == (off_t)-1)
|
|
|
|
throw errno_error();
|
|
|
|
|
|
|
|
if (lseek (fd, 0, SEEK_SET) == (off_t)-1)
|
|
|
|
throw errno_error ();
|
|
|
|
|
|
|
|
// Allocate a buffer, and keep reading until it's full. We provide a null
|
|
|
|
// padding at the tail as a 'just in case' measure for string manipulation.
|
2012-04-26 18:18:09 +10:00
|
|
|
unique_ptr <char []> buffer (new char[size + 1]);
|
2011-05-25 23:02:39 +10:00
|
|
|
buffer.get()[size] = '\0';
|
|
|
|
|
2012-05-11 12:34:21 +10:00
|
|
|
CHECK_HARD (size >= 0);
|
2011-05-25 23:02:39 +10:00
|
|
|
size_t remaining = (size_t)size;
|
2012-04-26 18:18:09 +10:00
|
|
|
char *cursor = buffer.get();
|
2011-05-25 23:02:39 +10:00
|
|
|
|
|
|
|
while (remaining) {
|
2012-08-15 16:03:48 +10:00
|
|
|
ssize_t consumed = ::read (fd, cursor, remaining);
|
2011-05-25 23:02:39 +10:00
|
|
|
if (consumed == -1)
|
|
|
|
throw errno_error();
|
2012-05-11 12:34:21 +10:00
|
|
|
CHECK_HARD ( consumed > 0);
|
|
|
|
CHECK_HARD ((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
|
|
|
|
2012-08-15 16:03:48 +10:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
util::write (const boost::filesystem::path &path, const char *data, size_t len) {
|
|
|
|
CHECK_SOFT (len > 0);
|
|
|
|
CHECK_HARD (data);
|
|
|
|
|
2014-07-29 02:20:02 +10:00
|
|
|
fd_ref fd (path, ACCESS_WRITE);
|
2012-08-15 16:03:48 +10:00
|
|
|
const char *cursor = data;
|
|
|
|
size_t remaining = len;
|
|
|
|
|
|
|
|
while (remaining) {
|
|
|
|
ssize_t consumed = ::write (fd, cursor, remaining);
|
|
|
|
if (consumed < 0)
|
|
|
|
errno_error::throw_code ();
|
|
|
|
|
|
|
|
remaining -= sign_cast<size_t> (consumed);
|
|
|
|
cursor += sign_cast<size_t> (consumed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-25 23:02:39 +10:00
|
|
|
//----------------------------------------------------------------------------
|
2011-05-23 17:18:52 +10:00
|
|
|
fd_ref::fd_ref (int _fd):
|
2011-05-25 23:02:39 +10:00
|
|
|
fd (_fd)
|
2011-05-23 17:18:52 +10:00
|
|
|
{
|
2014-07-15 19:48:34 +10:00
|
|
|
if (fd < 0)
|
|
|
|
throw std::invalid_argument ("invalid descriptor");
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-29 02:20:02 +10:00
|
|
|
fd_ref::fd_ref (const boost::filesystem::path &path, access_t access):
|
2012-05-08 15:04:31 +10:00
|
|
|
#ifdef PLATFORM_WIN32
|
2014-12-05 13:20:44 +11:00
|
|
|
// Windows requires the use of 'string ()' to convert to char datatype
|
|
|
|
// rather than the typical wchar
|
|
|
|
fd (open (path.string ().c_str (), access_to_cflags (access) | O_BINARY))
|
2012-05-08 15:04:31 +10:00
|
|
|
#else
|
2014-07-29 02:20:02 +10:00
|
|
|
fd (open (path.native ().c_str (), access_to_cflags (access)))
|
2012-05-08 15:04:31 +10:00
|
|
|
#endif
|
2012-04-26 18:19:12 +10:00
|
|
|
{
|
|
|
|
if (fd < 0)
|
2014-07-15 19:48:34 +10:00
|
|
|
util::errno_error::throw_code ();
|
2012-04-26 18:19:12 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
fd_ref::~fd_ref () {
|
2012-05-11 12:34:21 +10:00
|
|
|
CHECK (fd >= 0);
|
2011-05-25 23:02:39 +10:00
|
|
|
close (fd);
|
2011-05-23 17:18:52 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fd_ref::operator int (void) const
|
2011-05-25 23:02:39 +10:00
|
|
|
{ return fd; }
|
2011-05-23 17:18:52 +10: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);
|
|
|
|
}
|
|
|
|
|
2012-04-26 18:19:37 +10:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
scoped_cwd::scoped_cwd ():
|
2012-05-08 15:04:55 +10:00
|
|
|
m_original(boost::filesystem::canonical (getcwd (nullptr, 0)))
|
|
|
|
{ ; }
|
2012-04-26 18:19:37 +10:00
|
|
|
|
|
|
|
|
|
|
|
scoped_cwd::~scoped_cwd () {
|
2012-05-08 15:04:55 +10:00
|
|
|
set_cwd (m_original);
|
2012-04-26 18:19:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
util::set_cwd (const boost::filesystem::path &path) {
|
2012-05-11 12:34:21 +10:00
|
|
|
CHECK (path.string ().size () > 0);
|
2012-05-08 15:04:55 +10:00
|
|
|
|
|
|
|
if (chdir (path.string ().c_str ()) != 0)
|
|
|
|
throw errno_error ();
|
2012-04-26 18:19:37 +10:00
|
|
|
}
|