From 598c5d4e48e0e3ad857e15e9fcbc62977079962f Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Sat, 8 Oct 2016 17:18:04 +1100 Subject: [PATCH] change const char paths to std::filesystem::path --- io.cpp | 10 +--------- io.hpp | 3 +-- io_posix.cpp | 3 ++- io_posix.hpp | 3 ++- library_posix.cpp | 10 ++-------- library_posix.hpp | 5 ++--- library_win32.cpp | 10 ++-------- library_win32.hpp | 6 +++--- posix/dir.cpp | 27 ++------------------------- posix/dir.hpp | 28 ++++------------------------ posix/dir.ipp | 4 ++-- posix/fd.cpp | 31 ++++++++----------------------- posix/fd.hpp | 8 ++++---- 13 files changed, 35 insertions(+), 113 deletions(-) diff --git a/io.cpp b/io.cpp index 3e1ff35d..a48d9d3e 100644 --- a/io.cpp +++ b/io.cpp @@ -36,7 +36,7 @@ using namespace util; //---------------------------------------------------------------------------- std::vector -util::slurp (const char *path) +util::slurp (const std::experimental::filesystem::path &path) { posix::fd out (path, O_RDONLY | O_BINARY); @@ -70,14 +70,6 @@ util::slurp (const char *path) } -//----------------------------------------------------------------------------- -std::vector -util::slurp (const std::string &path) -{ - return slurp (path.c_str ()); -} - - //----------------------------------------------------------------------------- std::vector util::slurp (FILE *stream) diff --git a/io.hpp b/io.hpp index 83f74726..ff25d6c4 100644 --- a/io.hpp +++ b/io.hpp @@ -38,8 +38,7 @@ namespace util { //------------------------------------------------------------------------- /// Reads an entire file into memory. - std::vector slurp (const char *path); - std::vector slurp (const std::string &path); + std::vector slurp (const std::experimental::filesystem::path&); std::vector slurp (FILE *); diff --git a/io_posix.cpp b/io_posix.cpp index f2163624..da0e017c 100644 --- a/io_posix.cpp +++ b/io_posix.cpp @@ -26,8 +26,9 @@ 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) { ; } diff --git a/io_posix.hpp b/io_posix.hpp index 03ea3eff..2d493959 100644 --- a/io_posix.hpp +++ b/io_posix.hpp @@ -22,6 +22,7 @@ #include "view.hpp" #include +#include #include #include @@ -30,7 +31,7 @@ namespace util { namespace detail { namespace posix { class mapped_file { 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 mapped_file&) = delete; diff --git a/library_posix.cpp b/library_posix.cpp index 2a1ae1ad..7980a1da 100644 --- a/library_posix.cpp +++ b/library_posix.cpp @@ -24,14 +24,8 @@ using util::detail::library_posix; /////////////////////////////////////////////////////////////////////////////// -library_posix::library_posix (const std::string &path): - library_posix (path.c_str ()) -{ ; } - - -//----------------------------------------------------------------------------- -library_posix::library_posix (const char *path): - m_handle (dlopen (path, RTLD_NOW)) +library_posix::library_posix (const std::experimental::filesystem::path &path): + m_handle (dlopen (path.c_str (), RTLD_NOW)) { if (!m_handle) throw std::runtime_error (dlerror ()); diff --git a/library_posix.hpp b/library_posix.hpp index 3a4e2134..1980be01 100644 --- a/library_posix.hpp +++ b/library_posix.hpp @@ -17,14 +17,13 @@ #ifndef __UTIL_LIBRARY_POSIX_HPP #define __UTIL_LIBRARY_POSIX_HPP -#include +#include namespace util { namespace detail { class library_posix { public: - explicit library_posix (const char *path); - explicit library_posix (const std::string &path); + explicit library_posix (const std::experimental::filesystem::path&); library_posix (library_posix&&); ~library_posix (); diff --git a/library_win32.cpp b/library_win32.cpp index 5fb0c29a..2ef1c03d 100644 --- a/library_win32.cpp +++ b/library_win32.cpp @@ -22,20 +22,14 @@ using util::detail::win32::library; /////////////////////////////////////////////////////////////////////////////// -library::library (const char *path): - m_handle (LoadLibraryA (path)) +library::library (const std::experimental::filesystem::path &path) + m_handle (LoadLibraryA (path.c_str ())) { if (!m_handle) win32_error::throw_code (); } -//----------------------------------------------------------------------------- -library::library (const std::string &path): - library (path.c_str ()) -{ ; } - - //----------------------------------------------------------------------------- library::~library () { diff --git a/library_win32.hpp b/library_win32.hpp index a3062fe0..dd744caa 100644 --- a/library_win32.hpp +++ b/library_win32.hpp @@ -17,15 +17,15 @@ #ifndef __UTIL_LIBRARY_WIN32_HPP #define __UTIL_LIBRARY_WIN32_HPP -#include #include +#include + namespace util { namespace detail { namespace win32 { class library { public: - library (const char *path); - library (const std::string &path); + library (const std::experimenal::filesystem::path&); ~library (); void* symbol (const char *name); diff --git a/posix/dir.cpp b/posix/dir.cpp index dcc11de9..2c48ad6c 100644 --- a/posix/dir.cpp +++ b/posix/dir.cpp @@ -22,31 +22,8 @@ using util::posix::dir; /////////////////////////////////////////////////////////////////////////////// -//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; -//}; - - -/////////////////////////////////////////////////////////////////////////////// -dir::dir (const char *path): - m_handle (opendir (path)) +dir::dir (const std::experimental::filesystem::path &p): + m_handle (::opendir (p.c_str ())) { if (!m_handle) errno_error::throw_code (); diff --git a/posix/dir.hpp b/posix/dir.hpp index 4415124e..1f448aab 100644 --- a/posix/dir.hpp +++ b/posix/dir.hpp @@ -21,43 +21,23 @@ #include #include +#include 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 { public: - explicit dir (const char *path); + explicit dir (const std::experimental::filesystem::path&); ~dir (); operator DIR* (void); template void - scan (std::function, Args&...); + scan (std::function, Args&...); template 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 end (void) { return { nullptr, m_handle }; } diff --git a/posix/dir.ipp b/posix/dir.ipp index a1ea6567..80508cfd 100644 --- a/posix/dir.ipp +++ b/posix/dir.ipp @@ -22,7 +22,7 @@ /////////////////////////////////////////////////////////////////////////////// template void -util::posix::dir::scan(std::function cb, Args &...args) +util::posix::dir::scan(std::function cb, Args &...args) { rewind (); @@ -36,7 +36,7 @@ util::posix::dir::scan(std::function cb, Args &...a //----------------------------------------------------------------------------- template 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 (); diff --git a/posix/fd.cpp b/posix/fd.cpp index 8dd716d0..0a8d6214 100644 --- a/posix/fd.cpp +++ b/posix/fd.cpp @@ -26,33 +26,18 @@ using util::posix::fd; /////////////////////////////////////////////////////////////////////////////// -fd::fd (const char *path, int flags): - m_fd (::open (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::experimental::filesystem::path &path, int flags): + fd (path, flags, 0666) { ; } //----------------------------------------------------------------------------- -fd::fd (const std::string &path, int flags, mode_t mode): - fd (path.c_str (), flags, mode) -{ ; } +fd::fd (const std::experimental::filesystem::path &path, int flags, mode_t mode): + m_fd (::open (path.c_str (), flags, mode)) +{ + if (m_fd < 0) + errno_error::throw_code (); +} /////////////////////////////////////////////////////////////////////////////// diff --git a/posix/fd.hpp b/posix/fd.hpp index b983b939..33bf1815 100644 --- a/posix/fd.hpp +++ b/posix/fd.hpp @@ -22,16 +22,16 @@ #include #include +#include + namespace util::posix { ///------------------------------------------------------------------------ /// A simple RAII wrapper for file descriptors class fd { public: /////////////////////////////////////////////////////////////////////// - fd (const std::string &path, int flags); - fd (const std::string &path, int flags, mode_t); - fd (const char *path, int flags); - fd (const char *path, int flags, mode_t); + fd (const std::experimental::filesystem::path &path, int flags); + fd (const std::experimental::filesystem::path &path, int flags, mode_t); fd (fd &&);