mkvwriter.cc 1.9 KB

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