Parse files consisting of multiple concatenated sequences.
[dirac_parser.git] / src / parser / .svn / text-base / StreamReader.hpp.svn-base
blob8129d768ab384ba62f7a70bf224453a5ecb26df5
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 #ifndef _STREAMREADER_HPP_
39 #define _STREAMREADER_HPP_
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
45 #include <common/dirac.h>
46 #include <common/util.hpp>
48 #include <parser/IInput.hpp>
50 #include <vector>
51 #include <memory>
52 #include <stdexcept>
53 #include <cstring>
56 namespace dirac
58 namespace parser_project
60   //! An exception class for StreamReader
61   class StreamReaderException : public std::runtime_error 
62   {
63   public:
64     StreamReaderException ( std::string const& message )
65     : std::runtime_error ( message ) {}
66   };
68   //! A stream buffer class loosely based on streambuf to manage buffered reads 
69   //! from any medium
70   class StreamReader
71   {
72     friend class ValidityChecker<StreamReader>;
73     
74   public: 
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;
81   
82   public: 
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 )
87     {
88       DIRAC_MESSAGE_ASSERT  ( "buffer cannot be empty", !buffer_.empty() );
89       DIRAC_MESSAGE_ASSERT  ( "input cannot be null", 0 != input_.get() );
90       
91       eback_ = &buffer_[0] + PAGE_SIZE * putback_page_number;
92       gptr_ = egptr_ = eback_;
93       if ( !addDataFwd() ) 
94         throw StreamReaderException ( "no data from input" );
95       checkValidity(); 
96     }
97     ~StreamReader() { checkValidity(); }
98   private: // compiler generated methods are not allowed
99     StreamReader ( class_type const& );
100     class_type& operator= ( class_type const& );
101     
102   public:
103     bool                addDataFwd()
104     {
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() );
111       }
112       else {
113         // this is a safe cast
114         memmove (const_cast<pointer> ( eback_ ), gptr_, sizeFwd() );
115       }
116       size_type bytesread = 0;
117       while ( input_->isAvailable() && bytesread < target ) {
118         bytesread += 
119           input_->read ( const_cast<pointer> ( eback_ ) + sizeFwd() + bytesread
120                        , target - bytesread );
121       }
122       setg  ( eback_, eback_, eback_ + sizeFwd() + bytesread );
123       global_offset_ += bytesread;
124       return bytesread;
125     }
126     
127     bool                addDataBwd()
128     {
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 ) {
137         // this cast is safe
138         bytesread += input_->read ( const_cast<pointer> ( eback_ ) + bytesread, target - bytesread );
139       }
140       // this cast is safe
141       memcpy ( const_cast<pointer> ( eback_ ) + bytesread, &buffer_[0], sizeBwd() );
142       setg  ( eback_
143             , eback_ + bytesread + sizeBwd()
144             , eback_ + bytesread + sizeBwd() );
145       global_offset_ -= bytesread;
146       return bytesread;
147     }
148     
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
157     //! if possible
158     bool              rewind()
159     {
160       ValidityChecker<class_type> checker ( *this );
161       bool ret = input_->rewind();
162       if ( ret ) {
163         setg ( eback(), eback(), eback() );
164         global_offset_ = 0;
165         return addDataFwd();
166       }
167       else return false;
168     }
169     
170     void              swap ( class_type& rhs )
171     {
172       ValidityChecker<class_type> checker ( *this );
173       std::auto_ptr<IInput> tmp ( input_.release() );
174       input_ = rhs.input_;
175       rhs.input_ = tmp;
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_ );
181     }
182     
183     int64             goffset() const
184     { checkValidity(); return global_offset_ - sizeFwd(); }
185     
186     bool              isAvailable() const
187     { checkValidity(); return input_->isAvailable(); }
189   private: 
190     void              checkValidity() const
191     {
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_ ) );
202     }
204     void              setg  ( const_pointer eback, const_pointer gptr
205                             , const_pointer egptr )
206     {
207       eback_  = eback;
208       gptr_   = gptr;
209       egptr_  = egptr;
210     }
211     
212     size_type         putback_size() const
213     {
214       return static_cast<size_type> ( eback_ - &buffer_[0] );
215     }
216     
217   private:
218     enum {
219         PAGE_SIZE       = 4096
220       , PUT_BACK_PAGE_N = 500
221       , BUFFER_SIZE     = 10
222     };
224     std::auto_ptr<IInput>       input_;
225     Buffer                      buffer_;
226     const_pointer               eback_, gptr_, egptr_;
227     int64                       global_offset_;
228   };
229     
230 } /* parse_project */
232 } /* dirac */
235 #endif /* _STREAMREADER_HPP_ */