Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  

file_iterator.h

00001 /***************************************************************************
00002     copyright            : (C) 2002-2005 by Stefano Barbato
00003     email                : stefano@codesink.org
00004 
00005     $Id: file_iterator.h,v 1.9 2005/02/23 10:26:15 tat Exp $
00006  ***************************************************************************/
00007 
00008 /***************************************************************************
00009  *                                                                         *
00010  *   This program is free software; you can redistribute it and/or modify  *
00011  *   it under the terms of the GNU General Public License as published by  *
00012  *   the Free Software Foundation; either version 2 of the License, or     *
00013  *   (at your option) any later version.                                   *
00014  *                                                                         *
00015  ***************************************************************************/
00016 #ifndef _MIMETIC_OS_FILE_ITERATOR_H_
00017 #define _MIMETIC_OS_FILE_ITERATOR_H_
00018 #include <string>
00019 #include <iterator>
00020 
00021 namespace mimetic
00022 {
00023 struct StdFile;
00024 
00025 struct ifile_iterator: public std::iterator<std::input_iterator_tag, char>
00026 {
00027     ifile_iterator();    
00028     ifile_iterator(StdFile* f);
00029     ifile_iterator(const ifile_iterator&);
00030     ifile_iterator& operator=(const ifile_iterator&);
00031     ~ifile_iterator();    
00032     inline ifile_iterator& operator++();
00033     inline ifile_iterator operator++(int);
00034     inline reference operator*();
00035     inline bool operator!=(const ifile_iterator& right) const;
00036     inline bool operator==(const ifile_iterator& right) const;
00037 private:
00038     void cp(const ifile_iterator&);
00039     void setBufsz();
00040     enum { defBufsz = 4096 }; // default buffer size(4 missing getpagesize)
00041     void underflow();
00042     bool m_eof;
00043     value_type* m_buf;
00044     value_type* m_ptr;
00045     int m_count;
00046     StdFile* m_pFile;
00047     unsigned int m_read; //bytes read so far
00048     unsigned int m_bufsz;
00049 };
00050 
00051 inline
00052 ifile_iterator ifile_iterator::operator++(int) // postfix
00053 {
00054     ifile_iterator cp = *this;
00055     operator++();
00056     return cp;
00057 }
00058 
00059 
00060 inline
00061 ifile_iterator& ifile_iterator::operator++() // prefix
00062 {
00063     if(--m_count > 0)
00064         ++m_ptr;
00065     else
00066         underflow();
00067     return *this;
00068 }
00069 
00070 
00071 inline
00072 ifile_iterator::reference ifile_iterator::operator*()
00073 {
00074     return *m_ptr;
00075 }
00076 
00077 inline
00078 bool ifile_iterator::operator!=(const ifile_iterator& right) const
00079 {
00080     // always different except when both are EOF
00081     return !operator==(right);
00082 }
00083 
00084 
00085 inline
00086 bool ifile_iterator::operator==(const ifile_iterator& right) const
00087 {
00088     return m_eof == right.m_eof == 1;
00089 }
00090 
00091 }
00092 
00093 #endif