修复上传时,中文乱码的问题。
This commit is contained in:
@@ -947,6 +947,9 @@ int main(int argc, TCHAR* argv[], TCHAR* envp[])
|
|||||||
|
|
||||||
if (argc >= 2 && _tcsicmp(argv[1], _T("upload")) == 0)
|
if (argc >= 2 && _tcsicmp(argv[1], _T("upload")) == 0)
|
||||||
{
|
{
|
||||||
|
#ifdef _DEBUG
|
||||||
|
Sleep(10000);
|
||||||
|
#endif
|
||||||
DWORD dwStartTick = LogCommandStart(_T("upload"));
|
DWORD dwStartTick = LogCommandStart(_T("upload"));
|
||||||
int nCmdRet = HandleUploadCommand(argc, argv);
|
int nCmdRet = HandleUploadCommand(argc, argv);
|
||||||
LogCommandEnd(_T("upload"), dwStartTick, nCmdRet);
|
LogCommandEnd(_T("upload"), dwStartTick, nCmdRet);
|
||||||
|
|||||||
@@ -50,7 +50,27 @@ static std::string ToUtf8String(LPCTSTR lpText)
|
|||||||
WideCharToMultiByte(CP_UTF8, 0, lpText, -1, &strValue[0], nSize, NULL, NULL);
|
WideCharToMultiByte(CP_UTF8, 0, lpText, -1, &strValue[0], nSize, NULL, NULL);
|
||||||
return strValue;
|
return strValue;
|
||||||
#else
|
#else
|
||||||
return std::string(lpText);
|
int nLen = (int)_tcslen(lpText);
|
||||||
|
int nWideSize = MultiByteToWideChar(CP_ACP, 0, lpText, nLen, NULL, 0);
|
||||||
|
if (nWideSize <= 0)
|
||||||
|
{
|
||||||
|
return std::string();
|
||||||
|
}
|
||||||
|
|
||||||
|
CStringW strWide;
|
||||||
|
MultiByteToWideChar(CP_ACP, 0, lpText, nLen, strWide.GetBuffer(nWideSize), nWideSize);
|
||||||
|
strWide.ReleaseBuffer(nWideSize);
|
||||||
|
|
||||||
|
int nUtf8Size = WideCharToMultiByte(CP_UTF8, 0, strWide, strWide.GetLength(), NULL, 0, NULL, NULL);
|
||||||
|
if (nUtf8Size <= 0)
|
||||||
|
{
|
||||||
|
return std::string();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string strValue;
|
||||||
|
strValue.resize(nUtf8Size);
|
||||||
|
WideCharToMultiByte(CP_UTF8, 0, strWide, strWide.GetLength(), &strValue[0], nUtf8Size, NULL, NULL);
|
||||||
|
return strValue;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,6 +116,88 @@ static CString Utf8ToCString(const std::string& strUtf8)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static CString AnsiToCString(const std::string& strAnsi)
|
||||||
|
{
|
||||||
|
if (strAnsi.empty())
|
||||||
|
{
|
||||||
|
return CString();
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef UNICODE
|
||||||
|
int nSize = MultiByteToWideChar(CP_ACP, 0, strAnsi.data(), (int)strAnsi.size(), NULL, 0);
|
||||||
|
if (nSize <= 0)
|
||||||
|
{
|
||||||
|
return CString();
|
||||||
|
}
|
||||||
|
|
||||||
|
CString strValue;
|
||||||
|
MultiByteToWideChar(CP_ACP, 0, strAnsi.data(), (int)strAnsi.size(), strValue.GetBuffer(nSize), nSize);
|
||||||
|
strValue.ReleaseBuffer(nSize);
|
||||||
|
return strValue;
|
||||||
|
#else
|
||||||
|
return CString(strAnsi.c_str(), (int)strAnsi.size());
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static BOOL IsLikelyValidUtf8(const std::string& strValue)
|
||||||
|
{
|
||||||
|
size_t i = 0;
|
||||||
|
while (i < strValue.size())
|
||||||
|
{
|
||||||
|
unsigned char ch = (unsigned char)strValue[i];
|
||||||
|
if (ch <= 0x7F)
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int nBytes = 0;
|
||||||
|
if ((ch & 0xE0) == 0xC0)
|
||||||
|
{
|
||||||
|
nBytes = 2;
|
||||||
|
}
|
||||||
|
else if ((ch & 0xF0) == 0xE0)
|
||||||
|
{
|
||||||
|
nBytes = 3;
|
||||||
|
}
|
||||||
|
else if ((ch & 0xF8) == 0xF0)
|
||||||
|
{
|
||||||
|
nBytes = 4;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i + (size_t)nBytes > strValue.size())
|
||||||
|
{
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int j = 1; j < nBytes; ++j)
|
||||||
|
{
|
||||||
|
if ((strValue[i + j] & 0xC0) != 0x80)
|
||||||
|
{
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
i += (size_t)nBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static CString InfoValueToCString(const std::string& strValue)
|
||||||
|
{
|
||||||
|
if (IsLikelyValidUtf8(strValue))
|
||||||
|
{
|
||||||
|
return Utf8ToCString(strValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
return AnsiToCString(strValue);
|
||||||
|
}
|
||||||
|
|
||||||
static std::string UrlEncodeUtf8(const std::string& strValue)
|
static std::string UrlEncodeUtf8(const std::string& strValue)
|
||||||
{
|
{
|
||||||
static const char* kHex = "0123456789ABCDEF";
|
static const char* kHex = "0123456789ABCDEF";
|
||||||
@@ -373,7 +475,7 @@ static std::unique_ptr<httplib::Client> CreatePmcClient(LPCTSTR lpBaseUrl)
|
|||||||
static void ApplyInfoKeyValue(UploadInfoConfig& config, const std::string& strKey, const std::string& strRawValue)
|
static void ApplyInfoKeyValue(UploadInfoConfig& config, const std::string& strKey, const std::string& strRawValue)
|
||||||
{
|
{
|
||||||
std::string strValue = UnescapeInfoValue(strRawValue);
|
std::string strValue = UnescapeInfoValue(strRawValue);
|
||||||
CString strCStringValue = Utf8ToCString(strValue);
|
CString strCStringValue = InfoValueToCString(strValue);
|
||||||
|
|
||||||
if (strKey == "url")
|
if (strKey == "url")
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user