00001 /**************************************************************************** 00002 ** libebml : parse EBML files, see http://embl.sourceforge.net/ 00003 ** 00004 ** <file/class description> 00005 ** 00006 ** Copyright (C) 2002-2004 Ingo Ralf Blum. All rights reserved. 00007 ** 00008 ** This library is free software; you can redistribute it and/or 00009 ** modify it under the terms of the GNU Lesser General Public 00010 ** License as published by the Free Software Foundation; either 00011 ** version 2.1 of the License, or (at your option) any later version. 00012 ** 00013 ** This library is distributed in the hope that it will be useful, 00014 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 ** Lesser General Public License for more details. 00017 ** 00018 ** You should have received a copy of the GNU Lesser General Public 00019 ** License along with this library; if not, write to the Free Software 00020 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 ** 00022 ** See http://www.matroska.org/license/lgpl/ for LGPL licensing information. 00023 ** 00024 ** Contact license@matroska.org if any conditions of this licensing are 00025 ** not clear to you. 00026 ** 00027 **********************************************************************/ 00028 00033 #ifndef MATROSKA_IOCALLBACK_H 00034 #define MATROSKA_IOCALLBACK_H 00035 00036 #include "EbmlTypes.h" 00037 00038 #include <cassert> 00039 #include <exception> 00040 #include <cstdio> 00041 // #include <iostream> 00042 00043 00044 START_LIBEBML_NAMESPACE 00045 00046 enum seek_mode 00047 { 00048 seek_beginning=SEEK_SET 00049 ,seek_end=SEEK_END 00050 ,seek_current=SEEK_CUR 00051 }; 00052 00053 class EBML_DLL_API IOCallback 00054 { 00055 public: 00056 virtual ~IOCallback(){} 00057 00058 // The read callback works like most other read functions. You specify the 00059 // file, the buffer and the size and the function returns the bytes read. 00060 // If an error occurs or the file pointer points to the end of the file 0 is returned. 00061 // Users are encouraged to throw a descriptive exception, when an error occurs. 00062 virtual uint32 read(void*Buffer,size_t Size)=0; 00063 00064 // Seek to the specified position. The mode can have either SEEK_SET, SEEK_CUR 00065 // or SEEK_END. The callback should return true(1) if the seek operation succeeded 00066 // or false (0), when the seek fails. 00067 virtual void setFilePointer(int64 Offset,seek_mode Mode=seek_beginning)=0; 00068 00069 // This callback just works like its read pendant. It returns the number of bytes written. 00070 virtual size_t write(const void*Buffer,size_t Size)=0; 00071 00072 // Although the position is always positive, the return value of this callback is signed to 00073 // easily allow negative values for returning errors. When an error occurs, the implementor 00074 // should return -1 and the file pointer otherwise. 00075 // 00076 // If an error occurs, an exception should be thrown. 00077 virtual uint64 getFilePointer()=0; 00078 00079 // The close callback flushes the file buffers to disk and closes the file. When using the stdio 00080 // library, this is equivalent to calling fclose. When the close is not successful, an exception 00081 // should be thrown. 00082 virtual void close()=0; 00083 00084 00085 // The readFully is made virtual to allow derived classes to use another 00086 // implementation for this method, which e.g. does not read any data 00087 // unlike this does 00088 void readFully(void*Buffer,size_t Size); 00089 00090 template<class STRUCT> void readStruct(STRUCT&Struct){readFully(&Struct,sizeof(Struct));} 00091 00092 void writeFully(const void*Buffer,size_t Size); 00093 00094 template<class STRUCT> void writeStruct(const STRUCT&Struct){writeFully(&Struct,sizeof(Struct));} 00095 }; 00096 00097 /* cygwin incompatible 00098 template<class TRAITS> std::basic_ostream<char,TRAITS>&operator<<(std::basic_ostream<char,TRAITS>&Stream,seek_mode Mode) 00099 { 00100 switch(Mode) 00101 { 00102 #define x(y) case seek_##y: Stream<<"seek_" #y; break 00103 x(beginning); 00104 x(current); 00105 x(end); 00106 #undef x 00107 default: 00108 assert(false); 00109 } 00110 00111 return Stream; 00112 } 00113 */ 00114 00115 END_LIBEBML_NAMESPACE 00116 00117 #endif // MATROSKA_IOCALLBACK_H