4 Commits

Author SHA1 Message Date
bb17bb6a1f chore: update build system
- use configuration-arch-based path in MSVC to make sure generated package can be used by native MSVC project.
- add github action and corresponding build script. but not tested.
- fix some testbench code.
2024-06-20 15:51:40 +08:00
3fa05b43d9 fix: use new method to do the convertion among UTF8, UTF16 and UTF32.
- use std::codevct as the convertion method among UTF8, UTF16 and UTF32.
- fix the issue that COM Guard was accidently dropped by compiler because no reference to it.
2024-06-20 10:16:13 +08:00
1fd132f0c9 fix: fix build error in Linux environment.
- fix various build error in Linux environment.
- the convertion between UTF8, UTF16 and UTF32 have error in Linux and will be fixed in future.
2024-06-19 13:28:51 +08:00
b61f718084 fix: add if macro for Windows only testbench code.
- add if macro for Windows only testbench code to make sure testbench can be run on other platforms.
2024-06-19 09:17:04 +08:00
12 changed files with 217 additions and 107 deletions

3
.gitattributes vendored
View File

@ -1 +1,2 @@
Doxyfile.in eol=lf Doxyfile.in eol=lf
*.bat eol=crlf

35
.github/workflows/nightly.yml vendored Normal file
View File

@ -0,0 +1,35 @@
name: YYCC Nightly Build
on:
workflow_dispatch:
push:
branches:
- master
jobs:
msvc-build:
strategy:
matrix:
vs: ['2019']
msvc_arch: ['x86']
runs-on: windows-2019
steps:
- name: Fetching Repository
uses: actions/checkout@v3
- name: Building YYCC
shell: cmd
run: |
set VS=${{ matrix.vs }}
set VCVARS="C:\Program Files (x86)\Microsoft Visual Studio\%VS%\Enterprise\VC\Auxiliary\Build\vcvarsall.bat"
if not exist %VCVARS% set VCVARS="C:\Program Files\Microsoft Visual Studio\%VS%\Enterprise\VC\Auxiliary\Build\vcvarsall.bat"
call %VCVARS% ${{ matrix.msvc_arch }}
.\script\build.bat
- name: Uploading Nightly Build
uses: actions/upload-artifact@v3
with:
name: YYCC-windows-nightly
path: bin/install/*
retention-days: 30

View File

@ -8,6 +8,18 @@ project(YYCC
option(YYCC_BUILD_TESTBENCH "Build testbench of YYCCommonplace." OFF) option(YYCC_BUILD_TESTBENCH "Build testbench of YYCCommonplace." OFF)
option(YYCC_BUILD_DOC "Build document of YYCCommonplace." OFF) option(YYCC_BUILD_DOC "Build document of YYCCommonplace." OFF)
# Detect MSVC IDE environment.
# If we in it, we should add configuration and build type in install path.
if (CMAKE_GENERATOR MATCHES "Visual Studio")
# Do Visual Studio specific
set(YYCC_INSTALL_PATH_LIB lib/${CMAKE_VS_PLATFORM_NAME}/$<CONFIG>)
set(YYCC_INSTALL_PATH_BIN bin/${CMAKE_VS_PLATFORM_NAME})
else()
# Other stuff
set(YYCC_INSTALL_PATH_LIB lib)
set(YYCC_INSTALL_PATH_BIN bin)
endif()
# Import 2 build targets # Import 2 build targets
add_subdirectory(src) add_subdirectory(src)
if (YYCC_BUILD_TESTBENCH) if (YYCC_BUILD_TESTBENCH)

35
script/build.bat Normal file
View File

@ -0,0 +1,35 @@
@ECHO OFF
SET README_PATH=%CD%\README.md
IF EXIST %README_PATH% (
REM DO NOTHING
) ELSE (
ECHO Error: You must run this script at the root folder of this project!
EXIT /b
)
:: Create essential folder
MKDIR bin
CD bin
MKDIR Win32
MKDIR x64
MKDIR install
:: Build for Win32
CD Win32
cmake -G "Visual Studio 16 2019" -A Win32 -DYYCC_BUILD_TESTBENCH=ON ../..
cmake --build . --config Debug
cmake --install . --prefix=../install --config Debug
cmake --build . --config Release
cmake --install . --prefix=../install --config Release
CD ..
:: Build for x64
CD x64
cmake -G "Visual Studio 16 2019" -A x64 -DYYCC_BUILD_TESTBENCH=ON ../..
cmake --build . --config Debug
cmake --install . --prefix=../install --config Debug
cmake --build . --config Release
cmake --install . --prefix=../install --config Release
CD ..
ECHO DONE

View File

@ -67,13 +67,11 @@ PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/utf-8> $<$<CXX_COMPILER_ID:MSVC>:/utf-8>
) )
# Install project # Install binary and headers
# Install binary
install(TARGETS YYCCommonplace install(TARGETS YYCCommonplace
EXPORT YYCCommonplaceTargets EXPORT YYCCommonplaceTargets
LIBRARY DESTINATION lib LIBRARY DESTINATION ${YYCC_INSTALL_PATH_LIB}
ARCHIVE DESTINATION lib ARCHIVE DESTINATION ${YYCC_INSTALL_PATH_LIB}
RUNTIME DESTINATION bin
INCLUDES DESTINATION include INCLUDES DESTINATION include
FILE_SET HEADERS DESTINATION include FILE_SET HEADERS DESTINATION include
) )

View File

@ -21,6 +21,10 @@ namespace YYCC::COMHelper {
} }
} }
bool IsInitialized() const {
return m_HasInit;
}
protected: protected:
bool m_HasInit; bool m_HasInit;
}; };
@ -33,7 +37,11 @@ namespace YYCC::COMHelper {
* So we use a static instance in here. * So we use a static instance in here.
* And make it be const so no one can change it. * And make it be const so no one can change it.
*/ */
static const ComGuard c_ComGuard; static const ComGuard c_ComGuard {};
bool IsInitialized() {
return c_ComGuard.IsInitialized();
}
} }

