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.

225 lines
6.0 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"
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
//按行读取FilePath 指定的文件保存到vec 中(FilePath 是程序根目录下的文件路径)
void CFileMgr::ReadFileToStringVec(const CString &FilePath,vector<CString> &vec)
{
CStdioFile file;
if(file.Open(FilePath,CFile::modeRead,NULL))
{
CString str;
while(file.ReadString(str))
{
vec.push_back(str);
}
file.Close();
}
}
//读取文件的每一行,以逗号分离数值
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 = Str.GetBuffer();
//第一个值
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,ofstream *file)
{
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
//检查文件是否存在(绝对路径)
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)
{
CString MkdirCmd("mkdir ");
MkdirCmd += DirPath;
system(MkdirCmd);
}
//将Path1 的所有文件copy 到Path2
void CFileMgr::CopyFile(CString Path1,CString Path2)
{
CString MoveFileCmd("copy ");
MoveFileCmd += Path1+" "+Path2;
system(MoveFileCmd);
}
//从路径中提取文件名(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;
}