More work on converting FSNode to C++17 filesystem API.

This commit is contained in:
Stephen Anthony 2022-07-10 18:35:00 -02:30
parent fd8e2f9255
commit 94e103e604
9 changed files with 35 additions and 51 deletions

View File

@ -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<FSNodeREGULAR>(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;
}

View File

@ -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

View File

@ -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

View File

@ -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;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -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);

View File

@ -676,7 +676,7 @@ unique_ptr<Console> 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

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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);
}