refactor: move functions to platorm

This commit is contained in:
Horror Proton
2022-10-01 12:45:06 +08:00
parent 446aefea8f
commit ae4fe7dbe8
4 changed files with 93 additions and 78 deletions

View File

@@ -8,22 +8,20 @@
#include <sstream>
#include <string>
#include "Platform/AsstPlatform.h"
#ifdef _WIN32
#include "Platform/SafeWindows.h"
#else
#include <ctime>
#include <fcntl.h>
#include <memory.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#endif
#include "Meta.hpp"
namespace asst::utils
{
using os_string = std::filesystem::path::string_type;
template <typename char_t = char>
using pair_of_string_view = std::pair<std::basic_string_view<char_t>, std::basic_string_view<char_t>>;
@@ -292,74 +290,20 @@ namespace asst::utils
return buff;
}
using os_string = platform::os_string;
#ifdef _WIN32
static_assert(std::same_as<os_string, std::wstring>);
os_string to_osstring(const std::string& utf8_str);
std::string from_osstring(const os_string& os_str);
#else
static_assert(std::same_as<os_string, std::string>);
inline os_string to_osstring(const std::string& utf8_str)
{
return utf8_str;
}
inline std::string from_osstring(const os_string& os_str)
{
return os_str;
}
#endif
inline std::filesystem::path path(const os_string& os_str)
{
return std::filesystem::path(os_str);
}
#ifdef _WIN32
// Allow construct a path from utf8-string in win32
inline std::filesystem::path path(const std::string& utf8_str)
{
return std::filesystem::path(to_osstring(utf8_str));
}
#endif
#ifdef _WIN32
std::string path_to_crt_string(const std::filesystem::path& path);
std::string path_to_ansi_string(const std::filesystem::path& path);
inline std::string path_to_utf8_string(const std::filesystem::path& path)
{
return from_osstring(path.native());
}
inline std::string path_to_crt_string(const std::string& utf8_path)
{
return path_to_crt_string(path(utf8_path));
}
inline std::string path_to_ansi_string(const std::string& utf8_path)
{
return path_to_crt_string(path(utf8_path));
}
#else
inline std::string path_to_utf8_string(const std::filesystem::path& path)
{
return path.native();
}
inline std::string path_to_ansi_string(const std::filesystem::path& path)
{
return path.native();
}
inline std::string path_to_crt_string(const std::filesystem::path& path)
{
return path.native();
}
#endif
using platform::from_osstring;
using platform::path;
using platform::path_to_ansi_string;
using platform::path_to_crt_string;
using platform::path_to_utf8_string;
using platform::to_osstring;
template <typename _ = void>
inline std::string ansi_to_utf8(std::string_view ansi_str)
@@ -487,7 +431,7 @@ namespace asst::utils
return str;
}
std::string callcmd(const std::string& cmdline);
using platform::callcmd;
namespace path_literals
{

View File

@@ -1,12 +1,81 @@
#pragma once
#include <cstddef>
#include <filesystem>
#include <new>
#include <string>
#include <type_traits>
#include <utility>
namespace asst::platform
{
std::string callcmd(const std::string&);
using os_string = std::filesystem::path::string_type;
inline std::filesystem::path path(const os_string& os_str)
{
return std::filesystem::path(os_str);
}
#ifdef _WIN32
std::string path_to_crt_string(const std::filesystem::path&);
std::string path_to_ansi_string(const std::filesystem::path&);
os_string to_osstring(const std::string& utf8_str);
std::string from_osstring(const os_string&);
// Allow construct a path from utf8-string in win32
inline std::filesystem::path path(const std::string& utf8_str)
{
return std::filesystem::path(to_osstring(utf8_str));
}
inline std::string path_to_utf8_string(const std::filesystem::path& path)
{
return from_osstring(path.native());
}
inline std::string path_to_crt_string(const std::string& utf8_path)
{
return path_to_crt_string(path(utf8_path));
}
inline std::string path_to_ansi_string(const std::string& utf8_path)
{
return path_to_crt_string(path(utf8_path));
}
#else
inline os_string to_osstring(const std::string& utf8_str)
{
return utf8_str;
}
inline std::string from_osstring(const os_string& os_str)
{
return os_str;
}
inline std::string path_to_utf8_string(const std::filesystem::path& path)
{
return path.native();
}
inline std::string path_to_ansi_string(const std::filesystem::path& path)
{
return path.native();
}
inline std::string path_to_crt_string(const std::filesystem::path& path)
{
return path.native();
}
#endif
// --------- detail ------------
extern const size_t page_size;
void* aligned_alloc(size_t len, size_t align);
@@ -50,4 +119,4 @@ namespace asst::platform
inline TElem* get() const { return _ptr; }
inline size_t size() const { return _ptr ? (page_size / sizeof(TElem)) : 0; }
};
}
} // namespace asst::platform

