Quellcode durchsuchen

Improved File class

- moved actual implementation of "canExecute" method to a
private method for reusability
Patrick vor 4 Jahren
Ursprung
Commit
a3912dc0b6
2 geänderte Dateien mit 19 neuen und 10 gelöschten Zeilen
  1. 17 9
      source/io/File.cpp
  2. 2 1
      source/io/File.hpp

+ 17 - 9
source/io/File.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-15
- * Changed:         2020-08-18
+ * Changed:         2020-08-19
  *
  * */
 
@@ -40,14 +40,7 @@ bool ls_std::File::operator!=(File &_file)
 
 bool ls_std::File::canExecute()
 {
-  bool executable {};
-  struct stat _stat {};
-
-  if(stat(this->absoluteFilePath.c_str(), &_stat) == 0) {
-    executable = (_stat.st_mode & (unsigned short) S_IEXEC) != 0;
-  }
-
-  return executable;
+  return ls_std::File::_isExecutable(this->absoluteFilePath);
 }
 
 void ls_std::File::createNewFile()
@@ -255,6 +248,21 @@ bool ls_std::File::_isDirectory(const std::string& _path)
   return match;
 }
 
+bool ls_std::File::_isExecutable(const std::string &_path)
+{
+  bool executable {};
+
+  if(ls_std::File::_exists(_path)) {
+    struct stat _stat {};
+
+    if(stat(_path.c_str(), &_stat) == 0) {
+      executable = (_stat.st_mode & (unsigned short) S_IEXEC) != 0;
+    }
+  }
+
+  return executable;
+}
+
 bool ls_std::File::_isFile(const std::string& _path)
 {
   bool match {};

+ 2 - 1
source/io/File.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-15
- * Changed:         2020-08-17
+ * Changed:         2020-08-19
  *
  * */
 
@@ -69,6 +69,7 @@ namespace ls_std {
       static bool _exists(const std::string& _path);
       static std::string _getParent(const std::string& _path);
       static bool _isDirectory(const std::string& _path);
+      static bool _isExecutable(const std::string& _path);
       static bool _isFile(const std::string& _path);
       static time_t _lastModified(const std::string& _path);
       static std::list<std::string> _list(const std::string& _path);