BOOL CPackFiles::RecurseUnpack(CFileNodePackFileNode* pcNode, char* szDestination) { CChars szFileName; CFileNodePackFileNode* pcChild; int i; CPackFileNode* pcFile; BOOL bResult; CPackFile* pcPackFile; CFileCopier cCopier; CDiskFile cDiskFile; CFileUtil cFileUtil; if (pcNode->IsDirectory()) { bResult = TRUE; for (i = 0; i < pcNode->Directory()->maNodeFiles.NumElements(); i++) { pcChild = (CFileNodePackFileNode*)pcNode->Directory()->maNodeFiles.Get(i); bResult &= RecurseUnpack(pcChild, szDestination); } return bResult; } else if (pcNode->IsFile()) { szFileName.Init(szDestination); szFileName.Append('/'); pcNode->GetFullName(&szFileName); szFileName.Replace('/', FILE_SEPARATOR[0]); pcFile = pcNode->File(); pcPackFile = PackFile(this, pcFile); if (!pcPackFile) { szFileName.Kill(); return FALSE; } cDiskFile.Init(szFileName.Text()); cFileUtil.RemoveLastFromPath(&szFileName); cFileUtil.MakeDir(szFileName.Text()); bResult = cCopier.Copy(pcPackFile, &cDiskFile); cDiskFile.Kill(); szFileName.Kill(); return bResult; } return FALSE; }
Bool CMemoryFile::SaveToDisk(const AString& sFile) { CDiskFile df; if (m_pData && df.Open(sFile, OPEN_WRITE)) { if (m_iFilePos > 0) { Int64 iSize = df.Write(m_pData, m_iFilePos); return iSize == m_iFilePos; } return true; } return false; }
BOOL CPackFiles::AddDirectory(char* szDirectory, char* szPackDirectory) { CFileUtil cFileUtil; CArrayChars aszFileNames; int i; CChars* pszFileName; CDiskFile cDiskFile; CChars szName; CChars szNameDirectory; BOOL bResult; BOOL bAnyFiles; aszFileNames.Init(); bAnyFiles = cFileUtil.FindAllFiles(szDirectory, &aszFileNames, TRUE, FALSE); if (!bAnyFiles) { aszFileNames.Kill(); return FALSE; } szNameDirectory.Init(szDirectory); for (i = 0; i < aszFileNames.NumElements(); i++) { pszFileName = aszFileNames.Get(i); cDiskFile.Init(pszFileName->Text()); cFileUtil.MakeNameFromDirectory(&szName, pszFileName, &szNameDirectory); if (szPackDirectory) { szName.Insert(0, '/'); szName.Insert(0, szPackDirectory); } bResult = AddFile(&cDiskFile, szName.Text()); szName.Kill(); cDiskFile.Kill(); if (!bResult) { aszFileNames.Kill(); return FALSE; } } aszFileNames.Kill(); return TRUE; }
Bool CMemoryFile::Open(const AString& sFile) { Close(); m_eOpenType = OPEN_READ; m_bExtra = false; CDiskFile df; if (df.Open(sFile)) { m_iFileSize = df.GetFileSize(); m_pData = (Char*)malloc((Size_t)m_iFileSize); memset(m_pData, 0, (Size_t)m_iFileSize); df.Read(m_pData, m_iFileSize); return true; } return false; }
CFileIndex CIndexTreeBlockFile::LoadRootFileIndex(char* szRootFileName) { CFileBasic cFileBasic; CDiskFile cDiskFile; CFileUtil cFileUtil; CFileIndex cIndex; filePos iSize; iSize = cFileUtil.Size(szRootFileName); if (iSize != -1) { if (iSize == sizeof(CFileIndex)) { cDiskFile.Init(szRootFileName); cFileBasic.Init(&cDiskFile); cFileBasic.Open(EFM_Read); cFileBasic.Read(&cIndex, 0, sizeof(CFileIndex)); cFileBasic.Close(); cFileBasic.Kill(); cDiskFile.Kill(); return cIndex; } else { gcLogger.Error2(__METHOD__, "Index Root file size [", LongLongToString(iSize), "] is incorrect. Should be SizeOf(CFileIndex).", NULL); cIndex.Init(); return cIndex; } } else { cIndex.Init(); return cIndex; } }
Bool CXmlFile::ParseFile(const AString& sFile, PVoid pDoc) { CDiskFile xFile; if (!pDoc || !xFile.Open(sFile)) return false; Size_t iSize = (Size_t)(xFile.GetFileSize() + 8); Char* pData = (Char*)UtilMalloc(iSize); CScope::CMallocPtr scope(pData); memset(pData, 0, iSize); iSize = (Size_t)xFile.Read(pData, xFile.GetFileSize()); xFile.Close(); //解析文档 return ParseDocument(pDoc, pData); }
void TestLogFileMultipleReadsAfterOpens(void) { CLogFile* pcLogFile; CDiskFile* pcDiskFile; CFileBasic cFile; CFileUtil cFileUtil; char szSource[] = {"The Name of the Wise Man"}; int iSourcelen; char szResult[50]; char szWrite[] = {"Cat Catt ct... "}; int iWriteLen; char szA[] = {"A"}; cFileUtil.RemoveDir("Output/LogFile2"); cFileUtil.MakeDir("Output/LogFile2"); pcDiskFile = DiskFile("Output/LogFile2/OpenClose.txt"); pcDiskFile->Open(EFM_ReadWrite_Create); iSourcelen = (int)strlen(szSource); pcDiskFile->Write(szSource, iSourcelen + 1, 1); pcDiskFile->Close(); AssertTrue(cFileUtil.Exists("Output/LogFile2/OpenClose.txt")); pcLogFile = LogFile(pcDiskFile); cFile.Init(pcLogFile); pcLogFile->Begin(); AssertTrue(cFile.Open(EFM_Read)); AssertInt(iSourcelen + 1, (int)cFile.GetFileSize()); cFile.ReadData(szResult, iSourcelen + 1); AssertString(szSource, szResult); cFile.Close(); cFile.Open(EFM_ReadWrite_Create); iWriteLen = (int)strlen(szWrite); cFile.WriteData(szWrite, iWriteLen); AssertInt(iSourcelen + 1, (int)cFile.GetFileSize()); pcLogFile->Close(); cFile.Delete(); AssertTrue(cFileUtil.Exists("Output/LogFile2/OpenClose.txt")); AssertInt(0, (int)cFile.GetFileSize()); cFile.Open(EFM_ReadWrite_Create); cFile.Write(szA, 2, 1); AssertInt(2, (int)cFile.GetFileSize()); cFile.Close(); pcLogFile->Commit(); cFile.Kill(); AssertTrue(cFileUtil.Exists("Output/LogFile2/OpenClose.txt")); AssertInt(2, cFileUtil.Size("Output/LogFile2/OpenClose.txt")); pcDiskFile = DiskFile("Output/LogFile2/OpenClose.txt"); pcLogFile = LogFile(pcDiskFile); cFile.Init(pcLogFile); pcLogFile->Begin(); AssertTrue(cFile.Open(EFM_Read)); cFile.ReadData(szResult, 2); AssertString("A", szResult); cFile.Seek(0); cFile.ReadData(szResult, 2); AssertString("A", szResult); cFile.Close(); cFile.Delete(); pcLogFile->Commit(); cFile.Kill(); AssertFalse(cFileUtil.Exists("Output/LogFile2/OpenClose.txt")); }