include/wipal/pcap/frame_iterator.hxx

00001 /*
00002  * WiPal - A library and a set of tools to manipulate wireless traces.
00003  * Copyright (C) 2007  Universite Pierre et Marie Curie - Paris 6
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018  * MA  02110-1301  USA
00019  *
00020  * Author: Thomas Claveirole <thomas.claveirole@lip6.fr>
00021  */
00022 #ifndef PCAP_FRAME_ITERATOR_HXX_
00023 # define PCAP_FRAME_ITERATOR_HXX_
00024 
00025 # include <fstream>
00026 # include <stdexcept>
00027 # include <memory>
00028 # include <cassert>
00029 
00030 # include "frame_iterator.hh"
00031 # include <wipal/tool/exceptions.hh>
00032 
00033 namespace pcapxx
00034 {
00035 
00036   namespace internals
00037   {
00038 
00039     template <class B>
00040     frame_iterator<B>::frame_iterator(const iterable_type& desc, bool end):
00041       super_type (),
00042       desc_ (&desc)
00043     {
00044       if (not end)
00045         {
00046           pos_ = sizeof (file_header);
00047           count_ = 0;
00048           increment();
00049         }
00050     }
00051 
00052     template <class B>
00053     frame_iterator<B>::frame_iterator(const iterable_type&      desc,
00054                                       const std::streampos&     pos,
00055                                       unsigned                  count):
00056       super_type (),
00057       desc_ (&desc),
00058       pos_ (pos),
00059       count_ (count)
00060     {
00061       increment();
00062     }
00063 
00064     template <class B>
00065     frame_iterator<B>::frame_iterator(const frame_iterator& it):
00066       super_type (),
00067       desc_ (it.desc_)
00068     {
00069       if (it.value())
00070         {
00071           pos_ = it.pos_;
00072           count_ = it.count_;
00073           this->value() = it.value();
00074         }
00075     }
00076 
00077     template <class B>
00078     frame_iterator<B>&
00079     frame_iterator<B>::operator = (const frame_iterator& rhs)
00080     {
00081       if (&rhs == this)
00082         return *this;
00083 
00084       desc_ = rhs.desc_;
00085       input_.release();
00086       if ((this->value() = rhs.value()))
00087         {
00088           pos_ = rhs.pos_;
00089           count_ = rhs.count_;
00090         }
00091       return *this;
00092     }
00093 
00094     template <class B>
00095     bool
00096     frame_iterator<B>::equal(const frame_iterator& rhs) const
00097     {
00098       if (not rhs.value())
00099         return not this->value();
00100       return (this->value() and
00101               pos_ == rhs.pos_ and
00102               desc_->file_name() == rhs.desc_->file_name());
00103     }
00104 
00105     template <class B>
00106     void
00107     frame_iterator<B>::increment()
00108     {
00109       try
00110         {
00111           this->value() = frame_descriptor (stream(),
00112                                             ++count_,
00113                                             desc_->swapped(),
00114                                             desc_->zone(),
00115                                             &pos_);
00116         }
00117       catch (const tool::read_error&)
00118         {
00119           if (stream().eof())
00120             this->value() = boost::none_t ();
00121           else
00122             throw;
00123         }
00124     }
00125 
00126     template <class B>
00127     std::istream&
00128     frame_iterator<B>::stream()
00129     {
00130       if (not input_.get())
00131         {
00132           input_.reset(new std::ifstream (desc_->file_name().c_str()));
00133           if (input_->fail())
00134             throw tool::file_error ("Open failed");
00135           if (not input_->seekg(pos_, std::ios::beg))
00136             throw tool::seek_error("Seek operation failed");
00137         }
00138       return *input_.get();
00139     }
00140 
00141   } // End of namespace pcapxx::internals.
00142 
00143 } // End of namespace pcapxx.
00144 
00145 #endif // ! PCAP_FRAME_ITERATOR_HXX_

Generated on Tue Jan 15 19:32:31 2008 for wipal by  doxygen 1.5.4