📄 ncbifile.cpp
字号:
} if ( other_mode ) { *other_mode = m_DefaultMode[eOther]; }}bool CDirEntry::GetTime(CTime *modification, CTime *creation, CTime *last_access) const{ struct stat st; if (stat(GetPath().c_str(), &st) != 0) { return false; } if (modification) { modification->SetTimeT(st.st_mtime); } if (creation) { creation->SetTimeT(st.st_ctime); } if (last_access) { last_access->SetTimeT(st.st_atime); } return true;}bool CDirEntry::SetTime(CTime *modification, CTime *last_access) const{ struct utimbuf times; times.modtime = modification ? modification->GetTimeT() : time(0); times.actime = last_access ? last_access->GetTimeT() : time(0); return utime(GetPath().c_str(), ×) == 0;} CDirEntry::EType CDirEntry::GetType(EFollowLinks follow) const{#if defined(NCBI_OS_MAC) OSErr err; long dirID; Boolean isDir; err = FSpGetDirectoryID(&FSS(), &dirID, &isDir); if ( err ) { return eUnknown; } return isDir ? eDir : eFile;#else struct stat st; int errcode;# if defined(NCBI_OS_MSWIN) errcode = stat(GetPath().c_str(), &st);# elif defined(NCBI_OS_UNIX) if (follow == eFollowLinks) { errcode = stat(GetPath().c_str(), &st); } else { errcode = lstat(GetPath().c_str(), &st); }# endif if (errcode != 0) { return eUnknown; } unsigned int mode = st.st_mode & S_IFMT; switch (mode) { case S_IFDIR: return eDir; case S_IFCHR: return eCharSpecial;# if defined(NCBI_OS_MSWIN) case _S_IFIFO: return ePipe;# elif defined(NCBI_OS_UNIX) case S_IFIFO: return ePipe; case S_IFLNK: return eLink; case S_IFSOCK: return eSocket; case S_IFBLK: return eBlockSpecial;# if defined(S_IFDOOR) /* only Solaris seems to have this */ case S_IFDOOR: return eDoor;# endif# endif } // Check regular file bit last if ( (st.st_mode & S_IFREG) == S_IFREG ) { return eFile; } return eUnknown;#endif}bool CDirEntry::Rename(const string& newname){#if defined(NCBI_OS_MAC) const int maxFilenameLength = 31; if (newname.length() > maxFilenameLength) return false; Str31 newNameStr; Pstrcpy(newNameStr, newname.c_str()); OSErr err = FSpRename(&FSS(), newNameStr); if (err != noErr) return false;#else if (rename(GetPath().c_str(), newname.c_str()) != 0) { return false; }#endif Reset(newname); return true;}bool CDirEntry::Remove(EDirRemoveMode mode) const{#if defined(NCBI_OS_MAC) OSErr err = ::FSpDelete(&FSS()); return err == noErr;#else if ( IsDir(eIgnoreLinks) ) { if (mode == eOnlyEmpty) { return rmdir(GetPath().c_str()) == 0; } else { CDir dir(GetPath()); return dir.Remove(eRecursive); } } else { return remove(GetPath().c_str()) == 0; }#endif}////////////////////////////////////////////////////////////////////////////////// CFile//CFile::CFile(const string& filename) : CParent(filename){ // Set default mode SetDefaultMode(eFile, fDefault, fDefault, fDefault);}CFile::~CFile(void){ return;}Int8 CFile::GetLength(void) const{#if defined(NCBI_OS_MAC) long dataSize, rsrcSize; OSErr err = FSpGetFileSize(&FSS(), &dataSize, &rsrcSize); if (err != noErr) { return -1; } else { return dataSize; }#else struct stat buf; if ( stat(GetPath().c_str(), &buf) != 0 ) { return -1; } return buf.st_size;#endif}string CFile::GetTmpName(ETmpFileCreationMode mode){#if defined(NCBI_OS_MSWIN) || defined(NCBI_OS_UNIX) return GetTmpNameEx(kEmptyStr, kEmptyStr, mode);#else if (mode == eTmpFileCreate) { ERR_POST(Warning << "CFile::GetTmpNameEx: " "The temporary file cannot be auto-created on this " \ "platform, returns its name only"); } char* filename = tempnam(0,0); if ( !filename ) { return kEmptyStr; } string res(filename); free(filename); return res;#endif}#if !defined(NCBI_OS_UNIX)static string s_StdGetTmpName(const char* dir, const char* prefix){ char* filename = tempnam(dir, prefix); if ( !filename ) { return kEmptyStr; } string str(filename); free(filename); return str;}#endifstring CFile::GetTmpNameEx(const string& dir, const string& prefix, ETmpFileCreationMode mode){#if defined(NCBI_OS_MSWIN) || defined(NCBI_OS_UNIX) string x_dir = dir; if ( x_dir.empty() ) { x_dir = CDir::GetTmpDir(); } if ( !x_dir.empty() ) { x_dir = AddTrailingPathSeparator(x_dir); } string fn;# if defined(NCBI_OS_UNIX) string pattern = x_dir + prefix + "XXXXXX"; AutoPtr<char, CDeleter<char> > filename(strdup(pattern.c_str())); int fd = mkstemp(filename.get()); close(fd); if (mode != eTmpFileCreate) { remove(filename.get()); } fn = filename.get();# elif defined(NCBI_OS_MSWIN) if (mode == eTmpFileGetName) { fn = s_StdGetTmpName(dir.c_str(), prefix.c_str()); } else { char buffer[MAX_PATH]; HANDLE hFile = INVALID_HANDLE_VALUE; srand((unsigned)time(0)); unsigned long ofs = rand(); while ( ofs < numeric_limits<unsigned long>::max() ) { _ultoa((unsigned long)ofs, buffer, 24); fn = x_dir + prefix + buffer; hFile = CreateFile(fn.c_str(), GENERIC_ALL, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_TEMPORARY, NULL); if (hFile != INVALID_HANDLE_VALUE) { break; } ofs++; } CloseHandle(hFile); if (ofs == numeric_limits<unsigned long>::max() ) { return kEmptyStr; } if (mode != eTmpFileCreate) { remove(fn.c_str()); } }# endif#else // defined(NCBI_OS_MSWIN) || defined(NCBI_OS_UNIX) if (mode == eTmpFileCreate) { ERR_POST(Warning << "CFile::GetTmpNameEx: " "The file cannot be auto-created on this platform, " \ "return its name only"); } fn = s_StdGetTmpName(dir.c_str(), prefix.c_str());#endif return fn;}class CTmpStream : public fstream{public: CTmpStream(const char *s, IOS_BASE::openmode mode) : fstream(s, mode) { m_FileName = s; } virtual ~CTmpStream(void) { close(); CFile(m_FileName).Remove(); }protected: string m_FileName;};fstream* CFile::CreateTmpFile(const string& filename, ETextBinary text_binary, EAllowRead allow_read){ ios::openmode mode = ios::out | ios::trunc; if ( text_binary == eBinary ) { mode = mode | ios::binary; } if ( allow_read == eAllowRead ) { mode = mode | ios::in; } string tmpname = filename.empty() ? GetTmpName(eTmpFileCreate) : filename; if ( tmpname.empty() ) { return 0; } fstream* stream = new CTmpStream(tmpname.c_str(), mode); return stream;}fstream* CFile::CreateTmpFileEx(const string& dir, const string& prefix, ETextBinary text_binary, EAllowRead allow_read){ return CreateTmpFile(GetTmpNameEx(dir, prefix, eTmpFileCreate), text_binary, allow_read);}////////////////////////////////////////////////////////////////////////////////// CDir//CDir::CDir(void){ return;}#if defined(NCBI_OS_MAC)CDir::CDir(const FSSpec& fss) : CParent(fss){ // Set default mode SetDefaultMode(eDir, fDefault, fDefault, fDefault);}#endifCDir::CDir(const string& dirname) : CParent(dirname){ // Set default mode SetDefaultMode(eDir, fDefault, fDefault, fDefault);}#if defined(NCBI_OS_UNIX)static bool s_GetHomeByUID(string& home){ // Get the info using user ID struct passwd *pwd; if ((pwd = getpwuid(getuid())) == 0) { return false; } home = pwd->pw_dir; return true;}static bool s_GetHomeByLOGIN(string& home){ char* ptr = 0; // Get user name if ( !(ptr = getenv("USER")) ) { if ( !(ptr = getenv("LOGNAME")) ) { if ( !(ptr = getlogin()) ) { return false; } } } // Get home dir for this user struct passwd* pwd = getpwnam(ptr); if ( !pwd || pwd->pw_dir[0] == '\0') { return false; } home = pwd->pw_dir; return true;}#endif // NCBI_OS_UNIXstring CDir::GetHome(void){ char* str; string home;#if defined(NCBI_OS_MSWIN) // Get home dir from environment variables // like - C:\Documents and Settings\user\Application Data str = getenv("APPDATA"); if ( str ) { home = str; } else { // like - C:\Documents and Settings\user str = getenv("USERPROFILE"); if ( str ) { home = str; } } #elif defined(NCBI_OS_UNIX) // Try get home dir from environment variable str = getenv("HOME"); if ( str ) { home = str; } else { // Try to retrieve the home dir -- first use user's ID, and if failed, // then use user's login name. if ( !s_GetHomeByUID(home) ) { s_GetHomeByLOGIN(home); } } #elif defined(NCBI_OS_MAC) // Make sure we can use FindFolder() if not, report error long gesResponse; if (Gestalt(gestaltFindFolderAttr, &gesResponse) != noErr || (gesResponse & (1 << gestaltFindFolderPresent) == 0)) { return kEmptyStr; } // Store the current active directory long saveDirID; short saveVRefNum; HGetVol((StringPtr) 0, &saveVRefNum, &saveDirID); // Find the preferences folder in the active System folder short vRefNum; long dirID; OSErr err = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder, &vRefNum, &dirID); FSSpec spec; if (err == noErr) { char dummy[FILENAME_MAX+1]; dummy [0] = '\0'; err = FSMakeFSSpec(vRefNum, dirID, (StringPtr) dummy, &spec); } // Restore the current active directory HSetVol((StringPtr) 0, saveVRefNum, saveDirID); // Convert to C++ string if (err == noErr) { str = 0; err = MacFSSpec2FullPathname(&spec, &str); home = str; }#endif // Add trailing separator if needed return AddTrailingPathSeparator(home);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -