Parse files consisting of multiple concatenated sequences.
[dirac_parser.git] / src / decoder / .svn / text-base / FileReader.cpp.svn-base
blobcbb5cbc1a7997e61f6f6b37afd855178df154955
1 /* ***** BEGIN LICENSE BLOCK *****
3 * $Id:  $
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License
8 * Version 1.1 (the "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14 * the specific language governing rights and limitations under the License.
16 * The Original Code is BBC Research code.
18 * The Initial Developer of the Original Code is the British Broadcasting
19 * Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2007.
21 * All Rights Reserved.
23 * Contributor(s): Andrea Gabriellini (Original Author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
27 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
28 * the GPL or the LGPL are applicable instead of those above. If you wish to
29 * allow use of your version of this file only under the terms of the either
30 * the GPL or LGPL and not to allow others to use your version of this file
31 * under the MPL, indicate your decision by deleting the provisions above
32 * and replace them with the notice and other provisions required by the GPL
33 * or LGPL. If you do not delete the provisions above, a recipient may use
34 * your version of this file under the terms of any one of the MPL, the GPL
35 * or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
42 #include <decoder/FileReader.hpp>
44 using namespace ::dirac::decoder;
46 FileReaderException::FileReaderException ( std::string const& message )
47 : std::runtime_error ( message ) {}
49 std::string FileReader::error_message_ = "";
51 FileReader::FileReader ( std::string const& filename )
52 : IInput ( true ), str_ ( 0 )
54   if ( !filename.compare ( "-" ) ) { // pipe, can't rewind
55     bwdScan_ = false;
56     str_ = fopen ("/dev/stdin", "rb" );
57   }
58   else {
59     str_ = fopen ( filename.c_str(), "rb" );
60   }
61   if ( !str_ )
62   {
63     error_message_ = "Error while opening " + filename;
64     throw FileReaderException ( error_message_ );
65   }
68 FileReader::~FileReader() 
69
70   if ( str_ ) fclose ( str_ ); 
73 bool
74 FileReader::available() const 
75
76   return str_ && !ferror ( str_ ) && !feof ( str_ );
79 FileReader::size_type     
80 FileReader::readInput ( pointer begin, size_type count )
82   const size_type NO_DATA = 0;
83   if ( !available() ) return NO_DATA;
84   size_type bytesread = NO_DATA;
85   bytesread = fread ( static_cast<void*> ( begin )
86                     , sizeof ( value_type ), count, str_ );
87   if ( ferror ( str_ ) ) return NO_DATA;
88   return bytesread;
92 bool          
93 FileReader::rewindStream ( size_type count )
95   if ( !str_ || ferror ( str_ ) ) return false;
96   long pos = ftell ( str_ );
97   if ( count > static_cast<size_type> ( pos ) ) return false;
98   if ( fseek ( str_, -count, SEEK_CUR ) ) return false;
99   else return true;
102 bool
103 FileReader::fullRewind()
105   if ( !str_ || ferror ( str_ ) ) return false;
106   return ( !fseek ( str_, 0, SEEK_SET ) );