file_stdio.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // Copyright (c) 2015-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_CORE_FILE_STDIO_HPP
  10. #define BOOST_BEAST_CORE_FILE_STDIO_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/error.hpp>
  13. #include <boost/beast/core/file_base.hpp>
  14. #include <cstdio>
  15. #include <cstdint>
  16. namespace boost {
  17. namespace beast {
  18. /** An implementation of File which uses cstdio.
  19. This class implements a file using the interfaces present
  20. in the C++ Standard Library, in `<stdio>`.
  21. */
  22. class file_stdio
  23. {
  24. FILE* f_ = nullptr;
  25. public:
  26. /** The type of the underlying file handle.
  27. This is platform-specific.
  28. */
  29. using native_handle_type = FILE*;
  30. /** Destructor
  31. If the file is open it is first closed.
  32. */
  33. BOOST_BEAST_DECL
  34. ~file_stdio();
  35. /** Constructor
  36. There is no open file initially.
  37. */
  38. file_stdio() = default;
  39. /** Constructor
  40. The moved-from object behaves as if default constructed.
  41. */
  42. BOOST_BEAST_DECL
  43. file_stdio(file_stdio&& other);
  44. /** Assignment
  45. The moved-from object behaves as if default constructed.
  46. */
  47. BOOST_BEAST_DECL
  48. file_stdio& operator=(file_stdio&& other);
  49. /// Returns the native handle associated with the file.
  50. FILE*
  51. native_handle() const
  52. {
  53. return f_;
  54. }
  55. /** Set the native handle associated with the file.
  56. If the file is open it is first closed.
  57. @param f The native file handle to assign.
  58. */
  59. BOOST_BEAST_DECL
  60. void
  61. native_handle(FILE* f);
  62. /// Returns `true` if the file is open
  63. bool
  64. is_open() const
  65. {
  66. return f_ != nullptr;
  67. }
  68. /** Close the file if open
  69. @param ec Set to the error, if any occurred.
  70. */
  71. BOOST_BEAST_DECL
  72. void
  73. close(error_code& ec);
  74. /** Open a file at the given path with the specified mode
  75. @param path The utf-8 encoded path to the file
  76. @param mode The file mode to use
  77. @param ec Set to the error, if any occurred
  78. */
  79. BOOST_BEAST_DECL
  80. void
  81. open(char const* path, file_mode mode, error_code& ec);
  82. /** Return the size of the open file
  83. @param ec Set to the error, if any occurred
  84. @return The size in bytes
  85. */
  86. BOOST_BEAST_DECL
  87. std::uint64_t
  88. size(error_code& ec) const;
  89. /** Return the current position in the open file
  90. @param ec Set to the error, if any occurred
  91. @return The offset in bytes from the beginning of the file
  92. */
  93. BOOST_BEAST_DECL
  94. std::uint64_t
  95. pos(error_code& ec) const;
  96. /** Adjust the current position in the open file
  97. @param offset The offset in bytes from the beginning of the file
  98. @param ec Set to the error, if any occurred
  99. */
  100. BOOST_BEAST_DECL
  101. void
  102. seek(std::uint64_t offset, error_code& ec);
  103. /** Read from the open file
  104. @param buffer The buffer for storing the result of the read
  105. @param n The number of bytes to read
  106. @param ec Set to the error, if any occurred
  107. */
  108. BOOST_BEAST_DECL
  109. std::size_t
  110. read(void* buffer, std::size_t n, error_code& ec) const;
  111. /** Write to the open file
  112. @param buffer The buffer holding the data to write
  113. @param n The number of bytes to write
  114. @param ec Set to the error, if any occurred
  115. */
  116. BOOST_BEAST_DECL
  117. std::size_t
  118. write(void const* buffer, std::size_t n, error_code& ec);
  119. };
  120. } // beast
  121. } // boost
  122. #ifdef BOOST_BEAST_HEADER_ONLY
  123. #include <boost/beast/core/impl/file_stdio.ipp>
  124. #endif
  125. #endif