Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  
file_iterator.h
00001 /***************************************************************************
00002     copyright            : (C) 2002-2008 by Stefano Barbato
00003     email                : stefano@codesink.org
00004 
00005     $Id: file_iterator.h,v 1.11 2008-10-27 18:30:42 tat Exp $
00006  ***************************************************************************/
00007 #ifndef _MIMETIC_OS_FILE_ITERATOR_H_
00008 #define _MIMETIC_OS_FILE_ITERATOR_H_
00009 #include <string>
00010 #include <iterator>
00011 
00012 namespace mimetic
00013 {
00014 struct StdFile;
00015 
00016 struct ifile_iterator: public std::iterator<std::input_iterator_tag, char>
00017 {
00018     ifile_iterator();    
00019     ifile_iterator(StdFile* f);
00020     ifile_iterator(const ifile_iterator&);
00021     ifile_iterator& operator=(const ifile_iterator&);
00022     ~ifile_iterator();    
00023     inline ifile_iterator& operator++();
00024     inline ifile_iterator operator++(int);
00025     inline reference operator*();
00026     inline bool operator!=(const ifile_iterator& right) const;
00027     inline bool operator==(const ifile_iterator& right) const;
00028 private:
00029     void cp(const ifile_iterator&);
00030     void setBufsz();
00031     enum { defBufsz = 4096 }; // default buffer size(4 missing getpagesize)
00032     void underflow();
00033     bool m_eof;
00034     value_type* m_buf;
00035     value_type* m_ptr;
00036     int m_count;
00037     StdFile* m_pFile;
00038     unsigned int m_read; //bytes read so far
00039     unsigned int m_bufsz;
00040 };
00041 
00042 inline
00043 ifile_iterator ifile_iterator::operator++(int) // postfix
00044 {
00045     ifile_iterator cp = *this;
00046     operator++();
00047     return cp;
00048 }
00049 
00050 
00051 inline
00052 ifile_iterator& ifile_iterator::operator++() // prefix
00053 {
00054     if(--m_count > 0)
00055         ++m_ptr;
00056     else
00057         underflow();
00058     return *this;
00059 }
00060 
00061 
00062 inline
00063 ifile_iterator::reference ifile_iterator::operator*()
00064 {
00065     return *m_ptr;
00066 }
00067 
00068 inline
00069 bool ifile_iterator::operator!=(const ifile_iterator& right) const
00070 {
00071     // always different except when both are EOF
00072     return !operator==(right);
00073 }
00074 
00075 
00076 inline
00077 bool ifile_iterator::operator==(const ifile_iterator& right) const
00078 {
00079     // are equal if both are EOF
00080     return (m_eof && right.m_eof);
00081 }
00082 
00083 }
00084 
00085 #endif