acl/app/jencode/DelBOM.cpp

115 lines
2.6 KiB
C++
Raw Normal View History

#include "StdAfx.h"
2014-11-19 00:25:21 +08:00
#include "DelBom.h"
CDelBOM::CDelBOM(void)
: m_nMsgDeleting(0)
, m_nMsgDeleted(0)
, m_hWnd(0)
, m_sPath(_T(""))
{
}
CDelBOM::~CDelBOM(void)
{
}
void CDelBOM::Init(HWND hWnd, CString &sPath)
{
2019-07-26 16:09:45 +08:00
m_hWnd = hWnd;
2014-11-19 00:25:21 +08:00
m_sPath = sPath;
}
void CDelBOM::OnDeleting(int nMsg)
{
m_nMsgDeleting = nMsg;
}
void CDelBOM::OnDeleted(int nMsg)
{
m_nMsgDeleted = nMsg;
}
2019-07-26 16:09:45 +08:00
void* CDelBOM::run(void)
2014-11-19 00:25:21 +08:00
{
2019-07-26 16:09:45 +08:00
ScanDel();
::PostMessage(m_hWnd, m_nMsgDeleted, 0, 0);
return NULL;
}
2014-11-19 00:25:21 +08:00
2019-07-26 16:09:45 +08:00
void CDelBOM::ScanDel(void)
{
if (m_sPath.GetLength() == 0) {
MessageBox(NULL, "<EFBFBD>ļ<EFBFBD>·<EFBFBD><EFBFBD>Ϊ<EFBFBD>գ<EFBFBD>", "Error", 0);
2019-07-26 16:09:45 +08:00
return;
2014-11-19 00:25:21 +08:00
}
2019-07-26 16:09:45 +08:00
acl::scan_dir scan;
if (!scan.open(m_sPath.GetString())) {
MessageBox(NULL, "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>·<EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>", "Error", 0);
2019-07-26 16:09:45 +08:00
return;
2014-11-19 00:25:21 +08:00
}
2019-07-26 16:09:45 +08:00
logger("open %s and start scanning for deleting BOM ...",
m_sPath.GetString());
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 16:09:45 +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")) {
DeleteBOM(path);
} else {
logger(">>skip file: %s", pFile);
}
2014-11-19 00:25:21 +08:00
}
2019-07-26 16:09:45 +08:00
logger("%s: all text files have been deleted BOM!", m_sPath.GetString());
2014-11-19 00:25:21 +08:00
}
2019-07-26 16:09:45 +08:00
bool CDelBOM::DeleteBOM(const acl::string& filePath)
2014-11-19 00:25:21 +08:00
{
2019-07-26 16:09:45 +08:00
acl::string buf;
if (!acl::ifstream::load(filePath.c_str(), &buf)) {
logger_error("load from %s error %s",
filePath.c_str(), acl::last_serror());
return false;
}
if (buf.size() < 3) {
return false;
2014-11-19 00:25:21 +08:00
}
// <20><><EFBFBD>ж<EFBFBD><D0B6>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ǰ׺<C7B0>Ƿ<EFBFBD><C7B7><EFBFBD>BOM<4F><4D>ʽ
2019-07-26 16:09:45 +08:00
if (buf[0] != (char) 0xEF || buf[1] != (char) 0xBB
|| buf[2] != (char) 0xBF) {
2014-11-19 00:25:21 +08:00
2019-07-26 16:09:45 +08:00
return false;
2014-11-19 00:25:21 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>ƫ<EFBFBD><C6AB> 3 <20><><EFBFBD>ֽڣ<D6BD><DAA3><EFBFBD>ȥ<EFBFBD><C8A5>BOM<4F><4D>ʽ
2019-07-26 16:09:45 +08:00
char* ptr = buf.c_str() + 3;
acl::ofstream fp;
if (!fp.open_write(filePath.c_str(), true)) {
logger_error("open %s error %s for write",
filePath.c_str(), acl::last_serror());
return false;
}
size_t len = buf.size() - 3;
if (fp.write(ptr, len) != len) {
logger_error("write to %s error %s",
filePath.c_str(), acl::last_serror());
return false;
}
return true;
2014-11-19 00:25:21 +08:00
}