acl/app/jencode/Gb2Utf8.cpp

116 lines
2.8 KiB
C++
Raw Normal View History

#include "StdAfx.h"
2014-11-19 00:25:21 +08:00
#include "Gb2Utf8.h"
CGb2Utf8::CGb2Utf8(const char* fromCharset, const char* toCharset)
: m_hWnd(0)
, m_sPath(_T(""))
, m_dPath(_T(""))
, m_fromCharset(fromCharset)
, m_toCharset(toCharset)
{
}
CGb2Utf8::~CGb2Utf8(void)
{
}
CGb2Utf8::CGb2Utf8(HWND hWnd, CString &sPath, CString &dPath)
{
Init(hWnd, sPath, dPath);
}
void CGb2Utf8::Init(HWND hWnd, CString &sPath, CString &dPath)
{
2019-07-26 17:27:39 +08:00
m_hWnd = hWnd;
2014-11-19 00:25:21 +08:00
m_sPath = sPath;
m_dPath = dPath;
}
void CGb2Utf8::OnTransing(int nMsg)
{
m_nMsgTransing = nMsg;
}
void CGb2Utf8::OnTransEnd(int nMsg)
{
m_nMsgTransEnd = nMsg;
}
2019-07-26 17:27:39 +08:00
bool CGb2Utf8::TransformPath(const char *path_from, const char *path_to)
2014-11-19 00:25:21 +08:00
{
2019-07-26 17:27:39 +08:00
acl::scan_dir scan;
if (!scan.open(path_from)) {
2014-11-19 00:25:21 +08:00
CString msg;
2019-07-26 17:27:39 +08:00
msg.Format("Open src path %s error", path_from);
2014-11-19 00:25:21 +08:00
MessageBox(NULL, msg, "Open path", 0);
2019-07-26 17:27:39 +08:00
return false;
2014-11-19 00:25:21 +08:00
}
2019-07-26 17:27:39 +08:00
const char* pFile;
while ((pFile = scan.next_file(true)) != NULL) {
acl::string path(pFile);
2019-07-26 18:30:38 +08:00
if (path.end_with("resource.h", false)) {
logger(">>skip file: %s", pFile);
} else if (path.end_with(".c") || path.end_with(".h") ||
2019-07-26 17:27:39 +08:00
path.end_with(".cpp") || path.end_with(".cxx") ||
path.end_with(".hpp") || path.end_with(".hxx") ||
path.end_with(".java") || path.end_with(".txt") ||
path.end_with(".php") || path.end_with(".html") ||
path.end_with(".js") || path.end_with(".css") ||
path.end_with(".d") || path.end_with(".py") ||
path.end_with(".perl") || path.end_with(".cs") ||
path.end_with(".as") || path.end_with(".go") ||
path.end_with(".rust") || path.end_with(".erl")) {
TransformFile(pFile, pFile);
} else {
logger(">>skip file: %s", pFile);
}
2014-11-19 00:25:21 +08:00
}
2019-07-26 17:27:39 +08:00
return true;
2014-11-19 00:25:21 +08:00
}
2019-07-26 17:27:39 +08:00
bool CGb2Utf8::TransformFile(const char *pFrom, const char *pTo)
2014-11-19 00:25:21 +08:00
{
2019-07-26 17:27:39 +08:00
acl::string sBuf;
if (!acl::ifstream::load(pFrom, sBuf)) {
logger_error("load from %s error %s", pFrom, acl::last_serror());
return false;
}
2014-11-19 00:25:21 +08:00
acl::charset_conv conv;
2019-07-26 17:27:39 +08:00
acl::string tBuf;
if (!conv.convert(m_fromCharset.GetString(), m_toCharset.GetString(),
sBuf.c_str(), sBuf.size(), &tBuf)) {
logger_error("conver from %s to %s error: %s, file: %s",
2014-11-19 00:25:21 +08:00
m_fromCharset.GetString(), m_toCharset.GetString(),
conv.serror(), pFrom);
2019-07-26 17:27:39 +08:00
return false;
2014-11-19 00:25:21 +08:00
}
2019-07-26 17:27:39 +08:00
acl::ofstream fp;
if (!fp.open_write(pTo, true)) {
logger_error("open %s error %s", pTo, acl::last_serror());
return false;
}
2019-07-26 17:27:39 +08:00
if (fp.write(tBuf) != (int) tBuf.size()) {
logger_error("write to %s error %s", pTo, acl::last_serror());
} else {
logger("transer from %s to %s ok, file: %s",
2019-07-26 17:27:39 +08:00
m_fromCharset.GetString(), m_toCharset.GetString(), pTo);
}
return true;
2014-11-19 00:25:21 +08:00
}
2019-07-26 17:27:39 +08:00
void *CGb2Utf8::run(void)
2014-11-19 00:25:21 +08:00
{
2019-07-26 17:27:39 +08:00
if (m_dPath.IsEmpty() || m_sPath.IsEmpty()) {
return NULL;
}
TransformPath(m_sPath, m_dPath);
::PostMessage(m_hWnd, m_nMsgTransEnd, 0, 0);
2014-11-19 00:25:21 +08:00
return NULL;
}