diff --git a/src/MeoAssistant/Utils/AsstUtils.hpp b/src/MeoAssistant/Utils/AsstUtils.hpp index 321174f07b..2b41691c79 100644 --- a/src/MeoAssistant/Utils/AsstUtils.hpp +++ b/src/MeoAssistant/Utils/AsstUtils.hpp @@ -8,22 +8,20 @@ #include #include +#include "Platform/AsstPlatform.h" + #ifdef _WIN32 #include "Platform/SafeWindows.h" #else #include #include -#include #include -#include -#include #endif #include "Meta.hpp" namespace asst::utils { - using os_string = std::filesystem::path::string_type; template using pair_of_string_view = std::pair, std::basic_string_view>; @@ -292,74 +290,20 @@ namespace asst::utils return buff; } + using os_string = platform::os_string; + #ifdef _WIN32 static_assert(std::same_as); - os_string to_osstring(const std::string& utf8_str); - std::string from_osstring(const os_string& os_str); #else static_assert(std::same_as); - 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 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 { diff --git a/src/MeoAssistant/Utils/Platform/AsstPlatform.h b/src/MeoAssistant/Utils/Platform/AsstPlatform.h index b1bec29c20..c3695d8d62 100644 --- a/src/MeoAssistant/Utils/Platform/AsstPlatform.h +++ b/src/MeoAssistant/Utils/Platform/AsstPlatform.h @@ -1,12 +1,81 @@ #pragma once #include +#include #include +#include #include #include 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 diff --git a/src/MeoAssistant/Utils/Platform/AsstPlatformPosix.cpp b/src/MeoAssistant/Utils/Platform/AsstPlatformPosix.cpp index 06e331129e..cdfd1c2da2 100644 --- a/src/MeoAssistant/Utils/Platform/AsstPlatformPosix.cpp +++ b/src/MeoAssistant/Utils/Platform/AsstPlatformPosix.cpp @@ -1,9 +1,11 @@ #if __has_include() #include "AsstPlatformPosix.h" #include "AsstPlatform.h" -#include "Utils/AsstUtils.hpp" #include +#include +#include +#include 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; diff --git a/src/MeoAssistant/Utils/Platform/AsstPlatformWin32.cpp b/src/MeoAssistant/Utils/Platform/AsstPlatformWin32.cpp index f287cba480..d52e145ec7 100644 --- a/src/MeoAssistant/Utils/Platform/AsstPlatformWin32.cpp +++ b/src/MeoAssistant/Utils/Platform/AsstPlatformWin32.cpp @@ -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(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'\\');