1
/* Copyright © 2003,2006 Roger Leigh <rleigh@debian.org>
3
* schroot is free software; you can redistribute it and/or modify it
4
* under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
8
* schroot is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18
*********************************************************************/
22
#include "sbuild-dirstream.h"
27
using namespace sbuild;
32
typedef std::pair<dirstream::error_code,const char *> emap;
35
* This is a list of the supported error codes. It's used to
36
* construct the real error codes map.
40
emap(dirstream::DIR_OPEN, N_("Failed to open directory")),
41
emap(dirstream::DIR_READ, N_("Failed to read directory"))
47
custom_error<dirstream::error_code>::map_type
48
custom_error<dirstream::error_code>::error_strings
50
init_errors + (sizeof(init_errors) / sizeof(init_errors[0])));
53
dirstream::dirstream(std::string const& dirname):
64
dirstream::~dirstream()
70
dirstream::open(std::string const& dirname)
72
this->dir = opendir(dirname.c_str());
75
this->dirname.clear();
76
this->error_status = true;
77
this->eof_status = true;
78
throw error(dirname, DIR_OPEN, errno);
80
this->dirname = dirname;
81
this->error_status = false;
82
this->eof_status = false;
87
dirstream::read(int quantity)
94
for (i = 0; i < quantity; ++i)
100
if (entry == 0) // EOF or error
102
//std::cerr << "Directory read error: ";
105
this->error_status = true;
106
throw error(this->dirname, DIR_READ, errno);
111
direntry newentry(entry); // make a direntry
112
this->data.push_back(newentry); // push onto the end of the list
116
// close the directory
117
// this also clears all the direntry data
122
closedir(this->dir); // don't throw an exception on failure -- it could
123
// be called in the destructor
125
this->data.clear(); // clear all data
126
this->dirname.clear();
127
this->error_status = true;
128
this->eof_status = true;
133
dirstream::eof() const
135
return this->eof_status;
139
dirstream::bad() const
141
return this->error_status;
144
sbuild::dirstream::operator bool ()
146
return !(bad() || eof());
150
sbuild::dirstream::operator ! ()
152
return bad() || eof();
157
sbuild::operator >> (dirstream& stream,
160
stream.read(); // read a new entry
161
if (stream && !stream.data.empty()) // not at end of file or bad.
163
entry = stream.data.front(); // assign next direntry to entry
164
stream.data.pop_front(); // remove the entry
166
else // blank the direntry and set EOF status
168
std::memset(&entry, 0, sizeof(direntry));
169
stream.eof_status = true;