Skip to content

Commit

Permalink
refs #18, fighting VS2015 sfinae issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gulrak committed May 19, 2019
1 parent 15788d8 commit aa1cb70
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
33 changes: 17 additions & 16 deletions examples/dir.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#include <iostream>
#include <iomanip>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <string>

#if defined(__cplusplus) && __cplusplus >= 201703L && defined(__has_include) && __has_include(<filesystem>)
#if defined(__cplusplus) && __cplusplus >= 201703L && defined(__has_include)
#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#else
#define GHC_USE_STD_FS
#endif
#endif
#ifndef GHC_USE_STD_FS
#include <ghc/filesystem.hpp>
namespace fs = ghc::filesystem;
#endif

template<typename TP>
template <typename TP>
std::time_t to_time_t(TP tp)
{
// Based on trick from: Nico Josuttis, C++17 - The Complete Guide
Expand All @@ -23,8 +27,8 @@ static std::string perm_to_str(fs::perms prms)
{
std::string result;
result.reserve(9);
for(int i = 0; i < 9; ++i) {
result = ((static_cast<int>(prms) & (1<<i)) ? "xwrxwrxwr"[i] : '-') + result;
for (int i = 0; i < 9; ++i) {
result = ((static_cast<int>(prms) & (1 << i)) ? "xwrxwrxwr"[i] : '-') + result;
}
return result;
}
Expand All @@ -33,27 +37,24 @@ int main(int argc, char* argv[])
{
#ifdef GHC_FILESYSTEM_VERSION
fs::u8arguments u8guard(argc, argv);
if(!u8guard.valid()) {
if (!u8guard.valid()) {
std::cerr << "Invalid character encoding, UTF-8 based encoding needed." << std::endl;
std::exit(EXIT_FAILURE);
}
#endif
if(argc > 2) {
if (argc > 2) {
std::cerr << "USAGE: dir <path>" << std::endl;
exit(1);
}
fs::path dir{"."};
if(argc == 2) {
if (argc == 2) {
dir = fs::u8path(argv[1]);
}
for(auto de : fs::directory_iterator(dir)) {
for (auto de : fs::directory_iterator(dir)) {
auto ft = to_time_t(de.last_write_time());
auto ftm = *std::localtime(&ft);
std::cout << (de.is_directory() ? "d" : "-") << perm_to_str(de.symlink_status().permissions()) << " "
<< std::setw(8) << (de.is_directory() ? "-" : std::to_string(de.file_size())) << " "
<< std::put_time(&ftm, "%Y-%m-%d %H:%M:%S") << " "
<< de.path().filename().string()
<< std::endl;
std::cout << (de.is_directory() ? "d" : "-") << perm_to_str(de.symlink_status().permissions()) << " " << std::setw(8) << (de.is_directory() ? "-" : std::to_string(de.file_size())) << " " << std::put_time(&ftm, "%Y-%m-%d %H:%M:%S") << " "
<< de.path().filename().string() << std::endl;
}
return 0;
}
8 changes: 6 additions & 2 deletions examples/du.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
#include <iomanip>
#include <chrono>

#if defined(__cplusplus) && __cplusplus >= 201703L && defined(__has_include) && __has_include(<filesystem>)
#if defined(__cplusplus) && __cplusplus >= 201703L && defined(__has_include)
#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#else
#define GHC_USE_STD_FS
#endif
#endif
#ifndef GHC_USE_STD_FS
#include <ghc/filesystem.hpp>
namespace fs = ghc::filesystem;
#endif
Expand Down
12 changes: 6 additions & 6 deletions include/ghc/filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1296,13 +1296,13 @@ inline StringType fromUtf8(const std::string& utf8String, const typename StringT
return result;
}

template <typename charT, typename traits, typename Alloc, typename std::enable_if<(sizeof(charT) == 1)>::type* = nullptr>
template <typename charT, typename traits, typename Alloc, typename std::enable_if<(sizeof(charT) == 1), int>::type size = 1>
inline std::string toUtf8(const std::basic_string<charT, traits, Alloc>& unicodeString)
{
return std::string(unicodeString.begin(), unicodeString.end());
}

template <typename charT, typename traits, typename Alloc, typename std::enable_if<(sizeof(charT) == 2)>::type* = nullptr>
template <typename charT, typename traits, typename Alloc, typename std::enable_if<(sizeof(charT) == 2), int>::type size = 2>
inline std::string toUtf8(const std::basic_string<charT, traits, Alloc>& unicodeString)
{
std::string result;
Expand All @@ -1324,7 +1324,7 @@ inline std::string toUtf8(const std::basic_string<charT, traits, Alloc>& unicode
return result;
}

template <typename charT, typename traits, typename Alloc, typename std::enable_if<(sizeof(charT) == 4)>::type* = nullptr>
template <typename charT, typename traits, typename Alloc, typename std::enable_if<(sizeof(charT) == 4), int>::type size = 4>
inline std::string toUtf8(const std::basic_string<charT, traits, Alloc>& unicodeString)
{
std::string result;
Expand All @@ -1334,10 +1334,10 @@ inline std::string toUtf8(const std::basic_string<charT, traits, Alloc>& unicode
return result;
}

template <typename SourceType>
inline std::string toUtf8(const SourceType* unicodeString)
template <typename charT>
inline std::string toUtf8(const charT* unicodeString)
{
return toUtf8(std::basic_string<SourceType, std::char_traits<SourceType>>(unicodeString));
return toUtf8(std::basic_string<charT, std::char_traits<charT>>(unicodeString));
}

} // namespace detail
Expand Down

0 comments on commit aa1cb70

Please sign in to comment.