twain3.0/huagao/Device/filetools.h

336 lines
8.0 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <vector>
#include <io.h>
#include <fstream>
#include <Windows.h>
#include <TlHelp32.h>
#include <timeapi.h>
#include <time.h>
#include <fcntl.h>
#include <codecvt>
#include <locale>
#include <log4cplus/log4cplus.h>
#include "PublicFunc.h"
#define enum2str(R) #R
enum log_lv :int {
log_TRACE = 0,
log_DEBUG = 10000,
log_INFO = 20000,
log_WARN = 30000,
log_ERROR = 40000,
log_FATAL = 50000,
};
class FileTools
{
public:
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;
}
static std::vector<std::string> getFiles(std::string path)
{
std::vector<std::string> files;
getFiles(path, files);
return files;
}
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);
}
static void write_log(std::string filename, std::string log)
{
std::ofstream ofs(filename, std::ios::app);
time_t timp;
tm* p;
time(&timp);
p=localtime(&timp);
ofs << p->tm_year+1900 << "/" << p->tm_mon+1 << "/" << p->tm_mday << " " << p->tm_hour << ":" << p->tm_min << ":" << p->tm_sec << " "<<log << std::endl;
}
static void writelog(std::wstring path, int lv, std::string log)
{
log4cplus::SharedAppenderPtr rf(new log4cplus::RollingFileAppender(
path,
1024 * 1024 * 10,
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);
}
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;
}
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;
}
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;
}
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;
}
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
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);
}
}
};