stdio_filebuf.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // File descriptor layer for filebuf -*- C++ -*-
  2. // Copyright (C) 2002-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file ext/stdio_filebuf.h
  21. * This file is a GNU extension to the Standard C++ Library.
  22. */
  23. #ifndef _STDIO_FILEBUF_H
  24. #define _STDIO_FILEBUF_H 1
  25. #pragma GCC system_header
  26. #include <fstream>
  27. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  28. {
  29. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  30. /**
  31. * @brief Provides a layer of compatibility for C/POSIX.
  32. * @ingroup io
  33. *
  34. * This GNU extension provides extensions for working with standard C
  35. * FILE*'s and POSIX file descriptors. It must be instantiated by the
  36. * user with the type of character used in the file stream, e.g.,
  37. * stdio_filebuf<char>.
  38. */
  39. template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
  40. class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits>
  41. {
  42. public:
  43. // Types:
  44. typedef _CharT char_type;
  45. typedef _Traits traits_type;
  46. typedef typename traits_type::int_type int_type;
  47. typedef typename traits_type::pos_type pos_type;
  48. typedef typename traits_type::off_type off_type;
  49. typedef std::size_t size_t;
  50. public:
  51. /**
  52. * deferred initialization
  53. */
  54. stdio_filebuf() : std::basic_filebuf<_CharT, _Traits>() {}
  55. /**
  56. * @param __fd An open file descriptor.
  57. * @param __mode Same meaning as in a standard filebuf.
  58. * @param __size Optimal or preferred size of internal buffer,
  59. * in chars.
  60. *
  61. * This constructor associates a file stream buffer with an open
  62. * POSIX file descriptor. The file descriptor will be automatically
  63. * closed when the stdio_filebuf is closed/destroyed.
  64. */
  65. stdio_filebuf(int __fd, std::ios_base::openmode __mode,
  66. size_t __size = static_cast<size_t>(_GLIBCXX_BUFSIZ));
  67. /**
  68. * @param __f An open @c FILE*.
  69. * @param __mode Same meaning as in a standard filebuf.
  70. * @param __size Optimal or preferred size of internal buffer,
  71. * in chars. Defaults to system's @c BUFSIZ.
  72. *
  73. * This constructor associates a file stream buffer with an open
  74. * C @c FILE*. The @c FILE* will not be automatically closed when the
  75. * stdio_filebuf is closed/destroyed.
  76. */
  77. stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
  78. size_t __size = static_cast<size_t>(_GLIBCXX_BUFSIZ));
  79. /**
  80. * Closes the external data stream if the file descriptor constructor
  81. * was used.
  82. */
  83. virtual
  84. ~stdio_filebuf();
  85. #if __cplusplus >= 201103L
  86. stdio_filebuf(stdio_filebuf&&) = default;
  87. stdio_filebuf& operator=(stdio_filebuf&&) = default;
  88. void
  89. swap(stdio_filebuf& __fb)
  90. { std::basic_filebuf<_CharT, _Traits>::swap(__fb); }
  91. #endif
  92. /**
  93. * @return The underlying file descriptor.
  94. *
  95. * Once associated with an external data stream, this function can be
  96. * used to access the underlying POSIX file descriptor. Note that
  97. * there is no way for the library to track what you do with the
  98. * descriptor, so be careful.
  99. */
  100. int
  101. fd() { return this->_M_file.fd(); }
  102. /**
  103. * @return The underlying FILE*.
  104. *
  105. * This function can be used to access the underlying "C" file pointer.
  106. * Note that there is no way for the library to track what you do
  107. * with the file, so be careful.
  108. */
  109. std::__c_file*
  110. file() { return this->_M_file.file(); }
  111. };
  112. template<typename _CharT, typename _Traits>
  113. stdio_filebuf<_CharT, _Traits>::~stdio_filebuf()
  114. { }
  115. template<typename _CharT, typename _Traits>
  116. stdio_filebuf<_CharT, _Traits>::
  117. stdio_filebuf(int __fd, std::ios_base::openmode __mode, size_t __size)
  118. {
  119. this->_M_file.sys_open(__fd, __mode);
  120. if (this->is_open())
  121. {
  122. this->_M_mode = __mode;
  123. this->_M_buf_size = __size;
  124. this->_M_allocate_internal_buffer();
  125. this->_M_reading = false;
  126. this->_M_writing = false;
  127. this->_M_set_buffer(-1);
  128. }
  129. }
  130. template<typename _CharT, typename _Traits>
  131. stdio_filebuf<_CharT, _Traits>::
  132. stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
  133. size_t __size)
  134. {
  135. this->_M_file.sys_open(__f, __mode);
  136. if (this->is_open())
  137. {
  138. this->_M_mode = __mode;
  139. this->_M_buf_size = __size;
  140. this->_M_allocate_internal_buffer();
  141. this->_M_reading = false;
  142. this->_M_writing = false;
  143. this->_M_set_buffer(-1);
  144. }
  145. }
  146. _GLIBCXX_END_NAMESPACE_VERSION
  147. } // namespace
  148. #endif