refactor(fd_streambuf): Cleanup

This commit is contained in:
Michael Carlberg 2016-12-31 15:42:46 +01:00
parent acb9ed910d
commit dd960cc5b5
5 changed files with 13 additions and 13 deletions

View File

@ -66,8 +66,8 @@ class fd_streambuf : public std::streambuf {
private: private:
file_descriptor m_fd; file_descriptor m_fd;
char m_out[BUFSIZ]{'\0'}; char m_out[BUFSIZ]{};
char m_in[BUFSIZ - 1]{'\0'}; char m_in[BUFSIZ - 1]{};
}; };
template <typename StreamType = std::ostream> template <typename StreamType = std::ostream>

View File

@ -272,7 +272,7 @@ void controller::read_events() {
// Process event on the internal fd // Process event on the internal fd
if (m_queuefd[PIPE_READ] && FD_ISSET(static_cast<int>(*m_queuefd[PIPE_READ]), &readfds)) { if (m_queuefd[PIPE_READ] && FD_ISSET(static_cast<int>(*m_queuefd[PIPE_READ]), &readfds)) {
char buffer[BUFSIZ]{'\0'}; char buffer[BUFSIZ];
if (read(static_cast<int>(*m_queuefd[PIPE_READ]), &buffer, BUFSIZ) == -1) { if (read(static_cast<int>(*m_queuefd[PIPE_READ]), &buffer, BUFSIZ) == -1) {
m_log.err("Failed to read from eventpipe (err: %s)", strerror(errno)); m_log.err("Failed to read from eventpipe (err: %s)", strerror(errno));
} }

View File

@ -52,8 +52,8 @@ file_descriptor::file_descriptor(const string& path, int flags) {
} }
file_descriptor::file_descriptor(int fd) : m_fd(fd) { file_descriptor::file_descriptor(int fd) : m_fd(fd) {
if (!*this) { if (m_fd != -1 && !*this) {
throw system_error("Given file descriptor is not valid"); throw system_error("Given file descriptor (" + to_string(m_fd) + ") is not valid");
} }
} }
@ -87,9 +87,7 @@ file_descriptor::operator bool() const {
} }
void file_descriptor::close() { void file_descriptor::close() {
if (m_fd == -1) { if (m_fd != -1 && ::close(m_fd) == -1) {
return;
} else if (::close(m_fd) == -1) {
throw system_error("Failed to close file descriptor"); throw system_error("Failed to close file descriptor");
} }
m_fd = -1; m_fd = -1;
@ -98,10 +96,12 @@ void file_descriptor::close() {
// }}} // }}}
// implementation of file_streambuf {{{ // implementation of file_streambuf {{{
fd_streambuf::fd_streambuf(int fd) : m_fd(fd) {} fd_streambuf::fd_streambuf(int fd) : m_fd(-1) {
open(fd);
}
fd_streambuf::~fd_streambuf() { fd_streambuf::~fd_streambuf() {
sync(); close();
} }
fd_streambuf::operator int() { fd_streambuf::operator int() {
@ -113,7 +113,7 @@ fd_streambuf::operator int() const {
void fd_streambuf::open(int fd) { void fd_streambuf::open(int fd) {
if (m_fd) { if (m_fd) {
sync(); close();
} }
m_fd = fd; m_fd = fd;
setg(m_in, m_in, m_in); setg(m_in, m_in, m_in);

View File

@ -1,3 +1,4 @@
#include "x11/fonts.hpp"
#include "components/logger.hpp" #include "components/logger.hpp"
#include "errors.hpp" #include "errors.hpp"
#include "utils/color.hpp" #include "utils/color.hpp"
@ -5,7 +6,6 @@
#include "utils/memory.hpp" #include "utils/memory.hpp"
#include "x11/connection.hpp" #include "x11/connection.hpp"
#include "x11/draw.hpp" #include "x11/draw.hpp"
#include "x11/fonts.hpp"
POLYBAR_NS POLYBAR_NS

View File

@ -1,11 +1,11 @@
#include <xcb/xcb_icccm.h> #include <xcb/xcb_icccm.h>
#include "components/types.hpp" #include "components/types.hpp"
#include "utils/memory.hpp"
#include "x11/atoms.hpp" #include "x11/atoms.hpp"
#include "x11/connection.hpp" #include "x11/connection.hpp"
#include "x11/extensions/randr.hpp" #include "x11/extensions/randr.hpp"
#include "x11/window.hpp" #include "x11/window.hpp"
#include "utils/memory.hpp"
POLYBAR_NS POLYBAR_NS