From 75f93c566bb11cabcd2a675889bf11a09f88655c Mon Sep 17 00:00:00 2001 From: Stephen Anthony Date: Mon, 10 Oct 2022 16:40:25 -0230 Subject: [PATCH] Some minor optimizations to FSNodePOSIX. --- src/emucore/FSNode.cxx | 2 -- src/os/unix/FSNodePOSIX.cxx | 49 ++++++++++++++++++------------------- src/os/unix/FSNodePOSIX.hxx | 17 ++++++------- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/emucore/FSNode.cxx b/src/emucore/FSNode.cxx index 32cfd0fdc..091f2a4f8 100644 --- a/src/emucore/FSNode.cxx +++ b/src/emucore/FSNode.cxx @@ -417,7 +417,6 @@ size_t FSNode::write(const ByteBuffer& buffer, size_t size) const out.seekp(0, std::ios::end); sizeWritten = static_cast(out.tellp()); - out.seekp(0, std::ios::beg); } else throw runtime_error("File open/write error"); @@ -442,7 +441,6 @@ size_t FSNode::write(const stringstream& buffer) const out.seekp(0, std::ios::end); sizeWritten = static_cast(out.tellp()); - out.seekp(0, std::ios::beg); } else throw runtime_error("File open/write error"); diff --git a/src/os/unix/FSNodePOSIX.cxx b/src/os/unix/FSNodePOSIX.cxx index ca7ad53b4..618d37fb3 100644 --- a/src/os/unix/FSNodePOSIX.cxx +++ b/src/os/unix/FSNodePOSIX.cxx @@ -15,8 +15,6 @@ // this file, and for a DISCLAIMER OF ALL WARRANTIES. //============================================================================ -#include "TimerManager.hxx" - #if defined(RETRON77) #define ROOT_DIR "/mnt/games/" #else @@ -39,9 +37,8 @@ FSNodePOSIX::FSNodePOSIX(const string& path, bool verify) // Expand '~' to the HOME environment variable if(_path[0] == '~') { - const char* home = std::getenv("HOME"); // NOLINT (not thread safe) - if (home != nullptr) - _path.replace(0, 1, home); + if (ourHomeDir != nullptr) + _path.replace(0, 1, ourHomeDir); } // Get absolute path (only used for relative directories) else if(_path[0] == '.') @@ -62,7 +59,7 @@ void FSNodePOSIX::setFlags() { struct stat st; - _isValid = (0 == stat(_path.c_str(), &st)); + _isValid = stat(_path.c_str(), &st) == 0; if(_isValid) { _isDirectory = S_ISDIR(st.st_mode); @@ -80,8 +77,7 @@ void FSNodePOSIX::setFlags() string FSNodePOSIX::getShortPath() const { // If the path starts with the home directory, replace it with '~' - const char* env_home = std::getenv("HOME"); // NOLINT (not thread safe) - const string& home = env_home != nullptr ? env_home : EmptyString; + const string& home = ourHomeDir != nullptr ? ourHomeDir : EmptyString; if(home != EmptyString && BSPF::startsWithIgnoreCase(_path, home)) { @@ -94,12 +90,31 @@ string FSNodePOSIX::getShortPath() const return _path; } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +size_t FSNodePOSIX::getSize() const +{ + struct stat st; + return (stat(_path.c_str(), &st) == 0) ? st.st_size : 0; +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool FSNodePOSIX::hasParent() const { return !_path.empty() && _path != ROOT_DIR; } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +AbstractFSNodePtr FSNodePOSIX::getParent() const +{ + if (_path == ROOT_DIR) + return nullptr; + + const char* start = _path.c_str(); + const char* end = lastPathComponent(_path); + + return make_unique(string(start, static_cast(end - start))); +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool FSNodePOSIX::getChildren(AbstractFSList& myList, ListMode mode) const { @@ -172,13 +187,6 @@ bool FSNodePOSIX::getChildren(AbstractFSList& myList, ListMode mode) const return true; } -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -size_t FSNodePOSIX::getSize() const -{ - struct stat st; - return (stat(_path.c_str(), &st) == 0) ? st.st_size : 0; -} - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool FSNodePOSIX::makeDir() { @@ -228,13 +236,4 @@ bool FSNodePOSIX::rename(const string& newfile) } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -AbstractFSNodePtr FSNodePOSIX::getParent() const -{ - if (_path == ROOT_DIR) - return nullptr; - - const char* start = _path.c_str(); - const char* end = lastPathComponent(_path); - - return make_unique(string(start, static_cast(end - start))); -} +const char* FSNodePOSIX::ourHomeDir = std::getenv("HOME"); // NOLINT (not thread safe) diff --git a/src/os/unix/FSNodePOSIX.hxx b/src/os/unix/FSNodePOSIX.hxx index 67bf1b2f8..6407b95ca 100644 --- a/src/os/unix/FSNodePOSIX.hxx +++ b/src/os/unix/FSNodePOSIX.hxx @@ -65,7 +65,6 @@ class FSNodePOSIX : public AbstractFSNode void setName(const string& name) override { _displayName = name; } const string& getPath() const override { return _path; } string getShortPath() const override; - bool hasParent() const override; bool isDirectory() const override { return _isDirectory; } bool isFile() const override { return _isFile; } bool isReadable() const override { return access(_path.c_str(), R_OK) == 0; } @@ -74,15 +73,9 @@ class FSNodePOSIX : public AbstractFSNode bool rename(const string& newfile) override; size_t getSize() const override; - bool getChildren(AbstractFSList& list, ListMode mode) const override; + bool hasParent() const override; AbstractFSNodePtr getParent() const override; - - protected: - string _path; - string _displayName; - bool _isValid{true}; - bool _isFile{false}; - bool _isDirectory{true}; + bool getChildren(AbstractFSList& list, ListMode mode) const override; private: /** @@ -90,6 +83,12 @@ class FSNodePOSIX : public AbstractFSNode * using the stat() function. */ void setFlags(); + + private: + string _path, _displayName; + bool _isValid{true}, _isFile{false}, _isDirectory{true}; + + static const char* ourHomeDir; }; #endif