io: rename fd_ref to fd
This commit is contained in:
parent
4f366b6b88
commit
33b3b48c74
37
io.cpp
37
io.cpp
@ -55,14 +55,14 @@ access_to_cflags (access_t a) {
|
|||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
std::unique_ptr<char []>
|
std::unique_ptr<char []>
|
||||||
util::slurp (const boost::filesystem::path& path) {
|
util::slurp (const boost::filesystem::path& path) {
|
||||||
fd_ref fd(path, ACCESS_READ);
|
fd out (path, ACCESS_READ);
|
||||||
|
|
||||||
// Calculate the total file size
|
// Calculate the total file size
|
||||||
off_t size = lseek (fd, 0, SEEK_END);
|
off_t size = lseek (out, 0, SEEK_END);
|
||||||
if (size == (off_t)-1)
|
if (size == (off_t)-1)
|
||||||
throw errno_error();
|
throw errno_error();
|
||||||
|
|
||||||
if (lseek (fd, 0, SEEK_SET) == (off_t)-1)
|
if (lseek (out, 0, SEEK_SET) == (off_t)-1)
|
||||||
throw errno_error ();
|
throw errno_error ();
|
||||||
|
|
||||||
// Allocate a buffer, and keep reading until it's full. We provide a null
|
// Allocate a buffer, and keep reading until it's full. We provide a null
|
||||||
@ -75,7 +75,7 @@ util::slurp (const boost::filesystem::path& path) {
|
|||||||
char *cursor = buffer.get();
|
char *cursor = buffer.get();
|
||||||
|
|
||||||
while (remaining) {
|
while (remaining) {
|
||||||
ssize_t consumed = ::read (fd, cursor, remaining);
|
ssize_t consumed = ::read (out, cursor, remaining);
|
||||||
if (consumed == -1)
|
if (consumed == -1)
|
||||||
throw errno_error();
|
throw errno_error();
|
||||||
CHECK_GT ( consumed, 0);
|
CHECK_GT ( consumed, 0);
|
||||||
@ -95,12 +95,12 @@ util::write (const boost::filesystem::path &path, const T *data, size_t len) {
|
|||||||
CHECK_GT (len, 0);
|
CHECK_GT (len, 0);
|
||||||
CHECK (data);
|
CHECK (data);
|
||||||
|
|
||||||
fd_ref fd (path, ACCESS_WRITE);
|
fd out (path, ACCESS_WRITE);
|
||||||
const char *cursor = reinterpret_cast<const char*> (data);
|
const char *cursor = reinterpret_cast<const char*> (data);
|
||||||
size_t remaining = len * sizeof (T);
|
size_t remaining = len * sizeof (T);
|
||||||
|
|
||||||
while (remaining) {
|
while (remaining) {
|
||||||
ssize_t consumed = ::write (fd, cursor, remaining);
|
ssize_t consumed = ::write (out, cursor, remaining);
|
||||||
if (consumed < 0)
|
if (consumed < 0)
|
||||||
errno_error::throw_code ();
|
errno_error::throw_code ();
|
||||||
|
|
||||||
@ -115,39 +115,34 @@ template void util::write<uint8_t> (const boost::filesystem::path&, const uint8_
|
|||||||
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
fd_ref::fd_ref (int _fd):
|
fd::fd (int _fd):
|
||||||
fd (_fd)
|
m_fd (_fd)
|
||||||
{
|
{
|
||||||
if (fd < 0)
|
if (_fd < 0)
|
||||||
throw std::invalid_argument ("invalid descriptor");
|
throw std::invalid_argument ("invalid descriptor");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fd_ref::fd_ref (const boost::filesystem::path &path, access_t access):
|
fd::fd (const boost::filesystem::path &path, access_t access):
|
||||||
#ifdef PLATFORM_WIN32
|
#ifdef PLATFORM_WIN32
|
||||||
// Windows requires the use of 'string ()' to convert to char datatype
|
// Windows requires the use of 'string ()' to convert to char datatype
|
||||||
// rather than the typical wchar
|
// rather than the typical wchar
|
||||||
fd (open (path.string ().c_str (), access_to_cflags (access) | O_BINARY, 0660))
|
m_fd (open (path.string ().c_str (), access_to_cflags (access) | O_BINARY, 0660))
|
||||||
#else
|
#else
|
||||||
fd (open (path.native ().c_str (), access_to_cflags (access), 0660))
|
m_fd (open (path.native ().c_str (), access_to_cflags (access), 0660))
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
if (fd < 0)
|
if (m_fd < 0)
|
||||||
util::errno_error::throw_code ();
|
util::errno_error::throw_code ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fd_ref::~fd_ref () {
|
fd::~fd () {
|
||||||
CHECK (fd >= 0);
|
CHECK (m_fd >= 0);
|
||||||
close (fd);
|
close (m_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fd_ref::operator int (void) const
|
|
||||||
{ return fd; }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
int
|
int
|
||||||
indenter::overflow (int ch) {
|
indenter::overflow (int ch) {
|
||||||
|
16
io.hpp
16
io.hpp
@ -51,15 +51,17 @@ namespace util {
|
|||||||
|
|
||||||
///------------------------------------------------------------------------
|
///------------------------------------------------------------------------
|
||||||
/// A simple RAII wrapper for file descriptors
|
/// A simple RAII wrapper for file descriptors
|
||||||
struct fd_ref {
|
struct fd {
|
||||||
public:
|
public:
|
||||||
int fd;
|
explicit fd (int);
|
||||||
|
explicit fd (const boost::filesystem::path&, access_t);
|
||||||
|
|
||||||
explicit fd_ref (int _fd);
|
~fd ();
|
||||||
explicit fd_ref (const boost::filesystem::path&, access_t access);
|
|
||||||
~fd_ref ();
|
|
||||||
|
|
||||||
operator int (void) const;
|
operator int (void) const { return m_fd; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
int m_fd;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ namespace util {
|
|||||||
namespace detail { namespace posix {
|
namespace detail { namespace posix {
|
||||||
class mapped_file {
|
class mapped_file {
|
||||||
private:
|
private:
|
||||||
fd_ref m_fd;
|
fd m_fd;
|
||||||
uint8_t *m_data;
|
uint8_t *m_data;
|
||||||
size_t m_size;
|
size_t m_size;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user