OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
| 2 // |
| 3 // Use of this source code is governed by a BSD-style license |
| 4 // that can be found in the LICENSE file in the root of the source |
| 5 // tree. An additional intellectual property rights grant can be found |
| 6 // in the file PATENTS. All contributing project authors may |
| 7 // be found in the AUTHORS file in the root of the source tree. |
| 8 |
| 9 #include "mkvreader.hpp" |
| 10 |
| 11 #include <cassert> |
| 12 |
| 13 namespace mkvparser |
| 14 { |
| 15 |
| 16 MkvReader::MkvReader() : |
| 17 m_file(NULL), |
| 18 reader_owns_file_(true) { |
| 19 } |
| 20 |
| 21 MkvReader::MkvReader(FILE* fp) : |
| 22 m_file(fp), |
| 23 reader_owns_file_(false) { |
| 24 GetFileSize(); |
| 25 } |
| 26 |
| 27 MkvReader::~MkvReader() { |
| 28 if (reader_owns_file_) |
| 29 Close(); |
| 30 m_file = NULL; |
| 31 } |
| 32 |
| 33 int MkvReader::Open(const char* fileName) |
| 34 { |
| 35 if (fileName == NULL) |
| 36 return -1; |
| 37 |
| 38 if (m_file) |
| 39 return -1; |
| 40 |
| 41 #ifdef _MSC_VER |
| 42 const errno_t e = fopen_s(&m_file, fileName, "rb"); |
| 43 |
| 44 if (e) |
| 45 return -1; //error |
| 46 #else |
| 47 m_file = fopen(fileName, "rb"); |
| 48 |
| 49 if (m_file == NULL) |
| 50 return -1; |
| 51 #endif |
| 52 return !GetFileSize(); |
| 53 } |
| 54 |
| 55 bool MkvReader::GetFileSize() { |
| 56 if (m_file == NULL) |
| 57 return false; |
| 58 #ifdef _MSC_VER |
| 59 int status = _fseeki64(m_file, 0L, SEEK_END); |
| 60 |
| 61 if (status) |
| 62 return false; //error |
| 63 |
| 64 m_length = _ftelli64(m_file); |
| 65 #else |
| 66 fseek(m_file, 0L, SEEK_END); |
| 67 m_length = ftell(m_file); |
| 68 #endif |
| 69 assert(m_length >= 0); |
| 70 |
| 71 if (m_length < 0) |
| 72 return false; |
| 73 |
| 74 #ifdef _MSC_VER |
| 75 status = _fseeki64(m_file, 0L, SEEK_SET); |
| 76 |
| 77 if (status) |
| 78 return false; //error |
| 79 #else |
| 80 fseek(m_file, 0L, SEEK_SET); |
| 81 #endif |
| 82 |
| 83 return true; |
| 84 } |
| 85 |
| 86 void MkvReader::Close() |
| 87 { |
| 88 if (m_file != NULL) |
| 89 { |
| 90 fclose(m_file); |
| 91 m_file = NULL; |
| 92 } |
| 93 } |
| 94 |
| 95 int MkvReader::Length(long long* total, long long* available) |
| 96 { |
| 97 if (m_file == NULL) |
| 98 return -1; |
| 99 |
| 100 if (total) |
| 101 *total = m_length; |
| 102 |
| 103 if (available) |
| 104 *available = m_length; |
| 105 |
| 106 return 0; |
| 107 } |
| 108 |
| 109 int MkvReader::Read(long long offset, long len, unsigned char* buffer) |
| 110 { |
| 111 if (m_file == NULL) |
| 112 return -1; |
| 113 |
| 114 if (offset < 0) |
| 115 return -1; |
| 116 |
| 117 if (len < 0) |
| 118 return -1; |
| 119 |
| 120 if (len == 0) |
| 121 return 0; |
| 122 |
| 123 if (offset >= m_length) |
| 124 return -1; |
| 125 |
| 126 #ifdef _MSC_VER |
| 127 const int status = _fseeki64(m_file, offset, SEEK_SET); |
| 128 |
| 129 if (status) |
| 130 return -1; //error |
| 131 #else |
| 132 fseek(m_file, offset, SEEK_SET); |
| 133 #endif |
| 134 |
| 135 const size_t size = fread(buffer, 1, len, m_file); |
| 136 |
| 137 if (size < size_t(len)) |
| 138 return -1; //error |
| 139 |
| 140 return 0; //success |
| 141 } |
| 142 |
| 143 } //end namespace mkvparser |
OLD | NEW |