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 "stlport_prefix.h" michael@0: michael@0: #ifdef _STLP_USE_UNIX_IO michael@0: # include "details/fstream_unistd.cpp" michael@0: #elif defined(_STLP_USE_STDIO_IO) michael@0: # include "details/fstream_stdio.cpp" michael@0: #elif defined(_STLP_USE_WIN32_IO) michael@0: # include "details/fstream_win32io.cpp" michael@0: #else michael@0: # error "Can't recognize IO scheme to use" michael@0: #endif michael@0: michael@0: _STLP_BEGIN_NAMESPACE michael@0: michael@0: // fbp : let us map 1 MB maximum, just be sure not to trash VM michael@0: #define MMAP_CHUNK 0x100000L michael@0: michael@0: _Underflow< char, char_traits >::int_type _STLP_CALL michael@0: _Underflow< char, char_traits >::_M_doit(basic_filebuf >* __this) michael@0: { michael@0: typedef char_traits traits_type; michael@0: typedef traits_type::int_type int_type; michael@0: michael@0: if (!__this->_M_in_input_mode) { michael@0: if (!__this->_M_switch_to_input_mode()) michael@0: return traits_type::eof(); michael@0: } michael@0: else if (__this->_M_in_putback_mode) { michael@0: __this->_M_exit_putback_mode(); michael@0: if (__this->gptr() != __this->egptr()) { michael@0: int_type __c = traits_type::to_int_type(*__this->gptr()); michael@0: return __c; michael@0: } michael@0: } michael@0: michael@0: // If it's a disk file, and if the internal and external character michael@0: // sequences are guaranteed to be identical, then try to use memory michael@0: // mapped I/O. Otherwise, revert to ordinary read. michael@0: if (__this->_M_base.__regular_file() michael@0: && __this->_M_always_noconv michael@0: && __this->_M_base._M_in_binary_mode()) { michael@0: // If we've mmapped part of the file already, then unmap it. michael@0: if (__this->_M_mmap_base) michael@0: __this->_M_base._M_unmap(__this->_M_mmap_base, __this->_M_mmap_len); michael@0: michael@0: // Determine the position where we start mapping. It has to be michael@0: // a multiple of the page size. michael@0: streamoff __cur = __this->_M_base._M_seek(0, ios_base::cur); michael@0: streamoff __size = __this->_M_base._M_file_size(); michael@0: if (__size > 0 && __cur >= 0 && __cur < __size) { michael@0: streamoff __offset = (__cur / __this->_M_base.__page_size()) * __this->_M_base.__page_size(); michael@0: streamoff __remainder = __cur - __offset; michael@0: michael@0: __this->_M_mmap_len = __size - __offset; michael@0: michael@0: if (__this->_M_mmap_len > MMAP_CHUNK) michael@0: __this->_M_mmap_len = MMAP_CHUNK; michael@0: michael@0: if ((__this->_M_mmap_base = __this->_M_base._M_mmap(__offset, __this->_M_mmap_len)) != 0) { michael@0: __this->setg(__STATIC_CAST(char*, __this->_M_mmap_base), michael@0: __STATIC_CAST(char*, __this->_M_mmap_base) + __STATIC_CAST(ptrdiff_t, __remainder), michael@0: __STATIC_CAST(char*, __this->_M_mmap_base) + __STATIC_CAST(ptrdiff_t, __this->_M_mmap_len)); michael@0: return traits_type::to_int_type(*__this->gptr()); michael@0: } michael@0: else michael@0: __this->_M_mmap_len = 0; michael@0: } michael@0: else { michael@0: __this->_M_mmap_base = 0; michael@0: __this->_M_mmap_len = 0; michael@0: } michael@0: } michael@0: michael@0: return __this->_M_underflow_aux(); michael@0: } michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Force instantiation of filebuf and fstream classes. michael@0: #if !defined(_STLP_NO_FORCE_INSTANTIATE) michael@0: michael@0: template class basic_filebuf >; michael@0: template class basic_ifstream >; michael@0: template class basic_ofstream >; michael@0: template class basic_fstream >; michael@0: michael@0: # if !defined (_STLP_NO_WCHAR_T) michael@0: template class _Underflow >; michael@0: template class basic_filebuf >; michael@0: template class basic_ifstream >; michael@0: template class basic_ofstream >; michael@0: template class basic_fstream >; michael@0: # endif /* _STLP_NO_WCHAR_T */ michael@0: michael@0: #endif michael@0: michael@0: _STLP_END_NAMESPACE