1 // Copyright 2010 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/process/systembuf.hpp"
35 #include "utils/auto_array.ipp"
36 #include "utils/sanity.hpp"
38 using utils::process::systembuf
;
41 /// Private implementation fields for systembuf.
42 struct systembuf::impl
{
43 /// File descriptor attached to the systembuf.
46 /// Size of the _read_buf and _write_buf buffers.
49 /// In-memory buffer for read operations.
50 utils::auto_array
< char > _read_buf
;
52 /// In-memory buffer for write operations.
53 utils::auto_array
< char > _write_buf
;
55 /// Initializes private implementation data.
57 /// \param fd The file descriptor.
58 /// \param bufsize The size of the created read and write buffers.
59 impl(const int fd
, const std::size_t bufsize
) :
62 _read_buf(new char[bufsize
]),
63 _write_buf(new char[bufsize
])
69 /// Constructs a new systembuf based on an open file descriptor.
71 /// This grabs ownership of the file descriptor.
73 /// \param fd The file descriptor to wrap. Must be open and valid.
74 /// \param bufsize The size to use for the internal read/write buffers.
75 systembuf::systembuf(const int fd
, std::size_t bufsize
) :
76 _pimpl(new impl(fd
, bufsize
))
78 setp(_pimpl
->_write_buf
.get(), _pimpl
->_write_buf
.get() + _pimpl
->_bufsize
);
82 /// Destroys a systembuf object.
84 /// \post The file descriptor attached to this systembuf is closed.
85 systembuf::~systembuf(void)
91 /// Reads new data when the systembuf read buffer underflows.
93 /// \return The new character to be read, or EOF if no more.
95 systembuf::underflow(void)
97 PRE(gptr() >= egptr());
100 ssize_t cnt
= ::read(_pimpl
->_fd
, _pimpl
->_read_buf
.get(),
102 ok
= (cnt
!= -1 && cnt
!= 0);
105 return traits_type::eof();
107 setg(_pimpl
->_read_buf
.get(), _pimpl
->_read_buf
.get(),
108 _pimpl
->_read_buf
.get() + cnt
);
109 return traits_type::to_int_type(*gptr());
114 /// Writes data to the file descriptor when the write buffer overflows.
116 /// \param c The character that causes the overflow.
118 /// \return EOF if error, some other value for success.
120 /// \throw something TODO(jmmv): According to the documentation, it is OK for
121 /// this method to throw in case of errors. Revisit this code to see if we
124 systembuf::overflow(int c
)
126 PRE(pptr() >= epptr());
128 return traits_type::eof();
129 if (!traits_type::eq_int_type(c
, traits_type::eof())) {
130 traits_type::assign(*pptr(), c
);
133 return traits_type::not_eof(c
);
137 /// Synchronizes the stream with the file descriptor.
139 /// \return 0 on success, -1 on error.
141 systembuf::sync(void)
143 ssize_t cnt
= pptr() - pbase();
146 ok
= ::write(_pimpl
->_fd
, pbase(), cnt
) == cnt
;