finish iconv code

This commit is contained in:
yyc12345 2023-03-01 15:51:56 +08:00
parent 8724077e97
commit 11d05615ba
3 changed files with 127 additions and 25 deletions

View File

@ -116,7 +116,7 @@ namespace LibCmo::CK2 {
if (decomp_buffer != nullptr) { if (decomp_buffer != nullptr) {
parser = std::unique_ptr<CKBufferParser>(new(std::nothrow) CKBufferParser(decomp_buffer, doc->m_FileInfo.Hdr1UnPackSize, true)); parser = std::unique_ptr<CKBufferParser>(new(std::nothrow) CKBufferParser(decomp_buffer, doc->m_FileInfo.Hdr1UnPackSize, true));
if (parser == nullptr) { if (parser == nullptr) {
delete[] decomp_buffer; delete[] reinterpret_cast<char*>(decomp_buffer);
return CKERROR::CKERR_OUTOFMEMORY; return CKERROR::CKERR_OUTOFMEMORY;
} }
} }
@ -221,7 +221,7 @@ namespace LibCmo::CK2 {
parser = std::unique_ptr<CKBufferParser>(new(std::nothrow) CKBufferParser(decomp_buffer, doc->m_FileInfo.DataUnPackSize, true)); parser = std::unique_ptr<CKBufferParser>(new(std::nothrow) CKBufferParser(decomp_buffer, doc->m_FileInfo.DataUnPackSize, true));
if (parser == nullptr) { if (parser == nullptr) {
delete[] decomp_buffer; delete[] reinterpret_cast<char*>(decomp_buffer);
return CKERROR::CKERR_OUTOFMEMORY; return CKERROR::CKERR_OUTOFMEMORY;
} }
} }

View File

@ -71,7 +71,70 @@ namespace LibCmo::EncodingHelper {
#else #else
//todo: linux implementation static constexpr const IconvInc = 16;
bool DoIconv(const char* enc_from, const char* enc_to,
std::string& str_from, std::string& str_to) {
iconv_t cd;
char *inbuf = nullptr, *outbuf = nullptr;
size_t inbytesleft, outbytesleft, nchars, result_len;
// check empty
if (str_from.empty()) {
str_to.clear();
return true;
}
// create iconv descriptor
cd = iconv_open(enc_to, enc_from);
if (cd == (iconv_t) -1) {
// fail to create iconv descriptor
return false;
}
// pre-resize
str_to.resize(str_from.size() + IconvInc);
// setup some variables
inbytesleft = str_from.size();
inbuf = str_from.data();
outbytesleft = str_to_size();
outbuf = str_to.data();
result_len = str_to.size();
// conv core
nchars = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
while (nchars == (size_t)-1 && errno == E2BIG) {
// record the length has been converted
size_t len = outbuf - str_to.data();
// resize for variables
result_len += IconvInc;
outbytesleft += IconvInc;
// resize for container
str_to.resize(result_len);
// assign new outbuf from failed position
outbuf = str_to.data() + len;
nchars = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
}
// close iconv descriptor
iconv_close(cd);
// check error
if (nchars == (size_t)-1) {
// failed
return false;
} else {
// success
// resize result to get correct data
str_to.resize(result_len - outbytesleft);
return true;
}
}
#endif #endif
@ -81,8 +144,6 @@ namespace LibCmo::EncodingHelper {
#if defined(LIBCMO_OS_WIN32) #if defined(LIBCMO_OS_WIN32)
const ENCODING_TOKEN ENCODING_TOKEN_DEFAULT = nullptr;
ENCODING_TOKEN CreateEncodingToken(std::string& token_string) { ENCODING_TOKEN CreateEncodingToken(std::string& token_string) {
ENCODING_TOKEN token = new(std::nothrow) UINT(); ENCODING_TOKEN token = new(std::nothrow) UINT();
if (token == nullptr) return ENCODING_TOKEN_DEFAULT; if (token == nullptr) return ENCODING_TOKEN_DEFAULT;
@ -138,9 +199,55 @@ namespace LibCmo::EncodingHelper {
#else #else
const ENCODING_TOKEN ENCODING_TOKEN_DEFAULT = nullptr; static const char UTF8_SYMBOL[] = "UTF-8";
ENCODING_TOKEN CreateEncodingToken(std::string& token_string) {
ENCODING_TOKEN token = new(std::nothrow) char[token_string.size() + 1];
if (token == nullptr) return ENCODING_TOKEN_DFAULT;
std::memcpy(token, token_string.c_str(), token_string.size() + 1);
return token;
}
void DestroyEncodingToken(ENCODING_TOKEN token) {
if (token != ENCODING_TOKEN_DEFAULT) {
delete[] token;
}
}
void GetUtf8VirtoolsName(std::string& native_name, std::string& u8_name, ENCODING_TOKEN token) {
if (token == ENCODING_TOKEN_DEFAULT) {
u8_name = native_name.c_str();
return;
}
// convert with fallback
if (!DoIconv(token, UTF8_SYMBOL, native_name, u8_name)) {
u8_name = native_name.c_str();
}
}
void GetNativeVirtoolsName(std::string& u8_name, std::string& native_name, ENCODING_TOKEN token) {
if (token == ENCODING_TOKEN_DEFAULT) {
native_name = u8_name.c_str();
return;
}
// convert with fallback
if (!DoIconv(UTF8_SYMBOL, token, u8_name, native_name)) {
native_name = u8_name.c_str();
}
}
void SetStdPathFromU8Path(std::filesystem::path& stdpath, const char* u8_path) {
stdpath = u8_path;
}
FILE* OpenStdPathFile(std::filesystem::path& u8_filepath, bool is_read) {
return fopen(u8_filepath.string().c_str(), is_read ? "rb" : "wb");
}
//todo: linux implementation
#endif #endif

View File

@ -29,7 +29,9 @@ namespace LibCmo::EncodingHelper {
bool CharToChar(std::string& src, std::string& dest, UINT src_codepage, UINT dest_codepage); bool CharToChar(std::string& src, std::string& dest, UINT src_codepage, UINT dest_codepage);
#else #else
#error NO IMPLEMENTATION FOR LINUX ENCODING!
bool DoIconv(const char* enc_from, const char* enc_to, std::string& str_from, std::string& str_to);
#endif #endif
#pragma endregion #pragma endregion
@ -38,12 +40,17 @@ namespace LibCmo::EncodingHelper {
#if defined(LIBCMO_OS_WIN32) #if defined(LIBCMO_OS_WIN32)
/// <summary> // Token is the ticket for using encoding functions.
/// Token is the ticket for using encoding functions. // It should be created by "GenerateEncodingToken" and free by "DestroyEncodingToken".
/// It should be created by "GenerateEncodingToken" and free by "DestroyEncodingToken".
/// </summary>
using ENCODING_TOKEN = UINT*; using ENCODING_TOKEN = UINT*;
extern const ENCODING_TOKEN ENCODING_TOKEN_DEFAULT; constexpr const ENCODING_TOKEN ENCODING_TOKEN_DEFAULT = nullptr;
#else
using ENCODING_TOKEN = char*;
constexpr const ENCODING_TOKEN ENCODING_TOKEN_DEFAULT = nullptr;
#endif
ENCODING_TOKEN CreateEncodingToken(std::string& token_string); ENCODING_TOKEN CreateEncodingToken(std::string& token_string);
void DestroyEncodingToken(ENCODING_TOKEN token); void DestroyEncodingToken(ENCODING_TOKEN token);
@ -54,18 +61,6 @@ namespace LibCmo::EncodingHelper {
void SetStdPathFromU8Path(std::filesystem::path& stdpath, const char* u8_path); void SetStdPathFromU8Path(std::filesystem::path& stdpath, const char* u8_path);
FILE* OpenStdPathFile(std::filesystem::path& u8_filepath, bool is_read); FILE* OpenStdPathFile(std::filesystem::path& u8_filepath, bool is_read);
#else
/// <summary>
/// Token is the ticket for using encoding functions.
/// It should be created by "GenerateEncodingToken" and free by "DestroyEncodingToken".
/// </summary>
using ENCODING_TOKEN = char*;
extern const ENCODING_TOKEN ENCODING_TOKEN_DEFAULT;
#error NO IMPLEMENTATION FOR LINUX ENCODING!
#endif
#pragma endregion #pragma endregion
} }