diff --git a/include/ghc/filesystem.hpp b/include/ghc/filesystem.hpp index b2c687c..ec8982c 100644 --- a/include/ghc/filesystem.hpp +++ b/include/ghc/filesystem.hpp @@ -65,33 +65,38 @@ #if defined(GHC_FILESYSTEM_IMPLEMENTATION) #define GHC_EXPAND_IMPL #define GHC_INLINE -# ifdef GHC_OS_WINDOWS -# define GHC_FS_API __declspec(dllexport) -# else -# define GHC_FS_API __attribute__((visibility("default"))) -# endif +#ifdef GHC_OS_WINDOWS +#define GHC_FS_API +#define GHC_FS_API_CLASS +#else +#define GHC_FS_API __attribute__((visibility("default"))) +#define GHC_FS_API_CLASS __attribute__((visibility("default"))) +#endif #elif defined(GHC_FILESYSTEM_FWD) #define GHC_INLINE -# ifdef GHC_OS_WINDOWS -# define GHC_FS_API __declspec(dllimport) -# else -# define GHC_FS_API -# endif +#ifdef GHC_OS_WINDOWS +#define GHC_FS_API extern +#define GHC_FS_API_CLASS +#else +#define GHC_FS_API extern +#define GHC_FS_API_CLASS +#endif #else #define GHC_EXPAND_IMPL #define GHC_INLINE inline #define GHC_FS_API +#define GHC_FS_API_CLASS #endif #ifdef GHC_EXPAND_IMPL #ifdef GHC_OS_WINDOWS +#include +#include #include #include #include -#include #include -#include #else #include #include @@ -127,7 +132,7 @@ #include #include -#else // GHC_EXPAND_IMPL +#else // GHC_EXPAND_IMPL #include #include #include @@ -135,7 +140,7 @@ #include #include #include -#endif // GHC_EXPAND_IMPL +#endif // GHC_EXPAND_IMPL //#define LWG_2935_BEHAVIOUR #define LWG_2937_BEHAVIOUR @@ -147,7 +152,7 @@ namespace ghc { namespace filesystem { // 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: not_implemented_exception() @@ -157,7 +162,7 @@ public: }; // 30.10.8 class path -class GHC_FS_API path +class GHC_FS_API_CLASS path { public: using value_type = std::string::value_type; @@ -369,7 +374,7 @@ template path u8path(InputIterator first, InputIterator last); // 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: filesystem_error(const std::string& what_arg, std::error_code ec); @@ -385,7 +390,7 @@ private: path _p1, _p2; }; -class GHC_FS_API path::iterator +class GHC_FS_API_CLASS path::iterator { public: using value_type = const path; @@ -495,7 +500,7 @@ enum class directory_options : uint16_t { }; // 30.10.11 class file_status -class GHC_FS_API file_status +class GHC_FS_API_CLASS file_status { public: // 30.10.11.1 constructors and destructor @@ -522,7 +527,7 @@ private: using file_time_type = std::chrono::time_point; // 30.10.12 Class directory_entry -class GHC_FS_API directory_entry +class GHC_FS_API_CLASS directory_entry { public: // 30.10.12.1 constructors and destructor @@ -598,10 +603,10 @@ private: }; // 30.10.13 Class directory_iterator -class GHC_FS_API directory_iterator +class GHC_FS_API_CLASS directory_iterator { public: - class GHC_FS_API proxy + class GHC_FS_API_CLASS proxy { public: 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; // 30.10.14 class recursive_directory_iterator -class GHC_FS_API recursive_directory_iterator +class GHC_FS_API_CLASS recursive_directory_iterator { public: using iterator_category = std::input_iterator_tag; @@ -947,7 +952,7 @@ typedef basic_ofstream wofstream; typedef basic_fstream fstream; typedef basic_fstream wfstream; -class GHC_FS_API u8arguments +class GHC_FS_API_CLASS u8arguments { public: u8arguments(int& argc, char**& argv); @@ -957,10 +962,7 @@ public: _refargv = _argv; } - bool valid() const - { - return _isvalid; - } + bool valid() const { return _isvalid; } private: int _argc; @@ -979,23 +981,23 @@ private: //------------------------------------------------------------------------------------------------- namespace detail { - 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 }; - 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_high_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); - enum class portable_error { - none = 0, - exists, - not_found, - not_supported, - not_implemented, - invalid_argument, - }; - GHC_FS_API std::error_code make_error_code(portable_error err); -} +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 }; +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_high_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); +enum class portable_error { + none = 0, + exists, + not_found, + not_supported, + not_implemented, + invalid_argument, +}; +GHC_FS_API std::error_code make_error_code(portable_error err); +} // namespace detail namespace detail { @@ -1037,7 +1039,7 @@ GHC_INLINE std::error_code make_error_code(portable_error err) return std::error_code(); } -#endif // GHC_EXPAND_IMPL +#endif // GHC_EXPAND_IMPL template using EnableBitmask = typename std::enable_if::value || std::is_same::value || std::is_same::value || std::is_same::value, Enum>::type; @@ -1152,7 +1154,7 @@ GHC_INLINE unsigned consumeUtf8Fragment(const unsigned state, const uint8_t frag return state == S_RJCT ? static_cast(S_RJCT) : static_cast((utf8_state_info[category + 16] >> (state << 2)) & 0xf); } -} // detail +} // namespace detail #endif @@ -1238,7 +1240,7 @@ inline std::string toUtf8(const SourceType* unicodeString) return toUtf8(std::basic_string>(unicodeString)); } -} // detail +} // namespace detail #ifdef GHC_EXPAND_IMPL @@ -1277,7 +1279,7 @@ GHC_INLINE void postprocess_path_with_format(path::string_type& p, path::format } // namespace detail -#endif // GHC_EXPAND_IMPL +#endif // GHC_EXPAND_IMPL template inline path::path(const Source& source, format fmt) @@ -1345,7 +1347,7 @@ GHC_INLINE std::string systemErrorText(ErrorNumber code = 0) std::string msg = toUtf8(std::wstring((LPWSTR)msgBuf)); LocalFree(msgBuf); 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]; int rc = strerror_r(code ? code : errno, buffer, sizeof(buffer)); 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; #else - (void)sz; (void)nhl; (void)lwt; + (void)sz; + (void)nhl; + (void)lwt; struct ::stat fs; auto result = ::lstat(p.c_str(), &fs); if (result == 0) { @@ -1779,7 +1783,7 @@ GHC_INLINE path::path(string_type&& source, format fmt) detail::postprocess_path_with_format(_path, fmt); } -#endif // GHC_EXPAND_IMPL +#endif // GHC_EXPAND_IMPL template inline path::path(const Source& source, const std::locale& loc, format fmt) @@ -1832,7 +1836,7 @@ GHC_INLINE path& path::assign(path::string_type&& source) return *this; } -#endif // GHC_EXPAND_IMPL +#endif // GHC_EXPAND_IMPL template inline path& path::operator=(const Source& source) @@ -1901,7 +1905,7 @@ GHC_INLINE path& path::operator/=(const path& p) return *this; } -#endif // GHC_EXPAND_IMPL +#endif // GHC_EXPAND_IMPL template inline path& path::operator/=(const Source& source) @@ -1963,7 +1967,7 @@ GHC_INLINE path& path::operator+=(value_type x) return *this; } -#endif // GHC_EXPAND_IMPL +#endif // GHC_EXPAND_IMPL template inline path::path_type& path::operator+=(const Source& x) @@ -2071,7 +2075,7 @@ GHC_INLINE path::operator path::string_type() const return native(); } -#endif // GHC_EXPAND_IMPL +#endif // GHC_EXPAND_IMPL template inline std::basic_string path::string(const Allocator& a) const @@ -2106,7 +2110,7 @@ GHC_INLINE std::u32string path::u32string() const return detail::fromUtf8(native()); } -#endif // GHC_EXPAND_IMPL +#endif // GHC_EXPAND_IMPL //----------------------------------------------------------------------------- // 30.10.8.4.7, generic format observers @@ -2604,7 +2608,7 @@ GHC_INLINE path operator/(const path& lhs, const path& rhs) return result; } -#endif // GHC_EXPAND_IMPL +#endif // GHC_EXPAND_IMPL //----------------------------------------------------------------------------- // 30.10.8.6.1 path inserter and extractor @@ -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)) { - if(!ec) { + if (!ec) { copy(iter->path(), to / iter->path().filename(), options | static_cast(0x8000), ec); } - if(ec) { + if (ec) { return; } } @@ -3792,9 +3796,9 @@ GHC_INLINE uintmax_t remove_all(const path& p, std::error_code& ec) noexcept } std::error_code 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)) { - if(ec) { + if (ec) { break; } 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(remove(p, ec)) { + if (!ec) { + if (remove(p, ec)) { ++count; } } @@ -4856,7 +4860,7 @@ GHC_INLINE recursive_directory_iterator end(const recursive_directory_iterator&) return recursive_directory_iterator(); } -#endif // GHC_EXPAND_IMPL +#endif // GHC_EXPAND_IMPL } // namespace filesystem } // namespace ghc diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e960910..df2dfd2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -42,3 +42,6 @@ target_link_libraries(multifile_test ghc_filesystem) add_executable(fwd_impl_test fwd_test.cpp impl_test.cpp) 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() diff --git a/test/filesystem_test.cpp b/test/filesystem_test.cpp index be87f2a..a8dfc39 100644 --- a/test/filesystem_test.cpp +++ b/test/filesystem_test.cpp @@ -34,12 +34,15 @@ #include #include #include -#include #include +#include #include #include #include -#ifndef WIN32 +#ifdef WIN32 +#define NOMINMAX +#include +#else #include #include #include @@ -53,7 +56,7 @@ using namespace std::filesystem; using ifstream = std::ifstream; using ofstream = std::ofstream; using fstream = std::fstream; -} +} // namespace fs #ifdef __GNUC__ #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) #endif @@ -75,7 +78,7 @@ using namespace ghc::filesystem; using ifstream = ghc::filesystem::ifstream; using ofstream = ghc::filesystem::ofstream; using fstream = ghc::filesystem::fstream; -} +} // namespace fs #endif #ifndef GHC_FILESYSTEM_FWD_TEST @@ -86,7 +89,7 @@ using fstream = ghc::filesystem::fstream; //#define TEST_LWG_2935_BEHAVIOUR #define TEST_LWG_2937_BEHAVIOUR -template +template std::time_t to_time_t(TP tp) { // Based on trick from: Nico Josuttis, C++17 - The Complete Guide @@ -110,7 +113,8 @@ struct StringMaker template <> struct StringMaker { - 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::tm ttm = *std::localtime(&t); std::ostringstream os; @@ -135,8 +139,7 @@ public: filename += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rng()]; } _path = fs::canonical(fs::temp_directory_path()) / filename; - } - while(fs::exists(_path)); + } while (fs::exists(_path)); fs::create_directories(_path); if (opt == TempOpt::change_path) { _orig_dir = fs::current_path(); @@ -210,25 +213,21 @@ static bool has_host_root_name_support() return fs::path("//host").has_root_name(); } - template class TestAllocator { public: - using value_type = T; + using value_type = T; TestAllocator() noexcept {} - template TestAllocator(TestAllocator const&) noexcept {} - - value_type* allocate(std::size_t n) + template + TestAllocator(TestAllocator const&) noexcept { - return static_cast(::operator new (n*sizeof(value_type))); } - void deallocate(value_type* p, std::size_t) noexcept - { - ::operator delete(p); - } + value_type* allocate(std::size_t n) { return static_cast(::operator new(n * sizeof(value_type))); } + + void deallocate(value_type* p, std::size_t) noexcept { ::operator delete(p); } }; template @@ -243,8 +242,6 @@ bool operator!=(TestAllocator const& x, TestAllocator const& y) noexcept return !(x == y); } - - TEST_CASE("Temporary Directory", "[temp dir]") { fs::path tempPath; diff --git a/test/impl_test.cpp b/test/impl_test.cpp index 092be63..1652178 100644 --- a/test/impl_test.cpp +++ b/test/impl_test.cpp @@ -3,6 +3,7 @@ // where exactly one cpp includes fs_impl.hpp and all others use // fs_fwd.hpp (to test this with maximum functionality, the unit tests // are included here, signaling they should only include the fs_fwd.hpp) +#define NOMINMAX #include #define CATCH_CONFIG_MAIN #include "catch.hpp"