sl@0: /* sl@0: * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. sl@0: * sl@0: * Copyright (c) 1999 sl@0: * Silicon Graphics Computer Systems, Inc. sl@0: * sl@0: * Copyright (c) 1999 sl@0: * Boris Fomitchev sl@0: * sl@0: * This material is provided "as is", with absolutely no warranty expressed sl@0: * or implied. Any use is at your own risk. sl@0: * sl@0: * Permission to use or copy this software for any purpose is hereby granted sl@0: * without fee, provided the above notices are retained on all copies. sl@0: * Permission to modify the code and to distribute modified code is granted, sl@0: * provided the above notices are retained, and a notice that the code was sl@0: * modified is included with the above copyright notice. sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "stlport_prefix.h" sl@0: sl@0: #if defined (__SUNPPRO_CC) && !defined (_STLP_NO_NEW_C_HEADERS) sl@0: # include sl@0: // For sunpro, it chokes if time.h is included through stat.h sl@0: #endif sl@0: sl@0: #include sl@0: sl@0: #ifdef __CYGWIN__ sl@0: # define __int64 long long sl@0: #endif sl@0: sl@0: #if defined (_STLP_USE_UNIX_IO) sl@0: extern "C" { sl@0: // open/close/read/write sl@0: # include // For stat sl@0: # if !defined (_CRAY) && ! defined (__EMX__) sl@0: # include // For mmap sl@0: # endif sl@0: sl@0: // on HP-UX 11, this one contradicts with pthread.h on pthread_atfork, unless we unset this sl@0: # if defined (__hpux) && defined (__GNUC__) sl@0: # undef _INCLUDE_POSIX1C_SOURCE sl@0: # endif sl@0: sl@0: # include sl@0: # include sl@0: } sl@0: # ifdef __APPLE__ sl@0: # include sl@0: # endif sl@0: #elif defined (_STLP_USE_WIN32_IO) sl@0: # define WIN32_LEAN_AND_MEAN sl@0: # include sl@0: sl@0: # ifdef __BORLANDC__ sl@0: # include // For _O_RDONLY, etc sl@0: # include // For _fstat sl@0: # elif !defined(_STLP_WCE) sl@0: # include // For _get_osfhandle sl@0: # include // For _O_RDONLY, etc sl@0: # include // For _fstat sl@0: # endif sl@0: # define _TEXTBUF_SIZE 0x1000 sl@0: #elif defined (_STLP_USE_UNIX_EMULATION_IO) sl@0: # if defined( __MSL__ ) sl@0: # include sl@0: # else sl@0: # include sl@0: # endif sl@0: # include sl@0: # include sl@0: #elif defined (_STLP_USE_STDIO_IO) sl@0: # include sl@0: # if !(defined(__MRC__) || defined(__SC__) || defined(__ISCPP__) ) sl@0: extern "C" { sl@0: # include sl@0: } sl@0: # endif sl@0: # if defined( __MSL__ ) sl@0: # include sl@0: # endif sl@0: # if defined(__ISCPP__) sl@0: # include sl@0: # endif sl@0: # if (defined(__BEOS__) && defined(__INTEL__)) || defined (__SYMBIAN32__) sl@0: # include sl@0: # include // For _fstat sl@0: # define _S_IREAD S_IREAD sl@0: # define _S_IWRITE S_IWRITE sl@0: # define _S_IFREG S_IFREG sl@0: # endif sl@0: #else sl@0: # error "Configure i/o !" sl@0: #endif sl@0: sl@0: #if defined (_STLP_USE_WIN32_IO) sl@0: const _STLP_fd INVALID_STLP_FD = INVALID_HANDLE_VALUE; sl@0: # if !defined (INVALID_SET_FILE_POINTER) sl@0: # define INVALID_SET_FILE_POINTER 0xffffffff sl@0: # endif sl@0: #elif defined (_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO) || defined (_STLP_USE_UNIX_IO) sl@0: const _STLP_fd INVALID_STLP_FD = -1; sl@0: #else sl@0: # error "Configure i/o !" sl@0: #endif sl@0: sl@0: // map permission masks sl@0: #if defined (_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO) sl@0: # ifndef S_IRUSR sl@0: # define S_IRUSR _S_IREAD sl@0: # define S_IWUSR _S_IWRITE sl@0: # define S_IRGRP _S_IREAD sl@0: # define S_IWGRP _S_IWRITE sl@0: # define S_IROTH _S_IREAD sl@0: # define S_IWOTH _S_IWRITE sl@0: # endif sl@0: # ifndef O_RDONLY sl@0: # define O_RDONLY _O_RDONLY sl@0: # define O_WRONLY _O_WRONLY sl@0: # define O_RDWR _O_RDWR sl@0: # define O_APPEND _O_APPEND sl@0: # define O_CREAT _O_CREAT sl@0: # define O_TRUNC _O_TRUNC sl@0: # define O_TEXT _O_TEXT sl@0: # define O_BINARY _O_BINARY sl@0: # endif sl@0: sl@0: # ifdef __MSL__ sl@0: # define _O_TEXT 0x0 sl@0: # if !defined( O_TEXT ) sl@0: # define O_TEXT _O_TEXT sl@0: # endif sl@0: # define _S_IFREG S_IFREG sl@0: # define S_IREAD S_IRUSR sl@0: # define S_IWRITE S_IWUSR sl@0: # define S_IEXEC S_IXUSR sl@0: # define _S_IWRITE S_IWRITE sl@0: # define _S_IREAD S_IREAD sl@0: # define _open open sl@0: # define _lseek lseek sl@0: # define _close close sl@0: # define _read read sl@0: # define _write write sl@0: # endif sl@0: #endif sl@0: sl@0: #ifndef O_ACCMODE sl@0: # define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR) sl@0: #endif sl@0: sl@0: #include "fstream_impl.h" sl@0: sl@0: #ifdef _STLP_LONG_LONG sl@0: # define ULL(x) ((unsigned _STLP_LONG_LONG)x) sl@0: #elif defined(__MRC__) || defined(__SC__) //*TY 02/25/2000 - added support for MPW compilers sl@0: # include sl@0: # define ULL(x) (U64SetU(x)) sl@0: #elif defined(__ISCPP__) sl@0: # include "uint64.h" sl@0: #elif defined(__SYMBIAN32__) sl@0: # include sl@0: #else sl@0: # error "there should be some long long type on the system!" sl@0: #endif sl@0: sl@0: sl@0: #if defined(__SYMBIAN32__WSD__) sl@0: # include "libstdcppwsd.h" sl@0: sl@0: _STLP_DECLSPEC size_t& get_fstream_Filebuf_Base_GetPageSize() sl@0: { sl@0: return get_libcpp_wsd().fstream_Filebuf_base_M_page_size; sl@0: } sl@0: sl@0: void filebuf_page_size_init() sl@0: { sl@0: get_fstream_Filebuf_Base_GetPageSize() = 4096; sl@0: } sl@0: #endif sl@0: sl@0: _STLP_BEGIN_NAMESPACE sl@0: // Compare with streamoff definition in stl/char_traits.h! sl@0: sl@0: #ifdef _STLP_USE_DEFAULT_FILE_OFFSET sl@0: # define FOPEN fopen sl@0: # define FSEEK fseek sl@0: # define FSTAT fstat sl@0: # define STAT stat sl@0: # define FTELL ftell sl@0: # define LSEEK lseek sl@0: # define MMAP mmap sl@0: # define OPEN open sl@0: #elif defined(_LARGEFILE_SOURCE) || defined(_LARGEFILE64_SOURCE) /* || defined(__USE_FILE_OFFSET64) */ \ sl@0: /* || (defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64)) */ /* || defined(__sgi) */ sl@0: # define FOPEN fopen64 sl@0: # define FSEEK fseeko64 sl@0: # define FSTAT fstat64 sl@0: # define STAT stat64 sl@0: # define FTELL ftello64 sl@0: # define LSEEK lseek64 sl@0: # define MMAP mmap64 sl@0: # define OPEN open64 sl@0: #else sl@0: # define OPEN open sl@0: # define FSEEK fseek sl@0: # define FSTAT fstat sl@0: # define STAT stat sl@0: # define FTELL ftell sl@0: # define LSEEK lseek sl@0: # define MMAP mmap sl@0: # define OPEN open sl@0: #endif sl@0: #ifdef _STLP_USE_UNIX_IO sl@0: # ifndef MAP_FAILED /* MMAP failure return code */ sl@0: # define MAP_FAILED -1 sl@0: # endif sl@0: #elif defined (_STLP_USE_UNIX_EMULATION_IO) sl@0: # define LSEEK _lseek sl@0: #endif sl@0: sl@0: #if !defined(__MSL__) && !defined(__MRC__) && !defined(__SC__) && !defined(_STLP_WCE) //*TY 04/15/2000 - exclude mpw compilers also sl@0: static ios_base::openmode flag_to_openmode(int mode) { sl@0: ios_base::openmode ret = ios_base::__default_mode; sl@0: sl@0: switch(mode & O_ACCMODE) { sl@0: case O_RDONLY: sl@0: ret = ios_base::in; break; sl@0: case O_WRONLY: sl@0: ret = ios_base::out; break; sl@0: case O_RDWR: sl@0: ret = ios_base::in | ios_base::out; break; sl@0: } sl@0: sl@0: if (mode & O_APPEND) sl@0: ret |= ios_base::app; sl@0: sl@0: # if defined (_STLP_USE_WIN32_IO) sl@0: if (mode & O_BINARY) sl@0: ret |= ios_base::binary; sl@0: # endif sl@0: sl@0: return ret; sl@0: } sl@0: #endif /* MSL */ sl@0: sl@0: _STLP_MOVE_TO_PRIV_NAMESPACE sl@0: sl@0: // Helper functions for _Filebuf_base. sl@0: sl@0: bool __is_regular_file(_STLP_fd fd) { sl@0: sl@0: #if defined (_STLP_UNIX) sl@0: sl@0: struct STAT buf; sl@0: return FSTAT(fd, &buf) == 0 && S_ISREG(buf.st_mode); sl@0: sl@0: #elif defined(__MRC__) || defined(__SC__) //*TY 02/25/2000 - added support for MPW compilers sl@0: sl@0: # pragma unused(fd) sl@0: return true; // each file is a regular file under mac os, isn't it? (we don't have fstat()) sl@0: sl@0: #elif defined(_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO) sl@0: sl@0: struct STAT buf; sl@0: return FSTAT(fd, &buf) == 0 && (buf.st_mode & _S_IFREG) != 0 ; sl@0: sl@0: #elif defined (_STLP_USE_WIN32_IO) && !defined(_STLP_WCE) sl@0: sl@0: return (GetFileType(fd) & ~FILE_TYPE_REMOTE) == FILE_TYPE_DISK; sl@0: sl@0: #else sl@0: (void)fd; // dwa 4/27/00 - suppress unused parameter warning sl@0: return false; sl@0: #endif sl@0: } sl@0: sl@0: // Number of characters in the file. sl@0: streamoff __file_size(_STLP_fd fd) { sl@0: streamoff ret = 0; sl@0: sl@0: #if defined (_STLP_UNIX) sl@0: sl@0: struct STAT buf; sl@0: if (FSTAT(fd, &buf) == 0 && S_ISREG(buf.st_mode)) sl@0: ret = buf.st_size > 0 ? buf.st_size : 0; sl@0: sl@0: #elif defined(__MRC__) || defined(__SC__) //*TY 02/25/2000 - added support for MPW compilers sl@0: sl@0: # pragma unused(fd) sl@0: sl@0: #elif defined(_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO) sl@0: sl@0: struct STAT buf; sl@0: if (FSTAT(fd, &buf) == 0 && (buf.st_mode & _S_IFREG) != 0) sl@0: ret = buf.st_size > 0 ? buf.st_size : 0; sl@0: sl@0: #elif defined (_STLP_USE_WIN32_IO) sl@0: sl@0: LARGE_INTEGER li; sl@0: li.LowPart = GetFileSize(fd, (unsigned long*) &li.HighPart); sl@0: if (li.LowPart != INVALID_FILE_SIZE || GetLastError() == NO_ERROR) sl@0: ret = li.QuadPart; sl@0: sl@0: #else sl@0: (void)fd; // dwa 4/27/00 - suppress unused parameter warning sl@0: #endif sl@0: return ret; sl@0: } sl@0: sl@0: _STLP_MOVE_TO_STD_NAMESPACE sl@0: sl@0: // Visual C++ and Intel use this, but not Metrowerks sl@0: // Also MinGW, msvcrt.dll (but not crtdll.dll) dependent version sl@0: #if (!defined (__MSL__) && !defined (_STLP_WCE) && defined (_STLP_MSVC_LIB) && defined (_WIN32)) || \ sl@0: (defined (__MINGW32__) && defined (__MSVCRT__) && !(defined (__SYMBIAN32__) && defined(__GCCXML__)) ) sl@0: sl@0: // fcntl(fileno, F_GETFL) for Microsoft library sl@0: // 'semi-documented' defines: sl@0: # define IOINFO_L2E 5 sl@0: # define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) sl@0: # define _pioinfo(i) ( __pioinfo[(i) >> IOINFO_L2E] + \ sl@0: ((i) & (IOINFO_ARRAY_ELTS - 1)) ) sl@0: # define FAPPEND 0x20 // O_APPEND flag sl@0: # define FTEXT 0x80 // O_TEXT flag sl@0: // end of 'semi-documented' defines sl@0: sl@0: // 'semi-documented' internal structure sl@0: extern "C" { sl@0: struct ioinfo { sl@0: long osfhnd; // the real os HANDLE sl@0: char osfile; // file handle flags sl@0: char pipech; // pipe buffer sl@0: # if defined (_MT) sl@0: // multi-threaded locking sl@0: int lockinitflag; sl@0: CRITICAL_SECTION lock; sl@0: # endif /* _MT */ sl@0: }; sl@0: # if defined (__MINGW32__) sl@0: __MINGW_IMPORT ioinfo * __pioinfo[]; sl@0: # else sl@0: extern _CRTIMP ioinfo * __pioinfo[]; sl@0: # endif sl@0: } // extern "C" sl@0: // end of 'semi-documented' declarations sl@0: sl@0: static ios_base::openmode _get_osfflags(int fd, HANDLE oshandle) { sl@0: char dosflags = 0; sl@0: if (fd >= 0) sl@0: dosflags = _pioinfo(fd)->osfile; sl@0: //else sl@0: //the file will be considered as open in binary mode with no append attribute sl@0: // end of 'semi-documented' stuff sl@0: sl@0: int mode = 0; sl@0: if (dosflags & FAPPEND) sl@0: mode |= O_APPEND; sl@0: sl@0: if (dosflags & FTEXT) sl@0: mode |= O_TEXT; sl@0: else sl@0: mode |= O_BINARY; sl@0: sl@0: // For Read/Write access we have to guess sl@0: DWORD dummy, dummy2; sl@0: BOOL writeOk = WriteFile(oshandle, &dummy2, 0, &dummy, 0); sl@0: BOOL readOk = ReadFile(oshandle, &dummy2, 0, &dummy, NULL); sl@0: if (writeOk && readOk) sl@0: mode |= O_RDWR; sl@0: else if (readOk) sl@0: mode |= O_RDONLY; sl@0: else sl@0: mode |= O_WRONLY; sl@0: sl@0: return flag_to_openmode(mode); sl@0: } sl@0: sl@0: #elif defined (__DMC__) sl@0: sl@0: # define FHND_APPEND 0x04 sl@0: # define FHND_DEVICE 0x08 sl@0: # define FHND_TEXT 0x10 sl@0: sl@0: extern "C" unsigned char __fhnd_info[_NFILE]; sl@0: sl@0: static ios_base::openmode _get_osfflags(int fd, HANDLE oshandle) { sl@0: int mode = 0; sl@0: sl@0: if (__fhnd_info[fd] & FHND_APPEND) sl@0: mode |= O_APPEND; sl@0: sl@0: if (__fhnd_info[fd] & FHND_TEXT == 0) sl@0: mode |= O_BINARY; sl@0: sl@0: for (FILE *fp = &_iob[0]; fp < &_iob[_NFILE]; fp++) { sl@0: if ((fileno(fp) == fd) && (fp->_flag & (_IOREAD | _IOWRT | _IORW))) { sl@0: const int osflags = fp->_flag; sl@0: sl@0: if ((osflags & _IOREAD) && !(osflags & _IOWRT) && !(osflags & _IORW)) sl@0: mode |= O_RDONLY; sl@0: else if ((osflags & _IOWRT) && !(osflags & _IOREAD) && !(osflags & _IORW)) sl@0: mode |= O_WRONLY; sl@0: else sl@0: mode |= O_RDWR; sl@0: break; sl@0: } sl@0: } sl@0: sl@0: return flag_to_openmode(mode); sl@0: } sl@0: #endif sl@0: sl@0: #if !defined(__SYMBIAN32__WSD__) sl@0: size_t _Filebuf_base::_M_page_size = 4096; sl@0: #endif //__SYMBIAN32__WSD__ sl@0: sl@0: _STLP_DECLSPEC _Filebuf_base::_Filebuf_base() sl@0: : _M_file_id(INVALID_STLP_FD), sl@0: #if defined (_STLP_USE_WIN32_IO) sl@0: _M_view_id(0), sl@0: #endif sl@0: _M_openmode(0), sl@0: _M_is_open(false), sl@0: _M_should_close(false) sl@0: {} sl@0: sl@0: void _Filebuf_base::_S_initialize() { sl@0: #if defined (_STLP_UNIX) && !defined (__DJGPP) && !defined (_CRAY) sl@0: # if defined (__APPLE__) sl@0: int mib[2]; sl@0: size_t pagesize, len; sl@0: mib[0] = CTL_HW; sl@0: mib[1] = HW_PAGESIZE; sl@0: len = sizeof(pagesize); sl@0: sysctl(mib, 2, &pagesize, &len, NULL, 0); sl@0: _M_page_size = pagesize; sl@0: # elif defined (__DJGPP) && defined (_CRAY) sl@0: _M_page_size = BUFSIZ; sl@0: # else sl@0: _M_page_size = sysconf(_SC_PAGESIZE); sl@0: # endif sl@0: #elif defined (_STLP_USE_WIN32_IO) sl@0: SYSTEM_INFO SystemInfo; sl@0: GetSystemInfo(&SystemInfo); sl@0: _M_page_size = SystemInfo.dwPageSize; sl@0: // might be .dwAllocationGranularity sl@0: #endif sl@0: } sl@0: sl@0: // Return the size of the file. This is a wrapper for stat. sl@0: // Returns zero if the size cannot be determined or is ill-defined. sl@0: _STLP_DECLSPEC streamoff _Filebuf_base::_M_file_size() { sl@0: return _STLP_PRIV __file_size(_M_file_id); sl@0: } sl@0: sl@0: _STLP_DECLSPEC bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode, sl@0: long permission) { sl@0: _STLP_fd file_no; sl@0: sl@0: if (_M_is_open) sl@0: return false; sl@0: sl@0: #if defined (_STLP_USE_UNIX_IO) || defined (_STLP_USE_UNIX_EMULATION_IO) sl@0: int flags = 0; sl@0: sl@0: // Unix makes no distinction between text and binary files. sl@0: switch(openmode & (~ios_base::ate & ~ios_base::binary)) { sl@0: case ios_base::out: sl@0: case ios_base::out | ios_base::trunc: sl@0: flags = O_WRONLY | O_CREAT | O_TRUNC; sl@0: break; sl@0: case ios_base::out | ios_base::app: sl@0: flags = O_WRONLY | O_CREAT | O_APPEND; sl@0: break; sl@0: case ios_base::in: sl@0: flags = O_RDONLY; sl@0: permission = 0; // Irrelevant unless we're writing. sl@0: break; sl@0: case ios_base::in | ios_base::out: sl@0: flags = O_RDWR; sl@0: break; sl@0: case ios_base::in | ios_base::out | ios_base::trunc: sl@0: flags = O_RDWR | O_CREAT | O_TRUNC; sl@0: break; sl@0: default: // The above are the only combinations of sl@0: return false; // flags allowed by the C++ standard. sl@0: } sl@0: sl@0: # if defined (_STLP_USE_UNIX_EMULATION_IO) sl@0: if (openmode & ios_base::binary) sl@0: flags |= O_BINARY; sl@0: else sl@0: flags |= O_TEXT; sl@0: sl@0: file_no = _open(name, flags, permission); sl@0: # else sl@0: file_no = OPEN(name, flags, permission); sl@0: # endif /* _STLP_USE_UNIX_EMULATION_IO */ sl@0: sl@0: if (file_no < 0) sl@0: return false; sl@0: sl@0: _M_is_open = true; sl@0: sl@0: if (openmode & ios_base::ate) sl@0: if (LSEEK(file_no, 0, SEEK_END) == -1) sl@0: _M_is_open = false; sl@0: sl@0: #elif defined (_STLP_USE_STDIO_IO) sl@0: // use FILE-based i/o sl@0: const char* flags; sl@0: sl@0: switch(openmode & (~ios_base::ate)) { sl@0: case ios_base::out: sl@0: case ios_base::out | ios_base::trunc: sl@0: flags = "w"; sl@0: break; sl@0: sl@0: case ios_base::out | ios_base::binary: sl@0: case ios_base::out | ios_base::trunc | ios_base::binary: sl@0: flags = "wb"; sl@0: break; sl@0: sl@0: case ios_base::out | ios_base::app: sl@0: flags = "a"; sl@0: break; sl@0: sl@0: case ios_base::out | ios_base::app | ios_base::binary: sl@0: flags = "ab"; sl@0: break; sl@0: sl@0: case ios_base::in: sl@0: flags = "r"; sl@0: break; sl@0: sl@0: case ios_base::in | ios_base::binary: sl@0: flags = "rb"; sl@0: break; sl@0: sl@0: case ios_base::in | ios_base::out: sl@0: flags = "r+"; sl@0: break; sl@0: sl@0: case ios_base::in | ios_base::out | ios_base::binary: sl@0: flags = "r+b"; sl@0: break; sl@0: sl@0: sl@0: case ios_base::in | ios_base::out | ios_base::trunc: sl@0: flags = "w+"; sl@0: break; sl@0: sl@0: case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary: sl@0: flags = "w+b"; sl@0: break; sl@0: sl@0: default: // The above are the only combinations of sl@0: return false; // flags allowed by the C++ standard. sl@0: } sl@0: sl@0: // fbp : : set permissions ! sl@0: (void)permission; // currently unused //*TY 02/26/2000 - added to suppress warning message sl@0: _M_file = FOPEN(name, flags); sl@0: sl@0: if (_M_file) { sl@0: file_no = fileno(_M_file); sl@0: } sl@0: else sl@0: return false; sl@0: sl@0: // unset buffering immediately sl@0: setbuf(_M_file, 0); sl@0: sl@0: _M_is_open = true; sl@0: sl@0: if (openmode & ios_base::ate) { sl@0: if (FSEEK(_M_file, 0, SEEK_END) == -1) sl@0: _M_is_open = false; sl@0: } sl@0: sl@0: #elif defined (_STLP_USE_WIN32_IO) sl@0: DWORD dwDesiredAccess, dwCreationDisposition; sl@0: bool doTruncate = false; sl@0: sl@0: switch (openmode & (~ios_base::ate & ~ios_base::binary)) { sl@0: case ios_base::out: sl@0: case ios_base::out | ios_base::trunc: sl@0: dwDesiredAccess = GENERIC_WRITE; sl@0: dwCreationDisposition = OPEN_ALWAYS; sl@0: // boris : even though it is very non-intuitive, standard sl@0: // requires them both to behave same. sl@0: doTruncate = true; sl@0: break; sl@0: case ios_base::out | ios_base::app: sl@0: dwDesiredAccess = GENERIC_WRITE; sl@0: dwCreationDisposition = OPEN_ALWAYS; sl@0: break; sl@0: case ios_base::in: sl@0: dwDesiredAccess = GENERIC_READ; sl@0: dwCreationDisposition = OPEN_EXISTING; sl@0: permission = 0; // Irrelevant unless we're writing. sl@0: break; sl@0: case ios_base::in | ios_base::out: sl@0: dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; sl@0: dwCreationDisposition = OPEN_EXISTING; sl@0: break; sl@0: case ios_base::in | ios_base::out | ios_base::trunc: sl@0: dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; sl@0: dwCreationDisposition = OPEN_ALWAYS; sl@0: doTruncate = true; sl@0: break; sl@0: default: // The above are the only combinations of sl@0: return false; // flags allowed by the C++ standard. sl@0: } sl@0: sl@0: DWORD dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; sl@0: sl@0: # if defined(_STLP_USE_WIDE_INTERFACE) sl@0: file_no = CreateFile (_STLP_PRIV __ASCIIToWide(name).c_str(), sl@0: # else sl@0: file_no = CreateFileA(name, sl@0: # endif sl@0: dwDesiredAccess, dwShareMode, 0, sl@0: dwCreationDisposition, permission, 0); sl@0: sl@0: if (file_no == INVALID_STLP_FD) sl@0: return false; sl@0: sl@0: if ((doTruncate && SetEndOfFile(file_no) == 0) || sl@0: (((openmode & ios_base::ate) != 0) && sl@0: (SetFilePointer(file_no, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER))) { sl@0: CloseHandle(file_no); sl@0: return false; sl@0: } sl@0: sl@0: _M_is_open = true; sl@0: sl@0: #else sl@0: # error "Port!" sl@0: #endif /* __unix */ sl@0: sl@0: _M_file_id = file_no; sl@0: _M_should_close = _M_is_open; sl@0: _M_openmode = openmode; sl@0: sl@0: if (_M_is_open) sl@0: _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id); sl@0: sl@0: return (_M_is_open != 0); sl@0: } sl@0: sl@0: sl@0: _STLP_DECLSPEC bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode) { sl@0: // This doesn't really grant everyone in the world read/write sl@0: // access. On Unix, file-creation system calls always clear sl@0: // bits that are set in the umask from the permissions flag. sl@0: #ifdef _STLP_USE_WIN32_IO sl@0: return this->_M_open(name, openmode, FILE_ATTRIBUTE_NORMAL); sl@0: #elif defined(__MRC__) || defined(__SC__) //*TY 02/26/2000 - added support for MPW compilers sl@0: return this->_M_open(name, openmode, 0); sl@0: #else sl@0: return this->_M_open(name, openmode, S_IRUSR | S_IWUSR | S_IRGRP | sl@0: S_IWGRP | S_IROTH | S_IWOTH); sl@0: #endif sl@0: } sl@0: sl@0: sl@0: #if defined (_STLP_USE_WIN32_IO) sl@0: bool _Filebuf_base::_M_open(_STLP_fd __id, ios_base::openmode init_mode) { sl@0: # if (defined (_STLP_MSVC_LIB) && !defined (_STLP_WCE)) || \ sl@0: (defined (__MINGW32__) && defined (__MSVCRT__)) || defined (__DMC__) sl@0: sl@0: if (_M_is_open || __id == INVALID_STLP_FD) sl@0: return false; sl@0: sl@0: if (init_mode != ios_base::__default_mode) sl@0: _M_openmode = init_mode; sl@0: else sl@0: _M_openmode = _get_osfflags(-1, __id); sl@0: sl@0: _M_is_open = true; sl@0: _M_file_id = __id; sl@0: _M_should_close = false; sl@0: _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id); sl@0: sl@0: return true; sl@0: # else sl@0: (void)__id; sl@0: (void)init_mode; // dwa 4/27/00 - suppress unused parameter warning sl@0: sl@0: // not available for the API sl@0: return false; sl@0: sl@0: # endif sl@0: } sl@0: #endif /* _STLP_USE_WIN32_IO */ sl@0: sl@0: // Associated the filebuf with a file descriptor pointing to an already- sl@0: // open file. Mode is set to be consistent with the way that the file sl@0: // was opened. sl@0: _STLP_DECLSPEC bool _Filebuf_base::_M_open(int file_no, ios_base::openmode init_mode) { sl@0: if (_M_is_open || file_no < 0) sl@0: return false; sl@0: sl@0: #if defined (_STLP_UNIX) sl@0: (void)init_mode; // dwa 4/27/00 - suppress unused parameter warning sl@0: int mode ; sl@0: mode = fcntl(file_no, F_GETFL); sl@0: sl@0: if (mode == -1) sl@0: return false; sl@0: sl@0: _M_openmode = flag_to_openmode(mode); sl@0: _M_file_id = file_no; sl@0: #elif defined(__MRC__) || defined(__SC__) //*TY 02/26/2000 - added support for MPW compilers sl@0: (void)init_mode; // dwa 4/27/00 - suppress unused parameter warning sl@0: switch (_iob[file_no]._flag & (_IOREAD|_IOWRT|_IORW) ) sl@0: { sl@0: case _IOREAD: sl@0: _M_openmode = ios_base::in; break; sl@0: case _IOWRT: sl@0: _M_openmode = ios_base::out; break; sl@0: case _IORW: sl@0: _M_openmode = ios_base::in | ios_base::out; break; sl@0: default: sl@0: return false; sl@0: } sl@0: _M_file_id = file_no; sl@0: #elif defined (_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO) sl@0: (void)init_mode; // dwa 4/27/00 - suppress unused parameter warning sl@0: int mode ; sl@0: struct STAT buf; sl@0: if (FSTAT(file_no, &buf) != 0) sl@0: return false; sl@0: mode = buf.st_mode; sl@0: sl@0: switch(mode & (_S_IWRITE | _S_IREAD) ) { sl@0: case _S_IREAD: sl@0: _M_openmode = ios_base::in; break; sl@0: case _S_IWRITE: sl@0: _M_openmode = ios_base::out; break; sl@0: case (_S_IWRITE | _S_IREAD): sl@0: _M_openmode = ios_base::in | ios_base::out; break; sl@0: default: sl@0: return false; sl@0: } sl@0: _M_file_id = file_no; sl@0: #elif (defined (_STLP_USE_WIN32_IO) && defined (_STLP_MSVC_LIB) && !defined (_STLP_WCE) ) || \ sl@0: (defined (__MINGW32__) && defined (__MSVCRT__)) || \ sl@0: defined (__DMC__) sl@0: sl@0: HANDLE oshandle = (HANDLE)_get_osfhandle(file_no); sl@0: if (oshandle == INVALID_STLP_FD) sl@0: return false; sl@0: sl@0: if (init_mode != ios_base::__default_mode) sl@0: _M_openmode = init_mode; sl@0: else sl@0: _M_openmode = _get_osfflags(file_no, oshandle); sl@0: sl@0: _M_file_id = oshandle; sl@0: #else sl@0: (void)init_mode; // dwa 4/27/00 - suppress unused parameter warning sl@0: // not available for the API sl@0: # define _STLP_NO_IMPLEMENTATION sl@0: #endif sl@0: sl@0: #if !defined (_STLP_NO_IMPLEMENTATION) sl@0: _M_is_open = true; sl@0: _M_should_close = false; sl@0: _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id); sl@0: return true; sl@0: #else sl@0: # undef _STLP_NO_IMPLEMENTATION sl@0: return false; sl@0: #endif sl@0: } sl@0: sl@0: _STLP_DECLSPEC bool _Filebuf_base::_M_close() { sl@0: if (!_M_is_open) sl@0: return false; sl@0: sl@0: bool ok; sl@0: sl@0: if (!_M_should_close) sl@0: ok = true; sl@0: else { sl@0: sl@0: #if defined (_STLP_USE_UNIX_IO) sl@0: sl@0: ok = (close(_M_file_id) == 0); sl@0: sl@0: #elif defined (_STLP_USE_UNIX_EMULATION_IO) sl@0: sl@0: ok = (_close(_M_file_id) == 0); sl@0: sl@0: #elif defined (_STLP_USE_STDIO_IO) sl@0: sl@0: ok = (fclose(_M_file) == 0); sl@0: sl@0: #elif defined (_STLP_USE_WIN32_IO) sl@0: sl@0: if (_M_file_id != INVALID_STLP_FD) { sl@0: ok = (CloseHandle(_M_file_id) != 0); sl@0: } sl@0: else { sl@0: ok = false; sl@0: } sl@0: sl@0: #else sl@0: sl@0: ok = false; sl@0: sl@0: #endif /* _STLP_USE_UNIX_IO */ sl@0: } sl@0: sl@0: _M_is_open = _M_should_close = false; sl@0: _M_openmode = 0; sl@0: return ok; sl@0: } sl@0: sl@0: sl@0: #define _STLP_LF 10 sl@0: #define _STLP_CR 13 sl@0: #define _STLP_CTRLZ 26 sl@0: sl@0: // Read up to n characters into a buffer. Return value is number of sl@0: // characters read. sl@0: _STLP_DECLSPEC ptrdiff_t _Filebuf_base::_M_read(char* buf, ptrdiff_t n) { sl@0: #if defined (_STLP_USE_UNIX_IO) sl@0: sl@0: return read(_M_file_id, buf, n); sl@0: sl@0: #elif defined (_STLP_USE_UNIX_EMULATION_IO) sl@0: sl@0: return _read(_M_file_id, buf, n); sl@0: sl@0: #elif defined (_STLP_USE_WIN32_IO) sl@0: ptrdiff_t readen = 0; sl@0: //Here cast to size_t is safe as n cannot be negative. sl@0: size_t chunkSize = (min)(size_t(0xffffffff), __STATIC_CAST(size_t, n)); sl@0: // The following, while validating that we are still able to extract chunkSize sl@0: // charaters to the buffer, avoids extraction of too small chunk of datas sl@0: // which would be counter performant. sl@0: while (__STATIC_CAST(size_t, (n - readen)) >= chunkSize) { sl@0: DWORD NumberOfBytesRead; sl@0: ReadFile(_M_file_id, buf + readen, __STATIC_CAST(DWORD, chunkSize), &NumberOfBytesRead, 0); sl@0: sl@0: if (NumberOfBytesRead == 0) sl@0: break; sl@0: sl@0: if (!(_M_openmode & ios_base::binary)) { sl@0: // translate CR-LFs to LFs in the buffer sl@0: char *to = buf + readen; sl@0: char *from = to; sl@0: char *last = from + NumberOfBytesRead - 1; sl@0: for (; from <= last && *from != _STLP_CTRLZ; ++from) { sl@0: if (*from != _STLP_CR) sl@0: *to++ = *from; sl@0: else { // found CR sl@0: if (from < last) { // not at buffer end sl@0: if (*(from + 1) != _STLP_LF) sl@0: *to++ = _STLP_CR; sl@0: } sl@0: else { // last char is CR, peek for LF sl@0: char peek = ' '; sl@0: DWORD NumberOfBytesPeeked; sl@0: ReadFile(_M_file_id, (LPVOID)&peek, 1, &NumberOfBytesPeeked, 0); sl@0: if (NumberOfBytesPeeked != 0) { sl@0: if (peek != _STLP_LF) { //not a combination sl@0: *to++ = _STLP_CR; sl@0: if ((to < buf + n) && (peek != _STLP_CR)) sl@0: //We have enough place to store peek and it is no a special sl@0: //_STLP_CR character, we can store it. sl@0: *to++ = peek; sl@0: else sl@0: SetFilePointer(_M_file_id, (LONG)-1, 0, SEEK_CUR); sl@0: } sl@0: else { sl@0: // A combination, we keep the : sl@0: *to++ = _STLP_LF; sl@0: } sl@0: } sl@0: else { sl@0: /* This case is tedious, we could sl@0: * - put peek back in the file but this would then generate an infinite loop sl@0: * - report an error as we don't know if in a future call to ReadFile we won't then sl@0: * get a . Doing so would make all files with a last an invalid file sl@0: * for STLport, a hard solution for STLport clients. sl@0: * - store the in the returned buffer, the chosen solution, even if in this sl@0: * case we could miss a combination. sl@0: */ sl@0: *to++ = _STLP_CR; sl@0: } sl@0: } sl@0: } // found CR sl@0: } // for sl@0: // seek back to TEXT end of file if hit CTRL-Z sl@0: if (from <= last) // terminated due to CTRLZ sl@0: SetFilePointer(_M_file_id, (LONG)((last+1) - from), 0, SEEK_CUR); sl@0: readen += to - (buf + readen); sl@0: } sl@0: else sl@0: readen += NumberOfBytesRead; sl@0: } sl@0: return readen; sl@0: sl@0: #elif defined (_STLP_USE_STDIO_IO) sl@0: sl@0: return fread(buf, 1, n, _M_file); sl@0: sl@0: #else sl@0: # error "Port!" sl@0: #endif /* __unix */ sl@0: } sl@0: sl@0: // Write n characters from a buffer. Return value: true if we managed sl@0: // to write the entire buffer, false if we didn't. sl@0: _STLP_DECLSPEC bool _Filebuf_base::_M_write(char* buf, ptrdiff_t n) { sl@0: for (;;) { sl@0: ptrdiff_t written; sl@0: sl@0: #if defined (_STLP_USE_UNIX_IO) sl@0: sl@0: written = write(_M_file_id, buf, n); sl@0: sl@0: #elif defined (_STLP_USE_UNIX_EMULATION_IO) sl@0: sl@0: written = _write(_M_file_id, buf, n); sl@0: sl@0: #elif defined (_STLP_USE_WIN32_IO) sl@0: sl@0: //In the following implementation we are going to cast most of the ptrdiff_t sl@0: //values in size_t to work with coherent unsigned values. Doing so make code sl@0: //more simple especially in the min function call. sl@0: sl@0: // In append mode, every write does an implicit seek to the end sl@0: // of the file. sl@0: if (_M_openmode & ios_base::app) sl@0: _M_seek(0, ios_base::end); sl@0: sl@0: if (_M_openmode & ios_base::binary) { sl@0: // binary mode sl@0: size_t bytes_to_write = (size_t)n; sl@0: DWORD NumberOfBytesWritten; sl@0: written = 0; sl@0: for (; bytes_to_write != 0;) { sl@0: WriteFile(_M_file_id, buf + written, sl@0: __STATIC_CAST(DWORD, (min)(size_t(0xffffffff), bytes_to_write)), sl@0: &NumberOfBytesWritten, 0); sl@0: if (NumberOfBytesWritten == 0) sl@0: return false; sl@0: bytes_to_write -= NumberOfBytesWritten; sl@0: written += NumberOfBytesWritten; sl@0: } sl@0: } sl@0: else { sl@0: char textbuf[_TEXTBUF_SIZE + 1]; // extra 1 in case LF at end sl@0: char * nextblock = buf, * ptrtextbuf = textbuf; sl@0: char * endtextbuf = textbuf + _TEXTBUF_SIZE; sl@0: char * endblock = buf + n; sl@0: ptrdiff_t nextblocksize = (min) (n, (ptrdiff_t)_TEXTBUF_SIZE); sl@0: char * nextlf; sl@0: sl@0: while ( (nextblocksize > 0) && sl@0: (nextlf = (char *)memchr(nextblock, _STLP_LF, nextblocksize)) != 0) { sl@0: ptrdiff_t linelength = nextlf - nextblock; sl@0: memcpy(ptrtextbuf, nextblock, linelength); sl@0: ptrtextbuf += linelength; sl@0: nextblock += (linelength + 1); sl@0: * ptrtextbuf ++ = _STLP_CR; sl@0: * ptrtextbuf ++ = _STLP_LF; sl@0: nextblocksize = (min) (ptrdiff_t(endblock - nextblock), sl@0: (max) (ptrdiff_t(0), ptrdiff_t(endtextbuf - ptrtextbuf))); sl@0: } sl@0: // write out what's left, > condition is here since for LF at the end , sl@0: // endtextbuf may get < ptrtextbuf ... sl@0: if (nextblocksize > 0) { sl@0: memcpy(ptrtextbuf, nextblock, nextblocksize); sl@0: ptrtextbuf += nextblocksize; sl@0: nextblock += nextblocksize; sl@0: } sl@0: // now write out the translated buffer sl@0: char * writetextbuf = textbuf; sl@0: for (size_t NumberOfBytesToWrite = (size_t)(ptrtextbuf - textbuf); sl@0: NumberOfBytesToWrite;) { sl@0: DWORD NumberOfBytesWritten; sl@0: WriteFile((HANDLE)_M_file_id, writetextbuf, sl@0: __STATIC_CAST(DWORD, (min)(size_t(0xffffffff), NumberOfBytesToWrite)), sl@0: &NumberOfBytesWritten, 0); sl@0: if (!NumberOfBytesWritten) // write shortfall sl@0: return false; sl@0: writetextbuf += NumberOfBytesWritten; sl@0: NumberOfBytesToWrite -= NumberOfBytesWritten; sl@0: } sl@0: // count non-translated characters sl@0: written = (nextblock - buf); sl@0: } sl@0: sl@0: #elif defined (_STLP_USE_STDIO_IO) sl@0: sl@0: written = fwrite(buf, 1, n, _M_file); sl@0: sl@0: #else sl@0: # error "Port!" sl@0: #endif /* __unix */ sl@0: sl@0: if (n == written) sl@0: return true; sl@0: else if (written > 0 && written < n) { sl@0: n -= written; sl@0: buf += written; sl@0: } sl@0: else sl@0: return false; sl@0: } sl@0: } sl@0: sl@0: sl@0: #ifdef _STLP_USE_WIN32_IO sl@0: # define STL_SEEK_SET FILE_BEGIN sl@0: # define STL_SEEK_CUR FILE_CURRENT sl@0: # define STL_SEEK_END FILE_END sl@0: #else sl@0: # define STL_SEEK_SET SEEK_SET sl@0: # define STL_SEEK_CUR SEEK_CUR sl@0: # define STL_SEEK_END SEEK_END sl@0: #endif sl@0: sl@0: // Wrapper for lseek or the like. sl@0: _STLP_DECLSPEC streamoff _Filebuf_base::_M_seek(streamoff offset, ios_base::seekdir dir) { sl@0: streamoff result = -1; sl@0: int whence; sl@0: sl@0: switch(dir) { sl@0: case ios_base::beg: sl@0: if (offset < 0 /* || offset > _M_file_size() */ ) sl@0: return streamoff(-1); sl@0: whence = STL_SEEK_SET; sl@0: break; sl@0: case ios_base::cur: sl@0: whence = STL_SEEK_CUR; sl@0: break; sl@0: case ios_base::end: sl@0: if (/* offset > 0 || */ -offset > _M_file_size() ) sl@0: return streamoff(-1); sl@0: whence = STL_SEEK_END; sl@0: break; sl@0: default: sl@0: return streamoff(-1); sl@0: } sl@0: sl@0: #if defined (_STLP_USE_UNIX_IO) || defined (_STLP_USE_UNIX_EMULATION_IO) sl@0: sl@0: result = LSEEK(_M_file_id, offset, whence); sl@0: sl@0: #elif defined (_STLP_USE_STDIO_IO) sl@0: sl@0: #if defined (__SYMBIAN32__) sl@0: /* This function is a wrapper for lseek and expects to return the offset in bytes from the beginning of the file. sl@0: It is used by tellg as well as tellp (for setting/getting the file pointer). sl@0: We don't want to use fseek for that. */ sl@0: if (FSEEK(_M_file, offset, whence) != 0) sl@0: return streamoff(-1); sl@0: result = FTELL(_M_file); sl@0: #else sl@0: result = FSEEK(_M_file, offset, whence); sl@0: #endif sl@0: sl@0: #elif defined (_STLP_USE_WIN32_IO) sl@0: sl@0: LARGE_INTEGER li; sl@0: li.QuadPart = offset; sl@0: li.LowPart = SetFilePointer(_M_file_id, li.LowPart, &li.HighPart, whence); sl@0: if (li.LowPart != INVALID_SET_FILE_POINTER || GetLastError() == NO_ERROR) sl@0: result = li.QuadPart; sl@0: sl@0: #else sl@0: # error "Port!" sl@0: #endif sl@0: sl@0: return result; sl@0: } sl@0: sl@0: sl@0: // Attempts to memory-map len bytes of the current file, starting sl@0: // at position offset. Precondition: offset is a multiple of the sl@0: // page size. Postcondition: return value is a null pointer if the sl@0: // memory mapping failed. Otherwise the return value is a pointer to sl@0: // the memory-mapped file and the file position is set to offset. sl@0: _STLP_DECLSPEC void* _Filebuf_base::_M_mmap(streamoff offset, streamoff len) { sl@0: void* base; sl@0: #if defined (_STLP_UNIX) && !defined(__DJGPP) && !defined(_CRAY) sl@0: base = MMAP(0, len, PROT_READ, MAP_PRIVATE, _M_file_id, offset); sl@0: if (base != (void*)MAP_FAILED) { sl@0: if (LSEEK(_M_file_id, offset + len, SEEK_SET) < 0) { sl@0: this->_M_unmap(base, len); sl@0: base = 0; sl@0: } sl@0: } else sl@0: base =0; sl@0: sl@0: #elif defined (_STLP_USE_WIN32_IO) sl@0: _M_view_id = CreateFileMapping(_M_file_id, (PSECURITY_ATTRIBUTES)0 , sl@0: PAGE_READONLY, 0 /* len >> 32 */ , sl@0: 0 /* len & 0xFFFFFFFF */ , // low-order DWORD of size sl@0: 0); sl@0: sl@0: if (_M_view_id) { sl@0: # if 0 sl@0: /* sl@0: printf("view %x created from file %x, error = %d, size = %d, map_offset = %d map_len = %d\n", sl@0: _M_view_id, _M_file_id, GetLastError(), sl@0: (int)cur_filesize, ULL(offset) & 0xffffffff, len); sl@0: */ sl@0: # endif sl@0: base = MapViewOfFile(_M_view_id, FILE_MAP_READ, __STATIC_CAST(DWORD, ULL(offset) >> 32), sl@0: __STATIC_CAST(DWORD, ULL(offset) & 0xffffffff), sl@0: # if !defined (__DMC__) sl@0: __STATIC_CAST(SIZE_T, len)); sl@0: # else sl@0: __STATIC_CAST(DWORD, len)); sl@0: # endif sl@0: // check if mapping succeded and is usable sl@0: if (base == 0 || _M_seek(offset + len, ios_base::beg) < 0) { sl@0: this->_M_unmap(base, len); sl@0: base = 0; sl@0: } sl@0: } else sl@0: base = 0; sl@0: #else sl@0: (void)len; //*TY 02/26/2000 - unused variables sl@0: (void)offset; //*TY 02/26/2000 - sl@0: base = 0; sl@0: #endif sl@0: return base; sl@0: } sl@0: sl@0: _STLP_DECLSPEC void _Filebuf_base::_M_unmap(void* base, streamoff len) { sl@0: // precondition : there is a valid mapping at the moment sl@0: #if defined (_STLP_UNIX) && !defined(__DJGPP) && !defined(_CRAY) sl@0: munmap((char*)base, len); sl@0: #elif defined (_STLP_USE_WIN32_IO) sl@0: if (base != NULL) sl@0: UnmapViewOfFile(base); sl@0: // destroy view handle as well sl@0: if (_M_view_id != NULL) sl@0: CloseHandle(_M_view_id); sl@0: _M_view_id = NULL; sl@0: (void)len; //unused variable sl@0: #else sl@0: (void)len; //*TY 02/26/2000 - unused variables sl@0: (void)base; //*TY 02/26/2000 - sl@0: #endif sl@0: } sl@0: sl@0: #if defined (__SYMBIAN32__) && defined(__EPOC32__) sl@0: _STLP_DECLSPEC size_t _Filebuf_base::__page_size() sl@0: { sl@0: return _Filebuf_base::_M_page_size; sl@0: } sl@0: #endif sl@0: // fbp : let us map 1 MB maximum, just be sure not to trash VM sl@0: #define MMAP_CHUNK 0x100000L sl@0: sl@0: _STLP_DECLSPEC int _STLP_CALL sl@0: _Underflow >::_M_doit (basic_filebuf >* __this) { sl@0: if (!__this->_M_in_input_mode) { sl@0: if (!__this->_M_switch_to_input_mode()) sl@0: return traits_type::eof(); sl@0: } sl@0: else if (__this->_M_in_putback_mode) { sl@0: __this->_M_exit_putback_mode(); sl@0: if (__this->gptr() != __this->egptr()) { sl@0: int_type __c = traits_type::to_int_type(*__this->gptr()); sl@0: return __c; sl@0: } sl@0: } sl@0: sl@0: // If it's a disk file, and if the internal and external character sl@0: // sequences are guaranteed to be identical, then try to use memory sl@0: // mapped I/O. Otherwise, revert to ordinary read. sl@0: if (__this->_M_base.__regular_file() sl@0: && __this->_M_always_noconv sl@0: && __this->_M_base._M_in_binary_mode()) { sl@0: // If we've mmapped part of the file already, then unmap it. sl@0: if (__this->_M_mmap_base) sl@0: __this->_M_base._M_unmap(__this->_M_mmap_base, __this->_M_mmap_len); sl@0: __this->_M_mmap_base = 0; sl@0: __this->_M_mmap_len = 0; sl@0: sl@0: // Determine the position where we start mapping. It has to be sl@0: // a multiple of the page size. sl@0: streamoff __cur = __this->_M_base._M_seek(0, ios_base::cur); sl@0: streamoff __size = __this->_M_base._M_file_size(); sl@0: if (__size > 0 && __cur >= 0 && __cur < __size) { sl@0: streamoff __offset = (__cur / __this->_M_base.__page_size()) * __this->_M_base.__page_size(); sl@0: streamoff __remainder = __cur - __offset; sl@0: sl@0: __this->_M_mmap_len = __size - __offset; sl@0: sl@0: if (__this->_M_mmap_len > MMAP_CHUNK) sl@0: __this->_M_mmap_len = MMAP_CHUNK; sl@0: sl@0: if ((__this->_M_mmap_base = sl@0: __this->_M_base._M_mmap(__offset, __this->_M_mmap_len)) != 0) { sl@0: __this->setg((char*) __this->_M_mmap_base, sl@0: (char*) __this->_M_mmap_base + __STATIC_CAST(ptrdiff_t, __remainder), sl@0: (char*) __this->_M_mmap_base + __STATIC_CAST(ptrdiff_t, __this->_M_mmap_len)); sl@0: return traits_type::to_int_type(*__this->gptr()); sl@0: } sl@0: } else /* size > 0 ... */ { sl@0: // There is nothing to map. We unmapped the file above, now zap pointers. sl@0: __this->_M_mmap_base = 0; sl@0: __this->_M_mmap_len = 0; sl@0: } sl@0: } sl@0: sl@0: return __this->_M_underflow_aux(); sl@0: } sl@0: sl@0: sl@0: //---------------------------------------------------------------------- sl@0: // Force instantiation of filebuf and fstream classes. sl@0: #if !defined(_STLP_NO_FORCE_INSTANTIATE) sl@0: sl@0: template class basic_filebuf >; sl@0: template class basic_ifstream >; sl@0: template class basic_ofstream >; sl@0: template class basic_fstream >; sl@0: sl@0: # if !defined (_STLP_NO_WCHAR_T) sl@0: template class _Underflow >; sl@0: template class basic_filebuf >; sl@0: template class basic_ifstream >; sl@0: template class basic_ofstream >; sl@0: template class basic_fstream >; sl@0: # endif /* _STLP_NO_WCHAR_T */ sl@0: sl@0: #endif sl@0: sl@0: _STLP_END_NAMESPACE sl@0: sl@0: // Local Variables: sl@0: // mode:C++ sl@0: // End: