aboutsummaryrefslogtreecommitdiff
path: root/io.h
diff options
context:
space:
mode:
Diffstat (limited to 'io.h')
-rw-r--r--io.h27
1 files changed, 17 insertions, 10 deletions
diff --git a/io.h b/io.h
index c9ce30e..87778ec 100644
--- a/io.h
+++ b/io.h
@@ -10,20 +10,27 @@
namespace io {
-struct FileDescriptorGuard {
- FileDescriptorGuard(const std::string& path) {
- this->fd = open(path.c_str(), O_WRONLY | O_APPEND);
+class FileDescriptorGuard {
+ public:
+ FileDescriptorGuard(const std::string& path) {
+ this->fd = open(path.c_str(), O_WRONLY | O_APPEND);
+
+ if ( !this->fd ) {
+ this->fd = STDERR_FILENO;
+ }
+ }
- if ( !this->fd ) {
- this->fd = STDERR_FILENO;
+ ~FileDescriptorGuard() {
+ close(this->fd);
}
- }
- ~FileDescriptorGuard() {
- close(this->fd);
- }
+ operator int() {
+ return this->fd;
+ }
+
+ private:
+ int fd;
- int fd;
};
std::string get_file_name(int fd) {