diff --git a/CMakeLists.txt b/CMakeLists.txt index 012d4ddb..0da378c0 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -139,6 +139,7 @@ if (DROGON_CXX_STANDARD EQUAL 14) target_link_libraries(${PROJECT_NAME} PUBLIC Boost::boost) target_link_libraries(${PROJECT_NAME} PUBLIC Boost::filesystem) list(APPEND INCLUDE_DIRS_FOR_DYNAMIC_VIEW ${Boost_INCLUDE_DIR}) + option(HAS_STD_FILESYSTEM_PATH "use boost::filesystem" OFF) elseif (DROGON_CXX_STANDARD EQUAL 17) # With C++17, use Boost if std::filesystem::path is missing message(STATUS "use c++17") @@ -153,6 +154,7 @@ elseif (DROGON_CXX_STANDARD EQUAL 17) if (CMAKE_CXX_COMPILER_ID MATCHES GNU) target_link_libraries(${PROJECT_NAME} PUBLIC stdc++fs) endif() + option(HAS_STD_FILESYSTEM_PATH "use std::filesystem" ON) else() find_package(Boost 1.49.0 COMPONENTS filesystem system REQUIRED) message(STATUS "Using Boost filesytem::path") @@ -161,9 +163,11 @@ elseif (DROGON_CXX_STANDARD EQUAL 17) message(STATUS "Boost libraries: " ${Boost_LIBRARIES}) target_link_libraries(${PROJECT_NAME} PUBLIC Boost::filesystem Boost::system) list(APPEND INCLUDE_DIRS_FOR_DYNAMIC_VIEW ${Boost_INCLUDE_DIR}) + option(HAS_STD_FILESYSTEM_PATH "use boost::filesystem" OFF) endif() else () message(STATUS "use c++20") + option(HAS_STD_FILESYSTEM_PATH "use std::filesystem" ON) endif () list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules/) diff --git a/cmake/templates/config.h.in b/cmake/templates/config.h.in index 6a9b62ba..d33f2478 100644 --- a/cmake/templates/config.h.in +++ b/cmake/templates/config.h.in @@ -4,6 +4,7 @@ #cmakedefine01 LIBPQ_SUPPORTS_BATCH_MODE #cmakedefine01 USE_MYSQL #cmakedefine01 USE_SQLITE3 +#cmakedefine01 HAS_STD_FILESYSTEM_PATH #cmakedefine OpenSSL_FOUND #cmakedefine Boost_FOUND diff --git a/lib/src/HttpFileImpl.cc b/lib/src/HttpFileImpl.cc index 8baef509..b289ee8f 100644 --- a/lib/src/HttpFileImpl.cc +++ b/lib/src/HttpFileImpl.cc @@ -14,17 +14,10 @@ #include "HttpFileImpl.h" #include "HttpAppFrameworkImpl.h" +#include "filesystem.h" #include #include #include -// Switch between native c++17 or boost for c++14 -#ifdef HAS_STD_FILESYSTEM_PATH -#include -namespace stl = std; -#else // HAS_STD_FILESYSTEM_PATH -#include -namespace stl = boost::system; -#endif // HAS_STD_FILESYSTEM_PATH using namespace drogon; @@ -64,7 +57,7 @@ int HttpFileImpl::saveAs(const std::string &fileName) const } if (fsFileName.has_parent_path()) { - stl::error_code err; + drogon::error_code err; filesystem::create_directories(fsFileName.parent_path(), err); if (err) return -1; diff --git a/lib/src/HttpResponseImpl.cc b/lib/src/HttpResponseImpl.cc index c0ef2f49..bc5a28b7 100644 --- a/lib/src/HttpResponseImpl.cc +++ b/lib/src/HttpResponseImpl.cc @@ -23,12 +23,6 @@ #include #include #include -// Switch between native c++17 or boost for c++14 -#ifdef HAS_STD_FILESYSTEM_PATH -namespace stl = std; -#else -namespace stl = boost::system; -#endif using namespace trantor; using namespace drogon; @@ -350,7 +344,7 @@ void HttpResponseImpl::makeHeaderString(trantor::MsgBuffer &buffer) } else { - stl::error_code err; + drogon::error_code err; filesystem::path fsSendfile(utils::toNativePath(sendfileName_)); auto fileSize = filesystem::file_size(fsSendfile, err); if (err) diff --git a/lib/src/StaticFileRouter.cc b/lib/src/StaticFileRouter.cc index afadeeff..a0190d19 100644 --- a/lib/src/StaticFileRouter.cc +++ b/lib/src/StaticFileRouter.cc @@ -30,13 +30,6 @@ #include // Switch between native c++17 or boost for c++14 #include "filesystem.h" -#ifdef HAS_STD_FILESYSTEM_PATH -#include -namespace stl = std; -#else -#include -namespace stl = boost::system; -#endif using namespace drogon; @@ -147,7 +140,7 @@ void StaticFileRouter::route( location.realLocation_ + std::string{restOfThePath.data(), restOfThePath.length()}; filesystem::path fsFilePath(utils::toNativePath(filePath)); - stl::error_code err; + drogon::error_code err; if (!filesystem::exists(fsFilePath, err)) { defaultHandler_(req, std::move(callback)); @@ -226,7 +219,7 @@ void StaticFileRouter::route( std::string directoryPath = HttpAppFrameworkImpl::instance().getDocumentRoot() + path; filesystem::path fsDirectoryPath(utils::toNativePath(directoryPath)); - stl::error_code err; + drogon::error_code err; if (filesystem::exists(fsDirectoryPath, err)) { if (filesystem::is_directory(fsDirectoryPath, err)) @@ -379,7 +372,7 @@ void StaticFileRouter::sendStaticFileResponse( if (!fileExists) { filesystem::path fsFilePath(utils::toNativePath(filePath)); - stl::error_code err; + drogon::error_code err; if (!filesystem::exists(fsFilePath, err) || !filesystem::is_regular_file(fsFilePath, err)) { @@ -402,7 +395,7 @@ void StaticFileRouter::sendStaticFileResponse( // Find compressed file first. auto brFileName = filePath + ".br"; filesystem::path fsBrFile(utils::toNativePath(brFileName)); - stl::error_code err; + drogon::error_code err; if (filesystem::exists(fsBrFile, err) && filesystem::is_regular_file(fsBrFile, err)) { @@ -419,7 +412,7 @@ void StaticFileRouter::sendStaticFileResponse( // Find compressed file first. auto gzipFileName = filePath + ".gz"; filesystem::path fsGzipFile(utils::toNativePath(gzipFileName)); - stl::error_code err; + drogon::error_code err; if (filesystem::exists(fsGzipFile, err) && filesystem::is_regular_file(fsGzipFile, err)) { diff --git a/lib/src/Utilities.cc b/lib/src/Utilities.cc index 73616258..aae422c2 100644 --- a/lib/src/Utilities.cc +++ b/lib/src/Utilities.cc @@ -52,15 +52,6 @@ #include #include -// Switch between native c++17 or boost for c++14 -#ifdef HAS_STD_FILESYSTEM_PATH -#include -namespace stl = std; -#else -#include -namespace stl = boost::system; -#endif - #ifdef _WIN32 char *strptime(const char *s, const char *f, struct tm *tm) { @@ -1061,7 +1052,7 @@ int createPath(const std::string &path) if (osPath.back() != filesystem::path::preferred_separator) osPath.push_back(filesystem::path::preferred_separator); filesystem::path fsPath(osPath); - stl::error_code err; + drogon::error_code err; filesystem::create_directories(fsPath, err); if (err) { diff --git a/lib/src/filesystem.h b/lib/src/filesystem.h index 25540086..5ac8f104 100644 --- a/lib/src/filesystem.h +++ b/lib/src/filesystem.h @@ -11,31 +11,25 @@ #pragma once -/* Check of std::filesystem::path availability: - * - OS X: depends on the target OSX version (>= 10.15 Catalina) - * - Windows: Visual Studio >= 2019 (c++20) - * - Others: should already have it in c++17 - */ -#if (defined(__APPLE__) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 101500L) || \ - (defined(_WIN32) && defined(_MSC_VER) && _MSC_VER >= 1920) || \ - (!defined(__APPLE__) && !defined(_WIN32) && __cplusplus >= 201703L) -#define HAS_STD_FILESYSTEM_PATH -#endif - +#include #include -#ifdef HAS_STD_FILESYSTEM_PATH +#if HAS_STD_FILESYSTEM_PATH #include +#include #else #include +#include #endif namespace drogon { -#ifdef HAS_STD_FILESYSTEM_PATH +#if HAS_STD_FILESYSTEM_PATH namespace filesystem = std::filesystem; +using std::error_code; #else namespace filesystem = boost::filesystem; +using boost::system::error_code; #endif } // namespace drogon