Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  
file_iterator.h
1 /***************************************************************************
2  copyright : (C) 2002-2008 by Stefano Barbato
3  email : stefano@codesink.org
4 
5  $Id: file_iterator.h,v 1.11 2008-10-27 18:30:42 tat Exp $
6  ***************************************************************************/
7 #ifndef _MIMETIC_OS_FILE_ITERATOR_H_
8 #define _MIMETIC_OS_FILE_ITERATOR_H_
9 #include <string>
10 #include <iterator>
11 
12 namespace mimetic
13 {
14 struct StdFile;
15 
16 struct ifile_iterator: public std::iterator<std::input_iterator_tag, char>
17 {
18  ifile_iterator();
19  ifile_iterator(StdFile* f);
20  ifile_iterator(const ifile_iterator&);
21  ifile_iterator& operator=(const ifile_iterator&);
22  ~ifile_iterator();
23  inline ifile_iterator& operator++();
24  inline ifile_iterator operator++(int);
25  inline reference operator*();
26  inline bool operator!=(const ifile_iterator& right) const;
27  inline bool operator==(const ifile_iterator& right) const;
28 private:
29  void cp(const ifile_iterator&);
30  void setBufsz();
31  enum { defBufsz = 4096 }; // default buffer size(4 missing getpagesize)
32  void underflow();
33  bool m_eof;
34  value_type* m_buf;
35  value_type* m_ptr;
36  int m_count;
37  StdFile* m_pFile;
38  unsigned int m_read; //bytes read so far
39  unsigned int m_bufsz;
40 };
41 
42 inline
43 ifile_iterator ifile_iterator::operator++(int) // postfix
44 {
45  ifile_iterator cp = *this;
46  operator++();
47  return cp;
48 }
49 
50 
51 inline
52 ifile_iterator& ifile_iterator::operator++() // prefix
53 {
54  if(--m_count > 0)
55  ++m_ptr;
56  else
57  underflow();
58  return *this;
59 }
60 
61 
62 inline
63 ifile_iterator::reference ifile_iterator::operator*()
64 {
65  return *m_ptr;
66 }
67 
68 inline
69 bool ifile_iterator::operator!=(const ifile_iterator& right) const
70 {
71  // always different except when both are EOF
72  return !operator==(right);
73 }
74 
75 
76 inline
77 bool ifile_iterator::operator==(const ifile_iterator& right) const
78 {
79  // are equal if both are EOF
80  return (m_eof && right.m_eof);
81 }
82 
83 }
84 
85 #endif