1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-04 23:37:10 +02:00

Simplified file extension filtering code

Don't create any temporary strings, just use the d_name field and
strcasecmp.
This commit is contained in:
Maarten ter Huurne 2014-08-14 06:43:37 +02:00
parent 404b2a1029
commit 47d349f78b

View File

@ -124,33 +124,18 @@ void FileLister::browse(const string& path, bool clean)
continue;
}
string file = dptr->d_name;
for (vector<string>::iterator it = filter.begin(); it != filter.end(); ++it) {
if (file.find('.') == string::npos) {
if (!it->empty())
continue;
// Determine file extension.
const char *ext = strrchr(dptr->d_name, '.');
if (ext) ext++; else ext = "";
fileSet.insert(file);
for (auto& filterExt : filter) {
// Note: strcasecmp can't compare multi-byte UTF-8 characters,
// but the filtered file extensions don't contain any of
// those.
if (strcasecmp(ext, filterExt.c_str()) == 0) {
fileSet.insert(string(dptr->d_name));
break;
}
if (it->length() < file.length()) {
if (file[file.length() - it->length() - 1] != '.')
continue;
string file_lowercase =
file.substr(file.length() - it->length());
/* XXX: This won't accept UTF-8 codes.
* Thanksfully file extensions shouldn't contain any. */
transform(file_lowercase.begin(), file_lowercase.end(),
file_lowercase.begin(), ::tolower);
if (file_lowercase.compare(0, it->length(), *it) == 0) {
fileSet.insert(file);
break;
}
}
}
}
}