📄 fstream.cpp
字号:
/*
* Copyright (c) 1999
* Silicon Graphics Computer Systems, Inc.
*
* Copyright (c) 1999
* Boris Fomitchev
*
* This material is provided "as is", with absolutely no warranty expressed
* or implied. Any use is at your own risk.
*
* Permission to use or copy this software for any purpose is hereby granted
* without fee, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is granted,
* provided the above notices are retained, and a notice that the code was
* modified is included with the above copyright notice.
*
*/
#include "stlport_prefix.h"
#if defined (__SUNPPRO_CC) && !defined (_STLP_NO_NEW_C_HEADERS)
# include <time.h>
// For sunpro, it chokes if time.h is included through stat.h
#endif
#include <fstream>
#ifdef __CYGWIN__
# define __int64 long long
#endif
#if defined (_STLP_USE_UNIX_IO)
extern "C" {
// open/close/read/write
# include <sys/stat.h> // For stat
# if !defined (_CRAY) && ! defined (__EMX__)
# include <sys/mman.h> // For mmap
# endif
// on HP-UX 11, this one contradicts with pthread.h on pthread_atfork, unless we unset this
# if defined (__hpux) && defined (__GNUC__)
# undef _INCLUDE_POSIX1C_SOURCE
# endif
# include <unistd.h>
# include <fcntl.h>
}
# ifdef __APPLE__
# include <sys/sysctl.h>
# endif
#elif defined (_STLP_USE_WIN32_IO)
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# ifdef __BORLANDC__
# include <cfcntl.h> // For _O_RDONLY, etc
# include <sys/stat.h> // For _fstat
# elif !defined(_STLP_WCE)
# include <io.h> // For _get_osfhandle
# include <fcntl.h> // For _O_RDONLY, etc
# include <sys/stat.h> // For _fstat
# endif
# define _TEXTBUF_SIZE 0x1000
#elif defined (_STLP_USE_UNIX_EMULATION_IO)
# if defined( __MSL__ )
# include <unistd.h>
# else
# include <io.h>
# endif
# include <fcntl.h>
# include <sys/stat.h>
#elif defined (_STLP_USE_STDIO_IO)
# include <cstdio>
# if !(defined(__MRC__) || defined(__SC__) || defined(__ISCPP__) )
extern "C" {
# include <sys/stat.h>
}
# endif
# if defined( __MSL__ )
# include <unix.h>
# endif
# if defined(__ISCPP__)
# include <c_locale_is/filestat.h>
# endif
# if defined(__BEOS__) && defined(__INTEL__)
# include <fcntl.h>
# include <sys/stat.h> // For _fstat
# define _S_IREAD S_IREAD
# define _S_IWRITE S_IWRITE
# define _S_IFREG S_IFREG
# endif
#else
# error "Configure i/o !"
#endif
#if defined (_STLP_USE_WIN32_IO)
const _STLP_fd INVALID_STLP_FD = INVALID_HANDLE_VALUE;
# if !defined (INVALID_SET_FILE_POINTER)
# define INVALID_SET_FILE_POINTER 0xffffffff
# endif
#elif defined (_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO) || defined (_STLP_USE_UNIX_IO)
const _STLP_fd INVALID_STLP_FD = -1;
#else
# error "Configure i/o !"
#endif
// map permission masks
#if defined (_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO)
# ifndef S_IRUSR
# define S_IRUSR _S_IREAD
# define S_IWUSR _S_IWRITE
# define S_IRGRP _S_IREAD
# define S_IWGRP _S_IWRITE
# define S_IROTH _S_IREAD
# define S_IWOTH _S_IWRITE
# endif
# ifndef O_RDONLY
# define O_RDONLY _O_RDONLY
# define O_WRONLY _O_WRONLY
# define O_RDWR _O_RDWR
# define O_APPEND _O_APPEND
# define O_CREAT _O_CREAT
# define O_TRUNC _O_TRUNC
# define O_TEXT _O_TEXT
# define O_BINARY _O_BINARY
# endif
# ifdef __MSL__
# define _O_TEXT 0x0
# if !defined( O_TEXT )
# define O_TEXT _O_TEXT
# endif
# define _S_IFREG S_IFREG
# define S_IREAD S_IRUSR
# define S_IWRITE S_IWUSR
# define S_IEXEC S_IXUSR
# define _S_IWRITE S_IWRITE
# define _S_IREAD S_IREAD
# define _open open
# define _lseek lseek
# define _close close
# define _read read
# define _write write
# endif
#endif
#ifndef O_ACCMODE
# define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
#endif
#include "fstream_impl.h"
#ifdef _STLP_LONG_LONG
# define ULL(x) ((unsigned _STLP_LONG_LONG)x)
#elif defined(__MRC__) || defined(__SC__) //*TY 02/25/2000 - added support for MPW compilers
# include <Math64.h>
# define ULL(x) (U64SetU(x))
#elif defined(__ISCPP__)
# include "uint64.h"
#else
# error "there should be some long long type on the system!"
#endif
_STLP_BEGIN_NAMESPACE
// Compare with streamoff definition in stl/char_traits.h!
#ifdef _STLP_USE_DEFAULT_FILE_OFFSET
# define FOPEN fopen
# define FSEEK fseek
# define FSTAT fstat
# define STAT stat
# define FTELL ftell
# define LSEEK lseek
# define MMAP mmap
# define OPEN open
#elif defined(_LARGEFILE_SOURCE) || defined(_LARGEFILE64_SOURCE) /* || defined(__USE_FILE_OFFSET64) */ \
/* || (defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64)) */ /* || defined(__sgi) */
# define FOPEN fopen64
# define FSEEK fseeko64
# define FSTAT fstat64
# define STAT stat64
# define FTELL ftello64
# define LSEEK lseek64
# define MMAP mmap64
# define OPEN open64
#else
# define OPEN open
# define FSEEK fseek
# define FSTAT fstat
# define STAT stat
# define FTELL ftell
# define LSEEK lseek
# define MMAP mmap
# define OPEN open
#endif
#ifdef _STLP_USE_UNIX_IO
# ifndef MAP_FAILED /* MMAP failure return code */
# define MAP_FAILED -1
# endif
#elif defined (_STLP_USE_UNIX_EMULATION_IO)
# define LSEEK _lseek
#endif
#if !defined(__MSL__) && !defined(__MRC__) && !defined(__SC__) && !defined(_STLP_WCE) //*TY 04/15/2000 - exclude mpw compilers also
static ios_base::openmode flag_to_openmode(int mode) {
ios_base::openmode ret = ios_base::__default_mode;
switch(mode & O_ACCMODE) {
case O_RDONLY:
ret = ios_base::in; break;
case O_WRONLY:
ret = ios_base::out; break;
case O_RDWR:
ret = ios_base::in | ios_base::out; break;
}
if (mode & O_APPEND)
ret |= ios_base::app;
# if defined (_STLP_USE_WIN32_IO)
if (mode & O_BINARY)
ret |= ios_base::binary;
# endif
return ret;
}
#endif /* MSL */
_STLP_MOVE_TO_PRIV_NAMESPACE
// Helper functions for _Filebuf_base.
bool __is_regular_file(_STLP_fd fd) {
#if defined (_STLP_UNIX)
struct STAT buf;
return FSTAT(fd, &buf) == 0 && S_ISREG(buf.st_mode);
#elif defined(__MRC__) || defined(__SC__) //*TY 02/25/2000 - added support for MPW compilers
# pragma unused(fd)
return true; // each file is a regular file under mac os, isn't it? (we don't have fstat())
#elif defined(_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO)
struct STAT buf;
return FSTAT(fd, &buf) == 0 && (buf.st_mode & _S_IFREG) != 0 ;
#elif defined (_STLP_USE_WIN32_IO) && !defined(_STLP_WCE)
return (GetFileType(fd) & ~FILE_TYPE_REMOTE) == FILE_TYPE_DISK;
#else
(void)fd; // dwa 4/27/00 - suppress unused parameter warning
return false;
#endif
}
// Number of characters in the file.
streamoff __file_size(_STLP_fd fd) {
streamoff ret = 0;
#if defined (_STLP_UNIX)
struct STAT buf;
if (FSTAT(fd, &buf) == 0 && S_ISREG(buf.st_mode))
ret = buf.st_size > 0 ? buf.st_size : 0;
#elif defined(__MRC__) || defined(__SC__) //*TY 02/25/2000 - added support for MPW compilers
# pragma unused(fd)
#elif defined(_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO)
struct STAT buf;
if (FSTAT(fd, &buf) == 0 && (buf.st_mode & _S_IFREG) != 0)
ret = buf.st_size > 0 ? buf.st_size : 0;
#elif defined (_STLP_USE_WIN32_IO)
LARGE_INTEGER li;
li.LowPart = GetFileSize(fd, (unsigned long*) &li.HighPart);
if (li.LowPart != INVALID_FILE_SIZE || GetLastError() == NO_ERROR)
ret = li.QuadPart;
#else
(void)fd; // dwa 4/27/00 - suppress unused parameter warning
#endif
return ret;
}
_STLP_MOVE_TO_STD_NAMESPACE
// Visual C++ and Intel use this, but not Metrowerks
// Also MinGW, msvcrt.dll (but not crtdll.dll) dependent version
#if (!defined (__MSL__) && !defined (_STLP_WCE) && defined (_STLP_MSVC_LIB) && defined (_WIN32)) || \
(defined (__MINGW32__) && defined (__MSVCRT__))
// fcntl(fileno, F_GETFL) for Microsoft library
// 'semi-documented' defines:
# define IOINFO_L2E 5
# define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
# define _pioinfo(i) ( __pioinfo[(i) >> IOINFO_L2E] + \
((i) & (IOINFO_ARRAY_ELTS - 1)) )
# define FAPPEND 0x20 // O_APPEND flag
# define FTEXT 0x80 // O_TEXT flag
// end of 'semi-documented' defines
// 'semi-documented' internal structure
extern "C" {
struct ioinfo {
long osfhnd; // the real os HANDLE
char osfile; // file handle flags
char pipech; // pipe buffer
# if defined (_MT)
// multi-threaded locking
int lockinitflag;
CRITICAL_SECTION lock;
# endif /* _MT */
};
# if defined (__MINGW32__)
__MINGW_IMPORT ioinfo * __pioinfo[];
# else
extern _CRTIMP ioinfo * __pioinfo[];
# endif
} // extern "C"
// end of 'semi-documented' declarations
static ios_base::openmode _get_osfflags(int fd, HANDLE oshandle) {
char dosflags = 0;
if (fd >= 0)
dosflags = _pioinfo(fd)->osfile;
//else
//the file will be considered as open in binary mode with no append attribute
// end of 'semi-documented' stuff
int mode = 0;
if (dosflags & FAPPEND)
mode |= O_APPEND;
if (dosflags & FTEXT)
mode |= O_TEXT;
else
mode |= O_BINARY;
// For Read/Write access we have to guess
DWORD dummy, dummy2;
BOOL writeOk = WriteFile(oshandle, &dummy2, 0, &dummy, 0);
BOOL readOk = ReadFile(oshandle, &dummy2, 0, &dummy, NULL);
if (writeOk && readOk)
mode |= O_RDWR;
else if (readOk)
mode |= O_RDONLY;
else
mode |= O_WRONLY;
return flag_to_openmode(mode);
}
#elif defined (__DMC__)
# define FHND_APPEND 0x04
# define FHND_DEVICE 0x08
# define FHND_TEXT 0x10
extern "C" unsigned char __fhnd_info[_NFILE];
static ios_base::openmode _get_osfflags(int fd, HANDLE oshandle) {
int mode = 0;
if (__fhnd_info[fd] & FHND_APPEND)
mode |= O_APPEND;
if (__fhnd_info[fd] & FHND_TEXT == 0)
mode |= O_BINARY;
for (FILE *fp = &_iob[0]; fp < &_iob[_NFILE]; fp++) {
if ((fileno(fp) == fd) && (fp->_flag & (_IOREAD | _IOWRT | _IORW))) {
const int osflags = fp->_flag;
if ((osflags & _IOREAD) && !(osflags & _IOWRT) && !(osflags & _IORW))
mode |= O_RDONLY;
else if ((osflags & _IOWRT) && !(osflags & _IOREAD) && !(osflags & _IORW))
mode |= O_WRONLY;
else
mode |= O_RDWR;
break;
}
}
return flag_to_openmode(mode);
}
#endif
size_t _Filebuf_base::_M_page_size = 4096;
_Filebuf_base::_Filebuf_base()
: _M_file_id(INVALID_STLP_FD),
#if defined (_STLP_USE_WIN32_IO)
_M_view_id(0),
#endif
_M_openmode(0),
_M_is_open(false),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -