michael@0: /* michael@0: * Copyright (c) 1999 michael@0: * Silicon Graphics Computer Systems, Inc. michael@0: * michael@0: * Copyright (c) 1999 michael@0: * Boris Fomitchev michael@0: * michael@0: * This material is provided "as is", with absolutely no warranty expressed michael@0: * or implied. Any use is at your own risk. michael@0: * michael@0: * Permission to use or copy this software for any purpose is hereby granted michael@0: * without fee, provided the above notices are retained on all copies. michael@0: * Permission to modify the code and to distribute modified code is granted, michael@0: * provided the above notices are retained, and a notice that the code was michael@0: * modified is included with the above copyright notice. michael@0: * michael@0: */ michael@0: michael@0: #include michael@0: michael@0: #if !defined (_STLP_WCE) michael@0: # ifdef __BORLANDC__ michael@0: # include // For _O_RDONLY, etc michael@0: # else michael@0: # include // For _get_osfhandle michael@0: # include // For _O_RDONLY, etc michael@0: # endif michael@0: # include // For _fstat michael@0: #endif michael@0: michael@0: #define _TEXTBUF_SIZE 0x1000 michael@0: michael@0: const _STLP_fd INVALID_STLP_FD = INVALID_HANDLE_VALUE; michael@0: michael@0: #if !defined (INVALID_SET_FILE_POINTER) michael@0: # define INVALID_SET_FILE_POINTER 0xffffffff michael@0: #endif michael@0: michael@0: #ifndef O_ACCMODE michael@0: # define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR) michael@0: #endif michael@0: michael@0: _STLP_BEGIN_NAMESPACE michael@0: michael@0: #if !defined(__MSL__) && !defined(_STLP_WCE) michael@0: static ios_base::openmode flag_to_openmode(int mode) { michael@0: ios_base::openmode ret = ios_base::__default_mode; michael@0: michael@0: switch (mode & O_ACCMODE) { michael@0: case O_RDONLY: michael@0: ret = ios_base::in; break; michael@0: case O_WRONLY: michael@0: ret = ios_base::out; break; michael@0: case O_RDWR: michael@0: ret = ios_base::in | ios_base::out; break; michael@0: } michael@0: michael@0: if (mode & O_APPEND) michael@0: ret |= ios_base::app; michael@0: michael@0: if (mode & O_BINARY) michael@0: ret |= ios_base::binary; michael@0: michael@0: return ret; michael@0: } michael@0: #endif michael@0: michael@0: _STLP_MOVE_TO_PRIV_NAMESPACE michael@0: michael@0: // Helper functions for _Filebuf_base. michael@0: michael@0: static bool __is_regular_file(_STLP_fd fd) { michael@0: BY_HANDLE_FILE_INFORMATION info; michael@0: michael@0: // Return true if the file handle isn't a directory. michael@0: return GetFileInformationByHandle(fd, &info) && michael@0: ((info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0); michael@0: } michael@0: michael@0: // Number of characters in the file. michael@0: static streamoff __file_size(_STLP_fd fd) { michael@0: streamoff ret = 0; michael@0: michael@0: LARGE_INTEGER li; michael@0: li.LowPart = GetFileSize(fd, (unsigned long*) &li.HighPart); michael@0: if (li.LowPart != INVALID_FILE_SIZE || GetLastError() == NO_ERROR) michael@0: ret = li.QuadPart; michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: _STLP_MOVE_TO_STD_NAMESPACE michael@0: michael@0: // Visual C++ and Intel use this, but not Metrowerks michael@0: // Also MinGW, msvcrt.dll (but not crtdll.dll) dependent version michael@0: #if (defined (_STLP_MSVC_LIB) && !defined (_STLP_WCE)) || \ michael@0: (defined (__MINGW32__) && defined (__MSVCRT__)) michael@0: michael@0: // fcntl(fileno, F_GETFL) for Microsoft library michael@0: // 'semi-documented' defines: michael@0: # define IOINFO_L2E 5 michael@0: # define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) michael@0: # define _pioinfo(i) ( __pioinfo[(i) >> IOINFO_L2E] + \ michael@0: ((i) & (IOINFO_ARRAY_ELTS - 1)) ) michael@0: # define FAPPEND 0x20 // O_APPEND flag michael@0: # define FTEXT 0x80 // O_TEXT flag michael@0: // end of 'semi-documented' defines michael@0: michael@0: // 'semi-documented' internal structure michael@0: extern "C" { michael@0: struct ioinfo { michael@0: long osfhnd; // the real os HANDLE michael@0: char osfile; // file handle flags michael@0: char pipech; // pipe buffer michael@0: # if defined (_MT) michael@0: // multi-threaded locking michael@0: int lockinitflag; michael@0: CRITICAL_SECTION lock; michael@0: # endif michael@0: }; michael@0: # if defined (__MINGW32__) michael@0: __MINGW_IMPORT ioinfo * __pioinfo[]; michael@0: # else michael@0: extern _CRTIMP ioinfo * __pioinfo[]; michael@0: # endif michael@0: } // extern "C" michael@0: // end of 'semi-documented' declarations michael@0: michael@0: static ios_base::openmode _get_osfflags(int fd, HANDLE oshandle) { michael@0: char dosflags = 0; michael@0: if (fd >= 0) michael@0: dosflags = _pioinfo(fd)->osfile; michael@0: //else michael@0: //the file will be considered as open in binary mode with no append attribute michael@0: // end of 'semi-documented' stuff michael@0: michael@0: int mode = 0; michael@0: if (dosflags & FAPPEND) michael@0: mode |= O_APPEND; michael@0: michael@0: if (dosflags & FTEXT) michael@0: mode |= O_TEXT; michael@0: else michael@0: mode |= O_BINARY; michael@0: michael@0: // For Read/Write access we have to guess michael@0: DWORD dummy, dummy2; michael@0: BOOL writeOk = WriteFile(oshandle, &dummy2, 0, &dummy, 0); michael@0: BOOL readOk = ReadFile(oshandle, &dummy2, 0, &dummy, NULL); michael@0: if (writeOk && readOk) michael@0: mode |= O_RDWR; michael@0: else if (readOk) michael@0: mode |= O_RDONLY; michael@0: else michael@0: mode |= O_WRONLY; michael@0: michael@0: return flag_to_openmode(mode); michael@0: } michael@0: michael@0: #elif defined (__DMC__) michael@0: michael@0: # define FHND_APPEND 0x04 michael@0: # define FHND_DEVICE 0x08 michael@0: # define FHND_TEXT 0x10 michael@0: michael@0: extern "C" unsigned char __fhnd_info[_NFILE]; michael@0: michael@0: static ios_base::openmode _get_osfflags(int fd, HANDLE oshandle) { michael@0: int mode = 0; michael@0: michael@0: if (__fhnd_info[fd] & FHND_APPEND) michael@0: mode |= O_APPEND; michael@0: michael@0: if (__fhnd_info[fd] & FHND_TEXT == 0) michael@0: mode |= O_BINARY; michael@0: michael@0: for (FILE *fp = &_iob[0]; fp < &_iob[_NFILE]; fp++) { michael@0: if ((fileno(fp) == fd) && (fp->_flag & (_IOREAD | _IOWRT | _IORW))) { michael@0: const int osflags = fp->_flag; michael@0: michael@0: if ((osflags & _IOREAD) && !(osflags & _IOWRT) && !(osflags & _IORW)) michael@0: mode |= O_RDONLY; michael@0: else if ((osflags & _IOWRT) && !(osflags & _IOREAD) && !(osflags & _IORW)) michael@0: mode |= O_WRONLY; michael@0: else michael@0: mode |= O_RDWR; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: return flag_to_openmode(mode); michael@0: } michael@0: #endif michael@0: michael@0: size_t _Filebuf_base::_M_page_size = 4096; michael@0: michael@0: _Filebuf_base::_Filebuf_base() michael@0: : _M_file_id(INVALID_STLP_FD), michael@0: _M_openmode(0), michael@0: _M_is_open(false), michael@0: _M_should_close(false), michael@0: _M_view_id(0) michael@0: {} michael@0: michael@0: void _Filebuf_base::_S_initialize() { michael@0: SYSTEM_INFO SystemInfo; michael@0: GetSystemInfo(&SystemInfo); michael@0: _M_page_size = SystemInfo.dwPageSize; michael@0: // might be .dwAllocationGranularity michael@0: } michael@0: michael@0: // Return the size of the file. This is a wrapper for stat. michael@0: // Returns zero if the size cannot be determined or is ill-defined. michael@0: streamoff _Filebuf_base::_M_file_size() { michael@0: return _STLP_PRIV __file_size(_M_file_id); michael@0: } michael@0: michael@0: bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode, michael@0: long permission) { michael@0: _STLP_fd file_no; michael@0: michael@0: if (_M_is_open) michael@0: return false; michael@0: michael@0: DWORD dwDesiredAccess, dwCreationDisposition; michael@0: bool doTruncate = false; michael@0: michael@0: switch (openmode & (~ios_base::ate & ~ios_base::binary)) { michael@0: case ios_base::out: michael@0: case ios_base::out | ios_base::trunc: michael@0: dwDesiredAccess = GENERIC_WRITE; michael@0: dwCreationDisposition = OPEN_ALWAYS; michael@0: // boris : even though it is very non-intuitive, standard michael@0: // requires them both to behave same. michael@0: doTruncate = true; michael@0: break; michael@0: case ios_base::out | ios_base::app: michael@0: dwDesiredAccess = GENERIC_WRITE; michael@0: dwCreationDisposition = OPEN_ALWAYS; michael@0: break; michael@0: case ios_base::in: michael@0: dwDesiredAccess = GENERIC_READ; michael@0: dwCreationDisposition = OPEN_EXISTING; michael@0: permission = 0; // Irrelevant unless we're writing. michael@0: break; michael@0: case ios_base::in | ios_base::out: michael@0: dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; michael@0: dwCreationDisposition = OPEN_EXISTING; michael@0: break; michael@0: case ios_base::in | ios_base::out | ios_base::trunc: michael@0: dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; michael@0: dwCreationDisposition = OPEN_ALWAYS; michael@0: doTruncate = true; michael@0: break; michael@0: default: // The above are the only combinations of michael@0: return false; // flags allowed by the C++ standard. michael@0: } michael@0: michael@0: DWORD dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; michael@0: michael@0: #if defined(_STLP_USE_WIDE_INTERFACE) michael@0: file_no = CreateFile (_STLP_PRIV __ASCIIToWide(name).c_str(), michael@0: #else michael@0: file_no = CreateFileA(name, michael@0: #endif michael@0: dwDesiredAccess, dwShareMode, 0, michael@0: dwCreationDisposition, permission, 0); michael@0: michael@0: if (file_no == INVALID_STLP_FD) michael@0: return false; michael@0: michael@0: if ( michael@0: #if !defined (_STLP_WCE) michael@0: GetFileType(file_no) == FILE_TYPE_DISK && michael@0: #endif michael@0: ((doTruncate && SetEndOfFile(file_no) == 0) || michael@0: (((openmode & ios_base::ate) != 0) && michael@0: (SetFilePointer(file_no, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER)))) { michael@0: CloseHandle(file_no); michael@0: return false; michael@0: } michael@0: michael@0: _M_is_open = true; michael@0: _M_file_id = file_no; michael@0: _M_should_close = _M_is_open; michael@0: _M_openmode = openmode; michael@0: michael@0: if (_M_is_open) michael@0: _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id); michael@0: michael@0: return (_M_is_open != 0); michael@0: } michael@0: michael@0: bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode) { michael@0: // This doesn't really grant everyone in the world read/write michael@0: // access. On Unix, file-creation system calls always clear michael@0: // bits that are set in the umask from the permissions flag. michael@0: return this->_M_open(name, openmode, FILE_ATTRIBUTE_NORMAL); michael@0: } michael@0: michael@0: bool _Filebuf_base::_M_open(_STLP_fd __id, ios_base::openmode init_mode) { michael@0: #if (defined (_STLP_MSVC_LIB) && !defined (_STLP_WCE)) || \ michael@0: (defined (__MINGW32__) && defined (__MSVCRT__)) || defined (__DMC__) michael@0: michael@0: if (_M_is_open || __id == INVALID_STLP_FD) michael@0: return false; michael@0: michael@0: if (init_mode != ios_base::__default_mode) michael@0: _M_openmode = init_mode; michael@0: else michael@0: _M_openmode = _get_osfflags(-1, __id); michael@0: michael@0: _M_is_open = true; michael@0: _M_file_id = __id; michael@0: _M_should_close = false; michael@0: _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id); michael@0: michael@0: return true; michael@0: #else michael@0: (void)__id; michael@0: (void)init_mode; // dwa 4/27/00 - suppress unused parameter warning michael@0: michael@0: // not available for the API michael@0: return false; michael@0: michael@0: #endif michael@0: } michael@0: michael@0: // Associated the filebuf with a file descriptor pointing to an already- michael@0: // open file. Mode is set to be consistent with the way that the file michael@0: // was opened. michael@0: bool _Filebuf_base::_M_open(int file_no, ios_base::openmode init_mode) { michael@0: if (_M_is_open || file_no < 0) michael@0: return false; michael@0: michael@0: #if (defined (_STLP_MSVC_LIB) && !defined (_STLP_WCE)) || \ michael@0: (defined (__MINGW32__) && defined (__MSVCRT__)) || defined (__DMC__) michael@0: michael@0: HANDLE oshandle = (HANDLE)_get_osfhandle(file_no); michael@0: if (oshandle == INVALID_STLP_FD) michael@0: return false; michael@0: michael@0: if (init_mode != ios_base::__default_mode) michael@0: _M_openmode = init_mode; michael@0: else michael@0: _M_openmode = _get_osfflags(file_no, oshandle); michael@0: michael@0: _M_file_id = oshandle; michael@0: _M_is_open = true; michael@0: _M_should_close = false; michael@0: _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id); michael@0: return true; michael@0: #else michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(&init_mode) michael@0: // not available for the API michael@0: return false; michael@0: #endif michael@0: } michael@0: michael@0: bool _Filebuf_base::_M_close() { michael@0: if (!_M_is_open) michael@0: return false; michael@0: michael@0: bool ok; michael@0: michael@0: if (!_M_should_close) michael@0: ok = true; michael@0: else { michael@0: if (_M_file_id != INVALID_STLP_FD) { michael@0: ok = (CloseHandle(_M_file_id) != 0); michael@0: } michael@0: else { michael@0: ok = false; michael@0: } michael@0: } michael@0: michael@0: _M_is_open = _M_should_close = false; michael@0: _M_openmode = 0; michael@0: return ok; michael@0: } michael@0: michael@0: michael@0: #define _STLP_LF 10 michael@0: #define _STLP_CR 13 michael@0: #define _STLP_CTRLZ 26 michael@0: michael@0: // Read up to n characters into a buffer. Return value is number of michael@0: // characters read. michael@0: ptrdiff_t _Filebuf_base::_M_read(char* buf, ptrdiff_t n) { michael@0: ptrdiff_t readen = 0; michael@0: //Here cast to size_t is safe as n cannot be negative. michael@0: size_t chunkSize = (min)(size_t(0xffffffff), __STATIC_CAST(size_t, n)); michael@0: // The following, while validating that we are still able to extract chunkSize michael@0: // charaters to the buffer, avoids extraction of too small chunk of datas michael@0: // which would be counter performant. michael@0: while (__STATIC_CAST(size_t, (n - readen)) >= chunkSize) { michael@0: DWORD numberOfBytesRead; michael@0: ReadFile(_M_file_id, buf + readen, __STATIC_CAST(DWORD, chunkSize), &numberOfBytesRead, 0); michael@0: michael@0: if (numberOfBytesRead == 0) michael@0: break; michael@0: michael@0: if (!(_M_openmode & ios_base::binary)) { michael@0: // translate CR-LFs to LFs in the buffer michael@0: char *to = buf + readen; michael@0: char *from = to; michael@0: char *last = from + numberOfBytesRead - 1; michael@0: for (; from <= last && *from != _STLP_CTRLZ; ++from) { michael@0: if (*from != _STLP_CR) michael@0: *to++ = *from; michael@0: else { // found CR michael@0: if (from < last) { // not at buffer end michael@0: if (*(from + 1) != _STLP_LF) michael@0: *to++ = _STLP_CR; michael@0: } michael@0: else { // last char is CR, peek for LF michael@0: char peek = ' '; michael@0: DWORD NumberOfBytesPeeked; michael@0: ReadFile(_M_file_id, (LPVOID)&peek, 1, &NumberOfBytesPeeked, 0); michael@0: if (NumberOfBytesPeeked != 0) { michael@0: if (peek != _STLP_LF) { //not a combination michael@0: *to++ = _STLP_CR; michael@0: if ((to < buf + n) && (peek != _STLP_CR)) michael@0: //We have enough place to store peek and it is no a special michael@0: //_STLP_CR character, we can store it. michael@0: *to++ = peek; michael@0: else michael@0: SetFilePointer(_M_file_id, (LONG)-1, 0, FILE_CURRENT); michael@0: } michael@0: else { michael@0: // A combination, we keep the : michael@0: *to++ = _STLP_LF; michael@0: } michael@0: } michael@0: else { michael@0: /* This case is tedious, we could michael@0: * - put peek back in the file but this would then generate an infinite loop michael@0: * - report an error as we don't know if in a future call to ReadFile we won't then michael@0: * get a . Doing so would make all files with a last an invalid file michael@0: * for STLport, a hard solution for STLport clients. michael@0: * - store the in the returned buffer, the chosen solution, even if in this michael@0: * case we could miss a combination. michael@0: */ michael@0: *to++ = _STLP_CR; michael@0: } michael@0: } michael@0: } // found CR michael@0: } // for michael@0: readen = to - buf; michael@0: // seek back to TEXT end of file if hit CTRL-Z michael@0: if (from <= last) { // terminated due to CTRLZ michael@0: SetFilePointer(_M_file_id, -(LONG)((last + 1) - from), 0, FILE_CURRENT); michael@0: break; michael@0: } michael@0: } michael@0: else michael@0: readen += numberOfBytesRead; michael@0: } michael@0: return readen; michael@0: } michael@0: michael@0: // Write n characters from a buffer. Return value: true if we managed michael@0: // to write the entire buffer, false if we didn't. michael@0: bool _Filebuf_base::_M_write(char* buf, ptrdiff_t n) { michael@0: for (;;) { michael@0: ptrdiff_t written; michael@0: michael@0: //In the following implementation we are going to cast most of the ptrdiff_t michael@0: //values in size_t to work with coherent unsigned values. Doing so make code michael@0: //more simple especially in the min function call. michael@0: michael@0: // In append mode, every write does an implicit seek to the end michael@0: // of the file. michael@0: if (_M_openmode & ios_base::app) michael@0: _M_seek(0, ios_base::end); michael@0: michael@0: if (_M_openmode & ios_base::binary) { michael@0: // binary mode michael@0: size_t bytes_to_write = (size_t)n; michael@0: DWORD NumberOfBytesWritten; michael@0: written = 0; michael@0: for (; bytes_to_write != 0;) { michael@0: WriteFile(_M_file_id, buf + written, michael@0: __STATIC_CAST(DWORD, (min)(size_t(0xffffffff), bytes_to_write)), michael@0: &NumberOfBytesWritten, 0); michael@0: if (NumberOfBytesWritten == 0) michael@0: return false; michael@0: bytes_to_write -= NumberOfBytesWritten; michael@0: written += NumberOfBytesWritten; michael@0: } michael@0: } michael@0: else { michael@0: char textbuf[_TEXTBUF_SIZE + 1]; // extra 1 in case LF at end michael@0: char * nextblock = buf, * ptrtextbuf = textbuf; michael@0: char * endtextbuf = textbuf + _TEXTBUF_SIZE; michael@0: char * endblock = buf + n; michael@0: ptrdiff_t nextblocksize = (min) (n, (ptrdiff_t)_TEXTBUF_SIZE); michael@0: char * nextlf; michael@0: michael@0: while ( (nextblocksize > 0) && michael@0: (nextlf = (char *)memchr(nextblock, _STLP_LF, nextblocksize)) != 0) { michael@0: ptrdiff_t linelength = nextlf - nextblock; michael@0: memcpy(ptrtextbuf, nextblock, linelength); michael@0: ptrtextbuf += linelength; michael@0: nextblock += (linelength + 1); michael@0: * ptrtextbuf ++ = _STLP_CR; michael@0: * ptrtextbuf ++ = _STLP_LF; michael@0: nextblocksize = (min) (ptrdiff_t(endblock - nextblock), michael@0: (max) (ptrdiff_t(0), ptrdiff_t(endtextbuf - ptrtextbuf))); michael@0: } michael@0: // write out what's left, > condition is here since for LF at the end , michael@0: // endtextbuf may get < ptrtextbuf ... michael@0: if (nextblocksize > 0) { michael@0: memcpy(ptrtextbuf, nextblock, nextblocksize); michael@0: ptrtextbuf += nextblocksize; michael@0: nextblock += nextblocksize; michael@0: } michael@0: // now write out the translated buffer michael@0: char * writetextbuf = textbuf; michael@0: for (size_t NumberOfBytesToWrite = (size_t)(ptrtextbuf - textbuf); michael@0: NumberOfBytesToWrite;) { michael@0: DWORD NumberOfBytesWritten; michael@0: WriteFile((HANDLE)_M_file_id, writetextbuf, michael@0: __STATIC_CAST(DWORD, (min)(size_t(0xffffffff), NumberOfBytesToWrite)), michael@0: &NumberOfBytesWritten, 0); michael@0: if (!NumberOfBytesWritten) // write shortfall michael@0: return false; michael@0: writetextbuf += NumberOfBytesWritten; michael@0: NumberOfBytesToWrite -= NumberOfBytesWritten; michael@0: } michael@0: // count non-translated characters michael@0: written = (nextblock - buf); michael@0: } michael@0: michael@0: if (n == written) michael@0: return true; michael@0: else if (written > 0 && written < n) { michael@0: n -= written; michael@0: buf += written; michael@0: } michael@0: else michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: // Wrapper for lseek or the like. michael@0: streamoff _Filebuf_base::_M_seek(streamoff offset, ios_base::seekdir dir) { michael@0: streamoff result = -1; michael@0: int whence; michael@0: michael@0: switch(dir) { michael@0: case ios_base::beg: michael@0: if (offset < 0 /* || offset > _M_file_size() */ ) michael@0: return streamoff(-1); michael@0: whence = FILE_BEGIN; michael@0: break; michael@0: case ios_base::cur: michael@0: whence = FILE_CURRENT; michael@0: break; michael@0: case ios_base::end: michael@0: if (/* offset > 0 || */ -offset > _M_file_size() ) michael@0: return streamoff(-1); michael@0: whence = FILE_END; michael@0: break; michael@0: default: michael@0: return streamoff(-1); michael@0: } michael@0: michael@0: LARGE_INTEGER li; michael@0: li.QuadPart = offset; michael@0: li.LowPart = SetFilePointer(_M_file_id, li.LowPart, &li.HighPart, whence); michael@0: if (li.LowPart != INVALID_SET_FILE_POINTER || GetLastError() == NO_ERROR) michael@0: result = li.QuadPart; michael@0: michael@0: return result; michael@0: } michael@0: michael@0: michael@0: // Attempts to memory-map len bytes of the current file, starting michael@0: // at position offset. Precondition: offset is a multiple of the michael@0: // page size. Postcondition: return value is a null pointer if the michael@0: // memory mapping failed. Otherwise the return value is a pointer to michael@0: // the memory-mapped file and the file position is set to offset. michael@0: void* _Filebuf_base::_M_mmap(streamoff offset, streamoff len) { michael@0: void* base; michael@0: _M_view_id = CreateFileMapping(_M_file_id, (PSECURITY_ATTRIBUTES)0 , michael@0: PAGE_READONLY, 0 /* len >> 32 */ , michael@0: 0 /* len & 0xFFFFFFFF */ , // low-order DWORD of size michael@0: 0); michael@0: michael@0: if (_M_view_id) { michael@0: #if 0 michael@0: /* michael@0: printf("view %x created from file %x, error = %d, size = %d, map_offset = %d map_len = %d\n", michael@0: _M_view_id, _M_file_id, GetLastError(), michael@0: (int)cur_filesize, ULL(offset) & 0xffffffff, len); michael@0: */ michael@0: #endif michael@0: LARGE_INTEGER li; michael@0: li.QuadPart = offset; michael@0: base = MapViewOfFile(_M_view_id, FILE_MAP_READ, li.HighPart, li.LowPart, michael@0: #if !defined (__DMC__) michael@0: __STATIC_CAST(SIZE_T, len)); michael@0: #else michael@0: __STATIC_CAST(DWORD, len)); michael@0: #endif michael@0: // check if mapping succeded and is usable michael@0: if (base == 0 || _M_seek(offset + len, ios_base::beg) < 0) { michael@0: this->_M_unmap(base, len); michael@0: base = 0; michael@0: } michael@0: } else michael@0: base = 0; michael@0: michael@0: return base; michael@0: } michael@0: michael@0: void _Filebuf_base::_M_unmap(void* base, streamoff len) { michael@0: // precondition : there is a valid mapping at the moment michael@0: if (base != NULL) michael@0: UnmapViewOfFile(base); michael@0: // destroy view handle as well michael@0: if (_M_view_id != NULL) michael@0: CloseHandle(_M_view_id); michael@0: _M_view_id = NULL; michael@0: (void)len; //unused variable michael@0: } michael@0: michael@0: _STLP_END_NAMESPACE