Some changes to make the forwarding/implementation way work on MSVC.

This commit is contained in:
Steffen Schümann 2019-03-24 12:46:19 +01:00
parent 243963558e
commit e8b2bee859
4 changed files with 90 additions and 85 deletions

View File

@ -65,33 +65,38 @@
#if defined(GHC_FILESYSTEM_IMPLEMENTATION) #if defined(GHC_FILESYSTEM_IMPLEMENTATION)
#define GHC_EXPAND_IMPL #define GHC_EXPAND_IMPL
#define GHC_INLINE #define GHC_INLINE
# ifdef GHC_OS_WINDOWS #ifdef GHC_OS_WINDOWS
# define GHC_FS_API __declspec(dllexport) #define GHC_FS_API
# else #define GHC_FS_API_CLASS
# define GHC_FS_API __attribute__((visibility("default"))) #else
# endif #define GHC_FS_API __attribute__((visibility("default")))
#define GHC_FS_API_CLASS __attribute__((visibility("default")))
#endif
#elif defined(GHC_FILESYSTEM_FWD) #elif defined(GHC_FILESYSTEM_FWD)
#define GHC_INLINE #define GHC_INLINE
# ifdef GHC_OS_WINDOWS #ifdef GHC_OS_WINDOWS
# define GHC_FS_API __declspec(dllimport) #define GHC_FS_API extern
# else #define GHC_FS_API_CLASS
# define GHC_FS_API #else
# endif #define GHC_FS_API extern
#define GHC_FS_API_CLASS
#endif
#else #else
#define GHC_EXPAND_IMPL #define GHC_EXPAND_IMPL
#define GHC_INLINE inline #define GHC_INLINE inline
#define GHC_FS_API #define GHC_FS_API
#define GHC_FS_API_CLASS
#endif #endif
#ifdef GHC_EXPAND_IMPL #ifdef GHC_EXPAND_IMPL
#ifdef GHC_OS_WINDOWS #ifdef GHC_OS_WINDOWS
#include <windows.h>
#include <shellapi.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <wchar.h> #include <wchar.h>
#include <windows.h>
#include <winioctl.h> #include <winioctl.h>
#include <shellapi.h>
#else #else
#include <dirent.h> #include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
@ -147,7 +152,7 @@ namespace ghc {
namespace filesystem { namespace filesystem {
// temporary existing exception type for yet unimplemented parts // temporary existing exception type for yet unimplemented parts
class GHC_FS_API not_implemented_exception : public std::logic_error class GHC_FS_API_CLASS not_implemented_exception : public std::logic_error
{ {
public: public:
not_implemented_exception() not_implemented_exception()
@ -157,7 +162,7 @@ public:
}; };
// 30.10.8 class path // 30.10.8 class path
class GHC_FS_API path class GHC_FS_API_CLASS path
{ {
public: public:
using value_type = std::string::value_type; using value_type = std::string::value_type;
@ -369,7 +374,7 @@ template <class InputIterator>
path u8path(InputIterator first, InputIterator last); path u8path(InputIterator first, InputIterator last);
// 30.10.9 class filesystem_error // 30.10.9 class filesystem_error
class GHC_FS_API filesystem_error : public std::system_error class GHC_FS_API_CLASS filesystem_error : public std::system_error
{ {
public: public:
filesystem_error(const std::string& what_arg, std::error_code ec); filesystem_error(const std::string& what_arg, std::error_code ec);
@ -385,7 +390,7 @@ private:
path _p1, _p2; path _p1, _p2;
}; };
class GHC_FS_API path::iterator class GHC_FS_API_CLASS path::iterator
{ {
public: public:
using value_type = const path; using value_type = const path;
@ -495,7 +500,7 @@ enum class directory_options : uint16_t {
}; };
// 30.10.11 class file_status // 30.10.11 class file_status
class GHC_FS_API file_status class GHC_FS_API_CLASS file_status
{ {
public: public:
// 30.10.11.1 constructors and destructor // 30.10.11.1 constructors and destructor
@ -522,7 +527,7 @@ private:
using file_time_type = std::chrono::time_point<std::chrono::system_clock>; using file_time_type = std::chrono::time_point<std::chrono::system_clock>;
// 30.10.12 Class directory_entry // 30.10.12 Class directory_entry
class GHC_FS_API directory_entry class GHC_FS_API_CLASS directory_entry
{ {
public: public:
// 30.10.12.1 constructors and destructor // 30.10.12.1 constructors and destructor
@ -598,10 +603,10 @@ private:
}; };
// 30.10.13 Class directory_iterator // 30.10.13 Class directory_iterator
class GHC_FS_API directory_iterator class GHC_FS_API_CLASS directory_iterator
{ {
public: public:
class GHC_FS_API proxy class GHC_FS_API_CLASS proxy
{ {
public: public:
const directory_entry& operator*() const& noexcept { return _dir_entry; } const directory_entry& operator*() const& noexcept { return _dir_entry; }
@ -660,7 +665,7 @@ GHC_FS_API directory_iterator begin(directory_iterator iter) noexcept;
GHC_FS_API directory_iterator end(const directory_iterator&) noexcept; GHC_FS_API directory_iterator end(const directory_iterator&) noexcept;
// 30.10.14 class recursive_directory_iterator // 30.10.14 class recursive_directory_iterator
class GHC_FS_API recursive_directory_iterator class GHC_FS_API_CLASS recursive_directory_iterator
{ {
public: public:
using iterator_category = std::input_iterator_tag; using iterator_category = std::input_iterator_tag;
@ -947,7 +952,7 @@ typedef basic_ofstream<wchar_t> wofstream;
typedef basic_fstream<char> fstream; typedef basic_fstream<char> fstream;
typedef basic_fstream<wchar_t> wfstream; typedef basic_fstream<wchar_t> wfstream;
class GHC_FS_API u8arguments class GHC_FS_API_CLASS u8arguments
{ {
public: public:
u8arguments(int& argc, char**& argv); u8arguments(int& argc, char**& argv);
@ -957,10 +962,7 @@ public:
_refargv = _argv; _refargv = _argv;
} }
bool valid() const bool valid() const { return _isvalid; }
{
return _isvalid;
}
private: private:
int _argc; int _argc;
@ -979,23 +981,23 @@ private:
//------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------
namespace detail { namespace detail {
GHC_FS_API void postprocess_path_with_format(path::string_type& p, path::format fmt); GHC_FS_API void postprocess_path_with_format(path::string_type& p, path::format fmt);
enum utf8_states_t { S_STRT = 0, S_RJCT = 8 }; enum utf8_states_t { S_STRT = 0, S_RJCT = 8 };
GHC_FS_API void appendUTF8(std::string& str, uint32_t unicode); GHC_FS_API void appendUTF8(std::string& str, uint32_t unicode);
GHC_FS_API bool is_surrogate(uint32_t c); GHC_FS_API bool is_surrogate(uint32_t c);
GHC_FS_API bool is_high_surrogate(uint32_t c); GHC_FS_API bool is_high_surrogate(uint32_t c);
GHC_FS_API bool is_low_surrogate(uint32_t c); GHC_FS_API bool is_low_surrogate(uint32_t c);
GHC_FS_API unsigned consumeUtf8Fragment(const unsigned state, const uint8_t fragment, uint32_t& codepoint); GHC_FS_API unsigned consumeUtf8Fragment(const unsigned state, const uint8_t fragment, uint32_t& codepoint);
enum class portable_error { enum class portable_error {
none = 0, none = 0,
exists, exists,
not_found, not_found,
not_supported, not_supported,
not_implemented, not_implemented,
invalid_argument, invalid_argument,
}; };
GHC_FS_API std::error_code make_error_code(portable_error err); GHC_FS_API std::error_code make_error_code(portable_error err);
} } // namespace detail
namespace detail { namespace detail {
@ -1152,7 +1154,7 @@ GHC_INLINE unsigned consumeUtf8Fragment(const unsigned state, const uint8_t frag
return state == S_RJCT ? static_cast<unsigned>(S_RJCT) : static_cast<unsigned>((utf8_state_info[category + 16] >> (state << 2)) & 0xf); return state == S_RJCT ? static_cast<unsigned>(S_RJCT) : static_cast<unsigned>((utf8_state_info[category + 16] >> (state << 2)) & 0xf);
} }
} // detail } // namespace detail
#endif #endif
@ -1238,7 +1240,7 @@ inline std::string toUtf8(const SourceType* unicodeString)
return toUtf8(std::basic_string<SourceType, std::char_traits<SourceType>>(unicodeString)); return toUtf8(std::basic_string<SourceType, std::char_traits<SourceType>>(unicodeString));
} }
} // detail } // namespace detail
#ifdef GHC_EXPAND_IMPL #ifdef GHC_EXPAND_IMPL
@ -1345,7 +1347,7 @@ GHC_INLINE std::string systemErrorText(ErrorNumber code = 0)
std::string msg = toUtf8(std::wstring((LPWSTR)msgBuf)); std::string msg = toUtf8(std::wstring((LPWSTR)msgBuf));
LocalFree(msgBuf); LocalFree(msgBuf);
return msg; return msg;
#elif defined(GHC_OS_MACOS) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)) || (defined(GHC_OS_ANDROID) && __ANDROID_API__<23) #elif defined(GHC_OS_MACOS) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)) || (defined(GHC_OS_ANDROID) && __ANDROID_API__ < 23)
char buffer[512]; char buffer[512];
int rc = strerror_r(code ? code : errno, buffer, sizeof(buffer)); int rc = strerror_r(code ? code : errno, buffer, sizeof(buffer));
return rc == 0 ? (const char*)buffer : "Error in strerror_r!"; return rc == 0 ? (const char*)buffer : "Error in strerror_r!";
@ -1637,7 +1639,9 @@ GHC_INLINE file_status symlink_status_ex(const path& p, std::error_code& ec, uin
} }
return ec ? file_status(file_type::none) : fs; return ec ? file_status(file_type::none) : fs;
#else #else
(void)sz; (void)nhl; (void)lwt; (void)sz;
(void)nhl;
(void)lwt;
struct ::stat fs; struct ::stat fs;
auto result = ::lstat(p.c_str(), &fs); auto result = ::lstat(p.c_str(), &fs);
if (result == 0) { if (result == 0) {
@ -2914,10 +2918,10 @@ GHC_INLINE void copy(const path& from, const path& to, copy_options options, std
} }
} }
for (auto iter = directory_iterator(from, ec); iter != directory_iterator(); iter.increment(ec)) { for (auto iter = directory_iterator(from, ec); iter != directory_iterator(); iter.increment(ec)) {
if(!ec) { if (!ec) {
copy(iter->path(), to / iter->path().filename(), options | static_cast<copy_options>(0x8000), ec); copy(iter->path(), to / iter->path().filename(), options | static_cast<copy_options>(0x8000), ec);
} }
if(ec) { if (ec) {
return; return;
} }
} }
@ -3792,9 +3796,9 @@ GHC_INLINE uintmax_t remove_all(const path& p, std::error_code& ec) noexcept
} }
std::error_code tec; std::error_code tec;
auto fs = status(p, tec); auto fs = status(p, tec);
if(exists(fs) && is_directory(fs)) { if (exists(fs) && is_directory(fs)) {
for (auto iter = directory_iterator(p, ec); iter != directory_iterator(); iter.increment(ec)) { for (auto iter = directory_iterator(p, ec); iter != directory_iterator(); iter.increment(ec)) {
if(ec) { if (ec) {
break; break;
} }
if (!iter->is_symlink() && iter->is_directory()) { if (!iter->is_symlink() && iter->is_directory()) {
@ -3812,8 +3816,8 @@ GHC_INLINE uintmax_t remove_all(const path& p, std::error_code& ec) noexcept
} }
} }
} }
if(!ec) { if (!ec) {
if(remove(p, ec)) { if (remove(p, ec)) {
++count; ++count;
} }
} }

View File

@ -42,3 +42,6 @@ target_link_libraries(multifile_test ghc_filesystem)
add_executable(fwd_impl_test fwd_test.cpp impl_test.cpp) add_executable(fwd_impl_test fwd_test.cpp impl_test.cpp)
target_link_libraries(fwd_impl_test ghc_filesystem) target_link_libraries(fwd_impl_test ghc_filesystem)
if(CMAKE_CXX_COMPILER_ID MATCHES MSVC)
target_compile_definitions(fwd_impl_test PRIVATE _CRT_SECURE_NO_WARNINGS)
endif()

View File

@ -34,12 +34,15 @@
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <functional> #include <functional>
#include <iostream>
#include <iomanip> #include <iomanip>
#include <iostream>
#include <random> #include <random>
#include <sstream> #include <sstream>
#include <thread> #include <thread>
#ifndef WIN32 #ifdef WIN32
#define NOMINMAX
#include <windows.h>
#else
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
@ -53,7 +56,7 @@ using namespace std::filesystem;
using ifstream = std::ifstream; using ifstream = std::ifstream;
using ofstream = std::ofstream; using ofstream = std::ofstream;
using fstream = std::fstream; using fstream = std::fstream;
} } // namespace fs
#ifdef __GNUC__ #ifdef __GNUC__
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#endif #endif
@ -75,7 +78,7 @@ using namespace ghc::filesystem;
using ifstream = ghc::filesystem::ifstream; using ifstream = ghc::filesystem::ifstream;
using ofstream = ghc::filesystem::ofstream; using ofstream = ghc::filesystem::ofstream;
using fstream = ghc::filesystem::fstream; using fstream = ghc::filesystem::fstream;
} } // namespace fs
#endif #endif
#ifndef GHC_FILESYSTEM_FWD_TEST #ifndef GHC_FILESYSTEM_FWD_TEST
@ -86,7 +89,7 @@ using fstream = ghc::filesystem::fstream;
//#define TEST_LWG_2935_BEHAVIOUR //#define TEST_LWG_2935_BEHAVIOUR
#define TEST_LWG_2937_BEHAVIOUR #define TEST_LWG_2937_BEHAVIOUR
template<typename TP> template <typename TP>
std::time_t to_time_t(TP tp) std::time_t to_time_t(TP tp)
{ {
// Based on trick from: Nico Josuttis, C++17 - The Complete Guide // Based on trick from: Nico Josuttis, C++17 - The Complete Guide
@ -110,7 +113,8 @@ struct StringMaker<fs::perms>
template <> template <>
struct StringMaker<fs::file_time_type> struct StringMaker<fs::file_time_type>
{ {
static std::string convert(fs::file_time_type const& value) { static std::string convert(fs::file_time_type const& value)
{
std::time_t t = to_time_t(value); std::time_t t = to_time_t(value);
std::tm ttm = *std::localtime(&t); std::tm ttm = *std::localtime(&t);
std::ostringstream os; std::ostringstream os;
@ -135,8 +139,7 @@ public:
filename += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rng()]; filename += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rng()];
} }
_path = fs::canonical(fs::temp_directory_path()) / filename; _path = fs::canonical(fs::temp_directory_path()) / filename;
} } while (fs::exists(_path));
while(fs::exists(_path));
fs::create_directories(_path); fs::create_directories(_path);
if (opt == TempOpt::change_path) { if (opt == TempOpt::change_path) {
_orig_dir = fs::current_path(); _orig_dir = fs::current_path();
@ -210,7 +213,6 @@ static bool has_host_root_name_support()
return fs::path("//host").has_root_name(); return fs::path("//host").has_root_name();
} }
template <class T> template <class T>
class TestAllocator class TestAllocator
{ {
@ -218,17 +220,14 @@ public:
using value_type = T; using value_type = T;
TestAllocator() noexcept {} TestAllocator() noexcept {}
template <class U> TestAllocator(TestAllocator<U> const&) noexcept {} template <class U>
TestAllocator(TestAllocator<U> const&) noexcept
value_type* allocate(std::size_t n)
{ {
return static_cast<value_type*>(::operator new (n*sizeof(value_type)));
} }
void deallocate(value_type* p, std::size_t) noexcept value_type* allocate(std::size_t n) { return static_cast<value_type*>(::operator new(n * sizeof(value_type))); }
{
::operator delete(p); void deallocate(value_type* p, std::size_t) noexcept { ::operator delete(p); }
}
}; };
template <class T, class U> template <class T, class U>
@ -243,8 +242,6 @@ bool operator!=(TestAllocator<T> const& x, TestAllocator<U> const& y) noexcept
return !(x == y); return !(x == y);
} }
TEST_CASE("Temporary Directory", "[temp dir]") TEST_CASE("Temporary Directory", "[temp dir]")
{ {
fs::path tempPath; fs::path tempPath;

View File

@ -3,6 +3,7 @@
// where exactly one cpp includes fs_impl.hpp and all others use // where exactly one cpp includes fs_impl.hpp and all others use
// fs_fwd.hpp (to test this with maximum functionality, the unit tests // fs_fwd.hpp (to test this with maximum functionality, the unit tests
// are included here, signaling they should only include the fs_fwd.hpp) // are included here, signaling they should only include the fs_fwd.hpp)
#define NOMINMAX
#include <ghc/fs_impl.hpp> #include <ghc/fs_impl.hpp>
#define CATCH_CONFIG_MAIN #define CATCH_CONFIG_MAIN
#include "catch.hpp" #include "catch.hpp"