fstream.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 1999
  3. * Silicon Graphics Computer Systems, Inc.
  4. *
  5. * Copyright (c) 1999
  6. * Boris Fomitchev
  7. *
  8. * This material is provided "as is", with absolutely no warranty expressed
  9. * or implied. Any use is at your own risk.
  10. *
  11. * Permission to use or copy this software for any purpose is hereby granted
  12. * without fee, provided the above notices are retained on all copies.
  13. * Permission to modify the code and to distribute modified code is granted,
  14. * provided the above notices are retained, and a notice that the code was
  15. * modified is included with the above copyright notice.
  16. *
  17. */
  18. #include "stlport_prefix.h"
  19. #ifdef _STLP_USE_UNIX_IO
  20. # include "details/fstream_unistd.cpp"
  21. #elif defined(_STLP_USE_STDIO_IO)
  22. # include "details/fstream_stdio.cpp"
  23. #elif defined(_STLP_USE_WIN32_IO)
  24. # include "details/fstream_win32io.cpp"
  25. #else
  26. # error "Can't recognize IO scheme to use"
  27. #endif
  28. _STLP_BEGIN_NAMESPACE
  29. // fbp : let us map 1 MB maximum, just be sure not to trash VM
  30. #define MMAP_CHUNK 0x100000L
  31. _Underflow< char, char_traits<char> >::int_type _STLP_CALL
  32. _Underflow< char, char_traits<char> >::_M_doit(basic_filebuf<char, char_traits<char> >* __this)
  33. {
  34. typedef char_traits<char> traits_type;
  35. typedef traits_type::int_type int_type;
  36. if (!__this->_M_in_input_mode) {
  37. if (!__this->_M_switch_to_input_mode())
  38. return traits_type::eof();
  39. }
  40. else if (__this->_M_in_putback_mode) {
  41. __this->_M_exit_putback_mode();
  42. if (__this->gptr() != __this->egptr()) {
  43. int_type __c = traits_type::to_int_type(*__this->gptr());
  44. return __c;
  45. }
  46. }
  47. // If it's a disk file, and if the internal and external character
  48. // sequences are guaranteed to be identical, then try to use memory
  49. // mapped I/O. Otherwise, revert to ordinary read.
  50. if (__this->_M_base.__regular_file()
  51. && __this->_M_always_noconv
  52. && __this->_M_base._M_in_binary_mode()) {
  53. // If we've mmapped part of the file already, then unmap it.
  54. if (__this->_M_mmap_base)
  55. __this->_M_base._M_unmap(__this->_M_mmap_base, __this->_M_mmap_len);
  56. // Determine the position where we start mapping. It has to be
  57. // a multiple of the page size.
  58. streamoff __cur = __this->_M_base._M_seek(0, ios_base::cur);
  59. streamoff __size = __this->_M_base._M_file_size();
  60. if (__size > 0 && __cur >= 0 && __cur < __size) {
  61. streamoff __offset = (__cur / __this->_M_base.__page_size()) * __this->_M_base.__page_size();
  62. streamoff __remainder = __cur - __offset;
  63. __this->_M_mmap_len = __size - __offset;
  64. if (__this->_M_mmap_len > MMAP_CHUNK)
  65. __this->_M_mmap_len = MMAP_CHUNK;
  66. if ((__this->_M_mmap_base = __this->_M_base._M_mmap(__offset, __this->_M_mmap_len)) != 0) {
  67. __this->setg(__STATIC_CAST(char*, __this->_M_mmap_base),
  68. __STATIC_CAST(char*, __this->_M_mmap_base) + __STATIC_CAST(ptrdiff_t, __remainder),
  69. __STATIC_CAST(char*, __this->_M_mmap_base) + __STATIC_CAST(ptrdiff_t, __this->_M_mmap_len));
  70. return traits_type::to_int_type(*__this->gptr());
  71. }
  72. else
  73. __this->_M_mmap_len = 0;
  74. }
  75. else {
  76. __this->_M_mmap_base = 0;
  77. __this->_M_mmap_len = 0;
  78. }
  79. }
  80. return __this->_M_underflow_aux();
  81. }
  82. //----------------------------------------------------------------------
  83. // Force instantiation of filebuf and fstream classes.
  84. #if !defined(_STLP_NO_FORCE_INSTANTIATE)
  85. template class basic_filebuf<char, char_traits<char> >;
  86. template class basic_ifstream<char, char_traits<char> >;
  87. template class basic_ofstream<char, char_traits<char> >;
  88. template class basic_fstream<char, char_traits<char> >;
  89. # if !defined (_STLP_NO_WCHAR_T)
  90. template class _Underflow<wchar_t, char_traits<wchar_t> >;
  91. template class basic_filebuf<wchar_t, char_traits<wchar_t> >;
  92. template class basic_ifstream<wchar_t, char_traits<wchar_t> >;
  93. template class basic_ofstream<wchar_t, char_traits<wchar_t> >;
  94. template class basic_fstream<wchar_t, char_traits<wchar_t> >;
  95. # endif /* _STLP_NO_WCHAR_T */
  96. #endif
  97. _STLP_END_NAMESPACE