添加项目文件。

This commit is contained in:
UnknownObject
2026-06-30 17:46:34 +08:00
parent 415124601e
commit d7ce0fe00a
75 changed files with 9723 additions and 0 deletions
+227
View File
@@ -0,0 +1,227 @@
// UUTextCodec.cpp : 定义静态库的函数。
//
#include "UTextCodec.h"
#include <string>
#include <vector>
#include <codecvt>
#include <locale>
#include <unicode/uconfig.h>
#include <unicode/unistr.h>
#include <unicode/utf.h>
namespace uns
{
// ICU中转封装
static icu_74::UnicodeString fromWString(const std::wstring& wstr)
{
#if WCHAR_MAX == 0xFFFF
return icu_74::UnicodeString(reinterpret_cast<const UChar*>(wstr.c_str()), (int32_t)wstr.length());
#else
std::u16string u16;
for (wchar_t wc : wstr)
{
if (wc <= 0xFFFF)
u16.push_back(static_cast<char16_t>(wc));
else
{
wc -= 0x10000;
u16.push_back((wc >> 10) + 0xD800);
u16.push_back((wc & 0x3FF) + 0xDC00);
}
}
return icu_74::UnicodeString(reinterpret_cast<const UChar*>(u16.c_str()), u16.length());
#endif
}
static std::wstring toWString(const icu_74::UnicodeString& ustr)
{
std::wstring wstr;
int32_t i = 0;
while (i < ustr.length())
{
UChar32 c;
U16_NEXT(ustr.getBuffer(), i, ustr.length(), c);
#if WCHAR_MAX == 0xFFFF
if (c <= 0xFFFF)
wstr.push_back(static_cast<wchar_t>(c));
else
{
c -= 0x10000;
wstr.push_back((c >> 10) + 0xD800);
wstr.push_back((c & 0x3FF) + 0xDC00);
}
#else
wstr.push_back(static_cast<wchar_t>(c));
#endif
}
return wstr;
}
static icu_74::UnicodeString fromU32String(const std::u32string& str)
{
std::u16string u16;
for (char32_t c : str)
{
if (c <= 0xFFFF)
u16.push_back(static_cast<char16_t>(c));
else
{
c -= 0x10000;
u16.push_back((c >> 10) + 0xD800);
u16.push_back((c & 0x3FF) + 0xDC00);
}
}
return icu_74::UnicodeString(reinterpret_cast<const UChar*>(u16.c_str()), (int32_t)u16.length());
}
static std::u32string toU32String(const icu_74::UnicodeString& ustr)
{
std::u32string result;
int32_t i = 0;
while (i < ustr.length())
{
UChar32 c;
U16_NEXT(ustr.getBuffer(), i, ustr.length(), c);
result.push_back(c);
}
return result;
}
}
std::wstring UTextCodec::UTF8to16(const std::string& utf8)
{
icu_74::UnicodeString ustr = icu_74::UnicodeString::fromUTF8(utf8);
return uns::toWString(ustr);
}
std::string UTextCodec::UTF16to8(const std::wstring& wstr)
{
icu_74::UnicodeString ustr = uns::fromWString(wstr);
std::string result;
ustr.toUTF8String(result);
return result;
}
std::u32string UTextCodec::UTF8to32(const std::string& utf8)
{
icu_74::UnicodeString ustr = icu_74::UnicodeString::fromUTF8(utf8);
return uns::toU32String(ustr);
}
std::string UTextCodec::UTF32to8(const std::u32string& u32str)
{
icu_74::UnicodeString ustr = uns::fromU32String(u32str);
std::string result;
ustr.toUTF8String(result);
return result;
}
std::u32string UTextCodec::WtoUTF32(const std::wstring& wstr)
{
return uns::toU32String(uns::fromWString(wstr));
}
std::wstring UTextCodec::UTF32toW(const std::u32string& u32str)
{
return uns::toWString(uns::fromU32String(u32str));
}
std::wstring UTextCodec::StoW(const std::string& str)
{
#ifdef _WIN32
return AtoW(str);
#else
// 默认按 UTF-8 解码
icu_74::UnicodeString ustr = icu_74::UnicodeString::fromUTF8(str);
return uns::toWString(ustr);
#endif
}
std::string UTextCodec::WtoS(const std::wstring& wstr)
{
#ifdef _WIN32
return WtoA(wstr);
#else
// 默认按 UTF-8 编码
icu_74::UnicodeString ustr = uns::fromWString(wstr);
std::string result;
ustr.toUTF8String(result);
return result;
#endif
}
#ifdef _WIN32
std::wstring UTextCodec::AtoW(const std::string& ansi, UINT codePage)
{
// 空指针
if (ansi.empty())
return L"";
// 计算长度
int nNeedSize = MultiByteToWideChar(codePage, 0, ansi.c_str(), -1, NULL, 0);
if (0 == nNeedSize)
return L"";
// 分配空间,转换
std::wstring strRet(L"");
wchar_t* pRet = new wchar_t[nNeedSize + 1];
memset(pRet, 0, (nNeedSize + 1) * sizeof(wchar_t));
if (0 == MultiByteToWideChar(codePage, 0, ansi.c_str(), -1, pRet, nNeedSize))
{
}
else
strRet = pRet;
delete[]pRet;
return strRet;
}
std::string UTextCodec::WtoA(const std::wstring& wstr, UINT codePage)
{
// 空指针输入
if (wstr.empty())
return "";
// 无法计算需要的长度.
int nNeedSize = WideCharToMultiByte(codePage, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
if (0 == nNeedSize)
return "";
// 分配空间,转换.
char* pRet = new char[nNeedSize + 1]; // 虽然返回WideCharToMultiByte的长度是包含 null 字符的长度, 还是多+一个字符.
memset(pRet, 0, nNeedSize + 1);
std::string strRet("");
if (0 == WideCharToMultiByte(codePage, 0, wstr.c_str(), -1, pRet, nNeedSize, NULL, NULL))
{
}
else
strRet = pRet;
delete[] pRet;
return strRet;
}
#endif
std::string UTextCodec::DetectEncoding()
{
#ifdef _WIN32
UINT cp = GetACP();
return "CP" + std::to_string(cp);
#else
const char* locale = std::getenv("LC_CTYPE");
if (!locale || std::string(locale).empty())
locale = std::getenv("LANG");
if (!locale || std::string(locale).empty())
return "unknown";
std::string loc(locale);
// locale 典型格式如 en_US.UTF-8,截取点号后面的编码
auto pos = loc.find('.');
if (pos != std::string::npos)
{
std::string encoding = loc.substr(pos + 1);
// 有些编码后面有 @variant,比如 UTF-8@xxx,去掉后面部分
auto atPos = encoding.find('@');
if (atPos != std::string::npos)
encoding = encoding.substr(0, atPos);
return encoding;
}
return "unknown";
#endif
}
+45
View File
@@ -0,0 +1,45 @@
#pragma once
#include <string>
#ifdef _WIN32
#include <Windows.h>
#if defined(UTEXTCODEC_EXPORTS)
#define UTEXTCODEC_EXPORT __declspec(dllexport)
#else
#define UTEXTCODEC_EXPORT __declspec(dllimport)
#ifdef _DEBUG
#pragma comment(lib, "../x64/Debug/UTextCodecd.lib")
#else
#pragma comment(lib, "../x64/Release/UTextCodec.lib")
#endif
#endif
#else
#define UTEXTCODEC_EXPORT __attribute__((visibility("default")))
#endif
class UTEXTCODEC_EXPORT UTextCodec
{
public:
// UTF-8 ⇄ UTF-16 (wstring)
static std::wstring UTF8to16(const std::string& utf8);
static std::string UTF16to8(const std::wstring& wstr);
// UTF-8 ⇄ UTF-32
static std::u32string UTF8to32(const std::string& utf8);
static std::string UTF32to8(const std::u32string& u32str);
// wstring ⇄ u32string
static std::u32string WtoUTF32(const std::wstring& wstr);
static std::wstring UTF32toW(const std::u32string& u32str);
// wstring ⇄ string
static std::wstring StoW(const std::string& str);
static std::string WtoS(const std::wstring& wstr);
#ifdef _WIN32
// Windows ANSI ⇄ UTF-16 (wstring)
static std::wstring AtoW(const std::string& ansi, UINT codePage = CP_ACP);
static std::string WtoA(const std::wstring& wstr, UINT codePage = CP_ACP);
#endif
static std::string DetectEncoding();
};
+174
View File
@@ -0,0 +1,174 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>18.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{a0da5943-b851-42f3-9425-6db8ec22ef34}</ProjectGuid>
<RootNamespace>UTextCodec</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v145</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v145</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v145</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v145</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<TargetName>$(ProjectName)d</TargetName>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;UTEXTCODEC_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
<Manifest>
<EnableSegmentHeap>true</EnableSegmentHeap>
</Manifest>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;UTEXTCODEC_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
<Manifest>
<EnableSegmentHeap>true</EnableSegmentHeap>
</Manifest>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;UTEXTCODEC_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
<Manifest>
<EnableSegmentHeap>true</EnableSegmentHeap>
</Manifest>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;UTEXTCODEC_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
<Manifest>
<EnableSegmentHeap>true</EnableSegmentHeap>
</Manifest>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="framework.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="UTextCodec.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="UTextCodec.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
+39
View File
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="framework.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="pch.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="UTextCodec.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="pch.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="UTextCodec.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>
+16
View File
@@ -0,0 +1,16 @@
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
+5
View File
@@ -0,0 +1,5 @@
#pragma once
#define WIN32_LEAN_AND_MEAN // 从 Windows 头文件中排除极少使用的内容
// Windows 头文件
#include <windows.h>
+5
View File
@@ -0,0 +1,5 @@
// pch.cpp: 与预编译标头对应的源文件
#include "pch.h"
// 当使用预编译的头时,需要使用此源文件,编译才能成功。
+13
View File
@@ -0,0 +1,13 @@
// pch.h: 这是预编译标头文件。
// 下方列出的文件仅编译一次,提高了将来生成的生成性能。
// 这还将影响 IntelliSense 性能,包括代码完成和许多代码浏览功能。
// 但是,如果此处列出的文件中的任何一个在生成之间有更新,它们全部都将被重新编译。
// 请勿在此处添加要频繁更新的文件,这将使得性能优势无效。
#ifndef PCH_H
#define PCH_H
// 添加要在此处预编译的标头
#include "framework.h"
#endif //PCH_H