]> granicus.if.org Git - libvpx/blob - third_party/libwebm/mkvmuxer/mkvwriter.cc
d7d6de9b6376da2c94202fb24ca0dbed42309f2a
[libvpx] / third_party / libwebm / mkvmuxer / mkvwriter.cc
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
17 MkvWriter::MkvWriter() : file_(NULL), writer_owns_file_(true) {}
18
19 MkvWriter::MkvWriter(FILE* fp) : file_(fp), writer_owns_file_(false) {}
20
21 MkvWriter::~MkvWriter() { Close(); }
22
23 int32_t MkvWriter::Write(const void* buffer, uint32_t 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
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
55 void MkvWriter::Close() {
56   if (file_ && writer_owns_file_) {
57     fclose(file_);
58   }
59   file_ = NULL;
60 }
61
62 int64_t 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
73 int32_t MkvWriter::Position(int64_t position) {
74   if (!file_)
75     return -1;
76
77 #ifdef _MSC_VER
78   return _fseeki64(file_, position, SEEK_SET);
79 #else
80   return fseek(file_, position, SEEK_SET);
81 #endif
82 }
83
84 bool MkvWriter::Seekable() const { return true; }
85
86 void MkvWriter::ElementStartNotify(uint64_t, int64_t) {}
87
88 }  // namespace mkvmuxer