📄 ptlib.cxx
字号:
PUInt64 total = 0;
const char * ptr = theArray;
while (isspace(*ptr))
ptr++;
for (;;) {
unsigned c = *ptr++;
if (c < '0')
break;
if (c <= '9')
c -= '0';
else
c = toupper(c) - 'A' + 10;
if (c >= base)
break;
total = base * total + c;
}
return total;
}
///////////////////////////////////////////////////////////////////////////////
// PTime
struct 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;
}
///////////////////////////////////////////////////////////////////////////////
// PChannel
void 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;
}
///////////////////////////////////////////////////////////////////////////////
// Directories
PDirectory 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 Path
PFilePath::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;
}
///////////////////////////////////////////////////////////////////////////////
// PFile
void 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_WCE
BOOL 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -