io: remove access_flag abstraction

This commit is contained in:
Danny Robson 2015-10-29 10:48:11 +11:00
parent 1de33ce53c
commit 73720feceb
4 changed files with 34 additions and 87 deletions

62
io.cpp
View File

@ -34,26 +34,11 @@
using namespace std; using namespace std;
using namespace util; using namespace util;
//----------------------------------------------------------------------------
static int
access_to_cflags (access_t a) {
int flags = 0;
if ((a & ACCESS_READWRITE) == ACCESS_READWRITE) {
flags = O_RDWR | O_CREAT;
} else if (a & ACCESS_READ) {
flags = O_RDONLY;
} else if (a & ACCESS_WRITE) {
flags = O_WRONLY | O_CREAT;
}
return flags;
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
std::vector<char> std::vector<char>
util::slurp (const boost::filesystem::path& path) { util::slurp (const boost::filesystem::path& path) {
fd out (path, ACCESS_READ); fd out (path, O_RDONLY | O_BINARY);
// Calculate the total file size // Calculate the total file size
off_t size = lseek (out, 0, SEEK_END); off_t size = lseek (out, 0, SEEK_END);
@ -112,28 +97,7 @@ util::write (const fd &out,
} }
template void util::write (const fd&, const char*, const char*); //////////////////////////////////////////////////////////////////////////////
template void util::write (const fd&, const int8_t*, const int8_t*);
template void util::write (const fd&, const uint8_t*, const uint8_t*);
//-----------------------------------------------------------------------------
template <typename T>
void
util::write (const boost::filesystem::path &path,
const T *restrict first,
const T *restrict last)
{
write (fd (path, ACCESS_WRITE), first, last);
}
template void util::write (const boost::filesystem::path&, const char*, const char*);
template void util::write (const boost::filesystem::path&, const int8_t*, const int8_t*);
template void util::write (const boost::filesystem::path&, const uint8_t*, const uint8_t*);
//----------------------------------------------------------------------------
fd::fd (int _fd): fd::fd (int _fd):
m_fd (_fd) m_fd (_fd)
{ {
@ -142,27 +106,29 @@ fd::fd (int _fd):
} }
fd::fd (const boost::filesystem::path &path, access_t access): //-----------------------------------------------------------------------------
#ifdef PLATFORM_WIN32 fd::fd (const char *path, int flags, mode_t mode):
// Windows requires the use of 'string ()' to convert to char datatype m_fd (open (path, flags, mode))
// rather than the typical wchar
m_fd (open (path.string ().c_str (), access_to_cflags (access) | O_BINARY, 0660))
#else
m_fd (open (path.native ().c_str (), access_to_cflags (access), 0660))
#endif
{ {
if (m_fd < 0) if (m_fd < 0)
util::errno_error::throw_code (); errno_error::throw_code ();
} }
//-----------------------------------------------------------------------------
fd::fd (const boost::filesystem::path &path, int flags):
fd (path.string ().c_str (), flags)
{ ; }
//-----------------------------------------------------------------------------
fd::~fd () { fd::~fd () {
CHECK (m_fd >= 0); CHECK (m_fd >= 0);
close (m_fd); close (m_fd);
} }
//---------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////
int int
indenter::overflow (int ch) { indenter::overflow (int ch) {
if (m_line_start && ch != '\n') if (m_line_start && ch != '\n')

19
io.hpp
View File

@ -29,23 +29,18 @@
#ifdef PLATFORM_WIN32 #ifdef PLATFORM_WIN32
#include <windows.h> #include <windows.h>
#else
#define O_BINARY 0
#endif #endif
namespace util { namespace util {
/// Specifies bitwise combinations of IO access rights.
enum access_t {
ACCESS_READ = 1 << 0,
ACCESS_WRITE = 1 << 1,
ACCESS_READWRITE = ACCESS_READ | ACCESS_WRITE
};
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
/// A simple RAII wrapper for file descriptors /// A simple RAII wrapper for file descriptors
struct fd { struct fd {
public: public:
explicit fd (int); explicit fd (int);
explicit fd (const boost::filesystem::path&, access_t); fd (const char *path, int flags, mode_t mode = 0660);
fd (const boost::filesystem::path&, int flags);
~fd (); ~fd ();
@ -151,11 +146,11 @@ namespace util {
} }
#ifdef PLATFORM_WIN32 #ifdef PLATFORM_WIN32
#include "io_win32.hpp" #include "./io_win32.hpp"
#else #else
#include "io_posix.hpp" #include "./io_posix.hpp"
#endif #endif
#include "io.ipp" #include "./io.ipp"
#endif #endif

View File

@ -20,16 +20,17 @@
#include "except.hpp" #include "except.hpp"
#include <fcntl.h> #include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
using util::detail::posix::mapped_file; using util::detail::posix::mapped_file;
//---------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////
mapped_file::mapped_file (const boost::filesystem::path &_path, access_t _access): mapped_file::mapped_file (const char *_path, int fflags, int mflags):
m_fd (_path, _access) m_fd (_path, fflags)
{ load_fd (_access); } {
load_fd (mflags);
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -39,30 +40,15 @@ mapped_file::~mapped_file () {
} }
//----------------------------------------------------------------------------
int
mapped_file::access_to_flags (access_t a) {
int flags = 0;
if (a & ACCESS_READ)
flags |= PROT_READ;
if (a & ACCESS_WRITE)
flags |= PROT_WRITE;
return flags;
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
void void
mapped_file::load_fd (access_t access) { mapped_file::load_fd (int mflags) {
struct stat meta; struct stat meta;
if (fstat (m_fd, &meta) < 0) if (fstat (m_fd, &meta) < 0)
throw errno_error (); throw errno_error ();
m_size = (size_t)meta.st_size; m_size = (size_t)meta.st_size;
m_data = (uint8_t *)mmap (NULL, m_size, access_to_flags (access), MAP_SHARED, m_fd, 0); m_data = (uint8_t *)mmap (NULL, m_size, mflags, MAP_SHARED, m_fd, 0);
if (m_data == MAP_FAILED) if (m_data == MAP_FAILED)
throw errno_error (); throw errno_error ();
} }

View File

@ -19,6 +19,9 @@
#include "io.hpp" #include "io.hpp"
#include <sys/mman.h>
#include <fcntl.h>
namespace util { namespace util {
namespace detail { namespace posix { namespace detail { namespace posix {
class mapped_file { class mapped_file {
@ -27,13 +30,10 @@ namespace util {
uint8_t *m_data; uint8_t *m_data;
size_t m_size; size_t m_size;
void load_fd (access_t); void load_fd (int mflags);
protected:
int access_to_flags (access_t);
public: public:
mapped_file (const boost::filesystem::path &path, access_t access = ACCESS_READ); mapped_file (const char *path, int fflags = O_RDONLY | O_BINARY, int mflags = PROT_READ);
mapped_file (const mapped_file&) = delete; mapped_file (const mapped_file&) = delete;
mapped_file& operator= (const mapped_file&) = delete; mapped_file& operator= (const mapped_file&) = delete;