Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:28

0001 //
0002 // Copyright (c) 2015-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/beast
0008 //
0009 
0010 #ifndef BOOST_BEAST_CORE_FILE_POSIX_HPP
0011 #define BOOST_BEAST_CORE_FILE_POSIX_HPP
0012 
0013 #include <boost/beast/core/detail/config.hpp>
0014 
0015 #if ! defined(BOOST_BEAST_NO_POSIX_FILE)
0016 # if ! defined(__APPLE__) && ! defined(__linux__)
0017 #  define BOOST_BEAST_NO_POSIX_FILE
0018 # endif
0019 #endif
0020 
0021 #if ! defined(BOOST_BEAST_USE_POSIX_FILE)
0022 # if ! defined(BOOST_BEAST_NO_POSIX_FILE)
0023 #  define BOOST_BEAST_USE_POSIX_FILE 1
0024 # else
0025 #  define BOOST_BEAST_USE_POSIX_FILE 0
0026 # endif
0027 #endif
0028 
0029 #if BOOST_BEAST_USE_POSIX_FILE
0030 
0031 #include <boost/beast/core/error.hpp>
0032 #include <boost/beast/core/file_base.hpp>
0033 #include <cstdint>
0034 
0035 namespace boost {
0036 namespace beast {
0037 
0038 /** An implementation of File for POSIX systems.
0039 
0040     This class implements a <em>File</em> using POSIX interfaces.
0041 */
0042 class file_posix
0043 {
0044     int fd_ = -1;
0045 
0046     BOOST_BEAST_DECL
0047     static
0048     int
0049     native_close(int& fd);
0050 
0051 public:
0052     /** The type of the underlying file handle.
0053 
0054         This is platform-specific.
0055     */
0056     using native_handle_type = int;
0057 
0058     /** Destructor
0059 
0060         If the file is open it is first closed.
0061     */
0062     BOOST_BEAST_DECL
0063     ~file_posix();
0064 
0065     /** Constructor
0066 
0067         There is no open file initially.
0068     */
0069     file_posix() = default;
0070 
0071     /** Constructor
0072 
0073         The moved-from object behaves as if default constructed.
0074     */
0075     BOOST_BEAST_DECL
0076     file_posix(file_posix&& other);
0077 
0078     /** Assignment
0079 
0080         The moved-from object behaves as if default constructed.
0081     */
0082     BOOST_BEAST_DECL
0083     file_posix& operator=(file_posix&& other);
0084 
0085     /// Returns the native handle associated with the file.
0086     native_handle_type
0087     native_handle() const
0088     {
0089         return fd_;
0090     }
0091 
0092     /** Set the native handle associated with the file.
0093 
0094         If the file is open it is first closed.
0095 
0096         @param fd The native file handle to assign.
0097     */
0098     BOOST_BEAST_DECL
0099     void
0100     native_handle(native_handle_type fd);
0101 
0102     /// Returns `true` if the file is open
0103     bool
0104     is_open() const
0105     {
0106         return fd_ != -1;
0107     }
0108 
0109     /** Close the file if open
0110 
0111         @param ec Set to the error, if any occurred.
0112     */
0113     BOOST_BEAST_DECL
0114     void
0115     close(error_code& ec);
0116 
0117     /** Open a file at the given path with the specified mode
0118 
0119         @param path The utf-8 encoded path to the file
0120 
0121         @param mode The file mode to use
0122 
0123         @param ec Set to the error, if any occurred
0124     */
0125     BOOST_BEAST_DECL
0126     void
0127     open(char const* path, file_mode mode, error_code& ec);
0128 
0129     /** Return the size of the open file
0130 
0131         @param ec Set to the error, if any occurred
0132 
0133         @return The size in bytes
0134     */
0135     BOOST_BEAST_DECL
0136     std::uint64_t
0137     size(error_code& ec) const;
0138 
0139     /** Return the current position in the open file
0140 
0141         @param ec Set to the error, if any occurred
0142 
0143         @return The offset in bytes from the beginning of the file
0144     */
0145     BOOST_BEAST_DECL
0146     std::uint64_t
0147     pos(error_code& ec) const;
0148 
0149     /** Adjust the current position in the open file
0150 
0151         @param offset The offset in bytes from the beginning of the file
0152 
0153         @param ec Set to the error, if any occurred
0154     */
0155     BOOST_BEAST_DECL
0156     void
0157     seek(std::uint64_t offset, error_code& ec);
0158 
0159     /** Read from the open file
0160 
0161         @param buffer The buffer for storing the result of the read
0162 
0163         @param n The number of bytes to read
0164 
0165         @param ec Set to the error, if any occurred
0166     */
0167     BOOST_BEAST_DECL
0168     std::size_t
0169     read(void* buffer, std::size_t n, error_code& ec) const;
0170 
0171     /** Write to the open file
0172 
0173         @param buffer The buffer holding the data to write
0174 
0175         @param n The number of bytes to write
0176 
0177         @param ec Set to the error, if any occurred
0178     */
0179     BOOST_BEAST_DECL
0180     std::size_t
0181     write(void const* buffer, std::size_t n, error_code& ec);
0182 };
0183 
0184 } // beast
0185 } // boost
0186 
0187 #ifdef BOOST_BEAST_HEADER_ONLY
0188 #include <boost/beast/core/impl/file_posix.ipp>
0189 #endif
0190 
0191 #endif
0192 
0193 #endif