파일관련 유틸 클래스
2017. 2. 2. 08:50ㆍ장비제어개발관한이야기
반응형
이유 없는 PC다운으로 하루종일 고민에 빠져있는 장비개발자입니다.
PC를 통째로 갈아 봤는데도 PC가 멈추는 현상을 잡을 수가 없네요...
전장 쪽에 문제가 있는 거 같기도 하고, 소프트웨어로 PC를 멈추는 거는 힘들어 보이고, 그렇다고 디버깅을 할 수 있게 프로그램이 죽어버리는 것도 아니가 단순히 PC 다운입니다.
변경된 부분부터 차근차근 잡아야 하는 일인거 같습니다.
장비쪽일을 하면서 PC관련된 문제는 거의 소프트웨어에서 처리를 하게 되는 듯합니다. 개발자라고 컴퓨터를 잘 아는 게 아닌데 말입니다. 저도 네이버에서 검색해서 PC를 고치는 수준입니다.
그런 의미에서 개발자라는 타이틀을 잊지 않으려 오늘 포스팅하려고 합니다. ^^*
편하게 쓰는 파일관련 클래스
포스팅할 내용은 장비제어를 하면서 자주 사용하게 되는 파일 및 디렉터리 생성과 수정에 관련한 파일관리클래스입니다.
기존에 파일 관련 기능중 자주 쓰이는 기능을 한 클래스에 모아 놓은 Util 클래스입니다.
멤버함수 하나하나 독립적으로 기능을 하고 있어 NameSpace만 같이 엮은 거라고 보면 편할듯합니다.
관련 기능에 대한 설명은 소스의 주석을 통해 설명하였습니다.
<헤더>
/**
* CImFileUtil.h : File 처리를 위한 Class의 Header 파일이다.
*
* @author 오세영
* @version 1.0
* @see
*/
// ImFileUtil.h: interface for the CImFileUtil class.
//
//////////////////////////////////////////////////////////////////////
#pragma once
// Error Code Define
#define E_FU_FILE_NOT_FOUND -1
#define E_FU_FILE_COPY_FAIL -2
#define E_FU_NOFILE_IN_DIR -3
/**
* File 삭제,복사, 갯수 구하기 등의 Uitlity들을 제공하는 Class이다.
*/
class CImFileUtil
{
public:
/**
* CImFileUtil의 생성자
*/
CImFileUtil(); // 생성자
/**
* CImFileUtil의 소멸자
*/
virtual ~CImFileUtil(); // 소멸자
/**
* 지정한 Path가 디렉토리를 포함하지 않은 최하위 디렉토리인지 확인한다.
*
* @Param pDir : 확인할 디렉토리 Path
* @return TRUE : 다른 디렉토리 포함 하지 않음, FALSE : 다른 디렉토리 포함
*/
BOOL IsLeafDir(LPCTSTR pDir);
/**
* 디렉토리내 파일들을 삭제한다.
* 디렉토리내 하위디렉토리는 삭제하지 않는다.
* Progress Bar를 뛰어서 진행 경과를 표시한다.
*
* @Param pDir : 삭제할 디렉토리 Path
* @return TRUE/FALSE
*/
int DeleteFilesInDir(LPCTSTR pDir);
/**
* 디렉토리에 존재하는 파일의 갯수를 구한다.
*
* @Param pDir : 디렉토리명
* @return 파일의 갯수
*/
int GetFileCountInDir(LPCTSTR pDir);
/**
* 디렉토리내 하위 디렉토리의 갯수를 구한다.
*
* @Param pDir : 삭제할 디렉토리 Path
* @return 하위 디렉토리의 갯수
*/
int GetDirCountInDir(LPCTSTR pDir);
/**
* 디렉토리의 파일들을 복사한다.
*
* @Param pDest : 목적지 디렉토리
* @Param pSrc : 원본 디렉토리
* @Param bOverWrite : OverWrite 여부 (TRUE/FALSSE)
* @return 하위 디렉토리의 갯수
*/
int CopyFilesInDir(LPCTSTR pDest,LPCTSTR pSrc,BOOL bOverWrite);
/**
* 디렉토리에 파일이 존재 하는지 검사한다.
*
* @Param pFile : 검색하고자 하는 파일명 (Path 포함)
* @return TRUE / FALSE
*/
BOOL IsFileExist(LPCTSTR pFile);
/**
* 디렉토리내 디렉토리의 리스트를 차즌다.
*
* @Param pDir : 디렉토리명
* @Param strDirname[] : string의 배열
* @Param iCount : 갯수
* @return SUCCESS / ERROR CODE
*/
int GetDirListInDir(LPCTSTR pDir,CString strDirName[], int iCount);
/**
* 디렉토리내 디렉토리의 리스트를 차즌다.
*
* @Param pDir : 디렉토리명
* @Param strDirname[] : string의 배열
* @Param iCount : 갯수
* @return SUCCESS / ERROR CODE
*/
int GetDirListInDir(LPCTSTR pDir,CString strDirName[], int *iCount);
/**
* 존재하지 않는 디렉토리의 경우 새로 생성한다.
*
* @author 오세영
* @param pDir : Path (파일 포함 또는 미포함)
* @param bIsIncludeFile : pDir에 파일을 포함 하는가 ?, Default TRUE
* @return TRUE : 성공, FALSE : 실패
* @see
* @version
*/
BOOL MakeDir(LPCTSTR pDir,BOOL bIsIncludeFile = TRUE);
BOOL GetFilePathSubDir(LPCTSTR pDir,CString strSearchFileName,CString& strPath);
BOOL GetFilePathSubDir(LPCTSTR pDir,CString strOption,CStringArray& arryStr);
};
<CPP파일>
/**
* CImFileUtil.cpp : File 처리를 위한 인터페이스 Class를 구현한 프로그램이다.
*
* @author 오세영
* @version 1.0
* @see
*/
// CImFileUtil.cpp: implementation of the CImFileUtil class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ImFileUtil.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
/**
* CImFileUtil의 생성자
*/
CImFileUtil::CImFileUtil()
{
}
/**
* CImFileUtil의 소멸자
*/
CImFileUtil::~CImFileUtil()
{
}
/**
* 지정한 Path가 디렉토리를 포함하지 않은 최하위 디렉토리인지 확인한다.
*
* @Param pDir : 확인할 디렉토리 Path
* @return TRUE : 다른 디렉토리 포함 하지 않음, FALSE : 다른 디렉토리 포함
*/
BOOL CImFileUtil::IsLeafDir(LPCTSTR pDir)
{
CFileFind fn;
CString strFile;
int nextfile;
strFile.Format(_T("%s\\*.*"),pDir); // *.*로 검색한다.
// Open A File Serach
if(!fn.FindFile(strFile,0)) return FALSE;
// 디렉토리에서 해당 파일들을 찾기 시작한다.
nextfile = fn.FindNextFile(); // 다음 파일을 찾는다.
for(;;)
{
// '.'나 '..'이 아닌 디렉토리를 포함하면 FALSE를 리턴한다. Leaf디렉토리가 아님
if( fn.IsDirectory() )
if( !fn.IsDots() ) return FALSE;
if(!nextfile) break; // 마지막에 도달했드면 루프를 빠진다.
nextfile = fn.FindNextFile(); // 다음을 검색한다.
}
return TRUE;
}
/**
* 디렉토리내 파일들을 삭제한다.
* 디렉토리내 하위디렉토리는 삭제하지 않는다.
* Progress Bar를 뛰어서 진행 경과를 표시한다.
*
* @Param pDir : 삭제할 디렉토리 Path
* @return TRUE/FALSE
*/
int CImFileUtil::DeleteFilesInDir(LPCTSTR pDir)
{
CFileFind fn;
CString strFile;
int filecnt=0;
// Directory내 파일 갯수를 구한다.
if((filecnt = GetFileCountInDir(pDir)) <= 0) return TRUE;
// Show Progress Bar
// Path내 모들 파일을 검색하기 위한 파일명을 만든다.
strFile.Format(_T("%s\\*.*"),pDir);
if( !fn.FindFile(strFile,0) ) return FALSE;
filecnt = fn.FindNextFile();
for(;;)
{
// 디렉토리가 아닌 파일들을 삭제한다.
if( !fn.IsDirectory() )
{
strFile = fn.GetFilePath();
CFile::Remove(strFile);
}
if(!filecnt) break;
filecnt = fn.FindNextFile();
}
return TRUE;
}
/**
* 디렉토리내 하위 디렉토리의 갯수를 구한다.
*
* @Param pDir : 삭제할 디렉토리 Path
* @return 하위 디렉토리의 갯수
*/
int CImFileUtil::GetFileCountInDir(LPCTSTR pDir)
{
CFileFind ff;
int cnt=0, nt;
CString strFile;
strFile.Format(_T("%s\\*.*"),pDir); // 디렉토리명을 설정한다.
if( !ff.FindFile(strFile,0) ) return 0; // FileFind Open
nt = ff.FindNextFile(); // 파일을 검색하기 시작한다.
for(;;)
{
if( !ff.IsDirectory() ) cnt++; // 디렉토리가 아니면 파일 갯수 증가
if( !nt ) break; // 마지막일 경우
nt = ff.FindNextFile(); // 다음 파일을 찾는다
}
return cnt;
}
/**
* 디렉토리에 존재하는 파일의 갯수를 구한다.
*
* @Param pDir : 디렉토리명
* @return 파일의 갯수
*/
int CImFileUtil::GetDirCountInDir(LPCTSTR pDir)
{
CFileFind ff;
int cnt=0, nt;
CString strFind;
strFind.Format(_T("%s\\*.*"),pDir);
if( !ff.FindFile(strFind,0) ) return 0;
nt = ff.FindNextFile();
for(;;)
{
if( ff.IsDirectory() )
{
if( !ff.IsDots() ) cnt++;
}
if( !nt ) break;
nt = ff.FindNextFile();
}
return cnt;
}
/**
* 디렉토리내 파일들을 다른 디렉토리로 복사한다.
*
* @author 오세영
* @Param pFile : 검색할 파일명
* @return
* @exception
* @see
* @version
*/
int CImFileUtil::CopyFilesInDir(LPCTSTR pDestDir, LPCTSTR pSrcDir, BOOL bOverWrite)
{
int cnt=0;
CFileFind ff;
CString strSearchFile; // File 검색을 위한 String
CString strSrcFile; // 소스 파일 (Path 포함)
CString strDestFile; // 목적지 파일 (Path 포함)
cnt = GetFileCountInDir(pSrcDir);
if( !cnt ) return (E_FU_NOFILE_IN_DIR);
if(bOverWrite) DeleteFilesInDir(pDestDir);
strSearchFile.Format(_T("%s\\*.*"),pSrcDir);
if( !ff.FindFile(strSearchFile,0) ) return (E_FU_FILE_NOT_FOUND);
cnt = ff.FindNextFile();
for(;;)
{
if( !ff.IsDirectory() )
{
strSrcFile = ff.GetFilePath();
strDestFile.Format(_T("%s\\%s"),pDestDir,ff.GetFileName());
if(!CopyFile(strSrcFile, strDestFile, FALSE))
{
return (E_FU_FILE_COPY_FAIL);
}
}
if( !cnt ) break;
cnt = ff.FindNextFile();
}
return 0;
}
/**
* 파일을 검색한다.
*
* @author 오세영
* @Param pFile : 검색할 파일명
* @return
* @exception
* @see
* @version
*/
BOOL CImFileUtil::IsFileExist(LPCTSTR pFile)
{
CFileFind fn;
if( !fn.FindFile(pFile,0) ) return FALSE;
else return TRUE;
}
/**
* 디렉토리내 디렉토리의 리스트를 리턴한다.
*
* @author 오세영
* @Param pFile : 검색할 파일명
* @return
* @exception
* @see
* @version
*/
int CImFileUtil::GetDirListInDir(LPCTSTR pDir,CString strDirName[], int count)
{
CFileFind ff;
int iCount=0; // 리턴할 갯수, Arguement Count 이하의 값
int nt; // 남은 파일의 갯수
CString strFind; // 파일 검색을 위한 String
// 파일을 검색하여 strDirName에 하나씩 집어 넣는다
strFind.Format(_T("%s\\*.*"),pDir);
if( !ff.FindFile(strFind,0) ) return 0;
nt = ff.FindNextFile();
for(;;)
{
if(ff.IsDirectory())
{
if(!ff.IsDots())
{
// Argument로 입력된 한계를 넘는지 체크한다.
if(iCount > (count-1)) break;
strDirName[iCount] = ff.GetFileName();
iCount++;
}
}
if( !nt ) break;
nt = ff.FindNextFile();
}
return iCount; // 디렉토리 갯수를 리턴한다.
}
/**
* 디렉토리내 디렉토리의 리스트를 리턴한다.
*
* @author 오세영
* @Param pFile : 검색할 파일명
* @return
* @exception
* @see
* @version
*/
int CImFileUtil::GetDirListInDir(LPCTSTR pDir,CString strDirName[], int *count)
{
CFileFind ff;
int iMaxCount=256; // 리턴할 갯수, Arguement Count 이하의 값
int nt; // 남은 파일의 갯수
CString strFind; // 파일 검색을 위한 String
int iCount = 0;
// 파일을 검색하여 strDirName에 하나씩 집어 넣는다
strFind.Format(_T("%s\\*.*"),pDir);
if( !ff.FindFile(strFind,0) ) return 0;
nt = ff.FindNextFile();
for(;;)
{
if(ff.IsDirectory())
{
if(!ff.IsDots())
{
// Argument로 입력된 한계를 넘는지 체크한다.
if(iCount > (iMaxCount-1)) break;
strDirName[iCount] = ff.GetFileName();
iCount++;
}
}
if( !nt ) break;
nt = ff.FindNextFile();
}
*count = iCount;
return 0; // 디렉토리 갯수를 리턴한다.
}
/**
* 존재하지 않는 디렉토리의 경우 새로 생성한다.
*
* @author 오세영
* @param pDir : Path (파일 포함 또는 미포함)
* @param bIsIncludeFile : pDir에 파일을 포함 하는가 ?, Default TRUE
* @return TRUE : 성공, FALSE : 실패
* @see
* @version
*/
BOOL CImFileUtil::MakeDir(LPCTSTR pDir,BOOL bIsIncludeFile)
{
CFileFind fn;
CString Dir;
CString SubDir;
CFile file;
CFileException e;
int iIndex, vIndex;
BOOL bFirst = TRUE;
Dir = pDir;
iIndex = 0;
vIndex = 0;
while(TRUE)
{
iIndex = Dir.Find(_T("\\"),vIndex+1);
if(iIndex == -1)
{
iIndex = Dir.Find(_T("/"), vIndex+ 1);
if( iIndex == -1 ) break;
else vIndex = iIndex;
}
else
vIndex = iIndex;
SubDir = Dir.Left(iIndex);
if(bFirst && fn.FindFile(SubDir,0))
{
bFirst = FALSE;
fn.FindNextFile();
if(!fn.IsDirectory())
{
return FALSE;
}
}
else
{
// if(CreateDirectory(SubDir,NULL)==0) return FALSE;
CreateDirectory(SubDir, NULL);
}
}
#if 0
if(!bIsIncludeFile)
{
if(fn.FindFile(Dir,0))
{
fn.FindNextFile();
if(fn.IsDirectory()) return TRUE;
else return FALSE;
}
else
{
if(CreateDirectory(Dir,NULL)== 0) return FALSE;
CreateDirectory(SubDir, NULL);
}
}
#endif
return TRUE;
}
BOOL CImFileUtil::GetFilePathSubDir(LPCTSTR pDir,CString strSearchFileName,CString& strPath)
{
CString strSearchDir = pDir;
strSearchDir += + "\\*.*";
CFileFind Finder;
BOOL bIs = Finder.FindFile(strSearchDir);
if(!bIs) return FALSE;
while(bIs){
bIs = Finder.FindNextFile();
if(Finder.IsDots()) continue;
if(Finder.IsDirectory()){
CString NewDir = Finder.GetFilePath();
if(GetFilePathSubDir(NewDir,strSearchFileName,strPath)){
return TRUE;
}
}else{
CString strDestFile = Finder.GetFileName();
strSearchFileName.MakeUpper();
strDestFile.MakeUpper();
strDestFile = strDestFile.Mid(strDestFile.ReverseFind('.')+1);
if(strSearchFileName == strDestFile){
strPath = Finder.GetFilePath();
return TRUE;
}
}
}
return FALSE;
}
BOOL CImFileUtil::GetFilePathSubDir(LPCTSTR pDir,CString strOption,CStringArray& arryStr)
{
CString strSearchDir = pDir;
strSearchDir += strOption;
CFileFind Finder;
BOOL bIs = Finder.FindFile(strSearchDir);
arryStr.RemoveAll();
if(!bIs) return FALSE;
while(bIs){
bIs = Finder.FindNextFile();
if(Finder.IsDots()) continue;
if(Finder.IsDirectory()) continue;
else{
CString str = Finder.GetFilePath();
arryStr.Add(str);
}
}
int nSize = (int)arryStr.GetSize();
if(nSize>0) return TRUE;
else return FALSE;
}
/* End Of Code */
반응형
'장비제어개발관한이야기' 카테고리의 다른 글
장비제어에서 Offline Mode 와 Online모드 (0) | 2017.02.07 |
---|---|
장비제어에서 Log란? (0) | 2017.02.03 |
장비제어 - 쓰레드의 생성 (0) | 2017.02.01 |
카메라 제어하자 - 카메라종류 (0) | 2017.01.23 |
나는 이렇게 한다. 장비개발에서 소스관리!!! (0) | 2017.01.21 |