refactor: update project
- add documentation CMake build script. re-organise document layout for future changes. - move LIBCMO_EXPORT to BMap and rename it to BMAP_EXPORT because only BMap need to use this macro. - fully refactor VTEncoding to make it more like Python - Now language name is platform independent. - Hide implementation detail as possible as I can. - Language mapping are still work in progress. - add code gen for new added universal encoding feature to generate language name mapping in Windows and Iconv respectively. - remove old code of CMake build script. - update VTUtils for new requirement. - remove useless functions. - create LibCmo specific custom exception classes.
This commit is contained in:
@ -1,85 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "VTUtils.hpp"
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
#if defined(LIBCMO_OS_WIN32)
|
||||
#include <Windows.h>
|
||||
#include <fileapi.h>
|
||||
// disable annoy macro at the same time
|
||||
#undef GetObject
|
||||
#undef GetClassName
|
||||
#undef LoadImage
|
||||
#undef GetTempPath
|
||||
#else
|
||||
#include <iconv.h>
|
||||
#endif
|
||||
#include <string_view>
|
||||
|
||||
namespace LibCmo::EncodingHelper {
|
||||
|
||||
#pragma region assist functions
|
||||
using EncodingToken = void*;
|
||||
constexpr EncodingToken INVALID_ENCODING_TOKEN = nullptr;
|
||||
|
||||
#if defined(LIBCMO_OS_WIN32)
|
||||
EncodingToken CreateEncodingToken(const std::u8string_view& enc_name);
|
||||
void DestroyEncodingToken(EncodingToken token);
|
||||
|
||||
bool GetWindowsCodePage(const char* u8_encoding_spec, UINT* result);
|
||||
bool ToOrdinary(const std::u8string_view& src, std::string& dst, EncodingToken token);
|
||||
bool ToOrdinary(const char8_t* src, std::string& dst, EncodingToken token);
|
||||
std::string ToOrdinary(const std::u8string_view& src, EncodingToken token);
|
||||
std::string ToOrdinary(const char8_t* src, EncodingToken token);
|
||||
|
||||
bool WcharToChar(const wchar_t* src, std::string& dest, const UINT codepage);
|
||||
bool WcharToChar(const std::wstring& src, std::string& dest, const UINT codepage);
|
||||
|
||||
bool CharToWchar(const char* src, std::wstring& dest, const UINT codepage);
|
||||
bool CharToWchar(const std::string& src, std::wstring& dest, const UINT codepage);
|
||||
|
||||
bool CharToChar(const char* src, std::string& dest, const UINT src_codepage, const UINT dest_codepage);
|
||||
bool CharToChar(const std::string& src, std::string& dest, const UINT src_codepage, const UINT dest_codepage);
|
||||
|
||||
#else
|
||||
|
||||
bool CreateIconvDescriptor(const char* enc_from, const char* enc_to, iconv_t& val);
|
||||
void DestroyIconvDescriptor(iconv_t& val);
|
||||
|
||||
bool DoIconv(iconv_t& cd, const std::string& str_from, std::string& str_to);
|
||||
|
||||
#endif
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region core functions
|
||||
|
||||
#if defined(LIBCMO_OS_WIN32)
|
||||
|
||||
// Token is the ticket for using encoding functions.
|
||||
// It should be created by "GenerateEncodingToken" and free by "DestroyEncodingToken".
|
||||
using ENCODING_TOKEN = UINT*;
|
||||
constexpr const ENCODING_TOKEN ENCODING_TOKEN_DEFAULT = nullptr;
|
||||
|
||||
#else
|
||||
|
||||
class IconvPair {
|
||||
public:
|
||||
IconvPair();
|
||||
IconvPair(const IconvPair&) = delete;
|
||||
IconvPair& operator=(const IconvPair&) = delete;
|
||||
~IconvPair();
|
||||
|
||||
iconv_t FromUtf8;
|
||||
iconv_t ToUtf8;
|
||||
};
|
||||
using ENCODING_TOKEN = IconvPair*;
|
||||
constexpr const ENCODING_TOKEN ENCODING_TOKEN_DEFAULT = nullptr;
|
||||
|
||||
#endif
|
||||
|
||||
ENCODING_TOKEN CreateEncodingToken(const std::string& token_string);
|
||||
void DestroyEncodingToken(const ENCODING_TOKEN& token);
|
||||
|
||||
bool GetUtf8VirtoolsName(const std::string& native_name, std::string& u8_name, const ENCODING_TOKEN& token);
|
||||
bool GetNativeVirtoolsName(const std::string& u8_name, std::string& native_name, const ENCODING_TOKEN& token);
|
||||
|
||||
void U8PathToStdPath(std::filesystem::path& stdpath, const char* u8_path);
|
||||
void StdPathToU8Path(std::string& u8path, std::filesystem::path& stdpath);
|
||||
FILE* U8FOpen(const char* u8_filepath, const char* u8_mode);
|
||||
|
||||
#pragma endregion
|
||||
bool ToUTF8(const std::string_view& src, std::u8string& dst, EncodingToken token);
|
||||
bool ToUTF8(const char* src, std::u8string& dst, EncodingToken token);
|
||||
std::u8string ToUTF8(const std::string_view& src, EncodingToken token);
|
||||
std::u8string ToUTF8(const char* src, EncodingToken token);
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user