📄 ncbifile.cpp
字号:
/* * =========================================================================== * PRODUCTION $Log: ncbifile.cpp,v $ * PRODUCTION Revision 1000.5 2004/06/01 19:09:09 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.78 * PRODUCTION * =========================================================================== *//* $Id: ncbifile.cpp,v 1000.5 2004/06/01 19:09:09 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Vladimir Ivanov * * File Description: Files and directories accessory functions * */#include <ncbi_pch.hpp>#include <corelib/ncbifile.hpp>#if !defined(NCBI_OS_MAC)# include <sys/types.h># if defined(HAVE_SYS_STAT_H)# include <sys/stat.h># endif#endif#include <stdio.h>#if defined(NCBI_OS_MSWIN)# include <corelib/ncbi_os_mswin.hpp># include <corelib/ncbi_limits.hpp># include <io.h># include <direct.h># include <sys/utime.h>#elif defined(NCBI_OS_UNIX)# include <unistd.h># include <dirent.h># include <pwd.h># include <fcntl.h># include <sys/mman.h># include <utime.h># if !defined(MAP_FAILED)# define MAP_FAILED ((void *) -1)# endif# if defined(NCBI_COMPILER_MW_MSL)# include <ncbi_mslextras.h># endif#elif defined(NCBI_OS_MAC)# include <fcntl.h># include <corelib/ncbi_os_mac.hpp># include <Script.h># include <Gestalt.h># include <Folders.h>#endif /* NCBI_OS_MSWIN, NCBI_OS_UNIX, NCBI_OS_MAC */BEGIN_NCBI_SCOPE// Path separators#undef DIR_SEPARATOR#undef DIR_SEPARATOR_ALT#undef DIR_SEPARATORS#undef DISK_SEPARATOR#undef ALL_SEPARATORS#undef ALL_OS_SEPARATORS#define DIR_PARENT ".."#define DIR_CURRENT "."#define ALL_OS_SEPARATORS ":/\\"#if defined(NCBI_OS_MSWIN)# define DIR_SEPARATOR '\\'# define DIR_SEPARATOR_ALT '/'# define DISK_SEPARATOR ':'# define DIR_SEPARATORS "/\\"# define ALL_SEPARATORS ":/\\"#elif defined(NCBI_OS_UNIX)# define DIR_SEPARATOR '/'# define DIR_SEPARATORS "/"# define ALL_SEPARATORS "/"#elif defined(NCBI_OS_MAC)# define DIR_SEPARATOR ':'# define DIR_SEPARATORS ':'# define ALL_SEPARATORS ":"# undef DIR_PARENT# undef DIR_CURRENT#endif////////////////////////////////////////////////////////////////////////////////// Static functions//#if defined(NCBI_OS_MAC)static const FSSpec sNullFSS = {0, 0, "\p"};static bool operator== (const FSSpec& one, const FSSpec& other){ return one.vRefNum == other.vRefNum && one.parID == other.parID && PString(one.name) == PString(other.name);}#endif /* NCBI_OS_MAC */// Construct real entry mode from parts. Parameters can not have "fDefault" // value.static CDirEntry::TMode s_ConstructMode(CDirEntry::TMode user_mode, CDirEntry::TMode group_mode, CDirEntry::TMode other_mode){ CDirEntry::TMode mode = 0; mode |= (user_mode << 6); mode |= (group_mode << 3); mode |= other_mode; return mode;}////////////////////////////////////////////////////////////////////////////////// CDirEntry//CDirEntry::CDirEntry()#if defined(NCBI_OS_MAC) : m_FSS(new FSSpec(sNullFSS))#endif{}#if defined(NCBI_OS_MAC)CDirEntry::CDirEntry(const CDirEntry& other) : m_FSS(new FSSpec(*other.m_FSS)){ m_DefaultMode[eUser] = other.m_DefaultMode[eUser]; m_DefaultMode[eGroup] = other.m_DefaultMode[eGroup]; m_DefaultMode[eOther] = other.m_DefaultMode[eOther];}CDirEntry&CDirEntry::operator= (const CDirEntry& other){ *m_FSS = *other.m_FSS; m_DefaultMode[eUser] = other.m_DefaultMode[eUser]; m_DefaultMode[eGroup] = other.m_DefaultMode[eGroup]; m_DefaultMode[eOther] = other.m_DefaultMode[eOther]; return *this;}CDirEntry::CDirEntry(const FSSpec& fss) : m_FSS(new FSSpec(fss)){ m_DefaultMode[eUser] = m_DefaultModeGlobal[eFile][eUser]; m_DefaultMode[eGroup] = m_DefaultModeGlobal[eFile][eGroup]; m_DefaultMode[eOther] = m_DefaultModeGlobal[eFile][eOther];}#endifCDirEntry::CDirEntry(const string& name)#if defined(NCBI_OS_MAC) : m_FSS(new FSSpec(sNullFSS))#endif{ Reset(name); m_DefaultMode[eUser] = m_DefaultModeGlobal[eFile][eUser]; m_DefaultMode[eGroup] = m_DefaultModeGlobal[eFile][eGroup]; m_DefaultMode[eOther] = m_DefaultModeGlobal[eFile][eOther];}#if defined(NCBI_OS_MAC)bool CDirEntry::operator== (const CDirEntry& other) const{ return *m_FSS == *other.m_FSS;}const FSSpec& CDirEntry::FSS() const{ return *m_FSS;}#endifCDirEntry::TMode CDirEntry::m_DefaultModeGlobal[eUnknown][3] ={ // eFile { CDirEntry::fDefaultUser, CDirEntry::fDefaultGroup, CDirEntry::fDefaultOther }, // eDir { CDirEntry::fDefaultDirUser, CDirEntry::fDefaultDirGroup, CDirEntry::fDefaultDirOther }, // ePipe { CDirEntry::fDefaultUser, CDirEntry::fDefaultGroup, CDirEntry::fDefaultOther }, // eLink { CDirEntry::fDefaultUser, CDirEntry::fDefaultGroup, CDirEntry::fDefaultOther }, // eSocket { CDirEntry::fDefaultUser, CDirEntry::fDefaultGroup, CDirEntry::fDefaultOther }, // eDoor { CDirEntry::fDefaultUser, CDirEntry::fDefaultGroup, CDirEntry::fDefaultOther }, // eBlockSpecial { CDirEntry::fDefaultUser, CDirEntry::fDefaultGroup, CDirEntry::fDefaultOther }, // eCharSpecial { CDirEntry::fDefaultUser, CDirEntry::fDefaultGroup, CDirEntry::fDefaultOther }};#if defined(NCBI_OS_MAC)void CDirEntry::Reset(const string& path){ OSErr err = MacPathname2FSSpec(path.c_str(), m_FSS); if (err != noErr && err != fnfErr) { *m_FSS = sNullFSS; }}string CDirEntry::GetPath(void) const{ OSErr err; char* path; err = MacFSSpec2FullPathname(&FSS(), &path); if (err != noErr) { return kEmptyStr; } return string(path);}#endifCDirEntry::~CDirEntry(void){#if defined(NCBI_OS_MAC) delete m_FSS;#endif}void CDirEntry::SplitPath(const string& path, string* dir, string* base, string* ext){ // Get file name size_t pos = path.find_last_of(ALL_SEPARATORS); string filename = (pos == NPOS) ? path : path.substr(pos+1); // Get dir if ( dir ) { *dir = (pos == NPOS) ? kEmptyStr : path.substr(0, pos+1); } // Split file name to base and extension pos = filename.rfind('.'); if ( base ) { *base = (pos == NPOS) ? filename : filename.substr(0, pos); } if ( ext ) { *ext = (pos == NPOS) ? kEmptyStr : filename.substr(pos); }}string CDirEntry::MakePath(const string& dir, const string& base, const string& ext){ string path; // Adding "dir" and file base if ( dir.length() ) { path = AddTrailingPathSeparator(dir); } path += base; // Adding extension if ( ext.length() && ext.at(0) != '.' ) { path += '.'; } path += ext; // Return result return path;}char CDirEntry::GetPathSeparator(void) { return DIR_SEPARATOR;}bool CDirEntry::IsPathSeparator(const char c){#if defined(DISK_SEPARATOR) if ( c == DISK_SEPARATOR ) { return true; }#endif#if defined(DIR_SEPARATOR_ALT) if ( c == DIR_SEPARATOR_ALT ) { return true; }#endif return c == DIR_SEPARATOR;}string CDirEntry::AddTrailingPathSeparator(const string& path){ size_t len = path.length();#if defined(NCBI_OS_MAC) if ( !len ) { return string(1,GetPathSeparator()); }#endif if (len && string(ALL_SEPARATORS).rfind(path.at(len - 1)) == NPOS) { return path + GetPathSeparator(); } return path;}string CDirEntry::DeleteTrailingPathSeparator(const string& path){ size_t pos = path.find_last_not_of(DIR_SEPARATORS); if (pos + 1 < path.length()) { return path.substr(0, pos + 1); } return path;}bool CDirEntry::IsAbsolutePath(const string& path){ if ( path.empty() ) return false; char first = path[0];#if defined(NCBI_OS_MAC) if ( path.find(DIR_SEPARATOR) != NPOS && first != DIR_SEPARATOR ) return true;#else if ( IsPathSeparator(first) ) return true;#endif#if defined(DISK_SEPARATOR) if ( path.find(DISK_SEPARATOR) != NPOS ) return true;#endif return false;}bool CDirEntry::IsAbsolutePathEx(const string& path){ if ( path.empty() ) return false; char first = path[0]; // MAC or WIN absolute if ( path.find(':') != NPOS && first != ':' ) return true; // MAC relative path if ( first == ':' ) return false; // UNIX or WIN absolute if ( first == '\\' || first == '/' ) return true; // Else - relative return false;}/// Helper : strips dir to parts:/// c:\a\b\ will be <c:><a><b>/// /usr/bin/ will be </><usr><bin>static void s_StripDir(const string& dir, vector<string> * dir_parts){ dir_parts->clear(); if ( dir.empty() ) return; const char sep = CDirEntry::GetPathSeparator(); size_t sep_pos = 0; size_t last_ind = dir.length() - 1; size_t part_start = 0; for (;;) { sep_pos = dir.find(sep, sep_pos); if (sep_pos == NPOS) { dir_parts->push_back(string(dir, part_start, dir.length() - part_start)); break; } // If path starts from '/' - it's a root directory if (sep_pos == 0) { dir_parts->push_back(string(1, sep)); } else { dir_parts->push_back(string(dir, part_start, sep_pos - part_start)); } sep_pos++; part_start = sep_pos; if (sep_pos >= last_ind) break; }}string CDirEntry::CreateRelativePath( const string& path_from, const string& path_to ){#if defined(NCBI_OS_MSWIN) || defined(NCBI_OS_UNIX) string path; // the result if ( !IsAbsolutePath(path_from) ) { NCBI_THROW(CFileException, eRelativePath, "path_from is not absolute path"); } if ( !IsAbsolutePath(path_to) ) { NCBI_THROW(CFileException, eRelativePath,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -