ptlib.cxx
来自「pwlib源码库」· CXX 代码 · 共 1,293 行 · 第 1/3 页
CXX
1,293 行
if (c <= '9') c -= '0'; else c = toupper(c) - 'A' + 10; if (c >= base) break; total = base * total + c; } return total;}///////////////////////////////////////////////////////////////////////////////// PTimestruct tm * PTime::os_localtime(const time_t * clock, struct tm * tb){ struct tm * tp = ::localtime(clock); if (tp != NULL) return tp; memset(tb, 0, sizeof(*tb)); return tb;}struct tm * PTime::os_gmtime(const time_t * clock, struct tm * tb){ struct tm * tp = ::gmtime(clock); if (tp != NULL) return tp; memset(tb, 0, sizeof(*tb)); return tb;}///////////////////////////////////////////////////////////////////////////////// PChannelvoid PChannel::Construct(){}PString PChannel::GetName() const{ PAssertAlways(PUnimplementedFunction); return PString();}BOOL PChannel::Read(void *, PINDEX){ PAssertAlways(PUnimplementedFunction); return FALSE;}BOOL PChannel::Write(const void *, PINDEX){ PAssertAlways(PUnimplementedFunction); return FALSE;}BOOL PChannel::Close(){ return FALSE;}///////////////////////////////////////////////////////////////////////////////// DirectoriesPDirectory PDirectory::GetParent() const{ if (IsRoot()) return *this; return *this + "..";}BOOL PDirectory::Change(const PString & p){ PDirectory d = p; if (d[0] != '\\') if (_chdrive(toupper(d[0])-'A'+1) != 0) return FALSE; return _chdir(d + ".") == 0;}BOOL PDirectory::Filtered(){#if defined(_WIN32)#ifdef _WIN32_WCE USES_CONVERSION; char * name = T2A(fileinfo.cFileName);#else char * name = fileinfo.cFileName;#endif // _WIN32_WCE#else char * name = fileinfo.name;#endif if (strcmp(name, ".") == 0) return TRUE; if (strcmp(name, "..") == 0) return TRUE; if (scanMask == PFileInfo::AllPermissions) return FALSE; PFileInfo inf; PAssert(PFile::GetInfo(*this+name, inf), POperatingSystemError); return (inf.type&scanMask) == 0;}BOOL PDirectory::IsRoot() const{ if ((*this)[1] == ':') return GetLength() == 3; PINDEX pos = FindOneOf("/\\", 2); return pos == GetLength();}PDirectory PDirectory::GetRoot() const{ if ((*this)[1] == ':') return Left(3); return Left(FindOneOf("/\\", 2));}PStringArray PDirectory::GetPath() const{ PStringArray path; if (IsEmpty()) return path; if ((*this)[1] == ':') path = Tokenise("/\\", FALSE); else { path = Mid(2).Tokenise("/\\", FALSE); path[0].Splice("\\\\", 0); } PINDEX last = path.GetSize()-1; while (path[last].IsEmpty()) path.SetSize(last--); return path;}BOOL PDirectory::GetInfo(PFileInfo & info) const{ return PFile::GetInfo(*this + GetEntryName(), info);}///////////////////////////////////////////////////////////////////////////////// File PathPFilePath::PFilePath(const PString & str) : PCaselessString(PDirectory::CreateFullPath(str, FALSE)){}PFilePath::PFilePath(const char * cstr) : PCaselessString(PDirectory::CreateFullPath(cstr, FALSE)){}PFilePath::PFilePath(const char * prefix, const char * dir){ if (dir != NULL) { PDirectory tmpdir(dir); operator=(tmpdir); } else { PConfig cfg(PConfig::Environment); PString path = cfg.GetString("TMPDIR"); if (path.IsEmpty()) { path = cfg.GetString("TMP"); if (path.IsEmpty()) path = cfg.GetString("TEMP"); } if (path.IsEmpty() || path[path.GetLength()-1] != '\\') path += '\\'; *this = path; } if (prefix != NULL) *this += prefix; else *this += "PW"; *this += "XXXXXX"; PAssert(_mktemp(GetPointer()) != NULL, "Could not make temporary file");}void PFilePath::AssignContents(const PContainer & cont){ PCaselessString::AssignContents(cont); PCaselessString::AssignContents(PDirectory::CreateFullPath(*this, FALSE));}static PINDEX GetVolumeSubStringLength(const PString & path){ if (path[1] == ':') return 2; if (path[0] == '\\' && path[1] == '\\') { PINDEX backslash = path.Find('\\', 2); if (backslash != P_MAX_INDEX) { backslash = path.Find('\\', backslash+1); if (backslash != P_MAX_INDEX) return backslash; } } PINDEX backslash = path.Find('\\'); if (backslash != P_MAX_INDEX) return backslash; return 0;}PCaselessString PFilePath::GetVolume() const{ return Left(GetVolumeSubStringLength(*this));}PDirectory PFilePath::GetDirectory() const{ PINDEX backslash = FindLast('\\'); if (backslash != P_MAX_INDEX) return Left(backslash+1); return PCaselessString();}PCaselessString PFilePath::GetPath() const{ return operator()(GetVolumeSubStringLength(*this), FindLast('\\', GetLength()-2));}PCaselessString PFilePath::GetFileName() const{ PINDEX backslash = FindLast('\\', GetLength()-2); if (backslash == P_MAX_INDEX) backslash = 0; else backslash++; return Mid(backslash);}PCaselessString PFilePath::GetTitle() const{ PINDEX backslash = FindLast('\\', GetLength()-2); if (backslash == P_MAX_INDEX) backslash = 0; else backslash++; PINDEX last_dot = FindLast('.'); if (last_dot < backslash) last_dot = P_MAX_INDEX; return operator()(backslash, last_dot-1);}PCaselessString PFilePath::GetType() const{ PINDEX slash = FindLast('\\'); if (slash == P_MAX_INDEX) slash = 0; PINDEX dot = FindLast('.'); if (dot < slash) return PCaselessString(); return operator()(dot, P_MAX_INDEX);}void PFilePath::SetType(const PCaselessString & type){ PINDEX dot = Find('.', FindLast('\\')); if (dot != P_MAX_INDEX) Splice(type, dot, GetLength()-dot); else *this += type;}///////////////////////////////////////////////////////////////////////////////// PFilevoid PFile::SetFilePath(const PString & newName){ if (!IsOpen()) path = newName;}BOOL PFile::Access(const PFilePath & name, OpenMode mode){ int accmode; switch (mode) { case ReadOnly :#ifndef R_OK#define R_OK 4#endif accmode = R_OK; break; case WriteOnly :#ifndef W_OK#define W_OK 2#endif accmode = W_OK; break; default : accmode = R_OK|W_OK; } return access(name, accmode) == 0;}BOOL PFile::Remove(const PFilePath & name, BOOL force){ if (remove(name) == 0) return TRUE; if (!force || errno != EACCES) return FALSE; if (_chmod(name, _S_IWRITE) != 0) return FALSE; return remove(name) == 0;}BOOL PFile::Rename(const PFilePath & oldname, const PString & newname, BOOL force){ if (newname.FindOneOf(":\\/") != P_MAX_INDEX) {#ifdef _WIN32_WCE set_errno(EINVAL);#else errno = EINVAL;#endif // _WIN32_WCE return FALSE; } PString fullname = oldname.GetDirectory() + newname; if (rename(oldname, fullname) == 0) return TRUE; if (!force || errno == ENOENT || !Exists(fullname)) return FALSE; if (!Remove(fullname, TRUE)) return FALSE; return rename(oldname, fullname) == 0;}BOOL PFile::Move(const PFilePath & oldname, const PFilePath & newname, BOOL force){ if (rename(oldname, newname) == 0) return TRUE; if (errno == ENOENT) return FALSE; if (force && Exists(newname)) { if (!Remove(newname, TRUE)) return FALSE; if (rename(oldname, newname) == 0) return TRUE; } return Copy(oldname, newname, force) && Remove(oldname);}#ifdef _WIN32_WCEBOOL PFile::GetInfo(const PFilePath & name, PFileInfo & info){ USES_CONVERSION; PString fn = name; PINDEX pos = fn.GetLength()-1; while (PDirectory::IsSeparator(fn[pos])) pos--; fn.Delete(pos+1, P_MAX_INDEX); HANDLE hFile = CreateFile(A2T((const char*)fn),0,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if (hFile==INVALID_HANDLE_VALUE) return false; bool res=false; BY_HANDLE_FILE_INFORMATION FInfo; if (GetFileInformationByHandle(hFile,&FInfo)) { info.created = FileTimeToTime(FInfo.ftCreationTime); info.modified = FileTimeToTime(FInfo.ftLastWriteTime); info.accessed = FileTimeToTime(FInfo.ftLastAccessTime); info.size = (__int64(FInfo.nFileSizeHigh)<<32)+__int64(FInfo.nFileSizeLow); info.permissions = PFileInfo::UserRead|PFileInfo::GroupRead|PFileInfo::WorldRead; if (FInfo.dwFileAttributes & FILE_ATTRIBUTE_READONLY==0) info.permissions |= PFileInfo::UserWrite|PFileInfo::GroupWrite|PFileInfo::WorldWrite; if (FInfo.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?