src/lib: Switch to ghc::filesystem in path utilities

This commit is contained in:
Povilas Kanapickas 2021-11-01 04:29:46 +02:00
parent a987605513
commit bcafdc6783
6 changed files with 26 additions and 19 deletions

View File

@ -301,6 +301,8 @@ elseif (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
) )
endif() endif()
include_directories("${CMAKE_SOURCE_DIR}/ext/gulrak-filesystem/include")
# #
# OpenSSL # OpenSSL
# #

View File

@ -18,7 +18,7 @@
#include "SslCertificate.h" #include "SslCertificate.h"
#include "common/DataDirectories.h" #include "common/DataDirectories.h"
#include "base/finally.h" #include "base/finally.h"
#include "io/fstream.h" #include "io/filesystem.h"
#include "net/FingerprintDatabase.h" #include "net/FingerprintDatabase.h"
#include "net/SecureUtils.h" #include "net/SecureUtils.h"

View File

@ -15,6 +15,9 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
// this header must come first so that it picks up the filesystem implementation
#include <ghc/fs_impl.hpp>
#include "filesystem.h" #include "filesystem.h"
#if SYSAPI_WIN32 #if SYSAPI_WIN32
#include "common/win32/encoding_utilities.h" #include "common/win32/encoding_utilities.h"
@ -26,43 +29,42 @@ namespace barrier {
namespace { namespace {
template<class Stream> template<class Stream>
void open_utf8_path_impl(Stream& stream, const std::string& path, std::ios_base::openmode mode) void open_utf8_path_impl(Stream& stream, const fs::path& path, std::ios_base::openmode mode)
{ {
#if SYSAPI_WIN32 #if SYSAPI_WIN32
// on Windows we need to use a private constructor from wchar_t* string. // on Windows we need to use a non-standard constructor from wchar_t* string
auto wchar_path = utf8_to_win_char(path); // which fs::path::native() returns
stream.open(wchar_path.data(), mode); stream.open(path.native().c_str(), mode);
#else #else
stream.open(path.c_str(), mode); stream.open(path.native().c_str(), mode);
#endif #endif
} }
} // namespace } // namespace
void open_utf8_path(std::ifstream& stream, const std::string& path, std::ios_base::openmode mode) void open_utf8_path(std::ifstream& stream, const fs::path& path, std::ios_base::openmode mode)
{ {
open_utf8_path_impl(stream, path, mode); open_utf8_path_impl(stream, path, mode);
} }
void open_utf8_path(std::ofstream& stream, const std::string& path, std::ios_base::openmode mode) void open_utf8_path(std::ofstream& stream, const fs::path& path, std::ios_base::openmode mode)
{ {
open_utf8_path_impl(stream, path, mode); open_utf8_path_impl(stream, path, mode);
} }
void open_utf8_path(std::fstream& stream, const std::string& path, std::ios_base::openmode mode) void open_utf8_path(std::fstream& stream, const fs::path& path, std::ios_base::openmode mode)
{ {
open_utf8_path_impl(stream, path, mode); open_utf8_path_impl(stream, path, mode);
} }
std::FILE* fopen_utf8_path(const std::string& path, const std::string& mode) std::FILE* fopen_utf8_path(const fs::path& path, const std::string& mode)
{ {
#if SYSAPI_WIN32 #if SYSAPI_WIN32
auto wchar_path = utf8_to_win_char(path);
auto wchar_mode = utf8_to_win_char(mode); auto wchar_mode = utf8_to_win_char(mode);
return _wfopen(reinterpret_cast<wchar_t*>(wchar_path.data()), return _wfopen(path.native().c_str(),
reinterpret_cast<wchar_t*>(wchar_mode.data())); reinterpret_cast<wchar_t*>(wchar_mode.data()));
#else #else
return std::fopen(path.c_str(), mode.c_str()); return std::fopen(path.native().c_str(), mode.c_str());
#endif #endif
} }

View File

@ -21,17 +21,20 @@
#include <cstdio> #include <cstdio>
#include <iosfwd> #include <iosfwd>
#include <ios> #include <ios>
#include <ghc/fs_fwd.hpp>
namespace barrier { namespace barrier {
void open_utf8_path(std::ifstream& stream, const std::string& path, namespace fs = ghc::filesystem;
void open_utf8_path(std::ifstream& stream, const fs::path& path,
std::ios_base::openmode mode = std::ios_base::in); std::ios_base::openmode mode = std::ios_base::in);
void open_utf8_path(std::ofstream& stream, const std::string& path, void open_utf8_path(std::ofstream& stream, const fs::path& path,
std::ios_base::openmode mode = std::ios_base::out); std::ios_base::openmode mode = std::ios_base::out);
void open_utf8_path(std::fstream& stream, const std::string& path, void open_utf8_path(std::fstream& stream, const fs::path& path,
std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out); std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out);
std::FILE* fopen_utf8_path(const std::string& path, const std::string& mode); std::FILE* fopen_utf8_path(const fs::path& path, const std::string& mode);
} // namespace barrier } // namespace barrier

View File

@ -17,7 +17,7 @@
#include "base/String.h" #include "base/String.h"
#include "FingerprintDatabase.h" #include "FingerprintDatabase.h"
#include "io/fstream.h" #include "io/filesystem.h"
#include <algorithm> #include <algorithm>
#include <fstream> #include <fstream>

View File

@ -26,7 +26,7 @@
#include "base/Log.h" #include "base/Log.h"
#include "base/String.h" #include "base/String.h"
#include "common/DataDirectories.h" #include "common/DataDirectories.h"
#include "io/fstream.h" #include "io/filesystem.h"
#include "net/FingerprintDatabase.h" #include "net/FingerprintDatabase.h"
#include <openssl/ssl.h> #include <openssl/ssl.h>