You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

532 lines
15 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "StdAfx.h"
#include "FileMgr.h"
#include "LogMgr.h"
#include "GlobalDefine.h"
#include "GlobalFunction.h"
#if 1
void CCsvData::AddData(CString Str,bool bLineEnd)
{
m_StrVecTemp.push_back(Str);
if(bLineEnd)
{
m_DataVec.push_back(m_StrVecTemp);
m_StrVecTemp.clear();
}
}
#endif
CFileMgr::CFileMgr(void)
{
}
CFileMgr::~CFileMgr(void)
{
}
//以当前程序目录为基础,获取文件的完整路径(放在FilePath 中)
void CFileMgr::GetFullFilePath(char *FilePath,CString FileName)
{
CString str;
GetFullFilePath(str,FileName);
strcpy(FilePath,(LPSTR)(LPCTSTR)str);
}
//以当前程序目录为基础,获取文件的完整路径(FileName 格式类似于\\Debug.txt)
void CFileMgr::GetFullFilePath(CString &FilePath,CString FileName)
{
CString sPath;
GetModuleFileName(NULL,sPath.GetBufferSetLength(1023),1024);
sPath.ReleaseBuffer();
int nPos;
nPos = sPath.ReverseFind('\\');
sPath = sPath.Left(nPos);
FilePath = sPath + FileName;
}
//获取当前工作目录
CString CFileMgr::GetWorkPath()
{
CString sPath;
GetModuleFileName(NULL,sPath.GetBufferSetLength(1023),1024);
sPath.ReleaseBuffer();
int nPos;
nPos = sPath.ReverseFind('\\');
sPath = sPath.Left(nPos);
return sPath;
}
#if 1
#define Max_ReadFile_Times 5 //最大尝试读取文件的次数,避免有时候打开文件出错
//按行读取FilePath 指定的文件保存到vec 中(FilePath 是程序根目录下的文件路径)
void CFileMgr::ReadFileToStringVec(const CString &FilePath,vector<CString> &vec)
{
for(int k=0;k<Max_ReadFile_Times;k++)
{
CStdioFile file;
if(file.Open(FilePath,CFile::modeRead,NULL))
{
CString str;
while(file.ReadString(str))
{
vec.push_back(str);
}
file.Close();
break;
}
else
{
CString Log = "File Open Err";
Log += FilePath;
gLogMgr->WriteDebugLog(Log);
Sleep(1000);
}
}
}
//读取文件的每一行,以逗号分离数值
void CFileMgr::ReadFileToDoubleVec(const CString &FilePath,vector<vector<double>> &Vec)
{
//提取每一行字符串
vector<CString> StrVec;
ReadFileToStringVec(FilePath,StrVec);
vector<CString>::iterator iter = StrVec.begin();
vector<CString>::iterator iter_end = StrVec.end();
for(;iter!=iter_end;iter++)
{
vector<double> VecTmp;
//从str 中提取double 数值,以逗号作为分隔符
GetDoubleVal((*iter),VecTmp);
if(VecTmp.empty()==false)
Vec.push_back(VecTmp);
}
}
//从str 中提取double 数值,以逗号作为分隔符
void CFileMgr::GetDoubleVal(CString Str,vector<double> &Vec)
{
int CommaPosPer;//第一个逗号的位置
CommaPosPer = Str.Find(",",0);
if(CommaPosPer == -1)//没找到直接返回
{
return;
}
char *recvBuf = CStringToLPCSTR(Str);
//第一个值
double val = 0;
CString Str1(recvBuf,CommaPosPer);
val= atof(Str1);
Vec.push_back(val);
int idx = 0;
int CommaPosNext = CommaPosPer;
do
{
CommaPosNext = Str.Find(",",CommaPosPer+1);//下一个逗号的位置
if(CommaPosNext==-1)//最后一个
{
CString Str1(recvBuf+CommaPosPer+1);
val= atof(Str1);
}
else
{
CString Str1(recvBuf+CommaPosPer+1,CommaPosNext-CommaPosPer);
val= atof(Str1);
}
Vec.push_back(val);
CommaPosPer = CommaPosNext;
idx++;
}while(CommaPosNext!=-1);
}
//将一个坐标容器写入到文本文件
void CFileMgr::WriteDbxyVecToFile(vector<Dbxy> &vec,CString Path)
{
ofstream file;
file.open(Path);
vector<Dbxy>::iterator iter = vec.begin();
vector<Dbxy>::iterator iter_end = vec.end();
for(;iter!=iter_end;iter++)
{
file<<(*iter).x<<","<<(*iter).y<<endl;
}
}
#endif
#if 1
int CFileMgr::ReadFileToStrVec(const CString &FilePath,vector<vector<CString>> &Vec,bool bGapTab)
{
int EmptyLine = -1;
//提取每一行字符串
vector<CString> StrVec;
ReadFileToStringVec(FilePath,StrVec);
vector<CString>::iterator iter = StrVec.begin();
vector<CString>::iterator iter_end = StrVec.end();
for(int k=1;iter!=iter_end;iter++,k++)
{
vector<CString> VecTmp;
//从str 中提取double 数值,以逗号作为分隔符
GetStringVal((*iter),VecTmp,bGapTab);
if(VecTmp.empty()==false)
{
Vec.push_back(VecTmp);
}
else
{
EmptyLine = k;
}
}
return EmptyLine;
}
//从str 中提取val 值,以逗号作为分隔符
void CFileMgr::GetStringVal(CString Str,vector<CString> &Vec,bool bGapTab)
{
int CommaPosPer;//第一个逗号的位置
if(bGapTab)
CommaPosPer = Str.Find(" ",0);
else
CommaPosPer = Str.Find(",",0);
if(CommaPosPer == -1)//没找到直接返回
{
return;
}
char *recvBuf = CStringToLPCSTR(Str);
//第一个值
char gap = ',';
if(bGapTab)
gap = '\t';
CString Str1(recvBuf,CommaPosPer);
Str1.Remove(gap);//删除逗号
Vec.push_back(Str1);
int idx = 0;
int CommaPosNext = CommaPosPer;
do
{
CString val;
CommaPosNext = Str.Find(gap,CommaPosPer+1);//下一个逗号的位置
if(CommaPosNext==-1)//最后一个
{
CString Str1(recvBuf+CommaPosPer+1);
val = (Str1);
}
else
{
CString Str1(recvBuf+CommaPosPer+1,CommaPosNext-CommaPosPer);
val = (Str1);
}
val.Remove(gap);//删除逗号
Vec.push_back(val);
CommaPosPer = CommaPosNext;
idx++;
}while(CommaPosNext!=-1);
}
#endif
//检查文件是否存在(绝对路径)
bool CFileMgr::IsFileExist(CString FilePath)
{
CFileFind finder;
bool bResult = finder.FindFile(FilePath);
finder.Close();
return bResult;
}
//目录是否存在
bool CFileMgr::IsDirectoryExists(CString const& path)
{
//判断是否存在
if(!PathFileExists(path))
return false;
//判断是否为目录
DWORD attributes = ::GetFileAttributes(path);
attributes &= FILE_ATTRIBUTE_DIRECTORY;
return attributes == FILE_ATTRIBUTE_DIRECTORY;
}
//获取文件夹下所有子文件/子文件的路径(Suffix 为指定的后缀名)
void CFileMgr::GetChildFileOrDirName(bool bDir,CString strPath,vector <CString>& Vec,CString Suffix)
{
CString strFilePath;
int dwDirSize = 0;
strFilePath += strPath;
strFilePath += "//*.*";
CFileFind finder;
BOOL bFind = finder.FindFile(strFilePath);
while (bFind)
{
bFind = finder.FindNextFile();
if (!finder.IsDots())
{
CString strTempPath = finder.GetFilePath();
if(bDir && finder.IsDirectory())
{
Vec.push_back(strTempPath);
}
else if(!bDir)
{
if(Suffix != "")//指定了后缀
{
if(strTempPath.Find(Suffix)!=-1)
Vec.push_back(strTempPath);
}
else
{
Vec.push_back(strTempPath);
}
}
}
}
finder.Close();
}
//删除指定目录下的所有文件
void CFileMgr::DeleteDirFile(CString FilePath)
{
CString DelStr("del /s/q ");
DelStr += FilePath;
system(DelStr);
}
//删除目录
void CFileMgr::DeleteDir(CString DirPath)
{
CString DelStr("rmdir ");
DelStr += DirPath;
system(DelStr);
}
//创建目录(只用给一个整的目录,会把一系列目录都创建)
void CFileMgr::CreatDir(CString DirPath)
{
if(!IsDirectoryExists(DirPath))
{
//CString MkdirCmd("mkdir ");
//MkdirCmd += DirPath;
//system(MkdirCmd);
CreateMultipleDirectory(DirPath);
}
}
//创建多级目录
bool CFileMgr::CreateMultipleDirectory(const CString& szPath)
{
CString log="CreateMultipleDirectory->";
log += szPath;
gLogMgr->WriteDebugLog(log);
CString strDir(szPath);//存放要创建的目录字符串
//确保以'\'结尾以创建最后一个目录
if (strDir.GetAt(strDir.GetLength()-1)!=_T('\\'))
{
strDir.AppendChar(_T('\\'));
}
std::vector<CString> vPath;//存放每一层目录字符串
CString strTemp;//一个临时变量,存放目录字符串
bool bSuccess = false;//成功标志
//遍历要创建的字符串
for (int i=0;i<strDir.GetLength();++i)
{
if (strDir.GetAt(i) != _T('\\'))
{//如果当前字符不是'\\'
strTemp.AppendChar(strDir.GetAt(i));
}
else
{//如果当前字符是'\\'
vPath.push_back(strTemp);//将当前层的字符串添加到数组中
strTemp.AppendChar(_T('\\'));
}
}
//遍历存放目录的数组,创建每层目录
std::vector<CString>::const_iterator vIter;
for (vIter = vPath.begin(); vIter != vPath.end(); vIter++)
{
//如果CreateDirectory执行成功,返回true,否则返回false
bSuccess = CreateDirectory(*vIter, NULL) ? true : false;
}
log = (bSuccess)?("Success"):("Error");
gLogMgr->WriteDebugLog(log);
return bSuccess;
}
//将Path1 的所有文件copy 到Path2 (也可以是单个文件)
void CFileMgr::CopyFile(CString Path1,CString Path2)
{
CString MoveFileCmd("copy ");
MoveFileCmd += Path1+" "+Path2;
//system(MoveFileCmd);
CopyFolder(Path1,Path2);
}
//重新命名文件(返回修改后文件的完整路径)
//FilePath是文件的路径,NewName是新名称,不含后缀
CString CFileMgr::FileReName(CString FilePath,CString NewName)
{
CString OldFilePath = FilePath;
CString Suffix = GetFileNameFromPath(FilePath,false);//有后缀
CString FileName = GetFileNameFromPath(FilePath,true);//无后缀
FilePath.Replace(Suffix,"");//得到路径
Suffix.Replace(FileName,"");//得到后缀
CString NewFilePath = FilePath + NewName + Suffix;
//CFile::Rename(OldFilePath, NewFilePath);
ReNameFolder(OldFilePath,NewFilePath);
return NewFilePath;
}
//打开目录
void CFileMgr::OpenDir(CString DirPath)
{
if(IsDirectoryExists(DirPath))
{
ShellExecute(0,"open",DirPath,"","",SW_SHOWNORMAL);
}
}
//从路径中提取文件名(bDelSuffix 去掉后缀)
CString CFileMgr::GetFileNameFromPath(CString Path,bool bDelSuffix)
{
CString name = Path;
int idx = name.ReverseFind('\\');
if(idx != -1)
{
int len = name.GetLength();
name = name.Right(len - idx -1);
}
if(bDelSuffix)
{
idx = name.ReverseFind('.');
if(idx != -1)
{
int len = name.GetLength();
name = name.Left(len - (len-idx));
}
}
return name;
}
//修改文件夹/ 文件名称
BOOL CFileMgr::ReNameFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)
{
int nLengthFrm = strlen(lpszFromPath);
char *NewPathFrm = new char[nLengthFrm+2];
strcpy(NewPathFrm,lpszFromPath);
NewPathFrm[nLengthFrm] = '\0';
NewPathFrm[nLengthFrm+1] = '\0';
SHFILEOPSTRUCT FileOp;
ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
FileOp.fFlags = FOF_NOCONFIRMATION ;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = NewPathFrm;
FileOp.pTo = lpszToPath;
FileOp.wFunc = FO_RENAME;
return SHFileOperation(&FileOp) == 0;
}
//移动文件/目录
BOOL CFileMgr::MoveFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)
{
int nLengthFrm = strlen(lpszFromPath);
char *NewPathFrm = new char[nLengthFrm+2];
strcpy(NewPathFrm,lpszFromPath);
NewPathFrm[nLengthFrm] = '\0';
NewPathFrm[nLengthFrm+1] = '\0';
SHFILEOPSTRUCT FileOp;
ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
FileOp.fFlags = FOF_NOCONFIRMATION ;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = NewPathFrm;
FileOp.pTo = lpszToPath;
FileOp.wFunc = FO_MOVE;
return SHFileOperation(&FileOp) == 0;
}
//复制文件/目录
BOOL CFileMgr::CopyFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)
{
int nLengthFrm = strlen(lpszFromPath);
char *NewPathFrm = new char[nLengthFrm+2];
strcpy(NewPathFrm,lpszFromPath);
NewPathFrm[nLengthFrm] = '\0';
NewPathFrm[nLengthFrm+1] = '\0';
SHFILEOPSTRUCT FileOp;
ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
FileOp.fFlags = FOF_NOCONFIRMATION ;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = NewPathFrm;
FileOp.pTo = lpszToPath;
FileOp.wFunc = FO_COPY;
return SHFileOperation(&FileOp) == 0;
}
//删除文件/目录
BOOL CFileMgr::DeleteFolder(LPCTSTR lpszPath)
{
int nLength = strlen(lpszPath);
char *NewPath = new char[nLength+2];
strcpy(NewPath,lpszPath);
NewPath[nLength] = '\0';
NewPath[nLength+1] = '\0';
SHFILEOPSTRUCT FileOp;
ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
FileOp.fFlags = FOF_NOCONFIRMATION;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = NewPath;
FileOp.pTo = NULL;
FileOp.wFunc = FO_DELETE;
return SHFileOperation(&FileOp) == 0;
}
BOOL CFileMgr::CopyDirOrFile(CString src,CString dest) //复制文件或文件夹
{
char pFrom[256]{};
char pTo[256]{};
strcpy(pFrom, src); // 第一个文件
strcpy(pTo, dest); // 第一个文件
SHFILEOPSTRUCT sfo;
sfo.hwnd = NULL;
sfo.wFunc = FO_COPY;
sfo.pFrom = src;
sfo.pTo = dest;
sfo.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR;
return (SHFileOperation(&sfo)==0);//执行成功返回0
}
#if 1
//保存数据到CSV文件
//比如DirPath "E:\LaipuLaserData\MonitoringData\WaferProcessData\2021\05\174357_FP03563_Port1_Slot3\"
//FileName没有后缀,bAppend 追加方式写入
void CFileMgr::WriteDataToExcel(CString DirPath,CString FileName,CCsvData &CsvData,bool bAppend)
{
CString FilePath = DirPath;
FilePath += FileName;
FilePath += ".xlsx";
ofstream fstream;
if(bAppend)//追加写入
fstream.open(FilePath, ios::app);
else
fstream.open(FilePath);
vector<vector<CString>> &DataVec = CsvData.m_DataVec;
int size = DataVec.size();
for(int k=0;k<size;k++)
{
vector<CString>&StrVec = DataVec[k];
int size1 = StrVec.size();
for(int i=0;i<size1;i++)
{
fstream<<StrVec[i];
if(i<(size1-1))
{
fstream<<" ";
}
}
fstream<<"\n";//换行
}
}
#endif