1 // Copyright (c) 2012 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 "mkvwriter.hpp" 10 11 #ifdef _MSC_VER 12 #include <share.h> // for _SH_DENYWR 13 #endif 14 15 #include <new> 16 17 namespace mkvmuxer { 18 19 MkvWriter::MkvWriter() : file_(NULL), writer_owns_file_(true) {} 20 21 MkvWriter::MkvWriter(FILE* fp) : file_(fp), writer_owns_file_(false) {} 22 23 MkvWriter::~MkvWriter() { Close(); } 24 25 int32 MkvWriter::Write(const void* buffer, uint32 length) { 26 if (!file_) 27 return -1; 28 29 if (length == 0) 30 return 0; 31 32 if (buffer == NULL) 33 return -1; 34 35 const size_t bytes_written = fwrite(buffer, 1, length, file_); 36 37 return (bytes_written == length) ? 0 : -1; 38 } 39 40 bool MkvWriter::Open(const char* filename) { 41 if (filename == NULL) 42 return false; 43 44 if (file_) 45 return false; 46 47 #ifdef _MSC_VER 48 file_ = _fsopen(filename, "wb", _SH_DENYWR); 49 #else 50 file_ = fopen(filename, "wb"); 51 #endif 52 if (file_ == NULL) 53 return false; 54 return true; 55 } 56 57 void MkvWriter::Close() { 58 if (file_ && writer_owns_file_) { 59 fclose(file_); 60 } 61 file_ = NULL; 62 } 63 64 int64 MkvWriter::Position() const { 65 if (!file_) 66 return 0; 67 68 #ifdef _MSC_VER 69 return _ftelli64(file_); 70 #else 71 return ftell(file_); 72 #endif 73 } 74 75 int32 MkvWriter::Position(int64 position) { 76 if (!file_) 77 return -1; 78 79 #ifdef _MSC_VER 80 return _fseeki64(file_, position, SEEK_SET); 81 #else 82 return fseek(file_, position, SEEK_SET); 83 #endif 84 } 85 86 bool MkvWriter::Seekable() const { return true; } 87 88 void MkvWriter::ElementStartNotify(uint64, int64) {} 89 90 } // namespace mkvmuxer 91