some fixes and cleanup

This commit is contained in:
Thomas Jentzsch 2022-08-13 10:09:44 +02:00
parent b51d6c2fce
commit 50db2abc50
5 changed files with 22 additions and 86 deletions

View File

@ -320,6 +320,10 @@ namespace BSPF
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Search if string contains pattern including '?' as joker.
// @param str The searched string
// @param pattern The pattern to search for
// @return Position of pattern in string.
inline size_t matchWithJoker(const string& str, const string& pattern) inline size_t matchWithJoker(const string& str, const string& pattern)
{ {
if(str.length() >= pattern.length()) if(str.length() >= pattern.length())
@ -346,6 +350,11 @@ namespace BSPF
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Search if string contains pattern including wildcard '*'
// and '?' as joker.
// @param str The searched string
// @param pattern The pattern to search for
// @return True if pattern was found.
inline bool matchWithWildcards(const string& str, const string& pattern) inline bool matchWithWildcards(const string& str, const string& pattern)
{ {
string pat = pattern; string pat = pattern;

View File

@ -302,8 +302,8 @@ class FSNode
* and replace the extension (if present) with the given one. If no * and replace the extension (if present) with the given one. If no
* extension is present, the given one is appended instead. * extension is present, the given one is appended instead.
*/ */
string getNameWithExt(const string& ext) const; string getNameWithExt(const string& ext = "") const;
string getPathWithExt(const string& ext) const; string getPathWithExt(const string& ext = "") const;
private: private:
explicit FSNode(const AbstractFSNodePtr& realNode); explicit FSNode(const AbstractFSNodePtr& realNode);

View File

@ -561,65 +561,6 @@ string LauncherDialog::getRomDir()
return tmpromdir != EmptyString ? tmpromdir : settings.getString("romdir"); return tmpromdir != EmptyString ? tmpromdir : settings.getString("romdir");
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
size_t LauncherDialog::matchWithJoker(const string& str, const string& pattern)
{
if(str.length() >= pattern.length())
{
// optimize a bit
if(pattern.find('?') != string::npos)
{
for(size_t pos = 0; pos < str.length() - pattern.length() + 1; ++pos)
{
bool found = true;
for(size_t i = 0; found && i < pattern.length(); ++i)
if(pattern[i] != str[pos + i] && pattern[i] != '?')
found = false;
if(found)
return pos;
}
}
else
return str.find(pattern);
}
return string::npos;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool LauncherDialog::matchWithWildcards(const string& str, const string& pattern)
{
string pat = pattern;
// remove leading and trailing '*'
size_t i = 0;
while(pat[i++] == '*');
pat = pat.substr(i - 1);
i = pat.length();
while(pat[--i] == '*');
pat.erase(i + 1);
// Search for first '*'
const size_t pos = pat.find('*');
if(pos != string::npos)
{
// '*' found, split pattern into left and right part, search recursively
const string leftPat = pat.substr(0, pos);
const string rightPat = pat.substr(pos + 1);
const size_t posLeft = matchWithJoker(str, leftPat);
if(posLeft != string::npos)
return matchWithWildcards(str.substr(pos + posLeft), rightPat);
else
return false;
}
// no further '*' found
return matchWithJoker(str, pat) != string::npos;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool LauncherDialog::matchWithWildcardsIgnoreCase(const string& str, const string& pattern) bool LauncherDialog::matchWithWildcardsIgnoreCase(const string& str, const string& pattern)
{ {
@ -629,7 +570,7 @@ bool LauncherDialog::matchWithWildcardsIgnoreCase(const string& str, const strin
BSPF::toUpperCase(in); BSPF::toUpperCase(in);
BSPF::toUpperCase(pat); BSPF::toUpperCase(pat);
return matchWithWildcards(in, pat); return BSPF::matchWithWildcards(in, pat);
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -141,27 +141,6 @@ class LauncherDialog : public Dialog, CommandSender
*/ */
bool matchWithWildcardsIgnoreCase(const string& str, const string& pattern); bool matchWithWildcardsIgnoreCase(const string& str, const string& pattern);
/**
Search if string contains pattern including wildcard '*'
and '?' as joker.
@param str The searched string
@param pattern The pattern to search for
@return True if pattern was found.
*/
bool matchWithWildcards(const string& str, const string& pattern);
/**
Search if string contains pattern including '?' as joker.
@param str The searched string
@param pattern The pattern to search for
@return Position of pattern in string.
*/
size_t matchWithJoker(const string& str, const string& pattern);
void applyFiltering(); void applyFiltering();
float getRomInfoZoom(int listHeight) const; float getRomInfoZoom(int listHeight) const;

View File

@ -165,12 +165,19 @@ bool RomImageWidget::getImageList(const string& filename)
FSNode::NameFilter filter = ([&](const FSNode& node) { FSNode::NameFilter filter = ([&](const FSNode& node) {
return (!node.isDirectory() && return (!node.isDirectory() &&
(node.getPath() == filename + ".png" || (node.getPath() == filename + ".png" ||
BSPF::matchWithWildcards(node.getPath(), filename + "#*.png"))); BSPF::matchWithWildcards(node.getPath(), filename + " #*.png")));
}); });
FSNode node(instance().snapshotLoadDir().getPath()); FSNode node(instance().snapshotLoadDir().getPath());
node.getChildren(myImageList, FSNode::ListMode::FilesOnly, filter, false, false); node.getChildren(myImageList, FSNode::ListMode::FilesOnly, filter, false, false);
// Sort again, not considering extensions, else <filename.png> would be at the end of the list
std::sort(myImageList.begin(), myImageList.end(),
[](const FSNode& node1, const FSNode& node2)
{
return BSPF::compareIgnoreCase(node1.getNameWithExt(), node2.getNameWithExt()) < 0;
}
);
return myImageList.size() > 0; return myImageList.size() > 0;
} }
@ -271,7 +278,7 @@ void RomImageWidget::drawWidget(bool hilite)
myNavSurface->invalidate(); myNavSurface->invalidate();
if(isHighlighted() && if(isHighlighted() &&
((leftArrow && myImageIdx) || (!leftArrow && myImageIdx < myImageList.size() - 1)) || true) ((leftArrow && myImageIdx) || (!leftArrow && myImageIdx < myImageList.size() - 1)))
{ {
const int w = _w / 64; const int w = _w / 64;
const int w2 = 1; // w / 2; const int w2 = 1; // w / 2;