2020-03-11 01:58:06 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <io.h>
|
|
|
|
|
#include <fstream>
|
2022-05-24 12:05:47 +00:00
|
|
|
|
#include <Windows.h>
|
|
|
|
|
#include <TlHelp32.h>
|
2021-06-21 11:38:36 +00:00
|
|
|
|
#include <timeapi.h>
|
2020-03-11 01:58:06 +00:00
|
|
|
|
#include <time.h>
|
2021-06-21 11:38:36 +00:00
|
|
|
|
#include <fcntl.h>
|
2023-04-24 11:30:39 +00:00
|
|
|
|
#include <codecvt>
|
|
|
|
|
#include <locale>
|
2021-05-13 10:13:08 +00:00
|
|
|
|
#include <log4cplus/log4cplus.h>
|
2020-12-09 08:02:51 +00:00
|
|
|
|
#include "PublicFunc.h"
|
2020-03-11 01:58:06 +00:00
|
|
|
|
|
2021-11-20 03:09:04 +00:00
|
|
|
|
#define enum2str(R) #R
|
2021-05-13 10:13:08 +00:00
|
|
|
|
|
2021-04-13 09:30:05 +00:00
|
|
|
|
enum log_lv :int {
|
|
|
|
|
log_TRACE = 0,
|
|
|
|
|
log_DEBUG = 10000,
|
|
|
|
|
log_INFO = 20000,
|
|
|
|
|
log_WARN = 30000,
|
|
|
|
|
log_ERROR = 40000,
|
|
|
|
|
log_FATAL = 50000,
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-13 10:13:08 +00:00
|
|
|
|
|
2020-03-11 01:58:06 +00:00
|
|
|
|
class FileTools
|
|
|
|
|
{
|
|
|
|
|
public:
|
2023-04-24 11:30:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static std::string GetProcessName()
|
|
|
|
|
{
|
|
|
|
|
char Path[_MAX_PATH]{ 0 };
|
|
|
|
|
char name[_MAX_PATH]{ 0 };
|
|
|
|
|
::GetModuleFileNameA(NULL, Path, _MAX_PATH);
|
|
|
|
|
::_splitpath(Path, NULL, NULL, name, NULL);
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-11 01:58:06 +00:00
|
|
|
|
static std::vector<std::string> getFiles(std::string path)
|
|
|
|
|
{
|
|
|
|
|
std::vector<std::string> files;
|
|
|
|
|
getFiles(path, files);
|
|
|
|
|
return files;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-09 08:02:51 +00:00
|
|
|
|
static void write_log(std::string log)
|
|
|
|
|
{
|
|
|
|
|
TCHAR szIniFile[MAX_PATH] = { 0 };
|
|
|
|
|
SHGetSpecialFolderPath(NULL, szIniFile, CSIDL_LOCAL_APPDATA, TRUE);
|
|
|
|
|
_tcscat(szIniFile, HUAGAO_SCAN);
|
|
|
|
|
_tcscat(szIniFile, TWAIN_INIPATH);
|
|
|
|
|
_tcscat(szIniFile, TEXT("\\"));
|
|
|
|
|
_tcscat(szIniFile, TWAIN_LOG_NAME);
|
|
|
|
|
std::string savepath = TCHAR2STRING(szIniFile);
|
|
|
|
|
write_log(savepath, log);
|
|
|
|
|
}
|
2021-05-13 10:13:08 +00:00
|
|
|
|
|
2020-03-11 01:58:06 +00:00
|
|
|
|
static void write_log(std::string filename, std::string log)
|
|
|
|
|
{
|
2020-06-20 03:15:56 +00:00
|
|
|
|
std::ofstream ofs(filename, std::ios::app);
|
2020-03-11 01:58:06 +00:00
|
|
|
|
time_t timp;
|
2021-01-22 07:18:41 +00:00
|
|
|
|
tm* p;
|
2020-03-11 01:58:06 +00:00
|
|
|
|
time(&timp);
|
|
|
|
|
p=localtime(&timp);
|
2020-12-09 08:02:51 +00:00
|
|
|
|
ofs << p->tm_year+1900 << "/" << p->tm_mon+1 << "/" << p->tm_mday << " " << p->tm_hour << ":" << p->tm_min << ":" << p->tm_sec << " "<<log << std::endl;
|
2020-03-11 01:58:06 +00:00
|
|
|
|
}
|
2021-04-10 02:36:03 +00:00
|
|
|
|
|
2021-05-13 10:13:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void writelog(std::wstring path, int lv, std::string log)
|
|
|
|
|
{
|
|
|
|
|
log4cplus::SharedAppenderPtr rf(new log4cplus::RollingFileAppender(
|
|
|
|
|
path,
|
2022-08-10 03:10:20 +00:00
|
|
|
|
1024 * 1024 * 10,
|
2021-05-13 10:13:08 +00:00
|
|
|
|
5
|
|
|
|
|
));
|
|
|
|
|
rf->setName(LOG4CPLUS_TEXT("file"));
|
|
|
|
|
log4cplus::tstring pattern = LOG4CPLUS_TEXT("%D{%m/%d/%y %H:%M:%S,%Q} [%t] %-5p %c - %m %n");
|
|
|
|
|
rf->setLayout(std::unique_ptr<log4cplus::Layout>(new log4cplus::PatternLayout(pattern)));
|
|
|
|
|
|
|
|
|
|
log4cplus::Logger logger = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("hglog"));
|
|
|
|
|
logger.setLogLevel(lv);
|
|
|
|
|
logger.addAppender(rf);
|
|
|
|
|
switch (lv)
|
|
|
|
|
{
|
|
|
|
|
case log_lv::log_TRACE:
|
|
|
|
|
LOG4CPLUS_TRACE(logger, log.c_str()); break;
|
|
|
|
|
case log_lv::log_DEBUG:
|
|
|
|
|
LOG4CPLUS_DEBUG(logger, log.c_str()); break;
|
|
|
|
|
case log_lv::log_INFO:
|
|
|
|
|
LOG4CPLUS_INFO(logger, log.c_str()); break;
|
|
|
|
|
case log_lv::log_WARN:
|
|
|
|
|
LOG4CPLUS_WARN(logger, log.c_str()); break;
|
|
|
|
|
case log_lv::log_ERROR:
|
|
|
|
|
LOG4CPLUS_ERROR(logger, log.c_str()); break;
|
|
|
|
|
case log_lv::log_FATAL:
|
|
|
|
|
LOG4CPLUS_FATAL(logger, log.c_str()); break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
logger.removeAllAppenders();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void writelog(int lv, std::string log)
|
|
|
|
|
{
|
|
|
|
|
TCHAR szIniFile[MAX_PATH] = { 0 };
|
|
|
|
|
SHGetSpecialFolderPath(NULL, szIniFile, CSIDL_LOCAL_APPDATA, TRUE);
|
|
|
|
|
_tcscat(szIniFile, HUAGAO_SCAN);
|
|
|
|
|
_tcscat(szIniFile, TWAIN_INIPATH);
|
|
|
|
|
_tcscat(szIniFile, TEXT("\\"));
|
|
|
|
|
_tcscat(szIniFile, TWAIN_LOG_NAME);
|
|
|
|
|
writelog(std::wstring(szIniFile), lv, log);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void writedebuglog(std::string log)
|
|
|
|
|
{
|
|
|
|
|
TCHAR szIniFile[MAX_PATH] = { 0 };
|
|
|
|
|
SHGetSpecialFolderPath(NULL, szIniFile, CSIDL_LOCAL_APPDATA, TRUE);
|
|
|
|
|
_tcscat(szIniFile, HUAGAO_SCAN);
|
|
|
|
|
_tcscat(szIniFile, TWAIN_INIPATH);
|
|
|
|
|
_tcscat(szIniFile, TEXT("\\"));
|
|
|
|
|
_tcscat(szIniFile, TWAIN_LOG_NAME);
|
|
|
|
|
writelog(std::wstring(szIniFile), log_lv::log_DEBUG, log);
|
|
|
|
|
}
|
|
|
|
|
static void writeerrorlog(std::string log)
|
|
|
|
|
{
|
|
|
|
|
TCHAR szIniFile[MAX_PATH] = { 0 };
|
|
|
|
|
SHGetSpecialFolderPath(NULL, szIniFile, CSIDL_LOCAL_APPDATA, TRUE);
|
|
|
|
|
_tcscat(szIniFile, HUAGAO_SCAN);
|
|
|
|
|
_tcscat(szIniFile, TWAIN_INIPATH);
|
|
|
|
|
_tcscat(szIniFile, TEXT("\\"));
|
|
|
|
|
_tcscat(szIniFile, TWAIN_LOG_NAME);
|
|
|
|
|
writelog(std::wstring(szIniFile), log_lv::log_ERROR, log);
|
|
|
|
|
}
|
2021-06-21 11:38:36 +00:00
|
|
|
|
|
|
|
|
|
static std::string get_appdata_path()
|
|
|
|
|
{
|
|
|
|
|
TCHAR szIniFile[MAX_PATH] = { 0 };
|
|
|
|
|
SHGetSpecialFolderPath(NULL, szIniFile, CSIDL_LOCAL_APPDATA, TRUE);
|
|
|
|
|
_tcscat(szIniFile, HUAGAO_SCAN);
|
|
|
|
|
_tcscat(szIniFile, L"\\temp\\");
|
|
|
|
|
std::string path= TCHAR2STRING(szIniFile);
|
|
|
|
|
if (!isFolderExist(path.data()))
|
|
|
|
|
createDirectory(path.data());
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 12:05:47 +00:00
|
|
|
|
static std::wstring get_errorlog_path_w()
|
|
|
|
|
{
|
|
|
|
|
TCHAR szIniFile[MAX_PATH] = { 0 };
|
|
|
|
|
SHGetSpecialFolderPath(NULL, szIniFile, CSIDL_LOCAL_APPDATA, TRUE);
|
|
|
|
|
_tcscat(szIniFile, HUAGAO_SCAN);
|
|
|
|
|
_tcscat(szIniFile, TWAIN_ERROR_PATH);
|
|
|
|
|
auto path = TCHAR2STRING(szIniFile);
|
|
|
|
|
if (!isFolderExist(path.data()))
|
|
|
|
|
createDirectory(path.data());
|
|
|
|
|
return std::wstring(szIniFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static std::string get_errorlog_path()
|
|
|
|
|
{
|
|
|
|
|
TCHAR szIniFile[MAX_PATH] = { 0 };
|
|
|
|
|
SHGetSpecialFolderPath(NULL, szIniFile, CSIDL_LOCAL_APPDATA, TRUE);
|
|
|
|
|
_tcscat(szIniFile, HUAGAO_SCAN);
|
|
|
|
|
_tcscat(szIniFile, TWAIN_ERROR_PATH);
|
|
|
|
|
auto path = TCHAR2STRING(szIniFile);
|
|
|
|
|
if (!isFolderExist(path.data()))
|
|
|
|
|
createDirectory(path.data());
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 11:38:36 +00:00
|
|
|
|
static void deletedir(CString szPath)
|
|
|
|
|
{
|
|
|
|
|
CFileFind ff;
|
|
|
|
|
if (szPath.Right(1) != "\\")
|
|
|
|
|
szPath += "\\";
|
|
|
|
|
szPath += "*.*";
|
|
|
|
|
BOOL res = ff.FindFile(szPath);
|
|
|
|
|
while (res)
|
|
|
|
|
{
|
|
|
|
|
res = ff.FindNextFile();
|
|
|
|
|
auto x = ff.GetFilePath();
|
|
|
|
|
if (!ff.IsDots() && !ff.IsDirectory())//<2F><><EFBFBD>ļ<EFBFBD>ʱֱ<CAB1><D6B1>ɾ<EFBFBD><C9BE>
|
|
|
|
|
DeleteFile(ff.GetFilePath());
|
|
|
|
|
else if (ff.IsDots())
|
|
|
|
|
continue;
|
|
|
|
|
else if (ff.IsDirectory())
|
|
|
|
|
{
|
|
|
|
|
szPath = ff.GetFilePath();
|
|
|
|
|
deletedir(szPath.GetBuffer());//<2F><>Ŀ¼ʱ<C2BC><CAB1><EFBFBD><EFBFBD><EFBFBD>ݹ飬ɾ<E9A3AC><C9BE><EFBFBD><EFBFBD>Ŀ¼<C4BF>µ<EFBFBD><C2B5>ļ<EFBFBD>
|
|
|
|
|
RemoveDirectory(szPath);//Ŀ¼Ϊ<C2BC>պ<EFBFBD>ɾ<EFBFBD><C9BE>Ŀ¼
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
RemoveDirectory(szPath);//<2F><><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD>Ŀ¼
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static unsigned long get_file_size(const char* path)
|
|
|
|
|
{
|
|
|
|
|
unsigned long filesize = -1;
|
|
|
|
|
struct stat statbuff;
|
|
|
|
|
if (stat(path, &statbuff) < 0) {
|
|
|
|
|
return filesize;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
filesize = statbuff.st_size;
|
|
|
|
|
}
|
|
|
|
|
return filesize;
|
|
|
|
|
}
|
2022-05-24 12:05:47 +00:00
|
|
|
|
|
|
|
|
|
static bool kill_process(WCHAR* lpszProcessName)
|
|
|
|
|
{
|
|
|
|
|
unsigned int pid = -1;
|
|
|
|
|
bool retval = true;
|
|
|
|
|
|
|
|
|
|
if (lpszProcessName == NULL)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
DWORD dwRet = 0;
|
|
|
|
|
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
|
|
|
|
PROCESSENTRY32 processInfo;
|
|
|
|
|
processInfo.dwSize = sizeof(PROCESSENTRY32);
|
|
|
|
|
int flag = Process32First(hSnapshot, &processInfo);
|
|
|
|
|
|
|
|
|
|
while (flag != 0)
|
|
|
|
|
{
|
|
|
|
|
if (StrCmpCW(processInfo.szExeFile, lpszProcessName) == 0)
|
|
|
|
|
{
|
|
|
|
|
pid = processInfo.th32ProcessID;
|
|
|
|
|
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
|
|
|
|
|
|
|
|
|
|
if (TerminateProcess(hProcess, 0) != TRUE)
|
|
|
|
|
{
|
|
|
|
|
retval = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flag = Process32Next(hSnapshot, &processInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CloseHandle(hSnapshot);
|
|
|
|
|
|
|
|
|
|
if (pid == -1)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
2023-04-24 11:30:39 +00:00
|
|
|
|
static std::string to_web_utf(const std::wstring& unic)
|
|
|
|
|
{
|
|
|
|
|
std::string webu("");
|
|
|
|
|
char buf[20] = { 0 };
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < unic.length(); ++i)
|
|
|
|
|
{
|
|
|
|
|
wchar_t v = unic[i];
|
|
|
|
|
//if (v <= 0x7f)
|
|
|
|
|
// webu.append(1, (char)v);
|
|
|
|
|
//else
|
|
|
|
|
{
|
|
|
|
|
sprintf_s(buf, _countof(buf) - 1, "%04X", v);
|
|
|
|
|
webu += buf;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return webu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static std::wstring String2Wstring(std::string wstr)
|
|
|
|
|
{
|
|
|
|
|
std::wstring res;
|
|
|
|
|
int len = MultiByteToWideChar(CP_ACP, 0, wstr.c_str(), wstr.size(), nullptr, 0);
|
|
|
|
|
if (len < 0) {
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
wchar_t* buffer = new wchar_t[len + 1];
|
|
|
|
|
if (buffer == nullptr) {
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len);
|
|
|
|
|
buffer[len] = '\0';
|
|
|
|
|
res.append(buffer);
|
|
|
|
|
delete[] buffer;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static std::string utf82str(std::string str)
|
|
|
|
|
{
|
|
|
|
|
std::string u8;
|
|
|
|
|
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
|
|
|
|
|
for (size_t i = 0; i < str.length();)
|
|
|
|
|
{
|
|
|
|
|
char32_t c = strtol(str.substr(i, 4).c_str(), nullptr, 16);
|
|
|
|
|
u8 += converter.to_bytes(c);
|
|
|
|
|
i += 4;
|
|
|
|
|
}
|
|
|
|
|
return u8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<2F><>stringת<67><D7AA><EFBFBD><EFBFBD>wstring
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-03-11 01:58:06 +00:00
|
|
|
|
private:
|
|
|
|
|
static void getFiles(std::string path, std::vector<std::string>& files)
|
|
|
|
|
{
|
|
|
|
|
//<2F>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
long hFile = 0;
|
|
|
|
|
//<2F>ļ<EFBFBD><C4BC><EFBFBD>Ϣ
|
|
|
|
|
struct _finddata_t fileinfo;
|
|
|
|
|
std::string p;
|
|
|
|
|
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo))!=-1)
|
|
|
|
|
{
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼,<2C><><EFBFBD><EFBFBD>֮
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
|
|
|
|
|
if ((fileinfo.attrib & _A_SUBDIR))
|
|
|
|
|
{
|
|
|
|
|
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
|
|
|
|
|
getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
|
|
|
|
|
}
|
|
|
|
|
} while (_findnext(hFile, &fileinfo) == 0);
|
|
|
|
|
_findclose(hFile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|