View File

@ -64,6 +64,17 @@ namespace YYCC::COMHelper {
using SmartLPWSTR = std::unique_ptr<std::remove_pointer_t<LPWSTR>, CoTaskMemDeleter>; using SmartLPWSTR = std::unique_ptr<std::remove_pointer_t<LPWSTR>, CoTaskMemDeleter>;
/**
* @brief Check whether COM environment has been initialized.
* @return True if it is, otherwise false.
* @remarks
* This function will call corresponding function of COM Guard.
* Do not remove this function and you must preserve at least one reference to this function in final program.
* Some compiler will try to drop COM Guard in final program if no reference to it and it will cause the initialization of COM environment failed.
* This is the reason why I order you do the things said above.
*/
bool IsInitialized();
} }
#endif #endif

View File

@ -171,7 +171,7 @@ namespace YYCC::ConsoleHelper {
#else #else
// just return true and do nothing // just return true and do nothing
return true return true;
#endif #endif
} }
@ -226,7 +226,7 @@ namespace YYCC::ConsoleHelper {
WinConsoleWrite(strl, bIsErr); WinConsoleWrite(strl, bIsErr);
#else #else
// in linux, directly use C function to write. // in linux, directly use C function to write.
std::fputs(strl.c_str(), to_stderr ? stderr : stdout); std::fputs(strl.c_str(), bIsErr ? stderr : stdout);
#endif #endif
} }
@ -245,11 +245,13 @@ namespace YYCC::ConsoleHelper {
} }
void Write(const char* u8_strl) { void Write(const char* u8_strl) {
RawWrite<false, false, false>(u8_strl, va_list()); va_list empty{};
RawWrite<false, false, false>(u8_strl, empty);
} }
void WriteLine(const char* u8_strl) { void WriteLine(const char* u8_strl) {
RawWrite<false, false, true>(u8_strl, va_list()); va_list empty{};
RawWrite<false, false, true>(u8_strl, empty);
} }
void ErrFormat(const char* u8_fmt, ...) { void ErrFormat(const char* u8_fmt, ...) {
@ -267,11 +269,13 @@ namespace YYCC::ConsoleHelper {
} }
void ErrWrite(const char* u8_strl) { void ErrWrite(const char* u8_strl) {
RawWrite<false, true, false>(u8_strl, va_list()); va_list empty{};
RawWrite<false, true, false>(u8_strl, empty);
} }
void ErrWriteLine(const char* u8_strl) { void ErrWriteLine(const char* u8_strl) {
RawWrite<false, true, true>(u8_strl, va_list()); va_list empty{};
RawWrite<false, true, true>(u8_strl, empty);
} }
} }

View File

