You know when you search for a file using something like *.mp3 or whatever, does anyone have any code that will determine if a particular string matches a particular mask?
Printable View
You know when you search for a file using something like *.mp3 or whatever, does anyone have any code that will determine if a particular string matches a particular mask?
This is (presumably) OS specific. You'll need to tell us what OS you're using...
Im using Windows...
Hmmm, not quite what I had in mind. More like this...
Basically I need an implementation for 'DoesFilenameMatchMask'. I'd write one myself but im kinda busy with other stuff right now and its implementation seemed like it would be kinda involved.Code:string fname = "Eine Kleine Nachtmusik.mp3", mask = "*ee*ven*.mp?";
if (DoesFilenameMatchMask(fname, mask))
cout << "We have a match" << endl;
else
cout << "No match" << endl;
Unfortunately none of those pages seemed to actually have the source code on them... so I ended up writing my own
Here it is if anyone wants it. If anyone has a more efficient implementation i'd like to see it
Code:bool MatchMask(const char *str, const char *mask)
{
char c;
const char *p;
while (*mask) {
c = *mask++;
if (c == '*') {
while (*mask == '*' && *mask)
mask++;
if (*mask) {
p = str;
while (*p) {
while (*p && *p != *mask)
p++;
if (MatchMask(p, mask)) return true;
p++;
}
}
else
return true;
}
else if (c == '?') {
if (!*str)
return false;
}
else if (c != *str) {
return false;
}
str++;
}
return !*mask && !*str;
}