1 #ifndef MP4V2_PLATFORM_IO_FILE_H 2 #define MP4V2_PLATFORM_IO_FILE_H 3 4 namespace mp4v2 { namespace platform { namespace io { 5 6 /////////////////////////////////////////////////////////////////////////////// 7 8 class MP4V2_EXPORT FileProvider 9 { 10 public: 11 static FileProvider& standard(); 12 13 public: 14 //! file operation mode flags 15 enum Mode { 16 MODE_UNDEFINED, //!< undefined 17 MODE_READ, //!< file may be read 18 MODE_MODIFY, //!< file may be read/written 19 MODE_CREATE, //!< file will be created/truncated for read/write 20 }; 21 22 //! type used to represent all file sizes and offsets 23 typedef int64_t Size; 24 25 public: ~FileProvider()26 virtual ~FileProvider() { } 27 28 virtual bool open( std::string name, Mode mode ) = 0; 29 virtual bool seek( Size pos ) = 0; 30 virtual bool read( void* buffer, Size size, Size& nin, Size maxChunkSize ) = 0; 31 virtual bool write( const void* buffer, Size size, Size& nout, Size maxChunkSize ) = 0; 32 virtual bool close() = 0; 33 34 protected: FileProvider()35 FileProvider() { } 36 }; 37 38 /////////////////////////////////////////////////////////////////////////////// 39 /// 40 /// File implementation. 41 /// 42 /// File objects model real filesystem files in a 1:1 releationship and always 43 /// treated as binary; there are no translations of text content performed. 44 /// 45 /// The interface represents all sizes with a signed 64-bit value, thus 46 /// the limit to this interface is 63-bits of size, roughly 9.22 million TB. 47 /// 48 /////////////////////////////////////////////////////////////////////////////// 49 50 class MP4V2_EXPORT File : public FileProvider 51 { 52 public: 53 /////////////////////////////////////////////////////////////////////////// 54 //! 55 //! Constructor. 56 //! 57 //! A new file object is constructed but not opened. 58 //! 59 //! @param name filename of file object, or empty-string. 60 //! On Windows, this should be a UTF-8 encoded string. 61 //! On other platforms, it should be an 8-bit encoding that is 62 //! appropriate for the platform, locale, file system, etc. 63 //! (prefer to use UTF-8 when possible). 64 //! @param mode bitmask specifying mode flags. 65 //! See #Mode for bit constants. 66 //! @param provider a fileprovider instance. If NULL a standard file 67 //! provider will be used otherwise the supplied provider must be 68 //! new-allocated and will be delete'd via ~File(). 69 //! 70 /////////////////////////////////////////////////////////////////////////// 71 72 explicit File( std::string name = "", Mode mode = MODE_UNDEFINED, FileProvider* = NULL ); 73 74 /////////////////////////////////////////////////////////////////////////// 75 //! 76 //! Destructor. 77 //! 78 //! File object is destroyed. If the file is opened it is closed prior 79 //! to destruction. 80 //! 81 /////////////////////////////////////////////////////////////////////////// 82 83 ~File(); 84 85 /////////////////////////////////////////////////////////////////////////// 86 //! 87 //! Open file. 88 //! 89 //! @param name filename of file object, or empty-string to use #name. 90 //! On Windows, this should be a UTF-8 encoded string. 91 //! On other platforms, it should be an 8-bit encoding that is 92 //! appropriate for the platform, locale, file system, etc. 93 //! (prefer to use UTF-8 when possible). 94 //! 95 //! @return true on failure, false on success. 96 //! 97 /////////////////////////////////////////////////////////////////////////// 98 99 bool open( std::string name = "", Mode mode = MODE_UNDEFINED ); 100 101 /////////////////////////////////////////////////////////////////////////// 102 //! 103 //! Closes file. 104 //! 105 //! If the file has not been opened or is not considered the 106 //! owner of a filehandle, no action is taken. 107 //! 108 //! @return true on failure, false on success. 109 //! 110 /////////////////////////////////////////////////////////////////////////// 111 112 bool close(); 113 114 /////////////////////////////////////////////////////////////////////////// 115 //! 116 //! Set current file position in bytes. 117 //! 118 //! @param pos new file position in bytes. 119 //! 120 //! @return true on failure, false on success. 121 //! 122 /////////////////////////////////////////////////////////////////////////// 123 124 bool seek( Size pos ); 125 126 /////////////////////////////////////////////////////////////////////////// 127 //! 128 //! Binary stream read. 129 //! 130 //! The function reads up to a maximum <b>size</b> bytes from file, 131 //! storing them in <b>buffer</b>. The number of bytes actually read are 132 //! returned in <b>nin</b>. 133 //! 134 //! @param buffer storage for data read from file. 135 //! @param size maximum number of bytes to read from file. 136 //! @param nin output indicating number of bytes read from file. 137 //! @param maxChunkSize maximum chunk size for reads issued to operating 138 //! system or 0 for default. 139 //! 140 //! @return true on failure, false on success. 141 //! 142 /////////////////////////////////////////////////////////////////////////// 143 144 bool read( void* buffer, Size size, Size& nin, Size maxChunkSize = 0 ); 145 146 /////////////////////////////////////////////////////////////////////////// 147 //! 148 //! Binary stream write. 149 //! 150 //! The function writes up to a maximum <b>size</b> bytes from 151 //! <b>buffer</b> to file. The number of bytes actually written are 152 //! returned in <b>nout</b>. 153 //! 154 //! @param buffer data to be written out to file. 155 //! @param size maximum number of bytes to read from file. 156 //! @param nout output indicating number of bytes written to file. 157 //! @param maxChunkSize maximum chunk size for writes issued to operating 158 //! system or 0 for default. 159 //! 160 //! @return true on failure, false on success. 161 //! 162 /////////////////////////////////////////////////////////////////////////// 163 164 bool write( const void* buffer, Size size, Size& nout, Size maxChunkSize = 0 ); 165 166 private: 167 std::string _name; 168 bool _isOpen; 169 Mode _mode; 170 Size _size; 171 Size _position; 172 FileProvider& _provider; 173 174 public: 175 const std::string& name; //!< read-only: file pathname or empty-string if not applicable 176 const bool& isOpen; //!< read-only: true if file is open 177 const Mode& mode; //!< read-only: file mode 178 const Size& size; //!< read-only: file size 179 const Size& position; //!< read-only: file position 180 181 public: 182 void setName( const std::string& name ); 183 void setMode( Mode mode ); 184 }; 185 186 /////////////////////////////////////////////////////////////////////////////// 187 188 class CustomFileProvider : public FileProvider 189 { 190 public: 191 CustomFileProvider( const MP4FileProvider& ); 192 193 bool open( std::string name, Mode mode ); 194 bool seek( Size pos ); 195 bool read( void* buffer, Size size, Size& nin, Size maxChunkSize ); 196 bool write( const void* buffer, Size size, Size& nout, Size maxChunkSize ); 197 bool close(); 198 199 private: 200 MP4FileProvider _call; 201 void* _handle; 202 }; 203 204 /////////////////////////////////////////////////////////////////////////////// 205 206 }}} // namespace mp4v2::platform::io 207 208 #endif // MP4V2_PLATFORM_IO_FILE_H 209