From 69d0e959dab557cafac034329cb05abaa10b4537 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Wed, 13 Mar 2019 00:35:19 +0000 Subject: [PATCH] added incomplete Path.hpp and Path.cpp --- qse/include/qse/si/App.hpp | 19 ++- qse/include/qse/si/Path.hpp | 112 ++++++++++++++++++ qse/include/qse/si/Thread.hpp | 10 +- qse/lib/si/App.cpp | 16 --- qse/lib/si/Path.cpp | 215 ++++++++++++++++++++++++++++++++++ 5 files changed, 348 insertions(+), 24 deletions(-) create mode 100644 qse/include/qse/si/Path.hpp create mode 100644 qse/lib/si/Path.cpp diff --git a/qse/include/qse/si/App.hpp b/qse/include/qse/si/App.hpp index 2552d770..908d0d0a 100644 --- a/qse/include/qse/si/App.hpp +++ b/qse/include/qse/si/App.hpp @@ -32,6 +32,7 @@ #include #include #include +#include ///////////////////////////////// QSE_BEGIN_NAMESPACE(QSE) @@ -57,9 +58,21 @@ public: int chroot (const qse_mchar_t* mpath) QSE_CPP_NOEXCEPT; int chroot (const qse_wchar_t* wpath) QSE_CPP_NOEXCEPT; - void sleep (const qse_ntime_t* duration) QSE_CPP_NOEXCEPT; - void sleep (unsigned int seconds) QSE_CPP_NOEXCEPT; - void msleep (qse_mtime_t duration) QSE_CPP_NOEXCEPT; + void sleep (const qse_ntime_t& duration) QSE_CPP_NOEXCEPT + { + qse_sleep (&duration); + } + + void sleep (qse_long_t secs, qse_int32_t nsecs = 0) QSE_CPP_NOEXCEPT + { + qse_ntime_t duration = { secs, nsecs }; + qse_sleep (&duration); + } + + void msleep (qse_mtime_t duration) QSE_CPP_NOEXCEPT + { + qse_msleep (duration); + } #if 0 int switchUser (qse_uid_t uid, qse_gid_t gid, bool permanently) QSE_CPP_NOEXCEPT; diff --git a/qse/include/qse/si/Path.hpp b/qse/include/qse/si/Path.hpp new file mode 100644 index 00000000..91238cba --- /dev/null +++ b/qse/include/qse/si/Path.hpp @@ -0,0 +1,112 @@ +/* + * $Id$ + * + Copyright (c) 2006-2014 Chung, Hyung-Hwan. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _QSE_SI_PATH_CLASS_ +#define _QSE_SI_PATH_CLASS_ + +#include +#include + +QSE_BEGIN_NAMESPACE(QSE) +class Path +{ +public: + enum + { + MAX_LEN = QSE_PATH_MAX + }; + + Path (); + Path (const qse_char_t* n); + Path (const Path& fn); + Path& operator= (const Path& fn); + + const qse_char_t* name () const + { + return full_path; + } + + void setName (const qse_char_t* n) + { + if (n == QSE_NULL || n[0] == QSE_CHAR('\0')) set_to_root(); + else { + qse_strxcpy (full_path, qse_countof(full_path), n); + this->set_base_name (); + } + } + + const qse_char_t* baseName () const + { + return base_name; + } + const qse_char_t* baseDir () const + { + return base_dir; + } + + bool exists () + { + return exists (full_path); + } + static bool exists (const qse_char_t* path); + + int getSize (qse_off_t* sz); + bool isWritable (); + bool isReadable (); + bool isReadWritable (); +#ifndef _WIN32 + bool isExecutable (); +#endif + + bool isDirectory () const { return this->isDirectory (full_path); } + static bool isDirectory (const qse_char_t* path); + + bool isRegular () const { return this->isRegular (full_path); } + static bool isRegular (const qse_char_t* path); + + int chmod (qse_mode_t mode); + static int chmod (const qse_char_t* path, qse_mode_t mode); + + int unlink (); + static int unlink (const qse_char_t* path); + + int mkdir (qse_mode_t mode); + static int mkdir (const qse_char_t* path, qse_mode_t mode); + + int setToSelf (const qse_char_t* argv0 = QSE_NULL); + +protected: + qse_char_t full_path[QSE_PATH_MAX + 1]; + qse_char_t base_name[QSE_PATH_MAX + 1]; + qse_char_t base_dir [QSE_PATH_MAX + 1]; + + void set_base_name (); + void set_to_root (); +}; + +QSE_END_NAMESPACE(QSE) + +#endif diff --git a/qse/include/qse/si/Thread.hpp b/qse/include/qse/si/Thread.hpp index 8ed39876..8e4914d2 100644 --- a/qse/include/qse/si/Thread.hpp +++ b/qse/include/qse/si/Thread.hpp @@ -89,14 +89,14 @@ public: int blockAllSignals () QSE_CPP_NOEXCEPT { return qse_thr_blockallsigs(&this->thr); } int unblockAllSignals () QSE_CPP_NOEXCEPT { return qse_thr_unblockallsigs(&this->thr); } - void sleep (const qse_ntime_t* duration) QSE_CPP_NOEXCEPT + void sleep (const qse_ntime_t& duration) QSE_CPP_NOEXCEPT { - qse_sleep (duration); + qse_sleep (&duration); } - void sleep (unsigned int seconds) QSE_CPP_NOEXCEPT + void sleep (qse_long_t secs, qse_int32_t nsecs = 0) QSE_CPP_NOEXCEPT { - static qse_ntime_t duration = { seconds, 0 }; + static qse_ntime_t duration = { secs, nsecs }; qse_sleep (&duration); } @@ -104,7 +104,7 @@ public: { qse_msleep (duration); } - + protected: qse_thr_t thr; void* __exctx; diff --git a/qse/lib/si/App.cpp b/qse/lib/si/App.cpp index cb60295b..1eca8daa 100644 --- a/qse/lib/si/App.cpp +++ b/qse/lib/si/App.cpp @@ -196,22 +196,6 @@ int App::chroot (const qse_wchar_t* wpath) QSE_CPP_NOEXCEPT return n; } -void App::sleep (const qse_ntime_t* duration) QSE_CPP_NOEXCEPT -{ - qse_sleep (duration); -} - -void App::sleep (unsigned int seconds) QSE_CPP_NOEXCEPT -{ - qse_ntime_t duration = { seconds, 0 }; - qse_sleep (&duration); -} - -void App::msleep (const qse_mtime_t duration) QSE_CPP_NOEXCEPT -{ - qse_msleep (duration); -} - #if 0 int App::switchPrivilege (int gid, int uid, bool permanently) { diff --git a/qse/lib/si/Path.cpp b/qse/lib/si/Path.cpp new file mode 100644 index 00000000..d50fc606 --- /dev/null +++ b/qse/lib/si/Path.cpp @@ -0,0 +1,215 @@ +/* + * $Id$ + * + Copyright (c) 2006-2014 Chung, Hyung-Hwan. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +QSE_BEGIN_NAMESPACE(QSE) + +Path::Path () +{ + this->set_to_root (); +} + +Path::Path (const qse_char_t* n) +{ + this->setName (n); +} + +Path::Path (const Path& fn) +{ + qse_strxcpy (this->full_path, QSE_COUNTOF(this->full_path), fn.this->full_path); + this->set_base_name (); +} + +Path& Path::operator= (const Path& fn) +{ + qse_strxcpy (this->full_path, QSE_COUNTOF(this->full_path), fn.this->full_path); + this->set_base_name (); + return *this; +} + +bool Path::exists (const qse_char_t* path) +{ + return ::qse_access (path, QSE_ACCESS_EXIST) == 0; +} + +int Path::getSize (qse_off_t* sz) +{ + qse_stat_t buf; + if (::qse_stat (this->full_path, &buf) == -1) return -1; + *sz = buf.st_size; + return 0; +} + +bool Path::isWritable () +{ + return ::qse_access (this->full_path, QSE_ACCESS_WRITE) == 0; +} + +bool Path::isReadable () +{ + return ::qse_access (this->full_path, QSE_ACCESS_READ) == 0; +} + +bool Path::isReadWritable () +{ + return ::qse_access (this->full_path, + QSE_ACCESS_READ | QSE_ACCESS_WRITE) == 0; +} + +#if !defined(_WIN32) +bool Path::isExecutable () +{ + return ::qse_access (this->full_path, QSE_ACCESS_EXECUTE) == 0; +} +#endif + +bool Path::isDirectory (const qse_char_t* path) +{ + int n; + qse_stat_t st; + + n = ::qse_stat (path, &st); + if (n == -1) return 0; +#if defined(_WIN32) + return (st.st_mode & _S_IFDIR) != 0; +#else + return S_ISDIR(st.st_mode); +#endif +} + +bool Path::isRegular (const qse_char_t* path) +{ + int n; + qse_stat_t st; + + n = ::qse_stat(path, &st); + if (n == -1) return 0; +#if defined(_WIN32) + return (st.st_mode & _S_IFREG) != 0; +#else + return S_ISREG(st.st_mode); +#endif +} + +int Path::chmod (qse_mode_t mode) +{ + return qse_chmod (this->full_path, mode); +} + +int Path::chmod (const qse_char_t* path, qse_mode_t mode) +{ + return qse_chmod (path, mode); +} + +int Path::unlink () +{ + return ::qse_unlink (this->full_path); +} + +int Path::mkdir (qse_mode_t mode) +{ + return ::qse_mkdir(this->full_path, mode); +} + +int Path::mkdir (const qse_char_t* path, qse_mode_t mode) +{ + return ::qse_mkdir(path, mode); +} + +void Path::set_base_name () +{ + qse_size_t len = qse_strlen(this->full_path); + QSE_ASSERT (len > 0); + + for (qse_size_t i = len; i > 0; i--) { +#if defined(_WIN32) + if ((this->full_path[i - 1] == QSE_T('/') || this->full_path[i - 1] == QSE_T('\\')) && i != len) + { +#else + + if (this->full_path[i - 1] == QSE_T('/') && i != len) + { +#endif + const qse_char_t* p = this->full_path; +#if defined(_WIN32) + if (this->full_path[len - 1] == QSE_T('/') || this->full_path[len - 1] == QSE_T('\\')) +#else + if (this->full_path[len - 1] == QSE_T('/')) +#endif + qse_strxncpy (this->base_name, QSE_COUNTOF(this->base_name), &p[i], len - i - 1); + else + qse_strxncpy (this->base_name, QSE_COUNTOF(this->base_name), &p[i], len - i); + +#if defined(_WIN32) + if (i == 1 && (this->full_path[i - 1] == QSE_T('/') || this->full_path[i - 1] == QSE_T('\\'))) +#else + if (i == 1 && this->full_path[i - 1] == QSE_T('/')) +#endif + qse_strxncpy (this->base_dir, QSE_COUNTOF(this->base_dir), p, i); + else + qse_strxncpy (this->base_dir, QSE_COUNTOF(this->base_dir), p, i - 1); + + return; + } + } + +#if defined(_WIN32) + if (this->full_path[len - 1] == QSE_T('/') || this->full_path[len - 1] == QSE_T('\\')) +#else + if (this->full_path[len - 1] == QSE_T('/')) +#endif + qse_strxncpy (this->base_name, QSE_COUNTOF(this->base_name), this->full_path, len - 1); + else + qse_strxcpy (this->base_name, QSE_COUNTOF(this->base_name), this->full_path); + +#if defined(_WIN32) + if (this->full_path[0] == QSE_T('/') || this->full_path[0] == QSE_T('\\')) +#else + if (this->full_path[0] == QSE_T('/')) +#endif + qse_strcpy (this->base_dir, QSE_T("/")); + else + qse_strcpy (this->base_dir, QSE_T("")); +} + +void Path::set_to_root () +{ + qse_strcpy (this->full_path, QSE_T("/")); + //this->set_base_name (); + qse_strcpy (this->base_name, QSE_T("")); + qse_strcpy (this->base_dir, QSE_T("/")); +} + +int Path::setToSelf (const qse_char_t* argv0) +{ + qse_char_t p[MAX_LEN + 1]; + if (qse_getpnm(argv0, p, QSE_COUNTOF(p)) == -1) return -1; + this->setName (p); + return 0; +} + +QSE_END_NAMESPACE(QSE)