📄 fstream.cpp
字号:
_M_should_close(false)
{}
void _Filebuf_base::_S_initialize() {
#if defined (_STLP_UNIX) && !defined (__DJGPP) && !defined (_CRAY)
# if defined (__APPLE__)
int mib[2];
size_t pagesize, len;
mib[0] = CTL_HW;
mib[1] = HW_PAGESIZE;
len = sizeof(pagesize);
sysctl(mib, 2, &pagesize, &len, NULL, 0);
_M_page_size = pagesize;
# elif defined (__DJGPP) && defined (_CRAY)
_M_page_size = BUFSIZ;
# else
_M_page_size = sysconf(_SC_PAGESIZE);
# endif
#elif defined (_STLP_USE_WIN32_IO)
SYSTEM_INFO SystemInfo;
GetSystemInfo(&SystemInfo);
_M_page_size = SystemInfo.dwPageSize;
// might be .dwAllocationGranularity
#endif
}
// Return the size of the file. This is a wrapper for stat.
// Returns zero if the size cannot be determined or is ill-defined.
streamoff _Filebuf_base::_M_file_size() {
return _STLP_PRIV __file_size(_M_file_id);
}
bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode,
long permission) {
_STLP_fd file_no;
if (_M_is_open)
return false;
#if defined (_STLP_USE_UNIX_IO) || defined (_STLP_USE_UNIX_EMULATION_IO)
int flags = 0;
// Unix makes no distinction between text and binary files.
switch(openmode & (~ios_base::ate & ~ios_base::binary)) {
case ios_base::out:
case ios_base::out | ios_base::trunc:
flags = O_WRONLY | O_CREAT | O_TRUNC;
break;
case ios_base::out | ios_base::app:
flags = O_WRONLY | O_CREAT | O_APPEND;
break;
case ios_base::in:
flags = O_RDONLY;
permission = 0; // Irrelevant unless we're writing.
break;
case ios_base::in | ios_base::out:
flags = O_RDWR;
break;
case ios_base::in | ios_base::out | ios_base::trunc:
flags = O_RDWR | O_CREAT | O_TRUNC;
break;
default: // The above are the only combinations of
return false; // flags allowed by the C++ standard.
}
# if defined (_STLP_USE_UNIX_EMULATION_IO)
if (openmode & ios_base::binary)
flags |= O_BINARY;
else
flags |= O_TEXT;
file_no = _open(name, flags, permission);
# else
file_no = OPEN(name, flags, permission);
# endif /* _STLP_USE_UNIX_EMULATION_IO */
if (file_no < 0)
return false;
_M_is_open = true;
if (openmode & ios_base::ate)
if (LSEEK(file_no, 0, SEEK_END) == -1)
_M_is_open = false;
#elif defined (_STLP_USE_STDIO_IO)
// use FILE-based i/o
const char* flags;
switch(openmode & (~ios_base::ate)) {
case ios_base::out:
case ios_base::out | ios_base::trunc:
flags = "w";
break;
case ios_base::out | ios_base::binary:
case ios_base::out | ios_base::trunc | ios_base::binary:
flags = "wb";
break;
case ios_base::out | ios_base::app:
flags = "a";
break;
case ios_base::out | ios_base::app | ios_base::binary:
flags = "ab";
break;
case ios_base::in:
flags = "r";
break;
case ios_base::in | ios_base::binary:
flags = "rb";
break;
case ios_base::in | ios_base::out:
flags = "r+";
break;
case ios_base::in | ios_base::out | ios_base::binary:
flags = "r+b";
break;
case ios_base::in | ios_base::out | ios_base::trunc:
flags = "w+";
break;
case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
flags = "w+b";
break;
default: // The above are the only combinations of
return false; // flags allowed by the C++ standard.
}
// fbp : TODO : set permissions !
(void)permission; // currently unused //*TY 02/26/2000 - added to suppress warning message
_M_file = FOPEN(name, flags);
if (_M_file) {
file_no = fileno(_M_file);
}
else
return false;
// unset buffering immediately
setbuf(_M_file, 0);
_M_is_open = true;
if (openmode & ios_base::ate) {
if (FSEEK(_M_file, 0, SEEK_END) == -1)
_M_is_open = false;
}
#elif defined (_STLP_USE_WIN32_IO)
DWORD dwDesiredAccess, dwCreationDisposition;
bool doTruncate = false;
switch (openmode & (~ios_base::ate & ~ios_base::binary)) {
case ios_base::out:
case ios_base::out | ios_base::trunc:
dwDesiredAccess = GENERIC_WRITE;
dwCreationDisposition = OPEN_ALWAYS;
// boris : even though it is very non-intuitive, standard
// requires them both to behave same.
doTruncate = true;
break;
case ios_base::out | ios_base::app:
dwDesiredAccess = GENERIC_WRITE;
dwCreationDisposition = OPEN_ALWAYS;
break;
case ios_base::in:
dwDesiredAccess = GENERIC_READ;
dwCreationDisposition = OPEN_EXISTING;
permission = 0; // Irrelevant unless we're writing.
break;
case ios_base::in | ios_base::out:
dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
dwCreationDisposition = OPEN_EXISTING;
break;
case ios_base::in | ios_base::out | ios_base::trunc:
dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
dwCreationDisposition = OPEN_ALWAYS;
doTruncate = true;
break;
default: // The above are the only combinations of
return false; // flags allowed by the C++ standard.
}
DWORD dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
# if defined(_STLP_USE_WIDE_INTERFACE)
file_no = CreateFile (_STLP_PRIV __ASCIIToWide(name).c_str(),
# else
file_no = CreateFileA(name,
# endif
dwDesiredAccess, dwShareMode, 0,
dwCreationDisposition, permission, 0);
if (file_no == INVALID_STLP_FD)
return false;
if ((doTruncate && SetEndOfFile(file_no) == 0) ||
(((openmode & ios_base::ate) != 0) &&
(SetFilePointer(file_no, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER))) {
CloseHandle(file_no);
return false;
}
_M_is_open = true;
#else
# error "Port!"
#endif /* __unix */
_M_file_id = file_no;
_M_should_close = _M_is_open;
_M_openmode = openmode;
if (_M_is_open)
_M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
return (_M_is_open != 0);
}
bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode) {
// This doesn't really grant everyone in the world read/write
// access. On Unix, file-creation system calls always clear
// bits that are set in the umask from the permissions flag.
#ifdef _STLP_USE_WIN32_IO
return this->_M_open(name, openmode, FILE_ATTRIBUTE_NORMAL);
#elif defined(__MRC__) || defined(__SC__) //*TY 02/26/2000 - added support for MPW compilers
return this->_M_open(name, openmode, 0);
#else
return this->_M_open(name, openmode, S_IRUSR | S_IWUSR | S_IRGRP |
S_IWGRP | S_IROTH | S_IWOTH);
#endif
}
#if defined (_STLP_USE_WIN32_IO)
bool _Filebuf_base::_M_open(_STLP_fd __id, ios_base::openmode init_mode) {
# if (defined (_STLP_MSVC_LIB) && !defined (_STLP_WCE)) || \
(defined (__MINGW32__) && defined (__MSVCRT__)) || defined (__DMC__)
if (_M_is_open || __id == INVALID_STLP_FD)
return false;
if (init_mode != ios_base::__default_mode)
_M_openmode = init_mode;
else
_M_openmode = _get_osfflags(-1, __id);
_M_is_open = true;
_M_file_id = __id;
_M_should_close = false;
_M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
return true;
# else
(void)__id;
(void)init_mode; // dwa 4/27/00 - suppress unused parameter warning
// not available for the API
return false;
# endif
}
#endif /* _STLP_USE_WIN32_IO */
// Associated the filebuf with a file descriptor pointing to an already-
// open file. Mode is set to be consistent with the way that the file
// was opened.
bool _Filebuf_base::_M_open(int file_no, ios_base::openmode init_mode) {
if (_M_is_open || file_no < 0)
return false;
#if defined (_STLP_UNIX)
(void)init_mode; // dwa 4/27/00 - suppress unused parameter warning
int mode ;
mode = fcntl(file_no, F_GETFL);
if (mode == -1)
return false;
_M_openmode = flag_to_openmode(mode);
_M_file_id = file_no;
#elif defined(__MRC__) || defined(__SC__) //*TY 02/26/2000 - added support for MPW compilers
(void)init_mode; // dwa 4/27/00 - suppress unused parameter warning
switch (_iob[file_no]._flag & (_IOREAD|_IOWRT|_IORW) )
{
case _IOREAD:
_M_openmode = ios_base::in; break;
case _IOWRT:
_M_openmode = ios_base::out; break;
case _IORW:
_M_openmode = ios_base::in | ios_base::out; break;
default:
return false;
}
_M_file_id = file_no;
#elif defined (_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO)
(void)init_mode; // dwa 4/27/00 - suppress unused parameter warning
int mode ;
struct STAT buf;
if (FSTAT(file_no, &buf) != 0)
return false;
mode = buf.st_mode;
switch(mode & (_S_IWRITE | _S_IREAD) ) {
case _S_IREAD:
_M_openmode = ios_base::in; break;
case _S_IWRITE:
_M_openmode = ios_base::out; break;
case (_S_IWRITE | _S_IREAD):
_M_openmode = ios_base::in | ios_base::out; break;
default:
return false;
}
_M_file_id = file_no;
#elif (defined (_STLP_USE_WIN32_IO) && defined (_STLP_MSVC_LIB) && !defined (_STLP_WCE) ) || \
(defined (__MINGW32__) && defined (__MSVCRT__)) || \
defined (__DMC__)
HANDLE oshandle = (HANDLE)_get_osfhandle(file_no);
if (oshandle == INVALID_STLP_FD)
return false;
if (init_mode != ios_base::__default_mode)
_M_openmode = init_mode;
else
_M_openmode = _get_osfflags(file_no, oshandle);
_M_file_id = oshandle;
#else
(void)init_mode; // dwa 4/27/00 - suppress unused parameter warning
// not available for the API
# define _STLP_NO_IMPLEMENTATION
#endif
#if !defined (_STLP_NO_IMPLEMENTATION)
_M_is_open = true;
_M_should_close = false;
_M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
return true;
#else
# undef _STLP_NO_IMPLEMENTATION
return false;
#endif
}
bool _Filebuf_base::_M_close() {
if (!_M_is_open)
return false;
bool ok;
if (!_M_should_close)
ok = true;
else {
#if defined (_STLP_USE_UNIX_IO)
ok = (close(_M_file_id) == 0);
#elif defined (_STLP_USE_UNIX_EMULATION_IO)
ok = (_close(_M_file_id) == 0);
#elif defined (_STLP_USE_STDIO_IO)
ok = (fclose(_M_file) == 0);
#elif defined (_STLP_USE_WIN32_IO)
if (_M_file_id != INVALID_STLP_FD) {
ok = (CloseHandle(_M_file_id) != 0);
}
else {
ok = false;
}
#else
ok = false;
#endif /* _STLP_USE_UNIX_IO */
}
_M_is_open = _M_should_close = false;
_M_openmode = 0;
return ok;
}
#define _STLP_LF 10
#define _STLP_CR 13
#define _STLP_CTRLZ 26
// Read up to n characters into a buffer. Return value is number of
// characters read.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -