made all fdstreams RAII for fd
This commit is contained in:
parent
de371a94a5
commit
5012f2e582
28
fdstream.cpp
Normal file
28
fdstream.cpp
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
//
|
||||||
|
// fdstream.cpp
|
||||||
|
// libscio
|
||||||
|
//
|
||||||
|
// Created by Bob Polis at 2020-04-25
|
||||||
|
// Copyright (c) 2020 SwiftCoder. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "libscio.hpp"
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
sc::io::fdostream::~fdostream() {
|
||||||
|
if (_outbuf.fd() != -1) {
|
||||||
|
::close(_outbuf.fd());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sc::io::fdistream::~fdistream() {
|
||||||
|
if (_inbuf.fd() != -1) {
|
||||||
|
::close(_inbuf.fd());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sc::io::fdstream::~fdstream() {
|
||||||
|
if (_iobuf.fd() != -1) {
|
||||||
|
::close(_iobuf.fd());
|
||||||
|
}
|
||||||
|
}
|
@ -56,6 +56,8 @@ namespace sc {
|
|||||||
fdostream(int fd) : std::ostream(&_outbuf), _outbuf(fd) {}
|
fdostream(int fd) : std::ostream(&_outbuf), _outbuf(fd) {}
|
||||||
fdostream() : std::ostream(&_outbuf), _outbuf(-1) {}
|
fdostream() : std::ostream(&_outbuf), _outbuf(-1) {}
|
||||||
|
|
||||||
|
virtual ~fdostream();
|
||||||
|
|
||||||
int fd() const { return _outbuf.fd(); }
|
int fd() const { return _outbuf.fd(); }
|
||||||
void fd(int fd) { _outbuf.fd(fd); }
|
void fd(int fd) { _outbuf.fd(fd); }
|
||||||
|
|
||||||
@ -68,6 +70,8 @@ namespace sc {
|
|||||||
fdistream(int fd) : std::istream(&_inbuf), _inbuf(fd) {}
|
fdistream(int fd) : std::istream(&_inbuf), _inbuf(fd) {}
|
||||||
fdistream() : std::istream(&_inbuf), _inbuf(-1) {}
|
fdistream() : std::istream(&_inbuf), _inbuf(-1) {}
|
||||||
|
|
||||||
|
virtual ~fdistream();
|
||||||
|
|
||||||
int fd() const { return _inbuf.fd(); }
|
int fd() const { return _inbuf.fd(); }
|
||||||
void fd(int fd) { _inbuf.fd(fd); }
|
void fd(int fd) { _inbuf.fd(fd); }
|
||||||
|
|
||||||
@ -80,6 +84,8 @@ namespace sc {
|
|||||||
fdstream() : std::iostream(&_iobuf) {}
|
fdstream() : std::iostream(&_iobuf) {}
|
||||||
fdstream(int fd) : std::iostream(&_iobuf), _iobuf(fd) {}
|
fdstream(int fd) : std::iostream(&_iobuf), _iobuf(fd) {}
|
||||||
|
|
||||||
|
virtual ~fdstream();
|
||||||
|
|
||||||
int fd() const { return _iobuf.fd(); }
|
int fd() const { return _iobuf.fd(); }
|
||||||
void fd(int fd) { _iobuf.fd(fd); }
|
void fd(int fd) { _iobuf.fd(fd); }
|
||||||
|
|
||||||
|
@ -66,7 +66,6 @@ void test3() {
|
|||||||
throw_if_min1(fd = ::open("passwd", mode, perm));
|
throw_if_min1(fd = ::open("passwd", mode, perm));
|
||||||
sc::io::fdostream os {fd};
|
sc::io::fdostream os {fd};
|
||||||
os << passwd;
|
os << passwd;
|
||||||
throw_if_min1(::close(fd));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test4() {
|
void test4() {
|
||||||
@ -77,7 +76,6 @@ void test4() {
|
|||||||
while (getline(is, line)) {
|
while (getline(is, line)) {
|
||||||
cout << line << '\n';
|
cout << line << '\n';
|
||||||
}
|
}
|
||||||
throw_if_min1(::close(fd));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test5() {
|
void test5() {
|
||||||
@ -92,7 +90,6 @@ void test5() {
|
|||||||
if (getline(file, line)) {
|
if (getline(file, line)) {
|
||||||
cout << line << '\n';
|
cout << line << '\n';
|
||||||
}
|
}
|
||||||
throw_if_min1(::close(fd));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char * argv[]) {
|
int main(int argc, const char * argv[]) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user