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
17{
18 typedef std::input_iterator_tag iterator_category;
19 typedef char value_type;
20 typedef std::ptrdiff_t difference_type;
21 typedef char* pointer;
22 typedef char& reference;
23
24 ifile_iterator();
25 ifile_iterator(StdFile* f);
26 ifile_iterator(const ifile_iterator&);
27 ifile_iterator& operator=(const ifile_iterator&);
28 ~ifile_iterator();
29 inline ifile_iterator& operator++();
30 inline ifile_iterator operator++(int);
31 inline reference operator*();
32 inline bool operator!=(const ifile_iterator& right) const;
33 inline bool operator==(const ifile_iterator& right) const;
34 private:
35 void cp(const ifile_iterator&);
36 void setBufsz();
37 enum { defBufsz = 4096 }; // default buffer size(4 missing getpagesize)
38 void underflow();
39 bool m_eof;
40 value_type* m_buf;
41 value_type* m_ptr;
42 int m_count;
43 StdFile* m_pFile;
44 unsigned int m_read; //bytes read so far
45 unsigned int m_bufsz;
46};
47
48 inline
49ifile_iterator ifile_iterator::operator++(int) // postfix
50{
51 ifile_iterator cp = *this;
52 operator++();
53 return cp;
54}
55
56
57 inline
58ifile_iterator& ifile_iterator::operator++() // prefix
59{
60 if(--m_count > 0)
61 ++m_ptr;
62 else
63 underflow();
64 return *this;
65}
66
67
68 inline
69ifile_iterator::reference ifile_iterator::operator*()
70{
71 return *m_ptr;
72}
73
74 inline
75 bool ifile_iterator::operator!=(const ifile_iterator& right) const
76{
77 // always different except when both are EOF
78 return !operator==(right);
79}
80
81
82 inline
83 bool ifile_iterator::operator==(const ifile_iterator& right) const
84{
85 // are equal if both are EOF
86 return (m_eof && right.m_eof);
87}
88
89}
90
91#endif
mimetic
Definition body.h:18