2 // Automated Testing Framework (atf)
4 // Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "atf-c/error.h"
41 #include "atf-c++/exceptions.hpp"
42 #include "atf-c++/io.hpp"
43 #include "atf-c++/sanity.hpp"
45 namespace impl
= atf::io
;
46 #define IMPL_NAME "atf::io"
48 // ------------------------------------------------------------------------
49 // The "file_handle" class.
50 // ------------------------------------------------------------------------
52 impl::file_handle::file_handle(void) :
53 m_handle(invalid_value())
57 impl::file_handle::file_handle(handle_type h
) :
60 PRE(m_handle
!= invalid_value());
63 impl::file_handle::file_handle(const file_handle
& fh
) :
66 fh
.m_handle
= invalid_value();
69 impl::file_handle::~file_handle(void)
76 impl::file_handle::operator=(const file_handle
& fh
)
78 m_handle
= fh
.m_handle
;
79 fh
.m_handle
= invalid_value();
85 impl::file_handle::is_valid(void)
88 return m_handle
!= invalid_value();
92 impl::file_handle::close(void)
98 m_handle
= invalid_value();
101 impl::file_handle::handle_type
102 impl::file_handle::disown(void)
106 handle_type h
= m_handle
;
107 m_handle
= invalid_value();
111 impl::file_handle::handle_type
112 impl::file_handle::get(void)
121 impl::file_handle::posix_remap(handle_type h
)
128 if (::dup2(m_handle
, h
) == -1)
129 throw system_error(IMPL_NAME
"::file_handle::posix_remap",
130 "dup2(2) failed", errno
);
132 if (::close(m_handle
) == -1) {
134 throw system_error(IMPL_NAME
"::file_handle::posix_remap",
135 "close(2) failed", errno
);
141 impl::file_handle::handle_type
142 impl::file_handle::invalid_value(void)
147 // ------------------------------------------------------------------------
148 // The "systembuf" class.
149 // ------------------------------------------------------------------------
151 impl::systembuf::systembuf(handle_type h
, std::size_t bufsize
) :
161 m_read_buf
= new char[bufsize
];
162 m_write_buf
= new char[bufsize
];
164 if (m_read_buf
!= NULL
)
165 delete [] m_read_buf
;
166 if (m_write_buf
!= NULL
)
167 delete [] m_write_buf
;
171 setp(m_write_buf
, m_write_buf
+ m_bufsize
);
174 impl::systembuf::~systembuf(void)
176 sync(); // XXX Unsure if this is correct. But seems to be needed.
177 delete [] m_read_buf
;
178 delete [] m_write_buf
;
181 impl::systembuf::int_type
182 impl::systembuf::underflow(void)
184 PRE(gptr() >= egptr());
187 ssize_t cnt
= ::read(m_handle
, m_read_buf
, m_bufsize
);
188 ok
= (cnt
!= -1 && cnt
!= 0);
191 return traits_type::eof();
193 setg(m_read_buf
, m_read_buf
, m_read_buf
+ cnt
);
194 return traits_type::to_int_type(*gptr());
198 impl::systembuf::int_type
199 impl::systembuf::overflow(int c
)
201 PRE(pptr() >= epptr());
203 return traits_type::eof();
204 if (!traits_type::eq_int_type(c
, traits_type::eof())) {
205 traits_type::assign(*pptr(), c
);
208 return traits_type::not_eof(c
);
212 impl::systembuf::sync(void)
214 ssize_t cnt
= pptr() - pbase();
217 ok
= ::write(m_handle
, pbase(), cnt
) == cnt
;
224 // ------------------------------------------------------------------------
226 // ------------------------------------------------------------------------
228 impl::pipe::pipe(void)
230 file_handle::handle_type hs
[2];
232 if (::pipe(hs
) == -1)
233 throw system_error(IMPL_NAME
"::pipe::pipe",
234 "pipe(2) failed", errno
);
236 m_read_end
= file_handle(hs
[0]);
237 m_write_end
= file_handle(hs
[1]);
241 impl::pipe::rend(void)
247 impl::pipe::wend(void)
252 // ------------------------------------------------------------------------
253 // The "pistream" class.
254 // ------------------------------------------------------------------------
256 impl::pistream::pistream(impl::file_handle
& fh
) :
259 m_systembuf(m_handle
.get())
265 impl::pistream::close(void)
271 impl::pistream::handle(void)
276 // ------------------------------------------------------------------------
277 // The "postream" class.
278 // ------------------------------------------------------------------------
280 impl::postream::postream(impl::file_handle
& fh
) :
283 m_systembuf(m_handle
.get())
289 impl::postream::close(void)
295 impl::postream::handle(void)
300 // ------------------------------------------------------------------------
301 // The "pollable_istream" class.
302 // ------------------------------------------------------------------------
304 impl::unbuffered_istream::unbuffered_istream(impl::file_handle
& fh
) :
311 impl::unbuffered_istream::get_fh(void)
317 impl::unbuffered_istream::good(void)
324 impl::unbuffered_istream::read(void* buf
, size_t buflen
)
329 ssize_t res
= ::read(m_fh
.get(), buf
, buflen
);
331 return res
> 0 ? res
: 0;
335 impl::unbuffered_istream::close(void)
341 // ------------------------------------------------------------------------
343 // ------------------------------------------------------------------------
345 impl::unbuffered_istream
&
346 impl::getline(unbuffered_istream
& uis
, std::string
& str
)
350 while (uis
.read(&ch
, sizeof(ch
)) == sizeof(ch
) && ch
!= '\n') {
358 impl::cmp(const fs::path
& p1
, const fs::path
& p2
)
362 atf_error_t err
= atf_io_cmp(&r
, p1
.c_path(), p2
.c_path());
363 if (atf_is_error(err
))
364 throw_atf_error(err
);