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

📄 unixfile.cpp

📁 "More for C++" is a class library that provides some features that are usually common for object ori
💻 CPP
📖 第 1 页 / 共 2 页
字号:
////  This file is part of the "More for C++" library////  Copyright (c) 1999-2003 by Thorsten Goertz (thorsten@morefor.org)////  The "More for C++" library is free software; you can redistribute it and/or//  modify it under the terms of the license that comes with this package.////  Read "license.txt" for more details.////  THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED//  WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES//  OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.////////////////////////////////////////////////////////////////////////////////#include <errno.h>#include <fcntl.h>#include <more/create.hpp>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/stat.h>#include <sys/types.h>#include "unixfile.hpp"using namespace more;using namespace more::io;using namespace more::os;using namespace more::os::unix_;////////////////////////////////////////////////////////////////////////////////UnixDirectory::UnixEntry::UnixEntry(  const p<Directory::Entry>&  pParent,  const String&               sName): m_pParent( pParent ), m_sName( sName ){  if( pParent != 0 && pParent -> isDirectory( ) )  {    m_sFullName << pParent -> getFullName( ) << '/' << sName;  }}////////////////////////////////////////////////////////////////////////////////UnixDirectory::UnixEntry::UnixEntry(  const String& sFullName){  char  cFullName[2048];  char* pcRealName;  pcRealName = realpath( sFullName, cFullName );  if( pcRealName != 0 )  {    char* pcLastSlash = strrchr( pcRealName, '/' );    if( pcLastSlash != 0 )    {      m_sName = pcLastSlash + 1;      m_sFullName = cFullName;    }  }}////////////////////////////////////////////////////////////////////////////////String UnixDirectory::UnixEntry::getName( ) throw( IOException ){  throwIfInvalid( );  return m_sName;}////////////////////////////////////////////////////////////////////////////////String UnixDirectory::UnixEntry::getFullName( ) throw( IOException ){  throwIfInvalid( );  return m_sFullName;}////////////////////////////////////////////////////////////////////////////////String UnixDirectory::UnixEntry::getExtension( ) throw( IOException ){  throwIfInvalid( );  String      sResult;  const char* pcDot = strrchr( m_sName, '.' );  if( pcDot != 0 )  {    sResult = ++pcDot;  }  return sResult;}////////////////////////////////////////////////////////////////////////////////p<Directory::Entry> UnixDirectory::UnixEntry::getParent( ) throw( IOException ){  throwIfInvalid( );  if( m_pParent == 0 )  {    const char* pcFullName = m_sFullName;    char*       pcLastSlash = strrchr( pcFullName, '/' );    String      sFullNameOfParent( m_sFullName.getPrefix( pcLastSlash - pcFullName ) );    m_pParent = CREATE UnixEntry( sFullNameOfParent );  }  return m_pParent;}////////////////////////////////////////////////////////////////////////////////bool UnixDirectory::UnixEntry::isDirectory( ) throw( IOException ){  throwIfInvalid( );  struct stat stats;  if( lstat( m_sFullName, &stats ) < 0 )  {    throw IOException( -1, "UnixEntry::isDirectory: Invalid directory entry for " + m_sFullName );  }  return S_ISDIR( stats.st_mode );}////////////////////////////////////////////////////////////////////////////////bool UnixDirectory::UnixEntry::isReadOnly( ) throw( IOException ){  throwIfInvalid( );  struct stat stats;  if( lstat( m_sFullName, &stats ) < 0 )  {    throw IOException( -1, "UnixEntry::isReadOnly: Invalid directory entry for " + m_sFullName );  }  return S_ISREG( stats.st_mode ) && ( access( m_sFullName, W_OK ) < 0 );}////////////////////////////////////////////////////////////////////////////////size_t UnixDirectory::UnixEntry::getSize( ) throw( IOException ){  throwIfInvalid( );  struct stat stats;  if( lstat( m_sFullName, &stats ) < 0 )  {    throw IOException( -1, "UnixEntry::getSize: Invalid directory entry for " + m_sFullName );  }  return stats.st_size;}////////////////////////////////////////////////////////////////////////////////void UnixDirectory::UnixEntry::throwIfInvalid( ) const{  if( m_sFullName.getLength( ) == 0 )  {    throw IOException( -1, "Invalid directory entry for " + m_sFullName );  }}////////////////////////////////////////////////////////////////////////////////UnixDirectory::UnixDirectory(  const p<Directory>& pParent,  const String&       sName): m_pCurrentEntry( 0 ),  m_pDirHandle( 0 ){  try  {    m_sFullName << pParent -> getDescriptor( ) -> getFullName( ) << '/' << sName;  }  catch( ... )  {  }}////////////////////////////////////////////////////////////////////////////////UnixDirectory::UnixDirectory(  const p<Directory::Entry>& pDescriptor): m_pDescriptor( pDescriptor ),  m_pCurrentEntry( 0 ),  m_pDirHandle( 0 ){  try  {    m_sFullName = pDescriptor -> getFullName( );  }  catch( ... )  {  }}////////////////////////////////////////////////////////////////////////////////UnixDirectory::UnixDirectory(  const String& sFullName): m_sFullName( sFullName ),  m_pCurrentEntry( 0 ),  m_pDirHandle( 0 ){}////////////////////////////////////////////////////////////////////////////////void UnixDirectory::finalize( ){  if( m_pDirHandle != 0 )  {    closedir( m_pDirHandle );    m_pDirHandle = 0;  }}////////////////////////////////////////////////////////////////////////////////bool UnixDirectory::exists( ) throw( IOException ){  struct stat stats;  lstat( m_sFullName, &stats );  return S_ISDIR( stats.st_mode );}////////////////////////////////////////////////////////////////////////////////p<Directory::Entry> UnixDirectory::getDescriptor( ) throw( IOException ){  if( m_pDescriptor == 0 )  {    m_pDescriptor = CREATE UnixDirectory::UnixEntry( m_sFullName );  }  return m_pDescriptor;}////////////////////////////////////////////////////////////////////////////////p<Directory> UnixDirectory::getParent( ) throw( IOException ){  return CREATE UnixDirectory( getDescriptor( ) -> getParent( ) );}////////////////////////////////////////////////////////////////////////////////p<Directory::Entry> UnixDirectory::getFirstEntry( ) throw( IOException ){  p<Directory::Entry> pDescriptor = getDescriptor( );  m_pCurrentEntry = 0;  if( m_pDirHandle != 0 )  {    closedir( m_pDirHandle );  }  m_pDirHandle = opendir( pDescriptor -> getFullName( ) );  if( m_pDirHandle != 0 )  {    struct dirent* pDirEntry = readdir( m_pDirHandle );    while( pDirEntry != 0 && (  strcmp( pDirEntry -> d_name, "."  ) == 0 ||                  strcmp( pDirEntry -> d_name, ".." ) == 0    ) )    {      pDirEntry = readdir( m_pDirHandle );    }    if( pDirEntry != 0 )    {      m_pCurrentEntry = CREATE UnixDirectory::UnixEntry( pDescriptor, pDirEntry -> d_name );    }    else    {      m_pCurrentEntry = 0;      closedir( m_pDirHandle );      m_pDirHandle = 0;    }  }  return m_pCurrentEntry;}////////////////////////////////////////////////////////////////////////////////p<Directory::Entry> UnixDirectory::getNextEntry( ) throw( IOException ){  if( m_pDirHandle != 0 )  {    struct dirent* pDirEntry = readdir( m_pDirHandle );    if( pDirEntry != 0 && m_pCurrentEntry != 0 )    {      m_pCurrentEntry = CREATE UnixDirectory::UnixEntry( m_pDescriptor, pDirEntry -> d_name );    }    else    {      m_pCurrentEntry = 0;      closedir( m_pDirHandle );      m_pDirHandle = 0;    }  }  return m_pCurrentEntry;}////////////////////////////////////////////////////////////////////////////////UnixFile::UnixFile(  const p<Directory>& pParent,  const String&       sName): m_sFullName( pParent -> getDescriptor( ) -> getFullName( ) + '/' + sName ),  m_pDescriptor( 0 ),  m_nFileReadHandle( -1 ),  m_nFileWriteHandle( -1 ){}////////////////////////////////////////////////////////////////////////////////UnixFile::UnixFile(  const p<Directory::Entry>& pDescriptor): m_pDescriptor( pDescriptor ),  m_nFileReadHandle( -1 ),  m_nFileWriteHandle( -1 ){  try  {    m_sFullName = pDescriptor -> getFullName( );  }  catch( ... )  {  }}////////////////////////////////////////////////////////////////////////////////UnixFile::UnixFile(  const String& sFullName): m_sFullName( sFullName ),  m_pDescriptor( 0 ),  m_nFileReadHandle( -1 ),  m_nFileWriteHandle( -1 ){}////////////////////////////////////////////////////////////////////////////////void UnixFile::finalize( ){  if( !hasBeenClosed( ) )  {    close( );

⌨️ 快捷键说明

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