?? ptlib.cxx
字號:
/*
* ptlib.cxx
*
* General implementation of classes for Microsoft operating systems.
*
* Portable Windows Library
*
* Copyright (c) 1993-1998 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Portions are Copyright (C) 1993 Free Software Foundation, Inc.
* All Rights Reserved.
*
* Contributor(s): ______________________________________.
*
* $Log: ptlib.cxx,v $
* Revision 1.58 2001/09/10 02:51:23 robertj
* Major change to fix problem with error codes being corrupted in a
* PChannel when have simultaneous reads and writes in threads.
*
* Revision 1.57 2001/06/04 10:15:01 robertj
* Fixed bug if tried to get file info on empty file path.
*
* Revision 1.56 2001/03/19 05:49:44 robertj
* Redid int64 stream input to use library conversion function and
* set fail bit if no valid integer was found in the input stream.
*
* Revision 1.55 2001/03/15 23:49:42 robertj
* Added missing operators for reading 64 bit integers from streams.
*
* Revision 1.54 2001/02/13 04:39:08 robertj
* Fixed problem with operator= in container classes. Some containers will
* break unless the copy is virtual (eg PStringStream's buffer pointers) so
* needed to add a new AssignContents() function to all containers.
*
* Revision 1.53 2001/01/24 06:38:29 yurik
* Windows CE port-related changes
*
* Revision 1.52 2000/07/09 14:05:46 robertj
* Added file share options.
*
* Revision 1.51 2000/04/29 06:44:17 robertj
* Added some stuff to make sure symbols included in library.
*
* Revision 1.50 2000/02/19 23:46:09 robertj
* Fixed incorrect values for PFile::Access() function, thanks Stefan Ditscheid.
*
* Revision 1.49 1999/08/17 03:46:40 robertj
* Fixed usage of inlines in optimised version.
*
* Revision 1.48 1999/06/14 07:59:38 robertj
* Enhanced tracing again to add options to trace output (timestamps etc).
*
* Revision 1.47 1999/06/13 13:54:07 robertj
* Added PConsoleChannel class for access to stdin/stdout/stderr.
*
* Revision 1.46 1999/06/09 02:05:20 robertj
* Added ability to open file as standard input, output and error streams.
*
* Revision 1.45 1999/05/06 06:11:50 robertj
* Fixed date to be forgiving of rubbish at end of date string.
* Fixed PTime::GetHour() etc to not crash on time=-1.
*
* Revision 1.44 1998/11/30 07:30:31 robertj
* Fixed problems with PFilePath parsing functions.
*
* Revision 1.43 1998/11/26 10:34:19 robertj
* Fixed problem with PFileInfo::GetVolume() on UNC paths.
*
* Revision 1.42 1998/11/14 23:37:08 robertj
* Fixed file path directory extraction, not able to return root directory
*
* Revision 1.41 1998/10/19 00:20:38 robertj
* Moved error and trace stream functions to common code.
*
* Revision 1.40 1998/10/18 14:27:10 robertj
* Improved tracing functions.
*
* Revision 1.39 1998/10/13 14:13:17 robertj
* Complete rewrite of memory leak detection code.
*
* Revision 1.38 1998/09/24 03:30:53 robertj
* Added open software license.
*
* Revision 1.37 1998/09/14 12:55:06 robertj
* Changed memory debug to dump leaks not including static globals.
*
* Revision 1.36 1998/05/26 01:29:53 robertj
* Removed assert as this Close() function is now called all the time for Unix reasons.
*
* Revision 1.35 1998/05/21 04:27:31 robertj
* Compensated for MSC run time library bug.
*
* Revision 1.34 1998/04/01 01:54:45 robertj
* Added memory leak checking to debug version.
*
* Revision 1.33 1998/03/29 06:16:51 robertj
* Rearranged initialisation sequence so PProcess descendent constructors can do "things".
*
* Revision 1.32 1998/03/20 03:20:16 robertj
* Added MSVC RT debug support.
*
* Revision 1.31 1998/01/26 00:54:15 robertj
* 64 bit integer string conversions.
*
* Revision 1.30 1998/01/05 10:38:25 robertj
* Unix pthreads compatibility, added threadsafe time functions.
*
* Revision 1.29 1997/12/11 10:40:29 robertj
* Fixed bug in SetType() function of FilePath.
*
* Revision 1.28 1997/08/28 12:49:51 robertj
* Fixed bug where could not change directory to UNC.
*
* Revision 1.27 1997/04/27 05:50:26 robertj
* DLL support.
*
* Revision 1.26 1997/01/12 04:23:43 robertj
* Fixed PDirectory::IsRoot() so works with UNC's
*
* Revision 1.25 1996/08/17 10:00:37 robertj
* Changes for Windows DLL support.
*
* Revision 1.24 1996/08/08 10:09:23 robertj
* Directory structure changes for common files.
*
* Revision 1.23 1996/01/28 02:56:16 robertj
* Fixed bug in PFilePath functions for if path ends in a directory separator.
*
* Revision 1.22 1996/01/23 13:23:51 robertj
* Fixed bug in PFileInfo for if path ends in directory separator.
*
* Revision 1.21 1996/01/02 12:56:49 robertj
* Fixed copy of directories.
*
* Revision 1.20 1995/12/10 11:59:33 robertj
* Changes to main() startup mechanism to support Mac.
* Fixed bug in time interfval constant variable initialisation. Not guarenteed to work.
* Moved error code for specific WIN32 and MS-DOS versions.
*
* Revision 1.19 1995/10/14 15:12:29 robertj
* Added function to get parent directory.
*
* Revision 1.18 1995/07/31 12:18:11 robertj
* Removed PContainer from PChannel ancestor.
*
* Revision 1.17 1995/06/04 13:10:19 robertj
* Fixed rename bug.
*
* Revision 1.16 1995/06/04 12:48:06 robertj
* Changed unknown error string to hex.
* Added missing GetInfo function for directory entries
*
* Revision 1.15 1995/04/25 11:33:35 robertj
* Changes for DLL support.
*
* Revision 1.14 1995/04/22 00:53:49 robertj
* Added Move() function to PFile.
* Changed semantics of Rename() function in PFile.
* Changed file path string to use PFilePath object.
*
* Revision 1.13 1995/03/12 05:00:08 robertj
* Re-organisation of DOS/WIN16 and WIN32 platforms to maximise common code.
* Used built-in equate for WIN32 API (_WIN32).
*
* Revision 1.12 1995/02/27 10:37:06 robertj
* Added GetUserNmae().
* Removed superfluous Read() and Write() for text files.
*
* Revision 1.11 1995/01/06 10:41:43 robertj
* Moved identifiers into different scope.
* Changed file size to 64 bit integer.
*
* Revision 1.10 1994/12/21 11:36:07 robertj
* Fixed caseless string for file paths.
*
* Revision 1.9 1994/10/30 11:26:54 robertj
* Fixed set current directory function.
* Changed PFilePath to be case insignificant according to platform.
*
* Revision 1.8 1994/10/23 05:42:39 robertj
* PipeChannel headers.
* ConvertOSError function added.
* Numerous implementation enhancements.
*
* Revision 1.7 1994/08/04 13:24:27 robertj
* Added debug stream.
*
* Revision 1.6 1994/07/27 06:00:10 robertj
* Backup
*
* Revision 1.5 1994/07/21 12:35:18 robertj
* *** empty log message ***
*
* Revision 1.4 1994/07/17 11:01:04 robertj
* Ehancements, implementation, bug fixes etc.
*
* Revision 1.3 1994/07/02 03:18:09 robertj
* Multi-threading implementation.
*
* Revision 1.2 1994/06/25 12:13:01 robertj
* Synchronisation.
*
// Revision 1.1 1994/04/01 14:39:35 robertj
// Initial revision
//
*/
#include <ptlib.h>
#include <errno.h>
#include <fcntl.h>
#include <share.h>
#include <sys\stat.h>
#include <crtdbg.h>
#if !P_USE_INLINES
#include <ptlib/osutil.inl>
#include <ptlib/ptlib.inl>
#endif
ostream & operator<<(ostream & s, PInt64 v)
{
char buffer[25];
if ((s.flags()&ios::hex) != 0)
return s << _ui64toa(v, buffer, 16);
if ((s.flags()&ios::oct) != 0)
return s << _ui64toa(v, buffer, 8);
if (v < 0) {
s << '-';
v = -v;
}
return s << _i64toa(v, buffer, 10);
}
ostream & operator<<(ostream & s, PUInt64 v)
{
char buffer[25];
return s << _ui64toa(v, buffer, (s.flags()&ios::oct) ? 8 : ((s.flags()&ios::hex) ? 16 : 10));
}
const int MaxDigits = (64+2)/3+1; // Maximum is 22 digit octal number, plus sign
static void GetDigits(BOOL sign, istream & s, char * buffer)
{
PINDEX count = 0;
s.eatwhite();
if (s.peek() == '+')
s.get(); // Skip leading '+'
else if (sign && s.peek() == '-')
s.get(buffer[count++]);
if ((s.flags()&ios::oct) != 0) {
while (isdigit(s.peek()) && s.peek() < '8' && count < MaxDigits)
s.get(buffer[count++]);
}
else if ((s.flags()&ios::hex) != 0) {
while (isxdigit(s.peek()) && count < MaxDigits)
s.get(buffer[count++]);
}
else {
while (isdigit(s.peek()) && count < MaxDigits)
s.get(buffer[count++]);
}
buffer[count] = '\0';
if (count > (buffer[0] == '-' ? 1 : 0))
return;
s.clear(ios::failbit);
}
istream & operator>>(istream & s, PInt64 & v)
{
char b[MaxDigits+1];
GetDigits(TRUE, s, b);
v = _atoi64(b);
return s;
}
istream & operator>>(istream & s, PUInt64 & v)
{
char b[MaxDigits+1];
GetDigits(FALSE, s, b);
v = _atoi64(b);
return s;
}
PInt64 PString::AsInt64(unsigned base) const
{
if (base == 10)
return _atoi64(theArray);
PAssert(base >= 2 && base <= 36, PInvalidParameter);
PInt64 total = 0;
const char * ptr = theArray;
while (isspace(*ptr))
ptr++;
BOOL negative = *ptr == '-';
if (*ptr == '-' || *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;
c = *ptr++;
}
if (negative)
return -total;
else
return total;
}
PUInt64 PString::AsUnsigned64(unsigned base) const
{
PAssert(base >= 2 && base <= 36, PInvalidParameter);
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;
c = *ptr++;
}
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();
}
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)
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -