change const char paths to std::filesystem::path

This commit is contained in:
Danny Robson 2016-10-08 17:18:04 +11:00
parent 48364cd227
commit 598c5d4e48
13 changed files with 35 additions and 113 deletions

10
io.cpp
View File

@ -36,7 +36,7 @@ using namespace util;
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
std::vector<char> std::vector<char>
util::slurp (const char *path) util::slurp (const std::experimental::filesystem::path &path)
{ {
posix::fd out (path, O_RDONLY | O_BINARY); posix::fd out (path, O_RDONLY | O_BINARY);
@ -70,14 +70,6 @@ util::slurp (const char *path)
} }
//-----------------------------------------------------------------------------
std::vector<char>
util::slurp (const std::string &path)
{
return slurp (path.c_str ());
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
std::vector<char> std::vector<char>
util::slurp (FILE *stream) util::slurp (FILE *stream)

3
io.hpp
View File

@ -38,8 +38,7 @@
namespace util { namespace util {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
/// Reads an entire file into memory. /// Reads an entire file into memory.
std::vector<char> slurp (const char *path); std::vector<char> slurp (const std::experimental::filesystem::path&);
std::vector<char> slurp (const std::string &path);
std::vector<char> slurp (FILE *); std::vector<char> slurp (FILE *);

View File

@ -26,8 +26,9 @@
using util::detail::posix::mapped_file; using util::detail::posix::mapped_file;
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
mapped_file::mapped_file (const char *path, int fflags, int mflags): mapped_file::mapped_file (const std::experimental::filesystem::path &path, int fflags, int mflags):
mapped_file (util::posix::fd (path, fflags), mflags) mapped_file (util::posix::fd (path, fflags), mflags)
{ ; } { ; }

View File

@ -22,6 +22,7 @@
#include "view.hpp" #include "view.hpp"
#include <type_traits> #include <type_traits>
#include <experimental/filesystem>
#include <sys/mman.h> #include <sys/mman.h>
#include <fcntl.h> #include <fcntl.h>
@ -30,7 +31,7 @@ namespace util {
namespace detail { namespace posix { namespace detail { namespace posix {
class mapped_file { class mapped_file {
public: public:
mapped_file (const char *path, int fflags = O_RDONLY | O_BINARY, int mflags = PROT_READ); mapped_file (const std::experimental::filesystem::path&, int fflags = O_RDONLY | O_BINARY, int mflags = PROT_READ);
mapped_file (const util::posix::fd&, int mflags = PROT_READ); mapped_file (const util::posix::fd&, int mflags = PROT_READ);
mapped_file (const mapped_file&) = delete; mapped_file (const mapped_file&) = delete;

View File

@ -24,14 +24,8 @@ using util::detail::library_posix;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
library_posix::library_posix (const std::string &path): library_posix::library_posix (const std::experimental::filesystem::path &path):
library_posix (path.c_str ()) m_handle (dlopen (path.c_str (), RTLD_NOW))
{ ; }
//-----------------------------------------------------------------------------
library_posix::library_posix (const char *path):
m_handle (dlopen (path, RTLD_NOW))
{ {
if (!m_handle) if (!m_handle)
throw std::runtime_error (dlerror ()); throw std::runtime_error (dlerror ());

View File

@ -17,14 +17,13 @@
#ifndef __UTIL_LIBRARY_POSIX_HPP #ifndef __UTIL_LIBRARY_POSIX_HPP
#define __UTIL_LIBRARY_POSIX_HPP #define __UTIL_LIBRARY_POSIX_HPP
#include <string> #include <experimental/filesystem>
namespace util { namespace util {
namespace detail { namespace detail {
class library_posix { class library_posix {
public: public:
explicit library_posix (const char *path); explicit library_posix (const std::experimental::filesystem::path&);
explicit library_posix (const std::string &path);
library_posix (library_posix&&); library_posix (library_posix&&);
~library_posix (); ~library_posix ();

View File

@ -22,20 +22,14 @@ using util::detail::win32::library;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
library::library (const char *path): library::library (const std::experimental::filesystem::path &path)
m_handle (LoadLibraryA (path)) m_handle (LoadLibraryA (path.c_str ()))
{ {
if (!m_handle) if (!m_handle)
win32_error::throw_code (); win32_error::throw_code ();
} }
//-----------------------------------------------------------------------------
library::library (const std::string &path):
library (path.c_str ())
{ ; }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
library::~library () library::~library ()
{ {

View File

@ -17,15 +17,15 @@
#ifndef __UTIL_LIBRARY_WIN32_HPP #ifndef __UTIL_LIBRARY_WIN32_HPP
#define __UTIL_LIBRARY_WIN32_HPP #define __UTIL_LIBRARY_WIN32_HPP
#include <string>
#include <windows.h> #include <windows.h>
#include <experimental/filesystem>
namespace util { namespace util {
namespace detail { namespace win32 { namespace detail { namespace win32 {
class library { class library {
public: public:
library (const char *path); library (const std::experimenal::filesystem::path&);
library (const std::string &path);
~library (); ~library ();
void* symbol (const char *name); void* symbol (const char *name);

View File

@ -22,31 +22,8 @@ using util::posix::dir;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
//struct entry { dir::dir (const std::experimental::filesystem::path &p):
//public: m_handle (::opendir (p.c_str ()))
// entry (dirent *_handle, DIR *_parent): m_handle (_handle), m_parent (_parent) { ; }
//
// const char* name (void) const { return m_handle->d_name; }
//
// entry& operator++ (void) {
// dirent *next = readdir (m_parent);
// assert (!next || next != m_handle);
// m_handle = next;
// return *this;
// }
//
// entry& operator* (void) { return *this; }
// const entry& operator* (void) const;
// bool operator!= (entry rhs) const { return m_handle != rhs.m_handle || m_parent != rhs.m_parent; }
//
// struct dirent *m_handle;
// DIR *m_parent;
//};
///////////////////////////////////////////////////////////////////////////////
dir::dir (const char *path):
m_handle (opendir (path))
{ {
if (!m_handle) if (!m_handle)
errno_error::throw_code (); errno_error::throw_code ();

View File

@ -21,43 +21,23 @@
#include <dirent.h> #include <dirent.h>
#include <functional> #include <functional>
#include <experimental/filesystem>
namespace util { namespace posix { namespace util { namespace posix {
//struct entry {
//public:
// entry (dirent *_handle, DIR *_parent): m_handle (_handle), m_parent (_parent) { ; }
// const char* name (void) const { return m_handle->d_name; }
// entry& operator++ (void) {
// dirent *next = readdir (m_parent);
// assert (!next || next != m_handle);
// m_handle = next;
// return *this;
// }
// entry& operator* (void) { return *this; }
// const entry& operator* (void) const;
// bool operator!= (entry rhs) const { return m_handle != rhs.m_handle || m_parent != rhs.m_parent; }
// struct dirent *m_handle;
// DIR *m_parent;
//};
struct dir { struct dir {
public: public:
explicit dir (const char *path); explicit dir (const std::experimental::filesystem::path&);
~dir (); ~dir ();
operator DIR* (void); operator DIR* (void);
template <typename ...Args> template <typename ...Args>
void void
scan (std::function<void(const char*, Args&...)>, Args&...); scan (std::function<void(const std::experimental::filesystem::path&, Args&...)>, Args&...);
template <typename ...Args> template <typename ...Args>
void void
scan (void (*) (const char*, Args&...), Args&...); scan (void (*) (const std::experimental::filesystem::path&, Args&...), Args&...);
//entry begin (void) { rewind (); return { readdir (m_handle), m_handle }; } //entry begin (void) { rewind (); return { readdir (m_handle), m_handle }; }
//entry end (void) { return { nullptr, m_handle }; } //entry end (void) { return { nullptr, m_handle }; }

View File

@ -22,7 +22,7 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <typename ...Args> template <typename ...Args>
void void
util::posix::dir::scan(std::function<void(const char*, Args&...)> cb, Args &...args) util::posix::dir::scan(std::function<void(const std::experimental::filesystem::path&, Args&...)> cb, Args &...args)
{ {
rewind (); rewind ();
@ -36,7 +36,7 @@ util::posix::dir::scan(std::function<void(const char*, Args&...)> cb, Args &...a
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename ...Args> template <typename ...Args>
void void
util::posix::dir::scan (void (*cb) (const char*, Args&...), Args &...args) util::posix::dir::scan (void (*cb) (const std::experimental::filesystem::path&, Args&...), Args &...args)
{ {
rewind (); rewind ();

View File

@ -26,33 +26,18 @@ using util::posix::fd;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
fd::fd (const char *path, int flags): fd::fd (const std::experimental::filesystem::path &path, int flags):
m_fd (::open (path, flags, 0666)) fd (path, flags, 0666)
{
if (m_fd < 0)
errno_error::throw_code ();
}
//-----------------------------------------------------------------------------
fd::fd (const char *path, int flags, mode_t mode):
m_fd (::open (path, flags, mode))
{
if (m_fd < 0)
errno_error::throw_code ();
}
//-----------------------------------------------------------------------------
fd::fd (const std::string &path, int flags):
fd (path.c_str (), flags)
{ ; } { ; }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
fd::fd (const std::string &path, int flags, mode_t mode): fd::fd (const std::experimental::filesystem::path &path, int flags, mode_t mode):
fd (path.c_str (), flags, mode) m_fd (::open (path.c_str (), flags, mode))
{ ; } {
if (m_fd < 0)
errno_error::throw_code ();
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -22,16 +22,16 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <experimental/filesystem>
namespace util::posix { namespace util::posix {
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
/// A simple RAII wrapper for file descriptors /// A simple RAII wrapper for file descriptors
class fd { class fd {
public: public:
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
fd (const std::string &path, int flags); fd (const std::experimental::filesystem::path &path, int flags);
fd (const std::string &path, int flags, mode_t); fd (const std::experimental::filesystem::path &path, int flags, mode_t);
fd (const char *path, int flags);
fd (const char *path, int flags, mode_t);
fd (fd &&); fd (fd &&);