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 "mkvmuxer/mkvwriter.h" 10 11#include <sys/types.h> 12 13#ifdef _MSC_VER 14#include <share.h> // for _SH_DENYWR 15#endif 16 17namespace mkvmuxer { 18 19MkvWriter::MkvWriter() : file_(NULL), writer_owns_file_(true) {} 20 21MkvWriter::MkvWriter(FILE* fp) : file_(fp), writer_owns_file_(false) {} 22 23MkvWriter::~MkvWriter() { Close(); } 24 25int32 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 40bool 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 57void MkvWriter::Close() { 58 if (file_ && writer_owns_file_) { 59 fclose(file_); 60 } 61 file_ = NULL; 62} 63 64int64 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 75int32 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 fseeko(file_, static_cast<off_t>(position), SEEK_SET); 83#endif 84} 85 86bool MkvWriter::Seekable() const { return true; } 87 88void MkvWriter::ElementStartNotify(uint64, int64) {} 89 90} // namespace mkvmuxer 91