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

📄 ncbifile.cpp

📁 ncbi源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
string CDir::GetTmpDir(void){    string tmp;#if defined(NCBI_OS_UNIX)    char* tmpdir = getenv("TMPDIR");    if ( tmpdir ) {        tmp = tmpdir;    } else  {#  if defined(P_tmpdir)        tmp = P_tmpdir;#  else        tmp = "/tmp";#  endif    }#elif defined(NCBI_OS_MSWIN)    char* tmpdir = getenv("TEMP");    if ( tmpdir ) {        tmp = tmpdir;    } else  {#  if defined(P_tmpdir)        tmp = P_tmpdir;#  else        tmp = CDir::GetHome();#  endif    }#endif    return tmp;}string CDir::GetCwd(){    string cwd;#if defined(NCBI_OS_UNIX)    char buf[4096];    if (getcwd(buf, sizeof(buf) - 1)) {        cwd = buf;    }#elif defined(NCBI_OS_MSWIN)    char buf[4096];    if (_getcwd(buf, sizeof(buf) - 1)) {        cwd = buf;    }#endif    return cwd;}CDir::~CDir(void){    return;}#if defined(NCBI_OS_MAC)static const CDirEntry MacGetIndexedItem(const CDir& container, SInt16 index){    FSSpec dir = container.FSS();    FSSpec fss;     // FSSpec of item gotten.    SInt16 actual;  // Actual number of items gotten.  Should be one or zero.    SInt16 itemIndex = index;    OSErr err = GetDirItems(dir.vRefNum, dir.parID, dir.name, true, true,                             &fss, 1, &actual, &itemIndex);    if (err != noErr) {        throw err;    }    return CDirEntry(fss);}#endifCDir::TEntries CDir::GetEntries(const string&   mask,                                EGetEntriesMode mode) const{    TEntries contents;    string x_mask    = mask.empty() ? string("*") : mask;    string path_base = GetPath();    if ( path_base[path_base.size() - 1] != GetPathSeparator() ) {        path_base += GetPathSeparator();    }#if defined(NCBI_OS_MSWIN)    // Append to the "path" mask for all files in directory    string pattern = path_base + x_mask;    // Open directory stream and try read info about first entry    struct _finddata_t entry;    long desc = _findfirst(pattern.c_str(), &entry);  // get first entry's name    if (desc != -1) {        CDirEntry* dir_entry = new CDirEntry(path_base + entry.name);        if (mode == eIgnoreRecursive) {            if ((::strcmp(entry.name, ".") == 0) ||                (::strcmp(entry.name, "..") == 0))             {                delete dir_entry;                dir_entry = 0;            }        }        if (dir_entry)            contents.push_back(dir_entry);        while ( _findnext(desc, &entry) != -1 ) {            if (mode == eIgnoreRecursive) {                if ((::strcmp(entry.name, ".") == 0) ||                    (::strcmp(entry.name, "..") == 0)) continue;            }            contents.push_back(new CDirEntry(path_base + entry.name));        }        _findclose(desc);    }#elif defined(NCBI_OS_UNIX)    DIR* dir = opendir(GetPath().c_str());    if ( dir ) {        while (struct dirent* entry = readdir(dir)) {            if ( MatchesMask(entry->d_name, x_mask.c_str()) ) {                if (mode == eIgnoreRecursive) {                    if ((::strcmp(entry->d_name, ".") == 0) ||                        (::strcmp(entry->d_name, "..") == 0)) continue;                }                contents.push_back(new CDirEntry(path_base + entry->d_name));            }        }        closedir(dir);    }#elif defined(NCBI_OS_MAC)    try {        for (int index = 1;  ;  index++) {            CDirEntry entry = MacGetIndexedItem(*this, index);            if ( MatchesMask(entry.GetName().c_str(), x_mask.c_str()) ) {                contents.push_back(new CDirEntry(entry));            }        }    } catch (OSErr& err) {        if (err != fnfErr) {            throw COSErrException_Mac(err, "CDir::GetEntries() ");        }    }#endif    return contents;}CDir::TEntries CDir::GetEntries(const vector<string>&  masks,                                EGetEntriesMode        mode) const{    if (masks.empty())        return GetEntries();    TEntries contents;    string path_base = GetPath();    if ( path_base[path_base.size() - 1] != GetPathSeparator() ) {        path_base += GetPathSeparator();    }#if defined(NCBI_OS_MSWIN)    // Append to the "path" mask for all files in directory    string pattern = path_base + string("*");    bool skip_recursive_entry;    // Open directory stream and try read info about first entry    struct _finddata_t entry;    long desc = _findfirst(pattern.c_str(), &entry);  // get first entry's name    if (desc != -1) {        skip_recursive_entry =            (mode == eIgnoreRecursive) &&            ((::strcmp(entry.name, ".") == 0) ||             (::strcmp(entry.name, "..") == 0));        // check all masks        if (!skip_recursive_entry) {            ITERATE(vector<string>, it, masks) {                const string& mask = *it;                if (mask.empty()) {                    contents.push_back(new CDirEntry(path_base + entry.name));                    break;                }                if (MatchesMask(entry.name, mask.c_str()) ) {                    contents.push_back(new CDirEntry(path_base + entry.name));                    break;                }                            } // ITERATE        }        while ( _findnext(desc, &entry) != -1 ) {            skip_recursive_entry =                (mode == eIgnoreRecursive) &&                ((::strcmp(entry.name, ".") == 0) ||                 (::strcmp(entry.name, "..") == 0));            if (skip_recursive_entry) {                continue;            }            ITERATE(vector<string>, it, masks) {                const string& mask = *it;                if (mask.empty()) {                    contents.push_back(new CDirEntry(path_base + entry.name));                    break;                }                if (MatchesMask(entry.name, mask.c_str()) ) {                    contents.push_back(new CDirEntry(path_base + entry.name));                    break;                }            } // ITERATE        }        _findclose(desc);    }#elif defined(NCBI_OS_UNIX)    DIR* dir = opendir(GetPath().c_str());    if ( dir ) {        while (struct dirent* entry = readdir(dir)) {            bool skip_recursive_entry =                    (mode == eIgnoreRecursive) &&                    ((::strcmp(entry->d_name, ".") == 0) ||                     (::strcmp(entry->d_name, "..") == 0));            if (skip_recursive_entry) {                continue;            }            ITERATE(vector<string>, it, masks) {                const string& mask = *it;                if (mask.empty()) {                    contents.push_back(                         new CDirEntry(path_base + entry->d_name));                    break;                }                if ( MatchesMask(entry->d_name, mask.c_str()) ) {                    contents.push_back(                         new CDirEntry(path_base + entry->d_name));                    break;                }            } // ITERATE        } // while        closedir(dir);    }#elif defined(NCBI_OS_MAC)    try {        for (int index = 1;  ;  index++) {            CDirEntry entry = MacGetIndexedItem(*this, index);            ITERATE(vector<string>, it, masks) {                const string& mask = *it;                if (mask.empty()) {                    contents.push_back(new CDirEntry(entry));                    break;                }                if ( MatchesMask(entry.GetName().c_str(), mask.c_str()) ) {                    contents.push_back(new CDirEntry(entry));                    break;                }            }        }    } catch (OSErr& err) {        if (err != fnfErr) {            throw COSErrException_Mac(err, "CDir::GetEntries() ");        }    }#endif    return contents;}bool CDir::Create(void) const{    TMode user_mode, group_mode, other_mode;    GetDefaultMode(&user_mode, &group_mode, &other_mode);    TMode mode = s_ConstructMode(user_mode, group_mode, other_mode);#if defined(NCBI_OS_MSWIN)    if ( mkdir(GetPath().c_str()) != 0 ) {        return false;    }    return chmod(GetPath().c_str(), mode) == 0;#elif defined(NCBI_OS_UNIX)    return mkdir(GetPath().c_str(), mode) == 0;#elif defined(NCBI_OS_MAC)    OSErr err;    long dirID;	    err = ::FSpDirCreate(&FSS(), smRoman, &dirID);    return err == noErr;#endif}bool CDir::CreatePath(void) const{    if (Exists()) {        return true;    }    string path(GetPath());    if (path.empty()) {        return true;    }    if (path[path.length()-1] == GetPathSeparator()) {        path.erase(path.length() - 1);    }    CDir dir_this(path);    if (dir_this.Exists()) {        return true;    }    string path_up = dir_this.GetDir();    if (path_up == path) {        // special case: is this a disk name?        return true;    }     CDir dir_up(path_up);    if (dir_up.CreatePath()) {        return dir_this.Create();    }    return false;}bool CDir::Remove(EDirRemoveMode mode) const{    // Remove directory as empty    if ( mode == eOnlyEmpty ) {        return CParent::Remove(eOnlyEmpty);    }    // Read all entryes in derectory    TEntries contents = GetEntries();    // Remove    ITERATE(TEntries, entry, contents) {        string name = (*entry)->GetName();#if defined(NCBI_OS_MAC)        CDirEntry& item = **entry;#else        if ( name == "."  ||  name == ".."  ||               name == string(1,GetPathSeparator()) ) {            continue;        }        // Get entry item with full pathname        CDirEntry item(GetPath() + GetPathSeparator() + name);#endif        if ( mode == eRecursive ) {            if ( !item.Remove(eRecursive) ) {                return false;            }        } else {            if ( item.IsDir(eIgnoreLinks) ) {                continue;            }            if ( !item.Remove() ) {                return false;            }        }    }    // Remove main directory    return CParent::Remove(eOnlyEmpty);}////////////////////////////////////////////////////////////////////////////////// CMemoryFile//// Platform-dependent memory file handle definitionstruct SMemoryFileHandle {#if defined(NCBI_OS_MSWIN)    HANDLE  hMap;#else    int     hDummy;#endif};bool CMemoryFile::IsSupported(void){#if defined(NCBI_OS_MAC)    return false;#else    return true;#endif}CMemoryFile::CMemoryFile(const string&  file_name,                         EMemMapProtect protect,                         EMemMapShare   share)    : m_Handle(0), m_Size(-1), m_DataPtr(0){    x_Map(file_name, protect, share);    if (GetSize() < 0) {        NCBI_THROW(CFileException, eMemoryMap,                   "File memory mapping cannot be created");    }}CMemoryFile::~CMemoryFile(void){    Unmap();}void CMemoryFile::x_Map(const string&  file_name,                        EMemMapProtect protect_attr,                        EMemMapShare   share_attr){    if ( !IsSupported() )        return;    m_Handle = new SMemoryFileHandle();    for (;;) { // quasi-TRY block        CFile file(file_name);        m_Size = file.GetLength();        if (m_Size < 0)            break;        // Special case        if (m_Size == 0)            return;	#if defined(NCBI_OS_MSWIN)        // Name of a file-mapping object cannot contain '\'        string x_name = NStr::Replace(file_name, "\\", "/");        // Translate attributes         DWORD map_protect = 0, map_access = 0, file_share = 0, file_access = 0;        switch (protect_attr) {            case eMMP_Read:                map_access  = FILE_MAP_READ;                map_protect = PAGE_READONLY;                file_access = GENERIC_READ;                break;            case eMMP_Write:            case eMMP_ReadWrite:                // On MS Windows platform Write & ReadWrite access                // to the mapped memory is equivalent                if  (share_attr == eMMS_Shared ) {                    map_access = FILE_MAP_ALL_ACCESS;

⌨️ 快捷键说明

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