| 1 |
/* |
|---|
| 2 |
* DataHandlerCallback.h |
|---|
| 3 |
* |
|---|
| 4 |
* DataHandlerCallback.h - file I/O for libebml. |
|---|
| 5 |
* |
|---|
| 6 |
* |
|---|
| 7 |
* Copyright (c) 2006 David Conrad |
|---|
| 8 |
* |
|---|
| 9 |
* This program is free software; you can redistribute it and/or |
|---|
| 10 |
* modify it under the terms of the GNU Lesser General Public |
|---|
| 11 |
* License as published by the Free Software Foundation; |
|---|
| 12 |
* version 2.1 of the License. |
|---|
| 13 |
* |
|---|
| 14 |
* This program is distributed in the hope that it will be useful, |
|---|
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 17 |
* Lesser General Public License for more details. |
|---|
| 18 |
* |
|---|
| 19 |
* You should have received a copy of the GNU Lesser General Public |
|---|
| 20 |
* License along with this program; if not, write to the Free Software |
|---|
| 21 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 22 |
* |
|---|
| 23 |
*/ |
|---|
| 24 |
|
|---|
| 25 |
#ifndef __DATAHANDLERCALLBACK_H__ |
|---|
| 26 |
#define __DATAHANDLERCALLBACK_H__ |
|---|
| 27 |
|
|---|
| 28 |
#include <QuickTime/QuickTime.h> |
|---|
| 29 |
#include <ebml/IOCallback.h> |
|---|
| 30 |
|
|---|
| 31 |
#include <stdexcept> |
|---|
| 32 |
#include <cerrno> |
|---|
| 33 |
#include <sys/param.h> |
|---|
| 34 |
|
|---|
| 35 |
using namespace std; |
|---|
| 36 |
using namespace libebml; |
|---|
| 37 |
|
|---|
| 38 |
#define READ_SIZE 32*1024 |
|---|
| 39 |
|
|---|
| 40 |
class CRTError:public std::runtime_error |
|---|
| 41 |
{ |
|---|
| 42 |
// Variables... |
|---|
| 43 |
private: |
|---|
| 44 |
int Error; |
|---|
| 45 |
|
|---|
| 46 |
// Methods... |
|---|
| 47 |
public: |
|---|
| 48 |
CRTError(int Error, const std::string &Description); |
|---|
| 49 |
CRTError(const std::string &Description, int Error=errno); |
|---|
| 50 |
|
|---|
| 51 |
int getError() const throw() {return Error;} |
|---|
| 52 |
}; |
|---|
| 53 |
|
|---|
| 54 |
class DataHBuffer { |
|---|
| 55 |
private: |
|---|
| 56 |
uint8_t *buffer; |
|---|
| 57 |
size_t allocatedSize; |
|---|
| 58 |
int64_t fileOffset; |
|---|
| 59 |
size_t dataSize; |
|---|
| 60 |
|
|---|
| 61 |
// this clears the buffer |
|---|
| 62 |
void Realloc(size_t bufferSize); |
|---|
| 63 |
|
|---|
| 64 |
public: |
|---|
| 65 |
DataHBuffer(size_t bufferSize = READ_SIZE); |
|---|
| 66 |
|
|---|
| 67 |
~DataHBuffer(); |
|---|
| 68 |
|
|---|
| 69 |
bool ContainsOffset(uint64_t offset); |
|---|
| 70 |
|
|---|
| 71 |
// this returns a pointer to a buffer at least as big as size, and saves |
|---|
| 72 |
// the given offset and size of data that will be stored |
|---|
| 73 |
uint8_t * GetBuffer(uint64_t offset, size_t size); |
|---|
| 74 |
|
|---|
| 75 |
// this copies as much data from the buffer as possible from the given file offset, |
|---|
| 76 |
// returning the number of bytes actually copied. |
|---|
| 77 |
size_t Read(uint64_t offset, size_t size, uint8_t *store); |
|---|
| 78 |
}; |
|---|
| 79 |
|
|---|
| 80 |
// QuickTime Data Handler callback for libmatroska |
|---|
| 81 |
class DataHandlerCallback:public IOCallback |
|---|
| 82 |
{ |
|---|
| 83 |
private: |
|---|
| 84 |
ComponentInstance dataHandler; |
|---|
| 85 |
uint64 mCurrentPosition; |
|---|
| 86 |
bool closeHandler; |
|---|
| 87 |
bool supportsWideOffsets; |
|---|
| 88 |
open_mode aMode; |
|---|
| 89 |
SInt64 filesize; |
|---|
| 90 |
DataHBuffer dataBuffer; |
|---|
| 91 |
|
|---|
| 92 |
void Initialize(const open_mode aMode); |
|---|
| 93 |
|
|---|
| 94 |
public: |
|---|
| 95 |
DataHandlerCallback(ComponentInstance dataHandler, const open_mode aMode); |
|---|
| 96 |
DataHandlerCallback(Handle dataRef, OSType dataRefType, const open_mode aMode); |
|---|
| 97 |
virtual ~DataHandlerCallback() throw(); |
|---|
| 98 |
|
|---|
| 99 |
virtual uint32 read(void *Buffer, size_t Size); |
|---|
| 100 |
|
|---|
| 101 |
// Seek to the specified position. The mode can have either SEEK_SET, SEEK_CUR |
|---|
| 102 |
// or SEEK_END. The callback should return true(1) if the seek operation succeeded |
|---|
| 103 |
// or false (0), when the seek fails. |
|---|
| 104 |
virtual void setFilePointer(int64 Offset, seek_mode Mode=seek_beginning); |
|---|
| 105 |
|
|---|
| 106 |
// This callback just works like its read pendant. It returns the number of bytes written. |
|---|
| 107 |
virtual size_t write(const void *Buffer, size_t Size); |
|---|
| 108 |
|
|---|
| 109 |
// Although the position is always positive, the return value of this callback is signed to |
|---|
| 110 |
// easily allow negative values for returning errors. When an error occurs, the implementor |
|---|
| 111 |
// should return -1 and the file pointer otherwise. |
|---|
| 112 |
// |
|---|
| 113 |
// If an error occurs, an exception should be thrown. |
|---|
| 114 |
virtual uint64 getFilePointer(); |
|---|
| 115 |
|
|---|
| 116 |
// The close callback flushes the file buffers to disk and closes the file. When using the stdio |
|---|
| 117 |
// library, this is equivalent to calling fclose. When the close is not successful, an exception |
|---|
| 118 |
// should be thrown. |
|---|
| 119 |
virtual void close(); |
|---|
| 120 |
|
|---|
| 121 |
SInt64 getFileSize(); |
|---|
| 122 |
}; |
|---|
| 123 |
|
|---|
| 124 |
#endif |
|---|