⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unixfile.cpp

📁 "More for C++" is a class library that provides some features that are usually common for object ori
💻 CPP
📖 第 1 页 / 共 2 页
字号:
  }}////////////////////////////////////////////////////////////////////////////////bool UnixFile::exists( ) throw( IOException ){  struct stat stats;  lstat( m_sFullName, &stats );  return S_ISREG( stats.st_mode );}////////////////////////////////////////////////////////////////////////////////p<Directory::Entry> UnixFile::getDescriptor( ) throw( IOException ){  if( m_pDescriptor == 0 )  {    m_pDescriptor = CREATE UnixDirectory::UnixEntry( m_sFullName );  }  return m_pDescriptor;}////////////////////////////////////////////////////////////////////////////////p<Directory> UnixFile::getParent( ) throw( IOException ){  return CREATE UnixDirectory( getDescriptor( ) -> getParent( ) );}////////////////////////////////////////////////////////////////////////////////void UnixFile::open(  File::AccessMode eAccessMode) throw( IOException ){  if( m_nFileReadHandle != -1 || m_nFileWriteHandle != -1 )  {    throw IOException( -1, "File has already been opened" );  }  if( eAccessMode == READ_WRITE || eAccessMode == READ_ONLY )  {    m_nFileReadHandle = ::open( m_sFullName,                                convertAccessMode( eAccessMode ),                                S_IWUSR | S_IRUSR | S_IRGRP );    if( m_nFileReadHandle == -1 )    {      throw IOException( errno, "Error while opening file \"" + m_sFullName + "\"" );    }  }  if( eAccessMode == WRITE_APPEND && !exists( ) )  {    eAccessMode = WRITE_CREATE;  }  if( eAccessMode == WRITE_CREATE || eAccessMode == WRITE_APPEND || eAccessMode == READ_WRITE )  {    m_nFileWriteHandle = ::open( m_sFullName,                                 convertAccessMode( eAccessMode ),                                 S_IWUSR | S_IRUSR | S_IRGRP );    if( m_nFileWriteHandle == -1 )    {      if( m_nFileReadHandle != -1 )      {        close( );      }      throw IOException( errno, "Error while opening file \"" + m_sFullName + "\"" );    }  }  m_eAccessMode = eAccessMode;}////////////////////////////////////////////////////////////////////////////////void UnixFile::close( ) throw( IOException ){  throwIfNotOpen( );  if( m_nFileReadHandle != - 1 )  {    ::close( m_nFileReadHandle );    m_nFileReadHandle = -1;  }  if( m_nFileWriteHandle != - 1 )  {    ::close( m_nFileWriteHandle );    m_nFileWriteHandle = -1;  }}////////////////////////////////////////////////////////////////////////////////p<InputStream> UnixFile::getInputStream( ) throw( IOException ){  throwIfNotOpen( );  if( m_eAccessMode != READ_WRITE && m_eAccessMode != READ_ONLY )  {    throw IOException( -1, "File has not been opened for reading" );  }  return CREATE UnixFileInputStream( this );}////////////////////////////////////////////////////////////////////////////////p<OutputStream> UnixFile::getOutputStream( ) throw( IOException ){  throwIfNotOpen( );  if( m_eAccessMode == READ_ONLY )  {    throw IOException( -1, "File has been opened for read-only" );  }  return CREATE UnixFileOutputStream( this );}////////////////////////////////////////////////////////////////////////////////int UnixFile::convertAccessMode( File::AccessMode eAccessMode ) const{  int nResult;  switch( eAccessMode )  {    case WRITE_CREATE:    {      nResult = O_WRONLY | O_CREAT | O_TRUNC;      break;    }    case WRITE_APPEND:    {      nResult = O_WRONLY | O_APPEND;      break;    }    case READ_WRITE:    {      nResult = O_RDWR;      break;    }    case READ_ONLY:    {      nResult = O_RDONLY;      break;    }    default:    {      throw IOException( -1, "Unknown access mode" );      break;    }  }  return nResult;}////////////////////////////////////////////////////////////////////////////////void UnixFile::throwIfNotOpen( ) throw( IOException ){  if( hasBeenClosed( ) )  {    throw IOException( -1, "File has not been opened" );  }}////////////////////////////////////////////////////////////////////////////////size_t UnixFile::available( ) throw( IOException ){  return getDescriptor( ) -> getSize( ) - skip( 0 );}////////////////////////////////////////////////////////////////////////////////bool UnixFile::hasBeenClosed( ) throw( IOException ){  return m_nFileReadHandle == -1 && m_nFileWriteHandle == -1;}////////////////////////////////////////////////////////////////////////////////size_t UnixFile::skip(  size_t nNoOfBytes)  throw( IOException ){  int nResult;  throwIfNotOpen( );  nResult = lseek( m_nFileReadHandle, ( off_t ) nNoOfBytes, SEEK_CUR );  if( nResult == - 1 )  {    throw IOException( errno, "lseek returned an error" );  }  return ( size_t ) nResult;}////////////////////////////////////////////////////////////////////////////////size_t UnixFile::read(  void*   pBuffer,  size_t  nMaxNoOfBytes)  throw( IOException ){  int   nResult;  char* pcBuffer = ( ( char* ) pBuffer );  throwIfNotOpen(  );  nResult = ::read( m_nFileReadHandle, pcBuffer, nMaxNoOfBytes );  if( nResult == -1 )  {    throw IOException( errno, "Error while reading bytes from file" );  }  return ( size_t ) nResult;}////////////////////////////////////////////////////////////////////////////////void UnixFile::write(  const void* pBuffer,  size_t      nNoOfBytes)  throw( IOException ){  int nResult;  throwIfNotOpen( );  nResult = ::write( m_nFileWriteHandle, pBuffer, nNoOfBytes );  if( nNoOfBytes != ( size_t ) nResult )  {    throw IOException( errno, "Error while writing bytes to file" );  }}////////////////////////////////////////////////////////////////////////////////size_t UnixFile::rewind(  size_t nNoOfBytes) throw( more::io::IOException ){  off_t nResult;  throwIfNotOpen( );  nResult = lseek( m_nFileWriteHandle, 0 - ( off_t ) nNoOfBytes, SEEK_CUR );  if( nResult == - 1 )  {    throw IOException( errno, "lseek returned an error" );  }  return ( size_t ) nResult;}////////////////////////////////////////////////////////////////////////////////void UnixFile::flush( ) throw( IOException ){  throwIfNotOpen( );}////////////////////////////////////////////////////////////////////////////////UnixFileInputStream::UnixFileInputStream(  const p<UnixFile>& pUnixFile): m_pUnixFile( pUnixFile ){}////////////////////////////////////////////////////////////////////////////////size_t UnixFileInputStream::available( ) throw( IOException ){  return m_pUnixFile -> available( );}////////////////////////////////////////////////////////////////////////////////bool UnixFileInputStream::hasBeenClosed( ) throw( IOException ){  return m_pUnixFile -> hasBeenClosed( );}////////////////////////////////////////////////////////////////////////////////size_t UnixFileInputStream::skip(  size_t nNoOfBytes) throw( IOException ){  return m_pUnixFile -> skip( nNoOfBytes );}////////////////////////////////////////////////////////////////////////////////size_t UnixFileInputStream::read(  void*   pBuffer,  size_t  nMaxNoOfBytes) throw( IOException ){  return m_pUnixFile -> read( pBuffer, nMaxNoOfBytes );}////////////////////////////////////////////////////////////////////////////////UnixFileOutputStream::UnixFileOutputStream(  const p<UnixFile>& pUnixFile): m_pUnixFile( pUnixFile ){}////////////////////////////////////////////////////////////////////////////////void UnixFileOutputStream::write(  const void* pBuffer,  size_t      nNoOfBytes) throw( IOException ){  m_pUnixFile -> write( pBuffer, nNoOfBytes );}////////////////////////////////////////////////////////////////////////////////size_t UnixFileOutputStream::rewind(  size_t nNoOfBytes) throw( more::io::IOException ){  return m_pUnixFile -> rewind( nNoOfBytes );}////////////////////////////////////////////////////////////////////////////////bool UnixFileOutputStream::hasBeenClosed( ) throw( IOException ){  return m_pUnixFile -> hasBeenClosed( );}////////////////////////////////////////////////////////////////////////////////void UnixFileOutputStream::flush( ) throw( IOException ){  m_pUnixFile -> flush( );}////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -