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
17 namespace mkvmuxer {
18
MkvWriter()19 MkvWriter::MkvWriter() : file_(NULL), writer_owns_file_(true) {}
20
MkvWriter(FILE * fp)21 MkvWriter::MkvWriter(FILE* fp) : file_(fp), writer_owns_file_(false) {}
22
~MkvWriter()23 MkvWriter::~MkvWriter() { Close(); }
24
Write(const void * buffer,uint32 length)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
Open(const char * filename)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
Close()57 void MkvWriter::Close() {
58 if (file_ && writer_owns_file_) {
59 fclose(file_);
60 }
61 file_ = NULL;
62 }
63
Position() const64 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
Position(int64 position)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 #elif defined(_WIN32)
82 return fseeko64(file_, static_cast<off_t>(position), SEEK_SET);
83 #else
84 return fseeko(file_, static_cast<off_t>(position), SEEK_SET);
85 #endif
86 }
87
Seekable() const88 bool MkvWriter::Seekable() const { return true; }
89
ElementStartNotify(uint64,int64)90 void MkvWriter::ElementStartNotify(uint64, int64) {}
91
92 } // namespace mkvmuxer
93