📄 osutil.cxx
字号:
return rename(oldname, oldname.GetPath() + newname) == 0;}BOOL PFile::Move(const PFilePath & oldname, const PFilePath & newname, BOOL force){ PFilePath from = oldname.GetDirectory() + oldname.GetFileName(); PFilePath to = newname.GetDirectory() + newname.GetFileName(); if (rename(from, to) == 0) return TRUE; if (errno == EXDEV) return Copy(from, to, force) && Remove(from); if (force && errno == EEXIST) if (Remove(to, TRUE)) if (rename(from, to) == 0) return TRUE; return FALSE;}BOOL PFile::Access(const PFilePath & name, OpenMode mode){ int accmode; switch (mode) { case ReadOnly : accmode = 2; break; case WriteOnly : accmode = 4; break; default : accmode = 6; } return access(name, accmode) == 0;}BOOL PFile::GetInfo(const PFilePath & name, PFileInfo & status){ status.type = PFileInfo::UnknownFileType; struct stat s; if (lstat(name, &s) != 0) return FALSE; if (S_ISLNK(s.st_mode)) { status.type = PFileInfo::SymbolicLink; if (stat(name, &s) != 0) return FALSE; } status.created = s.st_ctime; status.modified = s.st_mtime; status.accessed = s.st_atime; status.size = s.st_size; status.permissions = s.st_mode & PFileInfo::AllPermissions; if (S_ISREG(s.st_mode)) status.type = PFileInfo::RegularFile; else if (S_ISDIR(s.st_mode)) status.type = PFileInfo::SubDirectory; else if (S_ISFIFO(s.st_mode)) status.type = PFileInfo::Fifo; else if (S_ISCHR(s.st_mode)) status.type = PFileInfo::CharDevice; else if (S_ISBLK(s.st_mode)) status.type = PFileInfo::BlockDevice;#ifndef __BEOS__ else if (S_ISSOCK(s.st_mode)) status.type = PFileInfo::SocketDevice;#endif // !__BEOS__ return TRUE;}BOOL PFile::SetPermissions(const PFilePath & name, int permissions){ mode_t mode = 0; mode |= S_IROTH; mode |= S_IRGRP; if (permissions & PFileInfo::WorldExecute) mode |= S_IXOTH; if (permissions & PFileInfo::WorldWrite) mode |= S_IWOTH; if (permissions & PFileInfo::WorldRead) mode |= S_IROTH; if (permissions & PFileInfo::GroupExecute) mode |= S_IXGRP; if (permissions & PFileInfo::GroupWrite) mode |= S_IWGRP; if (permissions & PFileInfo::GroupRead) mode |= S_IRGRP; if (permissions & PFileInfo::UserExecute) mode |= S_IXUSR; if (permissions & PFileInfo::UserWrite) mode |= S_IWUSR; if (permissions & PFileInfo::UserRead) mode |= S_IRUSR; return chmod ((const char *)name, mode) == 0;}///////////////////////////////////////////////////////////////////////////////// PTextFileBOOL PTextFile::WriteLine (const PString & line){ if (!Write((const char *)line, line.GetLength())) return FALSE; char ch = '\n'; return Write(&ch, 1);}BOOL PTextFile::ReadLine (PString & line){ int len = 0; int ch; char * base, * ptr; while (1) { len += LINE_SIZE_STEP; ptr = base = line.GetPointer(len); while ((ptr - base) < LINE_SIZE_STEP-1) { if ((ch = ReadChar()) < 0) { ConvertOSError(errno); return FALSE; } if (ch == '\n') { *ptr = '\0'; line.MakeMinimumSize(); return TRUE; } *ptr++ = ch; } } }///////////////////////////////////////////////////////////////////////////////// PFilePathPFilePath::PFilePath(const PString & str) : PString(CanonicaliseFilename(str)){}PFilePath::PFilePath(const char * cstr) : PString(CanonicaliseFilename(cstr)){}PFilePath::PFilePath(const char * prefix, const char * dir) : PString(){ if (prefix == NULL) prefix = "tmp"; char * n; if (dir == NULL) { n = tempnam(NULL, (char *)prefix); *this = CanonicaliseFilename(n); runtime_free (n); } else { PDirectory s(dir); PString p = s + prefix + "XXXXXX"; if (mktemp(p.GetPointer()) == NULL) { char extra = 'a'; do p = s + prefix + extra++ + "XXXXXX"; while (mktemp(p.GetPointer()) == NULL && extra <= 'z'); } *this = PString(p); }}PFilePath & PFilePath::operator=(const PString & str){ PString::operator=(CanonicaliseFilename(str)); return *this;}PString PFilePath::GetPath() const{ int i; PAssert((i = FindLast('/')) != P_MAX_INDEX, PInvalidArrayIndex); return Left(i+1);}PString PFilePath::GetTitle() const{ PString fn(GetFileName()); return fn(0, fn.FindLast('.')-1);}PString PFilePath::GetType() const{ int p = FindLast('.'); int l = (p == P_MAX_INDEX) ? 0 : (GetLength() - p); if (p < 0 || l < 2) return PString(""); else return (*this)(p, P_MAX_INDEX);}void PFilePath::SetType(const PString & type){ PINDEX dot = Find('.', FindLast('/')); if (dot != P_MAX_INDEX) Splice(type, dot, GetLength()-dot); else *this += type;}PString PFilePath::GetFileName() const{ int i; if ((i = FindLast('/')) == P_MAX_INDEX) return *this; else return Right(GetLength()-i-1);}PDirectory PFilePath::GetDirectory() const{ int i; if ((i = FindLast('/')) == P_MAX_INDEX) return "./"; else return Left(i);}BOOL PFilePath::IsValid(char c){ return c != '/';}BOOL PFilePath::IsValid(const PString & str){ return str.Find('/') == P_MAX_INDEX;}///////////////////////////////////////////////////////////////////////////////// PConsoleChannelPConsoleChannel::PConsoleChannel(){}PConsoleChannel::PConsoleChannel(ConsoleType type){ Open(type);}BOOL PConsoleChannel::Open(ConsoleType type){ switch (type) { case StandardInput : os_handle = 0; return TRUE; case StandardOutput : os_handle = 1; return TRUE; case StandardError : os_handle = 2; return TRUE; } return FALSE;}PString PConsoleChannel::GetName() const{ return ttyname(os_handle);}BOOL PConsoleChannel::Close(){ os_handle = -1; return TRUE;}////////////////////////////////////////////////////////// PTime//PTime::PTime(){ struct timeval tv; gettimeofday(&tv, NULL); theTime = tv.tv_sec; microseconds = tv.tv_usec;}BOOL PTime::GetTimeAMPM(){#if defined(P_USE_LANGINFO) return strstr(nl_langinfo(T_FMT), "%p") != NULL;#elif defined(P_USE_STRFTIME) char buf[30]; struct tm t; memset(&t, 0, sizeof(t)); t.tm_hour = 20; t.tm_min = 12; t.tm_sec = 11; strftime(buf, sizeof(buf), "%X", &t); return strstr(buf, "20") != NULL;#else#warning No AMPM implementation return FALSE;#endif}PString PTime::GetTimeAM(){#if defined(P_USE_LANGINFO) return PString(nl_langinfo(AM_STR));#elif defined(P_USE_STRFTIME) char buf[30]; struct tm t; memset(&t, 0, sizeof(t)); t.tm_hour = 10; t.tm_min = 12; t.tm_sec = 11; strftime(buf, sizeof(buf), "%p", &t); return buf;#else#warning Using default AM string return "AM";#endif}PString PTime::GetTimePM(){#if defined(P_USE_LANGINFO) return PString(nl_langinfo(PM_STR));#elif defined(P_USE_STRFTIME) char buf[30]; struct tm t; memset(&t, 0, sizeof(t)); t.tm_hour = 20; t.tm_min = 12; t.tm_sec = 11; strftime(buf, sizeof(buf), "%p", &t); return buf;#else#warning Using default PM string return "PM";#endif}PString PTime::GetTimeSeparator(){#if defined(P_LINUX) || defined(P_HPUX9) || defined(P_SOLARIS)# if defined(P_USE_LANGINFO) char * p = nl_langinfo(T_FMT);# elif defined(P_LINUX) char * p = _time_info->time; # endif char buffer[2]; while (*p == '%' || isalpha(*p)) p++; buffer[0] = *p; buffer[1] = '\0'; return PString(buffer);#elif defined(P_USE_STRFTIME) char buf[30]; struct tm t; memset(&t, 0, sizeof(t)); t.tm_hour = 10; t.tm_min = 11; t.tm_sec = 12; strftime(buf, sizeof(buf), "%X", &t); char * sp = strstr(buf, "11") + 2; char * ep = sp; while (*ep != '\0' && !isdigit(*ep)) ep++; return PString(sp, ep-sp);#else#warning Using default time separator return ":";#endif}PTime::DateOrder PTime::GetDateOrder(){#if defined(P_USE_LANGINFO) || defined(P_LINUX)# if defined(P_USE_LANGINFO) char * p = nl_langinfo(D_FMT);# else char * p = _time_info->date; # endif while (*p == '%') p++; switch (tolower(*p)) { case 'd': return DayMonthYear; case 'y': return YearMonthDay; case 'm': default: break; } return MonthDayYear;#elif defined(P_USE_STRFTIME) char buf[30]; struct tm t; memset(&t, 0, sizeof(t)); t.tm_mday = 22; t.tm_mon = 10; t.tm_year = 99; strftime(buf, sizeof(buf), "%x", &t); char * day_pos = strstr(buf, "22"); char * mon_pos = strstr(buf, "11"); char * yr_pos = strstr(buf, "99"); if (yr_pos < day_pos) return YearMonthDay; if (day_pos < mon_pos) return DayMonthYear; return MonthDayYear;#else#warning Using default date order return DayMonthYear;#endif}PString PTime::GetDateSeparator(){#if defined(P_USE_LANGINFO) || defined(P_LINUX)# if defined(P_USE_LANGINFO) char * p = nl_langinfo(D_FMT);# else char * p = _time_info->date; # endif char buffer[2]; while (*p == '%' || isalpha(*p)) p++; buffer[0] = *p; buffer[1] = '\0'; return PString(buffer);#elif defined(P_USE_STRFTIME) char buf[30]; struct tm t; memset(&t, 0, sizeof(t)); t.tm_mday = 22; t.tm_mon = 10; t.tm_year = 99; strftime(buf, sizeof(buf), "%x", &t); char * sp = strstr(buf, "22") + 2; char * ep = sp; while (*ep != '\0' && !isdigit(*ep)) ep++; return PString(sp, ep-sp);#else#warning Using default date separator return "/";#endif}PString PTime::GetDayName(PTime::Weekdays day, NameType type){#if defined(P_USE_LANGINFO) return PString( (type == Abbreviated) ? nl_langinfo((nl_item)(ABDAY_1+(int)day)) : nl_langinfo((nl_item)(DAY_1+(int)day)) );#elif defined(P_LINUX) return (type == Abbreviated) ? PString(_time_info->abbrev_wkday[(int)day]) : PString(_time_info->full_wkday[(int)day]);#elif defined(P_USE_STRFTIME) char buf[30]; struct tm t; memset(&t, 0, sizeof(t)); t.tm_wday = day; strftime(buf, sizeof(buf), type == Abbreviated ? "%a" : "%A", &t); return buf;#else#warning Using default day names static char *defaultNames[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; static char *defaultAbbrev[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; return (type == Abbreviated) ? PString(defaultNames[(int)day]) : PString(defaultAbbrev[(int)day]);#endif}PString PTime::GetMonthName(PTime::Months month, NameType type) {#if defined(P_USE_LANGINFO) return PString( (type == Abbreviated) ? nl_langinfo((nl_item)(ABMON_1+(int)month-1)) : nl_langinfo((nl_item)(MON_1+(int)month-1)) );#elif defined(P_LINUX) return (type == Abbreviated) ? PString(_time_info->abbrev_month[(int)month-1]) : PString(_time_info->full_month[(int)month-1]);#elif defined(P_USE_STRFTIME) char buf[30]; struct tm t; memset(&t, 0, sizeof(t)); t.tm_mon = month-1; strftime(buf, sizeof(buf), type == Abbreviated ? "%b" : "%B", &t); return buf;#else#warning Using default monthnames static char *defaultNames[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; static char *defaultAbbrev[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; return (type == Abbreviated) ? PString(defaultNames[(int)month-1]) : PString(defaultAbbrev[(int)month-1]);#endif}BOOL PTime::IsDaylightSavings(){ time_t theTime = ::time(NULL); return ::localtime(&theTime)->tm_isdst != 0;}int PTime::GetTimeZone(PTime::TimeZoneType type) {#if defined(P_LINUX) || defined(P_SOLARIS) || defined (P_AIX) long tz = -::timezone/60; if (type == StandardTime) return tz; else return tz + ::daylight*60;#elif defined(P_FREEBSD) || defined(P_OPENBSD) || defined(P_NETBSD) || defined(P_MACOSX) time_t t; time(&t); struct tm * tm = localtime(&t); int tz = tm->tm_gmtoff/60; if (type == StandardTime && tm->tm_isdst) return tz-60; if (type != StandardTime && !tm->tm_isdst) return tz + 60; return tz;#elif defined(P_SUN4) struct timeb tb; ftime(&tb); if (type == StandardTime || tb.dstflag == 0) return -tb.timezone; else return -tb.timezone + 60;#else#warning No timezone information return 0;#endif}PString PTime::GetTimeZoneString(PTime::TimeZoneType type) {#if defined(P_LINUX) || defined(P_SUN4) || defined(P_SOLARIS) || defined (P_AIX) const char * str = (type == StandardTime) ? ::tzname[0] : ::tzname[1]; if (str != NULL) return str; return PString(); #elif defined(P_USE_STRFTIME) char buf[30]; struct tm t; memset(&t, 0, sizeof(t)); t.tm_isdst = type != StandardTime; strftime(buf, sizeof(buf), "%Z", &t); return buf;#else#warning No timezone name information return PString(); #endif}// note that PX_tm is local storage inside the PTime instance#ifdef P_PTHREADSstruct tm * PTime::os_localtime(const time_t * clock, struct tm * ts){ return ::localtime_r(clock, ts);#elsestruct tm * PTime::os_localtime(const time_t * clock, struct tm *){ return ::localtime(clock);#endif}#ifdef P_PTHREADSstruct tm * PTime::os_gmtime(const time_t * clock, struct tm * ts){ return ::gmtime_r(clock, ts);#elsestruct tm * PTime::os_gmtime(const time_t * clock, struct tm *){ return ::gmtime(clock);#endif}// End Of File ///////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -