/[debian]/mimetic/branches/upstream/current/mimetic/os/file_iterator.cxx
ViewVC logotype

Contents of /mimetic/branches/upstream/current/mimetic/os/file_iterator.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 128 - (show annotations)
Sat Feb 25 16:49:20 2006 UTC (15 years, 5 months ago) by gregoa
File size: 3021 byte(s)
[svn-inject] Installing original source of mimetic
1 /***************************************************************************
2 copyright : (C) 2002-2005 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: file_iterator.cxx,v 1.2 2005/02/23 10:26:15 tat Exp $
6 ***************************************************************************/
7
8 /***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16 #include <iostream>
17 #include <iomanip>
18 #include <iterator>
19 #include <fstream>
20 #include <sys/stat.h>
21 #include <stdio.h>
22 #include <time.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <mimetic/libconfig.h>
26 #include <mimetic/os/file_iterator.h>
27 #include <mimetic/os/file.h>
28 #include <mimetic/libconfig.h>
29 #ifdef HAVE_DIRENT_H
30 #include <dirent.h>
31 #endif
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #ifdef HAVE_SYS_TIME_H
36 #include <sys/time.h>
37 #endif
38
39 namespace mimetic
40 {
41
42 using namespace std;
43
44 ifile_iterator::ifile_iterator()
45 : m_eof(1), m_buf(0), m_ptr(0), m_count(0), m_pFile(0), m_read(0)
46 {
47 setBufsz();
48 }
49
50
51 ifile_iterator::ifile_iterator(StdFile* pFile)
52 : m_eof(0), m_buf(0), m_ptr(0), m_count(0), m_pFile(pFile), m_read(0)
53 {
54 setBufsz();
55 if(m_pFile == 0)
56 {
57 m_eof = 1;
58 return;
59 }
60 m_ptr = m_buf = new value_type[m_bufsz];
61 underflow();
62 }
63
64 void ifile_iterator::setBufsz()
65 {
66 #ifdef HAVE_GETPAGESIZE
67 m_bufsz = getpagesize();
68 #else
69 m_bufsz = defBufsz;
70 #endif
71 }
72
73 void ifile_iterator::cp(const ifile_iterator& r)
74 {
75 if(m_buf)
76 delete[] m_buf;
77 m_eof = 1;
78 m_buf = m_ptr = 0;
79 m_count = m_read = 0;
80 m_pFile = 0;
81 if(r.m_eof || r.m_pFile == 0)
82 return;
83 m_eof = r.m_eof;
84 m_count = r.m_count;
85 m_pFile = r.m_pFile;
86 m_read = r.m_read;
87 m_bufsz = r.m_bufsz;
88
89 m_ptr = m_buf = new value_type[m_bufsz];
90 for(int i = 0; i < m_count; ++i)
91 m_ptr[i] = r.m_ptr[i];
92 return;
93 }
94
95 ifile_iterator& ifile_iterator::operator=(const ifile_iterator& r)
96 {
97 cp(r);
98 return *this;
99 }
100
101 ifile_iterator::ifile_iterator(const ifile_iterator& r)
102 : m_buf(0)
103 {
104 cp(r);
105 }
106
107
108 ifile_iterator::~ifile_iterator()
109 {
110 if(m_buf)
111 delete[] m_buf;
112 if(m_pFile == 0)
113 return;
114 m_eof = 1;
115 m_pFile = 0;
116 }
117
118
119 void ifile_iterator::underflow()
120 {
121 if(m_eof)
122 return;
123 m_count = m_pFile->read(m_buf, m_bufsz);
124 if(m_count > 0)
125 {
126 m_ptr = m_buf;
127 m_read += m_count;
128 } else {
129 m_count = 0;
130 m_eof = 1;
131 }
132 }
133
134 }
135

  ViewVC Help
Powered by ViewVC 1.1.26