From 5fb87ea4c4e5f30c9ee40a56e3487c0358d69783 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 26 Apr 2019 12:11:42 +1000 Subject: [PATCH] posix/ostream: add stat ostream operators --- CMakeLists.txt | 2 ++ posix/ostream.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++++ posix/ostream.hpp | 16 ++++++++++ 3 files changed, 96 insertions(+) create mode 100644 posix/ostream.cpp create mode 100644 posix/ostream.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c89ed9d1..3a405e79 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -91,6 +91,8 @@ list ( posix/except.hpp posix/fd.cpp posix/fd.hpp + posix/ostream.cpp + posix/ostream.hpp posix/util.cpp posix/util.hpp ) diff --git a/posix/ostream.cpp b/posix/ostream.cpp new file mode 100644 index 00000000..6a244ab9 --- /dev/null +++ b/posix/ostream.cpp @@ -0,0 +1,78 @@ +#include "ostream.hpp" + +#include +#include + + +/////////////////////////////////////////////////////////////////////////////// +struct type_printer { + type_printer (struct stat const &_data) + : data (_data) + { ; } + + struct stat const &data; +}; + + +//----------------------------------------------------------------------------- +std::ostream& operator<< (std::ostream &os, type_printer const &val) +{ + os << "[ "; + if (val.data.st_mode & S_ISUID) os << "SUID, "; + if (val.data.st_mode & S_ISGID) os << "SGID, "; + if (val.data.st_mode & S_ISVTX) os << "STICKY, "; + + os << "{ user: " << (val.data.st_mode & S_IRUSR ? 'r' : '_') + << (val.data.st_mode & S_IWUSR ? 'w' : '_') + << (val.data.st_mode & S_IXUSR ? 'x' : '_') + << " }, " + << "{ group: " << (val.data.st_mode & S_IRGRP ? 'r' : '_') + << (val.data.st_mode & S_IWGRP ? 'w' : '_') + << (val.data.st_mode & S_IXGRP ? 'x' : '_') + << " }, " + << "{ group: " << (val.data.st_mode & S_IROTH ? 'r' : '_') + << (val.data.st_mode & S_IWOTH ? 'w' : '_') + << (val.data.st_mode & S_IXOTH ? 'x' : '_') + << " }"; + + return os; +} + + +/////////////////////////////////////////////////////////////////////////////// +struct mode_printer { + mode_printer (struct stat const &_data) + : data (_data) + { ; } + + struct stat const &data; +}; + + +//----------------------------------------------------------------------------- +std::ostream& operator<< (std::ostream &os, mode_printer const &val) +{ + return S_ISREG (val.data.st_mode) ? os << "REGULAR" + : S_ISDIR (val.data.st_mode) ? os << "DIRECTORY" + : S_ISCHR (val.data.st_mode) ? os << "CHARACTER" + : S_ISBLK (val.data.st_mode) ? os << "BLOCK" + : S_ISFIFO (val.data.st_mode) ? os << "FIFO" + : S_ISLNK (val.data.st_mode) ? os << "SYMLINK" + : S_ISSOCK (val.data.st_mode) ? os << "SOCKET" + : (throw std::invalid_argument ("Unhandled mode_t"), os << "_error"); +} + + +//----------------------------------------------------------------------------- +std::ostream& operator<< (std::ostream &os, struct stat const &val) +{ + return os << "{ dev: " << val.st_dev + << ", ino: " << val.st_ino + << ", type: " << type_printer (val) + << ", mode: " << mode_printer (val) + << ", uid: " << val.st_uid + << ", gid: " << val.st_gid + << ", size: " << val.st_size + << ", blocksize: " << val.st_blksize + << " }"; +} diff --git a/posix/ostream.hpp b/posix/ostream.hpp new file mode 100644 index 00000000..31996d4f --- /dev/null +++ b/posix/ostream.hpp @@ -0,0 +1,16 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright 2019, Danny Robson + */ + +#pragma once + +#include + +#include + +/////////////////////////////////////////////////////////////////////////// +std::ostream& operator<< (std::ostream& os, struct ::stat const &);