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