@ -173,6 +173,9 @@ namespace YYCC::DialogHelper {
// prepare result variable // prepare result variable
HRESULT hr; HRESULT hr;
// check whether COM environment has been initialized
if (!COMHelper::IsInitialized()) return false;
// create file dialog instance // create file dialog instance
// fetch dialog CLSID first // fetch dialog CLSID first
CLSID dialog_clsid; CLSID dialog_clsid;

View File

@ -1,6 +1,6 @@
#include "EncodingHelper.hpp" #include "EncodingHelper.hpp"
#include <cuchar> #include <locale>
namespace YYCC::EncodingHelper { namespace YYCC::EncodingHelper {
@ -70,61 +70,49 @@ namespace YYCC::EncodingHelper {
#endif #endif
#if defined(__cpp_char8_t)
using CodecvtUTF8Char_t = char8_t;
#else
using CodecvtUTF8Char_t = char;
#endif
template<typename _TChar, std::enable_if_t<std::is_same_v<_TChar, char16_t> || std::is_same_v<_TChar, char32_t>, int> = 0> template<typename _TChar, std::enable_if_t<std::is_same_v<_TChar, char16_t> || std::is_same_v<_TChar, char32_t>, int> = 0>
static bool UTF8ToUTFOther(const char* src, std::basic_string<_TChar>& dest) { using CodecvtFacet_t = std::codecvt<_TChar, CodecvtUTF8Char_t, std::mbstate_t>;
// Reference:
// https://zh.cppreference.com/w/cpp/string/multibyte/mbrtoc32 template<typename _TChar, std::enable_if_t<std::is_same_v<_TChar, char16_t> || std::is_same_v<_TChar, char32_t>, int> = 0>
// https://zh.cppreference.com/w/cpp/string/multibyte/mbrtoc16 static bool UTF8ToUTFOther(const char* _src, std::basic_string<_TChar>& dest) {
// https://learn.microsoft.com/zh-cn/cpp/c-runtime-library/reference/mbrtoc16-mbrtoc323?view=msvc-170 // Reference:
// // https://zh.cppreference.com/w/cpp/locale/codecvt/in
// Due to the same reason introduced in UTFOtherToUTF8,
// we use these function as convertion function.
// init src string // init src string
if (src == nullptr) return false; if (_src == nullptr) return false;
std::string src_string(src); std::string src(_src);
// init result string
dest.clear();
// init essential cvt variables // init locale and get codecvt facet
std::mbstate_t state {}; // same reason in UTFOtherToUTF8 to keeping reference to locale
_TChar c1632; const auto& this_locale = std::locale::classic();
const char* ptr = src_string.c_str(); const auto& this_codecvt = std::use_facet<CodecvtFacet_t<_TChar>>(this_locale);
const char* end = src_string.c_str() + src_string.size() + 1;
// convertion preparation
// start convertion std::mbstate_t mb{};
while (true) { dest.resize(src.size());
// do convertion const CodecvtUTF8Char_t* intern_from = reinterpret_cast<const CodecvtUTF8Char_t*>(src.c_str()),
size_t rc; *intern_from_end = reinterpret_cast<const CodecvtUTF8Char_t*>(src.c_str() + src.size()),
if constexpr (std::is_same_v<_TChar, char16_t>) { *intern_from_next = nullptr;
rc = std::mbrtoc16(&c1632, ptr, end - ptr, &state); _TChar* extern_to = dest.data(),
} else { *extern_to_end = dest.data() + dest.size(),
rc = std::mbrtoc32(&c1632, ptr, end - ptr, &state); *extern_to_next = nullptr;
} // do convertion
if (!rc) break; auto result = this_codecvt.in(
mb,
// check result intern_from, intern_from_end, intern_from_next,
if (rc == static_cast<size_t>(-1)) { extern_to, extern_to_end, extern_to_next
// encoding error, return false );
return false;
} else if (rc == static_cast<size_t>(-2)) {
// insufficient sequence, return false
return false;
} else if (rc == static_cast<size_t>(-3)) {
// UTF16 pair case (usually is emoji, one emoji is represented by 2 UTF16)
//
// only push result char but do not increase pointer
// because this char is output from state.
dest.push_back(c1632);
} else {
// normal case
// append to result
dest.push_back(c1632);
// inc ptr
ptr += rc;
}
}
// check result
if (result != CodecvtFacet_t<_TChar>::ok)
return false;
// resize result and return
dest.resize(extern_to_next - dest.data());
return true; return true;
} }
@ -146,40 +134,41 @@ namespace YYCC::EncodingHelper {
} }
template<typename _TChar, std::enable_if_t<std::is_same_v<_TChar, char16_t> || std::is_same_v<_TChar, char32_t>, int> = 0> template<typename _TChar, std::enable_if_t<std::is_same_v<_TChar, char16_t> || std::is_same_v<_TChar, char32_t>, int> = 0>
static bool UTFOtherToUTF8(const _TChar* src, std::string& dest) { static bool UTFOtherToUTF8(const _TChar* _src, std::string& dest) {
// Reference: // Reference:
// https://zh.cppreference.com/w/cpp/string/multibyte/c32rtomb // https://zh.cppreference.com/w/cpp/locale/codecvt/out
// https://zh.cppreference.com/w/cpp/string/multibyte/c16rtomb
// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/c16rtomb-c32rtomb1?view=msvc-170
//
// Due to Microsoft implementation, c16rtomb and c32rtomb
// always convert UTF32 and UTF16 string into UTF8 string no matter current c locale.
// At the same time, most Linux use UTF8 as their locale.
// So using c16rtomb and c32rtomb do the convertion from UTF32 or UTF16 to UTF8 is reasonable.
// initialize src string // initialize src string
if (src == nullptr) return false; if (_src == nullptr) return false;
std::basic_string<_TChar> src_string(src); std::basic_string<_TChar> src(_src);
// init result string
dest.clear();
// init essential cvt variables // init locale and get codecvt facet
std::mbstate_t state {}; // the reference to locale must be preserved until convertion done.
char out[MB_LEN_MAX] {}; // because the life time of codecvt facet is equal to the reference to locale.
for (_TChar c : src_string) { const auto& this_locale = std::locale::classic();
// do convertion const auto& this_codecvt = std::use_facet<CodecvtFacet_t<_TChar>>(this_locale);
std::size_t rc;
if constexpr (std::is_same_v<_TChar, char16_t>) {
rc = std::c16rtomb(out, c, &state);
} else {
rc = std::c32rtomb(out, c, &state);
}
// convertion failed
if (rc == static_cast<size_t>(-1)) return false;
// otherwise append result
dest.append(out, rc);
}
// do convertion preparation
std::mbstate_t mb{};
dest.resize(src.size() * this_codecvt.max_length());
const _TChar* intern_from = src.c_str(),
*intern_from_end = src.c_str() + src.size(),
*intern_from_next = nullptr;
CodecvtUTF8Char_t* extern_to = reinterpret_cast<CodecvtUTF8Char_t*>(dest.data()),
*extern_to_end = reinterpret_cast<CodecvtUTF8Char_t*>(dest.data() + dest.size()),
*extern_to_next = nullptr;
// do convertion
auto result = this_codecvt.out(
mb,
intern_from, intern_from_end, intern_from_next,
extern_to, extern_to_end, extern_to_next
);
// check result
if (result != CodecvtFacet_t<_TChar>::ok)
return false;
// resize result and retuen
dest.resize(extern_to_next - reinterpret_cast<CodecvtUTF8Char_t*>(dest.data()));
return true; return true;
} }

View File

@ -33,11 +33,9 @@ PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/utf-8> $<$<CXX_COMPILER_ID:MSVC>:/utf-8>
) )
# Install binary # Install testbench only on Release mode
install(TARGETS YYCCTestbench install(TARGETS YYCCTestbench
EXPORT YYCCTestbenchTargets EXPORT YYCCTestbenchTargets
LIBRARY DESTINATION lib CONFIGURATIONS Release
ARCHIVE DESTINATION lib RUNTIME DESTINATION ${YYCC_INSTALL_PATH_BIN}
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
) )

View File

@ -23,7 +23,7 @@ namespace YYCCTestbench {
#define TEST_UNICODE_STR_EMOJI "\U0001F363 \u2716 \U0001F37A" // sushi x beer mug #define TEST_UNICODE_STR_EMOJI "\U0001F363 \u2716 \U0001F37A" // sushi x beer mug
#define CONCAT(prefix, strl) prefix ## strl #define CONCAT(prefix, strl) prefix ## strl
#define CPP_U8_LITERAL(strl) strl #define CPP_U8_LITERAL(strl) reinterpret_cast<const char*>(CONCAT(u8, strl))
#define CPP_U16_LITERAL(strl) CONCAT(u, strl) #define CPP_U16_LITERAL(strl) CONCAT(u, strl)
#define CPP_U32_LITERAL(strl) CONCAT(U, strl) #define CPP_U32_LITERAL(strl) CONCAT(U, strl)
#define CPP_WSTR_LITERAL(strl) CONCAT(L, strl) #define CPP_WSTR_LITERAL(strl) CONCAT(L, strl)
@ -164,6 +164,7 @@ namespace YYCCTestbench {
} }
// check wstring convertion on windows // check wstring convertion on windows
#if YYCC_OS == YYCC_OS_WINDOWS
for (size_t i = 0u; i < count; ++i) { for (size_t i = 0u; i < count; ++i) {
// get item // get item
const auto& u8str = c_UTF8TestStrTable[i]; const auto& u8str = c_UTF8TestStrTable[i];
@ -177,6 +178,7 @@ namespace YYCCTestbench {
Assert(YYCC::EncodingHelper::UTF8ToWchar(u8str.c_str(), wcache) && wcache == wstr, "YYCC::EncodingHelper::UTF8ToWchar"); Assert(YYCC::EncodingHelper::UTF8ToWchar(u8str.c_str(), wcache) && wcache == wstr, "YYCC::EncodingHelper::UTF8ToWchar");
Assert(YYCC::EncodingHelper::WcharToUTF8(wstr.c_str(), u8cache) && u8cache == u8str, "YYCC::EncodingHelper::WcharToUTF8"); Assert(YYCC::EncodingHelper::WcharToUTF8(wstr.c_str(), u8cache) && u8cache == u8str, "YYCC::EncodingHelper::WcharToUTF8");
} }
#endif
} }
@ -300,6 +302,8 @@ namespace YYCCTestbench {
} }
static void DialogTestbench() { static void DialogTestbench() {
#if YYCC_OS == YYCC_OS_WINDOWS
std::string ret; std::string ret;
std::vector<std::string> rets; std::vector<std::string> rets;
@ -327,9 +331,13 @@ namespace YYCCTestbench {
if (YYCC::DialogHelper::OpenFolderDialog(params, ret)) { if (YYCC::DialogHelper::OpenFolderDialog(params, ret)) {
Console::FormatLine("Open Folder: %s", ret.c_str()); Console::FormatLine("Open Folder: %s", ret.c_str());
} }
#endif
} }
static void ExceptionTestbench() { static void ExceptionTestbench() {
#if YYCC_OS == YYCC_OS_WINDOWS
YYCC::ExceptionHelper::Register(); YYCC::ExceptionHelper::Register();
// Perform a div zero exception. // Perform a div zero exception.
@ -337,10 +345,16 @@ namespace YYCCTestbench {
int k = i / j; int k = i / j;
YYCC::ExceptionHelper::Unregister(); YYCC::ExceptionHelper::Unregister();
#endif
} }
static void WinFctTestbench() { static void WinFctTestbench() {
Console::FormatLine("Current Module HANDLE: 0x%" PRI_XPTR_LEFT_PADDING PRIXPTR, YYCC::WinFctHelper::GetCurrentModule()); #if YYCC_OS == YYCC_OS_WINDOWS
HMODULE test_current_module;
Assert((test_current_module = YYCC::WinFctHelper::GetCurrentModule()) != nullptr, "YYCC::WinFctHelper::GetCurrentModule");
Console::FormatLine("Current Module HANDLE: 0x%" PRI_XPTR_LEFT_PADDING PRIXPTR, test_current_module);
std::string test_temp; std::string test_temp;
Assert(YYCC::WinFctHelper::GetTempDirectory(test_temp), "YYCC::WinFctHelper::GetTempDirectory"); Assert(YYCC::WinFctHelper::GetTempDirectory(test_temp), "YYCC::WinFctHelper::GetTempDirectory");
@ -353,6 +367,8 @@ namespace YYCCTestbench {
std::string test_localappdata_path; std::string test_localappdata_path;
Assert(YYCC::WinFctHelper::GetLocalAppData(test_localappdata_path), "YYCC::WinFctHelper::GetLocalAppData"); Assert(YYCC::WinFctHelper::GetLocalAppData(test_localappdata_path), "YYCC::WinFctHelper::GetLocalAppData");
Console::FormatLine("Local AppData: %s", test_localappdata_path.c_str()); Console::FormatLine("Local AppData: %s", test_localappdata_path.c_str());
#endif
} }
static void FsPathPatch() { static void FsPathPatch() {
@ -380,10 +396,10 @@ namespace YYCCTestbench {
int main(int argc, char** args) { int main(int argc, char** args) {
//YYCCTestbench::ConsoleTestbench(); //YYCCTestbench::ConsoleTestbench();
YYCCTestbench::EncodingTestbench(); YYCCTestbench::EncodingTestbench();
//YYCCTestbench::StringTestbench(); YYCCTestbench::StringTestbench();
//YYCCTestbench::ParserTestbench(); YYCCTestbench::ParserTestbench();
//YYCCTestbench::DialogTestbench(); YYCCTestbench::DialogTestbench();
//YYCCTestbench::ExceptionTestbench(); YYCCTestbench::ExceptionTestbench();
//YYCCTestbench::WinFctTestbench(); YYCCTestbench::WinFctTestbench();
//YYCCTestbench::FsPathPatch(); YYCCTestbench::FsPathPatch();
} }