3rdparty/licenseReport: Add seperate LGPL checks
[haiku.git] / src / add-ons / media / plugins / matroska / libebml / StdIOCallback.cpp
blob6bb43b17dd16336d812e8c858825b7e3be00eeaa
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
22 ** See http://www.matroska.org/license/lgpl/ for LGPL licensing information.
24 ** Contact license@matroska.org if any conditions of this licensing are
25 ** not clear to you.
27 **********************************************************************/
29 /*!
30 \file
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>
36 #include <cassert>
37 #include <climits>
38 #if !defined(__GNUC__) || (__GNUC__ > 2)
39 #include <sstream>
40 #endif // GCC2
42 #include "ebml/StdIOCallback.h"
43 #include "ebml/Debug.h"
44 #include "ebml/EbmlConfig.h"
46 using namespace std;
48 START_LIBEBML_NAMESPACE
50 CRTError::CRTError(int nError, const std::string & Description)
51 :std::runtime_error(Description+": "+strerror(nError))
52 ,Error(Error)
56 CRTError::CRTError(const std::string & Description,int nError)
57 :std::runtime_error(Description+": "+strerror(nError))
58 ,Error(Error)
63 StdIOCallback::StdIOCallback(const char*Path, const open_mode aMode)
65 assert(Path!=0);
67 const char *Mode;
68 switch (aMode)
70 case MODE_READ:
71 Mode = "rb";
72 break;
73 case MODE_SAFE:
74 Mode = "rb+";
75 break;
76 case MODE_WRITE:
77 Mode = "wb";
78 break;
79 case MODE_CREATE:
80 Mode = "wb+";
81 break;
82 default:
83 throw 0;
86 File=fopen(Path,Mode);
87 if(File==0)
89 #if !defined(__GNUC__) || (__GNUC__ > 2)
90 stringstream Msg;
91 Msg<<"Can't open stdio file \""<<Path<<"\" in mode \""<<Mode<<"\"";
92 throw CRTError(Msg.str());
93 #endif // GCC2
95 mCurrentPosition = 0;
99 StdIOCallback::~StdIOCallback()throw()
101 close();
106 uint32 StdIOCallback::read(void*Buffer,size_t Size)
108 assert(File!=0);
110 size_t result = fread(Buffer, 1, Size, File);
111 mCurrentPosition += result;
112 return result;
115 void StdIOCallback::setFilePointer(int64 Offset,seek_mode Mode)
117 assert(File!=0);
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)
134 ostringstream Msg;
135 Msg<<"Failed to seek file "<<File<<" to offset "<<(unsigned long)Offset<<" in mode "<<Mode;
136 throw CRTError(Msg.str());
137 #endif // GCC2
138 mCurrentPosition = ftell(File);
140 else
142 switch ( Mode )
144 case SEEK_CUR:
145 mCurrentPosition += Offset;
146 break;
147 case SEEK_END:
148 mCurrentPosition = ftell(File);
149 break;
150 case SEEK_SET:
151 mCurrentPosition = Offset;
152 break;
157 size_t StdIOCallback::write(const void*Buffer,size_t Size)
159 assert(File!=0);
160 uint32 Result = fwrite(Buffer,1,Size,File);
161 mCurrentPosition += Result;
162 return Result;
165 uint64 StdIOCallback::getFilePointer()
167 assert(File!=0);
169 #if 0
170 long Result=ftell(File);
171 if(Result<0)
173 #if !defined(__GNUC__) || (__GNUC__ > 2)
174 stringstream Msg;
175 Msg<<"Can't tell the current file pointer position for "<<File;
176 throw CRTError(Msg.str());
177 #endif // GCC2
179 #endif
181 return mCurrentPosition;
184 void StdIOCallback::close()
186 if(File==0)
187 return;
189 if(fclose(File)!=0)
191 #if !defined(__GNUC__) || (__GNUC__ > 2)
192 stringstream Msg;
193 Msg<<"Can't close file "<<File;
194 throw CRTError(Msg.str());
195 #endif // GCC2
198 File=0;
201 END_LIBEBML_NAMESPACE