View File

@@ -1,9 +1,11 @@
#if __has_include(<unistd.h>)
#include "AsstPlatformPosix.h"
#include "AsstPlatform.h"
#include "Utils/AsstUtils.hpp"
#include <cstdlib>
#include <fcntl.h>
#include <sys/wait.h>
#include <unistd.h>
static size_t get_page_size()
{
@@ -22,7 +24,7 @@ void asst::platform::aligned_free(void* ptr)
free(ptr);
}
std::string asst::utils::callcmd(const std::string& cmdline)
std::string asst::platform::callcmd(const std::string& cmdline)
{
constexpr int PipeBuffSize = 4096;
std::string pipe_str;

View File

@@ -67,7 +67,7 @@ static std::string get_ansi_short_path(const std::filesystem::path& path)
return result;
}
std::string asst::utils::path_to_crt_string(const std::filesystem::path& path)
std::string asst::platform::path_to_crt_string(const std::filesystem::path& path)
{
// UCRT may use UTF-8 encoding while ANSI code page is still some other MBCS encoding
// so we use CRT wcstombs instead of WideCharToMultiByte
@@ -86,7 +86,7 @@ std::string asst::utils::path_to_crt_string(const std::filesystem::path& path)
}
}
std::string asst::utils::path_to_ansi_string(const std::filesystem::path& path)
std::string asst::platform::path_to_ansi_string(const std::filesystem::path& path)
{
// UCRT may use UTF-8 encoding while ANSI code page is still some other MBCS encoding
// so we use CRT wcstombs instead of WideCharToMultiByte
@@ -104,15 +104,15 @@ std::string asst::utils::path_to_ansi_string(const std::filesystem::path& path)
}
}
asst::utils::os_string asst::utils::to_osstring(const std::string& utf8_str)
asst::platform::os_string asst::platform::to_osstring(const std::string& utf8_str)
{
int len = MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), (int)utf8_str.size(), nullptr, 0);
asst::utils::os_string result(len, 0);
os_string result(len, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), (int)utf8_str.size(), &result[0], len);
return result;
}
std::string asst::utils::from_osstring(const asst::utils::os_string& os_str)
std::string asst::platform::from_osstring(const os_string& os_str)
{
int len = WideCharToMultiByte(CP_UTF8, 0, os_str.c_str(), (int)os_str.size(), nullptr, 0, nullptr, nullptr);
std::string result(len, 0);
@@ -120,7 +120,7 @@ std::string asst::utils::from_osstring(const asst::utils::os_string& os_str)
return result;
}
std::string asst::utils::callcmd(const std::string& cmdline)
std::string asst::platform::callcmd(const std::string& cmdline)
{
constexpr int PipeBuffSize = 4096;
auto pipe_buffer = std::make_unique<char[]>(PipeBuffSize);
@@ -146,7 +146,7 @@ std::string asst::utils::callcmd(const std::string& cmdline)
PROCESS_INFORMATION process_info = { nullptr };
ASST_AUTO_DEDUCED_ZERO_INIT_END
auto cmdline_osstr = asst::utils::to_osstring(cmdline);
auto cmdline_osstr = to_osstring(cmdline);
BOOL create_ret =
CreateProcessW(nullptr, &cmdline_osstr[0], nullptr, nullptr, TRUE, 0, nullptr, nullptr, &si, &process_info);
if (!create_ret) {
@@ -282,7 +282,7 @@ HANDLE asst::win32::OpenDirectory(const std::filesystem::path& path, BOOL bReadW
bool asst::win32::SetDirectoryReparsePoint(const std::filesystem::path& link, const std::filesystem::path& target)
{
auto normtarget = asst::utils::path(asst::utils::string_replace_all(target.native(), L"/", L"\\"));
auto normtarget = asst::platform::path(asst::utils::string_replace_all(target.native(), L"/", L"\\"));
auto nttarget = L"\\GLOBAL??\\" + std::filesystem::absolute(normtarget).native();
if (nttarget.back() != L'\\') nttarget.push_back(L'\\');