1/****************************************************************************
2** libebml : parse EBML files, see http://embl.sourceforge.net/
3**
4** <file/class description>
5**
6** Copyright (C) 2002-2004 Ingo Ralf Blum.  All rights reserved.
7**
8** This library is free software; you can redistribute it and/or
9** modify it under the terms of the GNU Lesser General Public
10** License as published by the Free Software Foundation; either
11** version 2.1 of the License, or (at your option) any later version.
12**
13** This library is distributed in the hope that it will be useful,
14** but WITHOUT ANY WARRANTY; without even the implied warranty of
15** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16** Lesser General Public License for more details.
17**
18** You should have received a copy of the GNU Lesser General Public
19** License along with this library; if not, write to the Free Software
20** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21**
22** See http://www.matroska.org/license/lgpl/ for LGPL licensing information.
23**
24** Contact license@matroska.org if any conditions of this licensing are
25** not clear to you.
26**
27**********************************************************************/
28
29/*!
30	\file
31	\version \$Id: IOCallback.h 639 2004-07-09 20:59:14Z mosu $
32*/
33#ifndef MATROSKA_IOCALLBACK_H
34#define MATROSKA_IOCALLBACK_H
35
36#include "EbmlTypes.h"
37
38#include <exception>
39#include <cstdio>
40// #include <iostream>
41
42
43START_LIBEBML_NAMESPACE
44
45enum seek_mode
46{
47	seek_beginning=SEEK_SET
48	,seek_end=SEEK_END
49	,seek_current=SEEK_CUR
50};
51
52class EBML_DLL_API IOCallback
53{
54public:
55	virtual ~IOCallback(){}
56
57	// The read callback works like most other read functions. You specify the
58	// file, the buffer and the size and the function returns the bytes read.
59	// If an error occurs or the file pointer points to the end of the file 0 is returned.
60	// Users are encouraged to throw a descriptive exception, when an error occurs.
61	virtual uint32 read(void*Buffer,size_t Size)=0;
62
63	// Seek to the specified position. The mode can have either SEEK_SET, SEEK_CUR
64	// or SEEK_END. The callback should return true(1) if the seek operation succeeded
65	// or false (0), when the seek fails.
66	virtual void setFilePointer(int64 Offset,seek_mode Mode=seek_beginning)=0;
67
68	// This callback just works like its read pendant. It returns the number of bytes written.
69	virtual size_t write(const void*Buffer,size_t Size)=0;
70
71	// Although the position is always positive, the return value of this callback is signed to
72	// easily allow negative values for returning errors. When an error occurs, the implementor
73	// should return -1 and the file pointer otherwise.
74	//
75	// If an error occurs, an exception should be thrown.
76	virtual uint64 getFilePointer()=0;
77
78	// The close callback flushes the file buffers to disk and closes the file. When using the stdio
79	// library, this is equivalent to calling fclose. When the close is not successful, an exception
80	// should be thrown.
81	virtual void close()=0;
82
83
84	// The readFully is made virtual to allow derived classes to use another
85	// implementation for this method, which e.g. does not read any data
86	// unlike this does
87	void readFully(void*Buffer,size_t Size);
88
89	template<class STRUCT> void readStruct(STRUCT&Struct){readFully(&Struct,sizeof(Struct));}
90
91	void writeFully(const void*Buffer,size_t Size);
92
93	template<class STRUCT> void writeStruct(const STRUCT&Struct){writeFully(&Struct,sizeof(Struct));}
94};
95
96/* cygwin incompatible
97template<class TRAITS> std::basic_ostream<char,TRAITS>&operator<<(std::basic_ostream<char,TRAITS>&Stream,seek_mode Mode)
98{
99	switch(Mode)
100	{
101#define x(y) case seek_##y: Stream<<"seek_" #y; break
102		x(beginning);
103		x(current);
104		x(end);
105#undef x
106	default:
107		assert(false);
108	}
109
110	return Stream;
111}
112*/
113
114END_LIBEBML_NAMESPACE
115
116#endif // MATROSKA_IOCALLBACK_H
117