|
| 1 | +#include "src/impl.h" |
| 2 | +#include "libplatform/impl.h" /* for platform_win32_impl.h which declares Utf8ToFilename */ |
| 3 | +#include <windows.h> |
| 4 | + |
| 5 | +namespace mp4v2 { |
| 6 | + using namespace impl; |
| 7 | +} |
| 8 | + |
| 9 | +/** |
| 10 | + * Set this to 1 to compile in extra debugging |
| 11 | + */ |
| 12 | +#define EXTRA_DEBUG 0 |
| 13 | + |
| 14 | +/** |
| 15 | + * @def LOG_PRINTF |
| 16 | + * |
| 17 | + * call log.printf if EXTRA_DEBUG is defined to 1. Do |
| 18 | + * nothing otherwise |
| 19 | + */ |
| 20 | +#if EXTRA_DEBUG |
| 21 | +#define LOG_PRINTF(X) log.printf X |
| 22 | +#else |
| 23 | +#define LOG_PRINTF(X) |
| 24 | +#endif |
| 25 | + |
| 26 | +namespace mp4v2 { namespace platform { namespace io { |
| 27 | + |
| 28 | +/////////////////////////////////////////////////////////////////////////////// |
| 29 | + |
| 30 | +class StandardFileProvider : public FileProvider |
| 31 | +{ |
| 32 | +public: |
| 33 | + StandardFileProvider(); |
| 34 | + |
| 35 | + bool open( std::string name, Mode mode ); |
| 36 | + bool seek( Size pos ); |
| 37 | + bool read( void* buffer, Size size, Size& nin, Size maxChunkSize ); |
| 38 | + bool write( const void* buffer, Size size, Size& nout, Size maxChunkSize ); |
| 39 | + bool close(); |
| 40 | + |
| 41 | + int64_t getSize(); |
| 42 | + |
| 43 | +private: |
| 44 | + HANDLE _handle; |
| 45 | + |
| 46 | + /** |
| 47 | + * The UTF-8 encoded file name |
| 48 | + */ |
| 49 | + std::string _name; |
| 50 | +}; |
| 51 | + |
| 52 | +/////////////////////////////////////////////////////////////////////////////// |
| 53 | + |
| 54 | +StandardFileProvider::StandardFileProvider() |
| 55 | + : _handle( INVALID_HANDLE_VALUE ) |
| 56 | +{ |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Open a file |
| 61 | + * |
| 62 | + * @param name the name of a file to open |
| 63 | + * @param mode the mode to open @p name |
| 64 | + * |
| 65 | + * @retval false successfully opened @p name |
| 66 | + * @retval true error opening @p name |
| 67 | + */ |
| 68 | +bool |
| 69 | +StandardFileProvider::open( std::string name, Mode mode ) |
| 70 | +{ |
| 71 | + DWORD access = 0; |
| 72 | + DWORD share = 0; |
| 73 | + DWORD crdisp = 0; |
| 74 | + DWORD flags = FILE_ATTRIBUTE_NORMAL; |
| 75 | + |
| 76 | + switch( mode ) { |
| 77 | + case MODE_UNDEFINED: |
| 78 | + case MODE_READ: |
| 79 | + default: |
| 80 | + access |= GENERIC_READ; |
| 81 | + share |= FILE_SHARE_READ; |
| 82 | + crdisp |= OPEN_EXISTING; |
| 83 | + break; |
| 84 | + |
| 85 | + case MODE_MODIFY: |
| 86 | + access |= GENERIC_READ | GENERIC_WRITE; |
| 87 | + share |= FILE_SHARE_READ; |
| 88 | + crdisp |= OPEN_EXISTING; |
| 89 | + break; |
| 90 | + |
| 91 | + case MODE_CREATE: |
| 92 | + access |= GENERIC_READ | GENERIC_WRITE; |
| 93 | + share |= FILE_SHARE_READ; |
| 94 | + crdisp |= CREATE_ALWAYS; |
| 95 | + break; |
| 96 | + } |
| 97 | + |
| 98 | + win32::Utf8ToFilename filename(name); |
| 99 | + |
| 100 | + if (!filename.IsUTF16Valid()) |
| 101 | + { |
| 102 | + // The logging is done |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + ASSERT(LPCWSTR(filename)); |
| 107 | + _handle = CreateFileW( filename, access, share, NULL, crdisp, flags, NULL ); |
| 108 | + if (_handle == INVALID_HANDLE_VALUE) |
| 109 | + { |
| 110 | + log.errorf("%s: CreateFileW(%s) failed (%d)",__FUNCTION__,filename.utf8.c_str(),GetLastError()); |
| 111 | + return true; |
| 112 | + } |
| 113 | + |
| 114 | + /* |
| 115 | + ** Make a copy of the name for future log messages, etc. |
| 116 | + */ |
| 117 | + log.verbose2f("%s: CreateFileW(%s) succeeded",__FUNCTION__,filename.utf8.c_str()); |
| 118 | + |
| 119 | + _name = name; |
| 120 | + return false; |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * Seek to an offset in the file |
| 125 | + * |
| 126 | + * @param pos the offset from the beginning of the file to |
| 127 | + * seek to |
| 128 | + * |
| 129 | + * @retval false successfully seeked to @p pos |
| 130 | + * @retval true error seeking to @p pos |
| 131 | + */ |
| 132 | +bool |
| 133 | +StandardFileProvider::seek( Size pos ) |
| 134 | +{ |
| 135 | + LARGE_INTEGER n; |
| 136 | + |
| 137 | + ASSERT(_handle != INVALID_HANDLE_VALUE); |
| 138 | + |
| 139 | + n.QuadPart = pos; |
| 140 | + if (!SetFilePointerEx( _handle, n, NULL, FILE_BEGIN )) |
| 141 | + { |
| 142 | + log.errorf("%s: SetFilePointerEx(%s,%" PRId64 ") failed (%d)",__FUNCTION__,_name.c_str(), |
| 143 | + pos,GetLastError()); |
| 144 | + return true; |
| 145 | + } |
| 146 | + |
| 147 | + return false; |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * Read from the file |
| 152 | + * |
| 153 | + * @param buffer populated with at most @p size bytes from |
| 154 | + * the file |
| 155 | + * |
| 156 | + * @param size the maximum number of bytes to read |
| 157 | + * |
| 158 | + * @param nin the |
| 159 | + * |
| 160 | + * @retval false successfully read from the file |
| 161 | + * @retval true error reading from the file |
| 162 | + */ |
| 163 | +bool |
| 164 | +StandardFileProvider::read( void* buffer, Size size, Size& nin, Size maxChunkSize ) |
| 165 | +{ |
| 166 | + DWORD nread = 0; |
| 167 | + |
| 168 | + ASSERT(_handle != INVALID_HANDLE_VALUE); |
| 169 | + |
| 170 | + // ReadFile takes a DWORD for number of bytes to read so |
| 171 | + // make sure we're not asking for more than fits. |
| 172 | + // MAXDWORD from WinNT.h. |
| 173 | + ASSERT(size <= MAXDWORD); |
| 174 | + if( ReadFile( _handle, buffer, (DWORD)(size & MAXDWORD), &nread, NULL ) == 0 ) |
| 175 | + { |
| 176 | + log.errorf("%s: ReadFile(%s,%d) failed (%d)",__FUNCTION__,_name.c_str(), |
| 177 | + (DWORD)(size & MAXDWORD),GetLastError()); |
| 178 | + return true; |
| 179 | + } |
| 180 | + LOG_PRINTF((MP4_LOG_VERBOSE3,"%s: ReadFile(%s,%d) succeeded: read %d byte(s)",__FUNCTION__, |
| 181 | + _name.c_str(),(DWORD)(size & MAXDWORD),nread)); |
| 182 | + nin = nread; |
| 183 | + return false; |
| 184 | +} |
| 185 | + |
| 186 | +/** |
| 187 | + * Write to the file |
| 188 | + * |
| 189 | + * @param buffer the data to write |
| 190 | + * |
| 191 | + * @param size the number of bytes of @p buffer to write |
| 192 | + * |
| 193 | + * @param nout populated with the number of bytes actually |
| 194 | + * written if the function succeeds |
| 195 | + * |
| 196 | + * @retval false successfully wrote to the file |
| 197 | + * @retval true error writing to the file |
| 198 | + */ |
| 199 | +bool |
| 200 | +StandardFileProvider::write( const void* buffer, Size size, Size& nout, Size maxChunkSize ) |
| 201 | +{ |
| 202 | + DWORD nwrote = 0; |
| 203 | + |
| 204 | + ASSERT(_handle != INVALID_HANDLE_VALUE); |
| 205 | + |
| 206 | + // ReadFile takes a DWORD for number of bytes to read so |
| 207 | + // make sure we're not asking for more than fits. |
| 208 | + // MAXDWORD from WinNT.h. |
| 209 | + ASSERT(size <= MAXDWORD); |
| 210 | + if( WriteFile( _handle, buffer, (DWORD)(size & MAXDWORD), &nwrote, NULL ) == 0 ) |
| 211 | + { |
| 212 | + log.errorf("%s: WriteFile(%s,%d) failed (%d)",__FUNCTION__,_name.c_str(), |
| 213 | + (DWORD)(size & MAXDWORD),GetLastError()); |
| 214 | + return true; |
| 215 | + } |
| 216 | + log.verbose2f("%s: WriteFile(%s,%d) succeeded: wrote %d byte(s)",__FUNCTION__, |
| 217 | + _name.c_str(),(DWORD)(size & MAXDWORD),nwrote); |
| 218 | + nout = nwrote; |
| 219 | + return false; |
| 220 | +} |
| 221 | + |
| 222 | +/** |
| 223 | + * Close the file |
| 224 | + * |
| 225 | + * @retval false successfully closed the file |
| 226 | + * @retval true error closing the file |
| 227 | + */ |
| 228 | +bool |
| 229 | +StandardFileProvider::close() |
| 230 | +{ |
| 231 | + BOOL retval; |
| 232 | + |
| 233 | + retval = CloseHandle( _handle ); |
| 234 | + if (!retval) |
| 235 | + { |
| 236 | + log.errorf("%s: CloseHandle(%s) failed (%d)",__FUNCTION__, |
| 237 | + _name.c_str(),GetLastError()); |
| 238 | + } |
| 239 | + |
| 240 | + // Whether we succeeded or not, clear the handle and |
| 241 | + // forget the name |
| 242 | + _handle = INVALID_HANDLE_VALUE; |
| 243 | + _name.clear(); |
| 244 | + |
| 245 | + // CloseHandle return 0/false to indicate failure, but |
| 246 | + // we return 0/false to indicate success, so negate. |
| 247 | + return !retval; |
| 248 | +} |
| 249 | + |
| 250 | +int64_t StandardFileProvider::getSize() |
| 251 | +{ |
| 252 | + int64_t retSize = 0; |
| 253 | + FileSystem::getFileSize( _name, retSize ); |
| 254 | + return retSize; |
| 255 | +} |
| 256 | + |
| 257 | +/////////////////////////////////////////////////////////////////////////////// |
| 258 | + |
| 259 | +FileProvider& |
| 260 | +FileProvider::standard() |
| 261 | +{ |
| 262 | + return *new StandardFileProvider(); |
| 263 | +} |
| 264 | + |
| 265 | +/////////////////////////////////////////////////////////////////////////////// |
| 266 | + |
| 267 | +}}} // namespace mp4v2::platform::io |
0 commit comments