Adding mkvreadablewriter

Adding mkvreadablewriter files which was missed from previous commit.

Change-Id: I84363ca897ebe758a42538c179ad60c2adfe9247
This commit is contained in:
Vignesh Venkatasubramanian 2013-06-03 14:26:20 -07:00
parent 9b42d039ad
commit 74e5f0fa1d
2 changed files with 201 additions and 0 deletions

148
mkvreadablewriter.cpp Normal file
View File

@ -0,0 +1,148 @@
// Copyright (c) 2013 The WebM project authors. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS. All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
#include "mkvreadablewriter.hpp"
#ifdef _MSC_VER
#include <share.h> // for _SH_DENYWR
#endif
#include <cstdio>
#include <new>
namespace mkvmuxer {
MkvReadableWriter::MkvReadableWriter() : file_(NULL) {
}
MkvReadableWriter::~MkvReadableWriter() {
Close();
}
int32 MkvReadableWriter::Write(const void* buffer, uint32 length) {
if (file_ == NULL)
return -1;
if (length == 0)
return 0;
if (buffer == NULL)
return -1;
const size_t bytes_written = fwrite(buffer, 1, length, file_);
return (bytes_written == length) ? 0 : -1;
}
bool MkvReadableWriter::Open(const char* filename, bool create_temp_file) {
if (filename == NULL && !create_temp_file)
return false;
if (file_ != NULL)
return false;
#ifdef _MSC_VER
file_ = create_temp_file ? tmpfile() : _fsopen(filename, "wb+", _SH_DENYWR);
#else
file_ = create_temp_file ? tmpfile() : fopen(filename, "wb+");
#endif
if (file_ == NULL)
return false;
return true;
}
void MkvReadableWriter::Close() {
if (file_ != NULL) {
fclose(file_);
file_ = NULL;
}
}
int64 MkvReadableWriter::Position() const {
if (file_ == NULL)
return 0;
#ifdef _MSC_VER
return _ftelli64(file_);
#else
return ftell(file_);
#endif
}
int32 MkvReadableWriter::Position(int64 position) {
if (file_ == NULL)
return -1;
#ifdef _MSC_VER
return _fseeki64(file_, position, SEEK_SET);
#else
return fseek(file_, position, SEEK_SET);
#endif
}
bool MkvReadableWriter::Seekable() const {
return true;
}
void MkvReadableWriter::ElementStartNotify(uint64, int64) {
}
int MkvReadableWriter::Length(long long* total, long long* available) {
return 0;
}
int MkvReadableWriter::Read(long long offset,
long len,
unsigned char* buffer) {
if (file_ == NULL)
return -1;
if (offset < 0)
return -1;
if (len < 0)
return -1;
if (len == 0)
return 0;
fflush(file_);
#ifdef _MSC_VER
int64 pos = _ftelli64(file_);
#else
int64 pos = ftell(file_);
#endif
#ifdef WIN32
int status = _fseeki64(file_, offset, SEEK_SET);
if (status)
return -1; //error
#else
fseek(file_, offset, SEEK_SET);
#endif
const size_t size = fread(buffer, 1, len, file_);
#ifdef WIN32
status = _fseeki64(file_, pos, SEEK_SET);
if (status)
return -1; //error
#else
fseek(file_, pos, SEEK_SET);
#endif
if (size < static_cast<size_t>(len))
return -1; //error
return 0; //success
}
} // namespace mkvmuxer

53
mkvreadablewriter.hpp Normal file
View File

@ -0,0 +1,53 @@
// Copyright (c) 2013 The WebM project authors. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS. All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
#ifndef MKVREADABLEWRITER_HPP
#define MKVREADABLEWRITER_HPP
#include <stdio.h>
#include "mkvmuxer.hpp"
#include "mkvmuxertypes.hpp"
namespace mkvmuxer {
// Default implementation of the IMkvReadableWriter interface.
class MkvReadableWriter : public IMkvReadableWriter {
public:
MkvReadableWriter();
virtual ~MkvReadableWriter();
// IMkvReadableWriter interface
virtual int64 Position() const;
virtual int32 Position(int64 position);
virtual bool Seekable() const;
virtual int32 Write(const void* buffer, uint32 length);
virtual void ElementStartNotify(uint64 element_id, int64 position);
virtual int Read(long long position, long length, unsigned char* buffer);
virtual int Length(long long* total, long long* available);
// Creates and opens a file for reading and writing. |filename| is the name of
// the file to open. This function will overwrite the contents of |filename|.
// If |create_temp_file| is set, the |filename| parameter is ignored and a
// temporary file is created, which is automatically deleted upon close.
// Returns true on success.
bool Open(const char* filename, bool create_temp_file);
// Closes an opened file.
void Close();
private:
// File handle to output file.
FILE* file_;
LIBWEBM_DISALLOW_COPY_AND_ASSIGN(MkvReadableWriter);
};
} // end namespace mkvmuxer
#endif // MKVREADABLEWRITER_HPP