1 /****************************************************************************
2 ** libebml : parse EBML files, see http://embl.sourceforge.net/
4 ** <file/class description>
6 ** Copyright (C) 2002-2004 Ingo Ralf Blum. All rights reserved.
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.
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.
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
22 ** See http://www.matroska.org/license/lgpl/ for LGPL licensing information.
24 ** Contact license@matroska.org if any conditions of this licensing are
27 **********************************************************************/
31 \version \$Id: StdIOCallback.cpp 1298 2008-02-21 22:14:18Z mosu $
32 \author Steve Lhomme <robux4 @ users.sf.net>
33 \author Moritz Bunkus <moritz @ bunkus.org>
38 #if !defined(__GNUC__) || (__GNUC__ > 2)
42 #include "ebml/StdIOCallback.h"
43 #include "ebml/Debug.h"
44 #include "ebml/EbmlConfig.h"
48 START_LIBEBML_NAMESPACE
50 CRTError::CRTError(int nError
, const std::string
& Description
)
51 :std::runtime_error(Description
+": "+strerror(nError
))
56 CRTError::CRTError(const std::string
& Description
,int nError
)
57 :std::runtime_error(Description
+": "+strerror(nError
))
63 StdIOCallback::StdIOCallback(const char*Path
, const open_mode aMode
)
86 File
=fopen(Path
,Mode
);
89 #if !defined(__GNUC__) || (__GNUC__ > 2)
91 Msg
<<"Can't open stdio file \""<<Path
<<"\" in mode \""<<Mode
<<"\"";
92 throw CRTError(Msg
.str());
99 StdIOCallback::~StdIOCallback()throw()
106 uint32
StdIOCallback::read(void*Buffer
,size_t Size
)
110 size_t result
= fread(Buffer
, 1, Size
, File
);
111 mCurrentPosition
+= result
;
115 void StdIOCallback::setFilePointer(int64 Offset
,seek_mode Mode
)
119 // There is a numeric cast in the boost library, which would be quite nice for this checking
121 SL : replaced because unknown class in cygwin
122 assert(Offset <= numeric_limits<long>::max());
123 assert(Offset >= numeric_limits<long>::min());
126 assert(Offset
<= LONG_MAX
);
127 assert(Offset
>= LONG_MIN
);
129 assert(Mode
==SEEK_CUR
||Mode
==SEEK_END
||Mode
==SEEK_SET
);
131 if(fseek(File
,Offset
,Mode
)!=0)
133 #if !defined(__GNUC__) || (__GNUC__ > 2)
135 Msg
<<"Failed to seek file "<<File
<<" to offset "<<(unsigned long)Offset
<<" in mode "<<Mode
;
136 throw CRTError(Msg
.str());
138 mCurrentPosition
= ftell(File
);
145 mCurrentPosition
+= Offset
;
148 mCurrentPosition
= ftell(File
);
151 mCurrentPosition
= Offset
;
157 size_t StdIOCallback::write(const void*Buffer
,size_t Size
)
160 uint32 Result
= fwrite(Buffer
,1,Size
,File
);
161 mCurrentPosition
+= Result
;
165 uint64
StdIOCallback::getFilePointer()
170 long Result
=ftell(File
);
173 #if !defined(__GNUC__) || (__GNUC__ > 2)
175 Msg
<<"Can't tell the current file pointer position for "<<File
;
176 throw CRTError(Msg
.str());
181 return mCurrentPosition
;
184 void StdIOCallback::close()
191 #if !defined(__GNUC__) || (__GNUC__ > 2)
193 Msg
<<"Can't close file "<<File
;
194 throw CRTError(Msg
.str());
201 END_LIBEBML_NAMESPACE