diff --git a/FileUtils.cpp b/FileUtils.cpp index 5908198b..71257db0 100644 --- a/FileUtils.cpp +++ b/FileUtils.cpp @@ -136,6 +136,10 @@ int CFile::Move(const CString& sNewFileName, bool bOverwrite) { return CFile::Move(m_sLongName, sNewFileName, bOverwrite); } +int CFile::Copy(const CString& sNewFileName, bool bOverwrite) { + return CFile::Copy(m_sLongName, sNewFileName, bOverwrite); +} + bool CFile::Delete(const CString& sFileName) { if(!CFile::Exists(sFileName)) { return false; @@ -153,6 +157,35 @@ bool CFile::Move(const CString& sOldFileName, const CString& sNewFileName, bool return (rename(sOldFileName.c_str(), sNewFileName.c_str()) == 0) ? true : false; } +bool CFile::Copy(const CString& sOldFileName, const CString& sNewFileName, bool bOverwrite) { + if((!bOverwrite) && (CFile::Exists(sNewFileName))) { + return false; + } + + CFile OldFile(sOldFileName); + CFile NewFile(sNewFileName); + + if (!OldFile.Open(O_RDONLY)) { + return false; + } + + if (!NewFile.Open(O_WRONLY | O_CREAT | O_TRUNC)) { + return false; + } + + char szBuf[8192]; + unsigned int len = 0; + + while ((len = OldFile.Read(szBuf, 100))) { + NewFile.Write(szBuf, len); + } + + OldFile.Close(); + NewFile.Close(); + + return true; +} + bool CFile::Chmod(mode_t mode) { return CFile::Chmod(m_sLongName, mode); } diff --git a/FileUtils.h b/FileUtils.h index 0e27674c..07500fe3 100644 --- a/FileUtils.h +++ b/FileUtils.h @@ -89,9 +89,11 @@ public: // int Delete(); int Move(const CString& sNewFileName, bool bOverwrite = false); + int Copy(const CString& sNewFileName, bool bOverwrite = false); static bool Delete(const CString& sFileName); static bool Move(const CString& sOldFileName, const CString& sNewFileName, bool bOverwrite = false); + static bool Copy(const CString& sOldFileName, const CString& sNewFileName, bool bOverwrite = false); bool Chmod(mode_t mode); static bool Chmod(const CString& sFile, mode_t mode); bool Seek(unsigned long uPos);