file_stdio.hpp 3.8 KB

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