1 /* ***** BEGIN LICENSE BLOCK *****
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
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
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef _STREAMREADER_HPP_
39 #define _STREAMREADER_HPP_
45 #include <common/dirac.h>
46 #include <common/util.hpp>
48 #include <parser/IInput.hpp>
58 namespace parser_project
60 //! An exception class for StreamReader
61 class StreamReaderException : public std::runtime_error
64 StreamReaderException ( std::string const& message )
65 : std::runtime_error ( message ) {}
68 //! A stream buffer class loosely based on streambuf to manage buffered reads
72 friend class ValidityChecker<StreamReader>;
75 typedef StreamReader class_type;
76 typedef std::vector<uint8> Buffer;
77 typedef Buffer::value_type value_type;
78 typedef Buffer::pointer pointer;
79 typedef Buffer::const_pointer const_pointer;
80 typedef Buffer::size_type size_type;
83 explicit StreamReader ( std::auto_ptr<IInput> input
84 , size_type putback_page_number = PUT_BACK_PAGE_N )
85 : input_ ( input ), buffer_ ( PAGE_SIZE * putback_page_number * BUFFER_SIZE )
86 , eback_ ( 0 ), gptr_ ( 0 ), egptr_ ( 0 ), global_offset_ ( 0 )
88 DIRAC_MESSAGE_ASSERT ( "buffer cannot be empty", !buffer_.empty() );
89 DIRAC_MESSAGE_ASSERT ( "input cannot be null", 0 != input_.get() );
91 eback_ = &buffer_[0] + PAGE_SIZE * putback_page_number;
92 gptr_ = egptr_ = eback_;
94 throw StreamReaderException ( "no data from input" );
97 ~StreamReader() { checkValidity(); }
98 private: // compiler generated methods are not allowed
99 StreamReader ( class_type const& );
100 class_type& operator= ( class_type const& );
105 ValidityChecker<class_type> checker ( *this );
106 size_type original_size = buffer_.size() - putback_size();
107 size_type target = original_size - sizeFwd();
108 if ( sizeFwd() < sizeBwd() ) {
109 // this is a safe cast
110 memcpy ( const_cast<pointer> ( eback_ ), gptr_, sizeFwd() );
113 // this is a safe cast
114 memmove (const_cast<pointer> ( eback_ ), gptr_, sizeFwd() );
116 size_type bytesread = 0;
117 while ( input_->isAvailable() && bytesread < target ) {
119 input_->read ( const_cast<pointer> ( eback_ ) + sizeFwd() + bytesread
120 , target - bytesread );
122 setg ( eback_, eback_, eback_ + sizeFwd() + bytesread );
123 global_offset_ += bytesread;
129 if ( !input_->backwardScanSupported() ) return false;
130 if ( sizeBwd() >static_cast<size_t> ( eback_ - &buffer_[0] ) )
131 throw StreamReaderException ( "Putback is too small, increase buffer size" );
132 memcpy ( &buffer_[0], eback_, sizeBwd() );
133 size_type target = buffer_.size() - sizeBwd() - putback_size();
134 if ( !input_->rewind ( target ) ) return false;
135 size_type bytesread = 0;
136 while ( input_->isAvailable() && bytesread < target ) {
138 bytesread += input_->read ( const_cast<pointer> ( eback_ ) + bytesread, target - bytesread );
141 memcpy ( const_cast<pointer> ( eback_ ) + bytesread, &buffer_[0], sizeBwd() );
143 , eback_ + bytesread + sizeBwd()
144 , eback_ + bytesread + sizeBwd() );
145 global_offset_ -= bytesread;
149 const_pointer eback() const { checkValidity(); return eback_; }
150 const_pointer& gptr() { checkValidity(); return gptr_; }
151 const_pointer egptr() const { checkValidity(); return egptr_; }
152 size_type size() const { checkValidity(); return egptr_ - eback_; }
153 size_type sizeFwd() const { checkValidity(); return egptr_ - gptr_; }
154 size_type sizeBwd() const { checkValidity(); return gptr_ - eback_; }
156 //! rewind the associated physical medium to the beginning of the stream
160 ValidityChecker<class_type> checker ( *this );
161 bool ret = input_->rewind();
163 setg ( eback(), eback(), eback() );
170 void swap ( class_type& rhs )
172 ValidityChecker<class_type> checker ( *this );
173 std::auto_ptr<IInput> tmp ( input_.release() );
176 buffer_.swap ( rhs.buffer_ );
177 std::swap ( eback_, rhs.eback_ );
178 std::swap ( gptr_, rhs.gptr_ );
179 std::swap ( egptr_, rhs.egptr_ );
180 std::swap ( global_offset_, rhs.global_offset_ );
183 int64 goffset() const
184 { checkValidity(); return global_offset_ - sizeFwd(); }
186 bool isAvailable() const
187 { checkValidity(); return input_->isAvailable(); }
190 void checkValidity() const
192 DIRAC_MESSAGE_ASSERT ( "internal pointer cannot be null"
193 , 0 != eback_ && 0 != gptr_ && 0 != egptr_ );
194 DIRAC_MESSAGE_ASSERT ( "eback must lie within buffer"
195 , !( eback_ < &buffer_[0] )
196 && !( eback_ > &buffer_[0] + buffer_.size() ) );
197 DIRAC_MESSAGE_ASSERT ( "egptr must lie within buffer"
198 , !( egptr_ < &buffer_[0] )
199 && !( egptr_ > &buffer_[0] + buffer_.size() ) );
200 DIRAC_MESSAGE_ASSERT ( "gptr must lie within eback and egptr"
201 , !( gptr_ < eback_) && !( gptr_ > egptr_ ) );
204 void setg ( const_pointer eback, const_pointer gptr
205 , const_pointer egptr )
212 size_type putback_size() const
214 return static_cast<size_type> ( eback_ - &buffer_[0] );
220 , PUT_BACK_PAGE_N = 500
224 std::auto_ptr<IInput> input_;
226 const_pointer eback_, gptr_, egptr_;
227 int64 global_offset_;
230 } /* parse_project */
235 #endif /* _STREAMREADER_HPP_ */