📄 file.cpp
字号:
/******************************************************************************** file.cpp: File management*-------------------------------------------------------------------------------* (c)1999-2001 VideoLAN* $Id: file.cpp,v 1.4 2002/04/10 10:47:18 asmax Exp $** Authors: Benoit Steiner <benny@via.ecp.fr>* Arnaud de Bossoreille de Ribou <bozo@via.ecp.fr>** This program is free software; you can redistribute it and/or* modify it under the terms of the GNU General Public License* as published by the Free Software Foundation; either version 2* of the License, or (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.**-------------------------------------------------------------------------------* This class encapsulate the ANSI C/C++ file access fonctions, and uses its* semantic. It is therefore portable on every plateform********************************************************************************///------------------------------------------------------------------------------// Preamble//------------------------------------------------------------------------------#include "defs.h"#include <stdio.h>#include <string.h>#ifdef HAVE_OPENDIR#include <dirent.h>#endif#include <errno.h> #include <sys/stat.h>#include "common.h"#include "debug.h"#include "string.h"#include "exception.h"#include "system.h"#include "vector.h"#include "file.h"#include "vector.cpp"/******************************************************************************** E_File****************************************************************************************************************************************************************/E_File::E_File(const C_String& strMsg) : E_Exception(errno, strMsg){}/******************************************************************************** E_Directory****************************************************************************************************************************************************************/E_Directory::E_Directory(const C_String& strMsg) : E_Exception(errno, strMsg){}/******************************************************************************** C_File****************************************************************************************************************************************************************///------------------------------------------------------------------------------// Constructor//------------------------------------------------------------------------------////------------------------------------------------------------------------------C_File::C_File(const C_String& strPath) : m_strPath(strPath){ m_hFd = NULL;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// See the fread manual for an explanation of the strMode parameter.// Throws a FileException if the file could not be opened.//------------------------------------------------------------------------------void C_File::Open(const C_String& strMode, int iPermissions){ m_hFd = fopen(m_strPath.GetString(), strMode.GetString()); if(!m_hFd) { throw E_File("Could not open file '" + m_strPath + "': " + GetErrorMsg()); }}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Throws a FileException if the file could not be closed.//------------------------------------------------------------------------------void C_File::Close(){ // Close the file only if it has been sucessfully opened before if(m_hFd) { int iRc = fclose(m_hFd); if(iRc) throw E_File("Could not close file '" + m_strPath + "': " + GetErrorMsg()); }}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Returns the number of bytes read, FILE_EOF if the end of the file has been// reached so that no data could be read.// Throws a FileException if an error occurred.//------------------------------------------------------------------------------int C_File::Read(byte* pBuff, int iSize){ ASSERT(m_hFd); // Make sure that the file has been opened ASSERT(pBuff); ASSERT(iSize > 0); int iRc = fread(pBuff, sizeof(byte), iSize, m_hFd); if(iRc != iSize) { if(feof(m_hFd) && iRc == 0) { // No byte could be read before the end of the file iRc = FILE_EOF; } else if(ferror(m_hFd)) { // An error occurred throw E_File("Could not read file '" + m_strPath + "': " + GetErrorMsg()); } } return iRc;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Returns the number of byte written.// Throws a FileException if an error occurred.//------------------------------------------------------------------------------int C_File::Write(const byte* pData, int iSize){ ASSERT(m_hFd); // Make sure that the file has been opened ASSERT(pData); ASSERT(iSize > 0); int iRc = fwrite(pData, sizeof(byte), iSize, m_hFd); if(iRc != iSize) { ASSERT(!feof(m_hFd)); if(ferror(m_hFd)) { throw E_File("Could not write file '" + m_strPath + "': " + GetErrorMsg()); } } return iRc;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Return the position in the file.// Throws a FileException if an error occurred.//------------------------------------------------------------------------------s64 C_File::GetPos(){ ASSERT(m_hFd); s64 iRc = ftell(m_hFd); if(iRc < 0) { throw E_File("Could not get position in file '" + m_strPath + "': "+GetErrorMsg()); } return iRc; }//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Set the position for the file// Throws a FileException if an error occurred.//------------------------------------------------------------------------------int C_File::Seek(s64 iOffset, int iStartPos){ ASSERT(m_hFd); int iRc = fseek(m_hFd, iOffset, iStartPos); if(iRc < 0) { throw E_File("Could not seek in file '" + m_strPath + "': " + GetErrorMsg()); } return iRc;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------////------------------------------------------------------------------------------bool C_File::Exists() const{ FILE* hFd = fopen(m_strPath.GetString(), "r"); if(hFd) { fclose(hFd); return true; } else { return false; }}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Returns the name (ie the path) of the opened file//------------------------------------------------------------------------------C_String C_File::GetName() const{ return m_strPath;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Returns some info on the file//------------------------------------------------------------------------------C_String C_File::GetInfo() const{ return m_strPath;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// Returns the file handle//------------------------------------------------------------------------------FILE* C_File::GetHandle() const{ return m_hFd;}/******************************************************************************** C_LinkedFile****************************************************************************************************************************************************************///------------------------------------------------------------------------------// Constructor//------------------------------------------------------------------------------////------------------------------------------------------------------------------C_LinkedFile::C_LinkedFile(C_Vector<C_String>& vFileNames) : m_vFileNames(vFileNames){ m_iFileCount = m_vFileNames.Size(); ASSERT(m_iFileCount) for(unsigned int i = 0; i < m_iFileCount; i++) { C_String strFileName = m_vFileNames[i]; C_File* pFile = new C_File(strFileName); ASSERT(pFile); m_cFiles.Add(pFile); } m_iFileIndex = 0; m_pCurrentFile = &m_cFiles[0];}//------------------------------------------------------------------------------////------------------------------------------------------------------------------////------------------------------------------------------------------------------void C_LinkedFile::Open(const C_String& strMode, int iPermissions){ for(unsigned int i = 0; i < m_iFileCount; i++) { C_File* pFile = &m_cFiles[i]; pFile->Open(strMode, iPermissions); }}//------------------------------------------------------------------------------////------------------------------------------------------------------------------////------------------------------------------------------------------------------void C_LinkedFile::Close(){ C_String strExcept; for(unsigned int i = 0; i < m_iFileCount; i++) { C_File* pFile = &m_cFiles[i]; try { pFile->Close(); } catch(E_Exception e) { strExcept += e.Dump(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -