diff --git a/src/common/FSNodeREGULAR.cxx b/src/common/FSNodeREGULAR.cxx index feafcffef..4a0a13a8e 100644 --- a/src/common/FSNodeREGULAR.cxx +++ b/src/common/FSNodeREGULAR.cxx @@ -52,16 +52,10 @@ FSNodeREGULAR::FSNodeREGULAR(const string& path, bool verify) if (fs::exists(_fspath)) // get absolute path whenever possible _fspath = fs::canonical(_fspath); - _path = _fspath.string(); - _displayName = lastPathComponent(_path); - if (verify) setFlags(); - // Add a trailing slash, if necessary - if (_isDirectory && _path.length() > 0 && - _path[_path.length()-1] != FSNode::PATH_SEPARATOR) - _path += FSNode::PATH_SEPARATOR; + setPathAndDisplayNames(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -123,7 +117,7 @@ AbstractFSNodePtr FSNodeREGULAR::getParent() const const char* start = _path.c_str(); const char* end = lastPathComponent(_path); -cerr << " => path: " << _path << ", new: " << string(start, size_t(end - start)) << endl; +// cerr << " => path: " << _path << ", new: " << string(start, size_t(end - start)) << endl; return make_unique(string(start, size_t(end - start))); } @@ -134,8 +128,7 @@ bool FSNodeREGULAR::getChildren(AbstractFSList& myList, ListMode mode) const for (const auto& entry: fs::directory_iterator{_fspath, fs::directory_options::follow_directory_symlink | fs::directory_options::skip_permission_denied, - ec - }) + ec}) { const auto& path = entry.path(); @@ -242,14 +235,9 @@ bool FSNodeREGULAR::makeDir() if (!(exists() && _isDirectory) && fs::create_directory(_fspath)) { _fspath = fs::canonical(_fspath); - _path = _fspath.string(); - _displayName = lastPathComponent(_path); setFlags(); - - // Add a trailing slash, if necessary - if (_path.length() > 0 && _path[_path.length()-1] != FSNode::PATH_SEPARATOR) - _path += FSNode::PATH_SEPARATOR; + setPathAndDisplayNames(); return true; } @@ -266,15 +254,9 @@ bool FSNodeREGULAR::rename(const string& newfile) if (!ec) { _fspath = fs::canonical(newpath); - _path = _fspath.string(); - _displayName = lastPathComponent(_path); setFlags(); - - // Add a trailing slash, if necessary - if (_isDirectory && _path.length() > 0 && - _path[_path.length()-1] != FSNode::PATH_SEPARATOR) - _path += FSNode::PATH_SEPARATOR; + setPathAndDisplayNames(); return true; } diff --git a/src/common/FSNodeREGULAR.hxx b/src/common/FSNodeREGULAR.hxx index 4be6ec60f..650351590 100644 --- a/src/common/FSNodeREGULAR.hxx +++ b/src/common/FSNodeREGULAR.hxx @@ -78,6 +78,20 @@ class FSNodeREGULAR : public AbstractFSNode * Tests and sets the various flags for a file/directory. */ void setFlags(); + + /** + * Sets path and displayname, for accessors above. + */ + inline void setPathAndDisplayNames() + { + _path = _fspath.string(); + _displayName = (--_fspath.end())->string(); // last iterator in path + + // Add a trailing slash, if necessary + if (_isDirectory && _path.length() > 0 && + _path[_path.length()-1] != FSNode::PATH_SEPARATOR) + _path += FSNode::PATH_SEPARATOR; + } }; #endif diff --git a/src/common/PNGLibrary.cxx b/src/common/PNGLibrary.cxx index db7b17ffc..cdddd9848 100644 --- a/src/common/PNGLibrary.cxx +++ b/src/common/PNGLibrary.cxx @@ -298,7 +298,7 @@ void PNGLibrary::takeSnapshot(uInt32 number) string filename; string sspath = myOSystem.snapshotSaveDir().getPath() + (myOSystem.settings().getString("snapname") != "int" ? - myOSystem.romFile().getNameWithExt("") + myOSystem.romFile().getNameWithExt() : myOSystem.console().properties().get(PropType::Cart_Name)); // Check whether we want multiple snapshots created diff --git a/src/emucore/FSNode.cxx b/src/emucore/FSNode.cxx index a5c80340a..d7ac41571 100644 --- a/src/emucore/FSNode.cxx +++ b/src/emucore/FSNode.cxx @@ -251,7 +251,6 @@ string FSNode::getShortPath() const // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - string FSNode::getNameWithExt(const string& ext) const { -#if 1 fs::path p = getName(); if (ext != EmptyString) p.replace_extension(ext); @@ -259,29 +258,18 @@ string FSNode::getNameWithExt(const string& ext) const p.replace_extension(); return p.string(); -#else - if (!_realNode) - return EmptyString; - - size_t pos = _realNode->getName().find_last_of("/\\"); - string s = pos == string::npos ? _realNode->getName() : - _realNode->getName().substr(pos+1); - - pos = s.find_last_of('.'); - return (pos != string::npos) ? s.replace(pos, string::npos, ext) : s + ext; -#endif } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - string FSNode::getPathWithExt(const string& ext) const { - if (!_realNode) - return EmptyString; + fs::path p = getPath(); + if (ext != EmptyString) + p.replace_extension(ext); + else + p.replace_extension(); - string s = _realNode->getPath(); - - const size_t pos = s.find_last_of('.'); - return (pos != string::npos) ? s.replace(pos, string::npos, ext) : s + ext; + return p.string(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -350,8 +338,8 @@ FSNode FSNode::getParent() const if (!_realNode) return *this; - AbstractFSNodePtr node = _realNode->getParent(); - return node ? FSNode(node) : *this; + AbstractFSNodePtr parent_ptr = _realNode->getParent(); + return parent_ptr ? FSNode(parent_ptr) : *this; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/emucore/FSNode.hxx b/src/emucore/FSNode.hxx index e55bdaf9d..96eb27c40 100644 --- a/src/emucore/FSNode.hxx +++ b/src/emucore/FSNode.hxx @@ -298,8 +298,8 @@ class FSNode * and replace the extension (if present) with the given one. If no * extension is present, the given one is appended instead. */ - string getNameWithExt(const string& ext) const; - string getPathWithExt(const string& ext) const; + string getNameWithExt(const string& ext = EmptyString) const; + string getPathWithExt(const string& ext = EmptyString) const; private: explicit FSNode(const AbstractFSNodePtr& realNode); diff --git a/src/emucore/OSystem.cxx b/src/emucore/OSystem.cxx index 05dca1e17..269b693a7 100644 --- a/src/emucore/OSystem.cxx +++ b/src/emucore/OSystem.cxx @@ -676,7 +676,7 @@ unique_ptr OSystem::openConsole(const FSNode& romfile, string& md5) // Some properties may not have a name set; we can't leave it blank if(props.get(PropType::Cart_Name) == EmptyString) - props.set(PropType::Cart_Name, romfile.getNameWithExt("")); + props.set(PropType::Cart_Name, romfile.getNameWithExt()); // It's possible that the cart created was from a piece of the image, // and that the md5 (and hence the cart) has changed diff --git a/src/emucore/PropsSet.cxx b/src/emucore/PropsSet.cxx index f3e00a921..1b755d10f 100644 --- a/src/emucore/PropsSet.cxx +++ b/src/emucore/PropsSet.cxx @@ -170,7 +170,7 @@ void PropertiesSet::loadPerROM(const FSNode& rom, const string& md5) } if(toInsert || props.get(PropType::Cart_Name) == EmptyString) { - props.set(PropType::Cart_Name, rom.getNameWithExt("")); + props.set(PropType::Cart_Name, rom.getNameWithExt()); toInsert = true; } diff --git a/src/gui/FileListWidget.cxx b/src/gui/FileListWidget.cxx index c30bee31f..7c3cfae4e 100644 --- a/src/gui/FileListWidget.cxx +++ b/src/gui/FileListWidget.cxx @@ -120,7 +120,7 @@ void FileListWidget::setLocation(const FSNode& node, const string select) } else { - const string& displayName = _showFileExtensions ? name : file.getNameWithExt(EmptyString); + const string& displayName = _showFileExtensions ? name : file.getNameWithExt(); list.push_back(displayName); _iconTypeList.push_back(getIconType(file.getPath())); @@ -276,7 +276,7 @@ void FileListWidget::reload() { _selectedFile = _showFileExtensions ? selected().getName() - : selected().getNameWithExt(EmptyString); + : selected().getNameWithExt(); setLocation(_node, _selectedFile); } } diff --git a/src/gui/HighScoresDialog.cxx b/src/gui/HighScoresDialog.cxx index 3debb94b4..2d6544b8f 100644 --- a/src/gui/HighScoresDialog.cxx +++ b/src/gui/HighScoresDialog.cxx @@ -532,7 +532,7 @@ string HighScoresDialog::cartName() const instance().propSet().getMD5(myScores.md5, props); if(props.get(PropType::Cart_Name).empty()) - return instance().launcher().currentDir().getNameWithExt(""); + return instance().launcher().currentDir().getNameWithExt(); else return props.get(PropType::Cart_Name); }