📄 file.cpp
字号:
if(strExcept != "") throw E_File(strExcept);}//------------------------------------------------------------------------------////------------------------------------------------------------------------------////------------------------------------------------------------------------------int C_LinkedFile::Read(byte* pBuff, int iSize){ int iRc = 0; int iByteRead = 0; while((iByteRead != iSize) && (iRc != FILE_EOF)) { iRc = m_pCurrentFile->Read(pBuff + iByteRead, iSize - iByteRead); if(iRc != FILE_EOF) { iByteRead += iRc; } else { if(m_iFileIndex < m_iFileCount - 1) { m_pCurrentFile = &m_cFiles[++m_iFileIndex]; iRc = 0; } } } if((iByteRead == 0) && (iRc == FILE_EOF)) return FILE_EOF; else return iByteRead;}//------------------------------------------------------------------------------////------------------------------------------------------------------------------////------------------------------------------------------------------------------s64 C_LinkedFile::GetPos(){ s64 iPos = 0; for(unsigned int i = 0; i < m_iFileIndex; i++) { C_File* pFile = &m_cFiles[i]; pFile->Seek(0, FILE_SEEK_END); iPos += pFile->GetPos(); } iPos += m_pCurrentFile->GetPos(); return iPos;}//------------------------------------------------------------------------------////------------------------------------------------------------------------------////------------------------------------------------------------------------------int C_LinkedFile::Seek(s64 iOffset, int bStartPos){ switch(bStartPos) { case FILE_SEEK_CURRENT : if(iOffset < 0) { s64 iSeeked = 0; while(iSeeked != iOffset) { s64 iSubPos = m_pCurrentFile->GetPos(); if(iOffset - iSeeked >= -iSubPos) { m_pCurrentFile->Seek(iOffset - iSeeked, FILE_SEEK_CURRENT); iSeeked = iOffset; } else { m_pCurrentFile->Seek(0, FILE_SEEK_BEGIN); iSeeked -= iSubPos; if(m_iFileIndex != 0) { m_pCurrentFile = &m_cFiles[--m_iFileIndex]; m_pCurrentFile->Seek(0, FILE_SEEK_END); } else throw E_File("Could not seek in linked file: out of file"); } } } else if(iOffset > 0) { s64 iSeeked = 0; while(iSeeked != iOffset) { s64 iSubPos = m_pCurrentFile->GetPos(); m_pCurrentFile->Seek(0, FILE_SEEK_END); s64 iSubPos2 = m_pCurrentFile->GetPos(); if(iSubPos2 - iSubPos > iOffset - iSeeked) { m_pCurrentFile->Seek(iOffset - iSeeked + iSubPos - iSubPos2, FILE_SEEK_END); iSeeked = iOffset; } else if(iSubPos2 - iSubPos == iOffset - iSeeked) iSeeked = iOffset; else { iSeeked += iSubPos2 - iSubPos; if(m_iFileIndex < m_iFileCount) m_pCurrentFile = &m_cFiles[++m_iFileIndex]; else throw E_File("Could not seek in linked file: out of file"); } } } break; case FILE_SEEK_BEGIN : if(iOffset >= 0) { m_iFileIndex = 0; m_pCurrentFile = &m_cFiles[0]; m_pCurrentFile->Seek(0, FILE_SEEK_BEGIN); if(iOffset > 0) { s64 iSeeked = 0; while(iSeeked != iOffset) { m_pCurrentFile->Seek(0, FILE_SEEK_END); s64 iSubPos = m_pCurrentFile->GetPos(); if(iSubPos > iOffset - iSeeked) { m_pCurrentFile->Seek(iOffset - iSeeked - iSubPos, FILE_SEEK_END); iSeeked = iOffset; } else if(iSubPos == iOffset - iSeeked) iSeeked = iOffset; else { iSeeked += iSubPos; if(m_iFileIndex < m_iFileCount) m_pCurrentFile = &m_cFiles[++m_iFileIndex]; else throw E_File("Could not seek in linked file: out of file"); } } } for(unsigned int i = m_iFileIndex + 1; i < m_iFileCount; i++) m_cFiles[i].Seek(0, FILE_SEEK_BEGIN); } else throw E_File("Could not seek in linked file: offset out of file"); break; case FILE_SEEK_END : if(iOffset <=0) { m_iFileIndex = m_iFileCount - 1; m_pCurrentFile = &m_cFiles[m_iFileIndex]; m_pCurrentFile->Seek(0, FILE_SEEK_END); if(iOffset < 0) { s64 iSeeked = 0; while(iSeeked != iOffset) { s64 iSubPos = m_pCurrentFile->GetPos(); if(iOffset - iSeeked >= -iSubPos) { m_pCurrentFile->Seek(iOffset - iSeeked, FILE_SEEK_END); iSeeked = iOffset; } else { m_pCurrentFile->Seek(0, FILE_SEEK_BEGIN); iSeeked -= iSubPos; if(m_iFileIndex != 0) { m_pCurrentFile = &m_cFiles[--m_iFileIndex]; m_pCurrentFile->Seek(0, FILE_SEEK_END); } else throw E_File("Could not seek in linked file: out of file"); } } } for(int i = m_iFileIndex - 1; i >= 0; i--) m_cFiles[i].Seek(0, FILE_SEEK_END); } else throw E_File("Could not seek in linked file: offset out of file"); break; default : ASSERT(false); } return 0;}/******************************************************************************** C_Directory****************************************************************************************************************************************************************///------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------C_Directory::C_Directory(const C_String& strPath){ m_strPath = strPath;#ifdef HAVE_OPENDIR ZERO(m_pDir);#else ASSERT(false);#endif}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------void C_Directory::Open(){#ifdef HAVE_OPENDIR m_pDir = opendir(m_strPath.GetString()); if(!m_pDir) { throw E_Directory("Could not open directory '" + m_strPath + "': " + GetErrorMsg()); } else { // Read the directory content Update(); } #else ASSERT(false);#endif}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------void C_Directory::Close(){#ifdef HAVE_OPENDIR if(m_pDir) { int iRc = closedir(m_pDir); if(iRc) throw E_Directory("Could not close directory '" + m_strPath + "': " + GetErrorMsg()); } #else ASSERT(false);#endif}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// To do: update only if necessary by checking the modification date of the // file '.'// Return UPDATED if the directory content as been modified, NO_CHANGE if not//------------------------------------------------------------------------------int C_Directory::Update(){ ASSERT(m_pDir); // First empty old vectors m_vFiles.Empty(); m_vSubDirs.Empty();#ifdef HAVE_OPENDIR // Resets the position of the directory stream to the beginning rewinddir(m_pDir); // Read the directory content struct dirent* pFile = readdir(m_pDir); while(pFile) { C_String strFileName(pFile->d_name); // Skip the dummy '.' and '..' files if(strFileName != "." && strFileName != "..") { // Determines if the entry must be stored as a file or as a directory C_String strFilePath = m_strPath + "/" + strFileName; struct stat sStat; int iRc = stat(strFilePath.GetString(), &sStat); if(iRc) { // Error just means an invalid file, don't raise an exception // for that } else { if(sStat.st_mode & S_IFDIR) m_vSubDirs.Add(new C_String(strFileName)); else m_vFiles.Add(new C_String(strFileName)); } } pFile = readdir(m_pDir); } ASSERT(GetErrorCode() != EBADF);#else ASSERT(false);#endif return UPDATED;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------const C_Vector<C_String>& C_Directory::GetFiles() const{ ASSERT(m_pDir); return m_vFiles;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------const C_Vector<C_String>& C_Directory::GetSubDirs() const{ ASSERT(m_pDir); return m_vSubDirs;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -