twain3.0/huagao/Device/filetools.h

201 lines
5.1 KiB
C++
Raw 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 <timeapi.h>
#include <time.h>
#include <fcntl.h>
#include <log4cplus/log4cplus.h>
#include "PublicFunc.h"
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::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,
2048 * 1024,
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 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;
}
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);
}
}
};