Introduction
Added command line argument to ignore leading 'the ' when comparing files and folder names.
Background
There are few car owners complaining about their head units that do not
respect alphabetical order of the files and folders on USB drive or MP3 player but follow their
physical locations (including recent models of Honda and Toyota). Great work done by this project is particularly useful to fix the above. When applied this logic to music folders, it is nice to ignore leading 'the ' as it is done by many music players automatically.
Using the code
Command line (1st change)
Added check for ignoreThe command line argument in function main and set global flag ignoreTheInFolderCompare
:
std::vector<tstring> params(argv, argv+argc);
tstring param(_T("ignoreThe"));
if(std::find(params.begin(), params.end(), param) != params.end())
ignoreTheInFolderCompare = true;
Added feature (2nd change)
Below diagram shows original sequence of sorting the directory entries. This sequence did not change but last step implementation extended to use above command line argument and global flag (highlighted in orange).
comapreEntries
method is rewritten to implement the above feature plus general improvements as described below.
Hide pointer operations using STL
by replacing
WCHAR* o1Name = o1->getName();
...
bool ret = (_wcsicmp(o1Name, o2Name) <= 0);
with
wstring o1Name(o1->getName());
...
bool ret = (o1Name.compare(o2Name) <= 0);
Remove leading 'the '
if (o1Name.length() > 4 && !o1Name.compare(0, prefix.size(), prefix))
o1Name = o1Name.substr(4);
if (o2Name.length() > 4 && !o2Name.compare(0, prefix.size(), prefix))
o2Name = o2Name.substr(4);
Entire new method of compareEntries
bool compareEntries( CEntry* o1, CEntry* o2)
{
wstring o1Name(o1->getName());
wstring o2Name(o2->getName());
if(ignoreTheInFolderCompare)
{
transform(o1Name.begin(), o1Name.end(), o1Name.begin(), tolower);
transform(o2Name.begin(), o2Name.end(), o2Name.begin(), tolower);
wstring prefix(L"the ");
if (o1Name.length() > 4 && !o1Name.compare(0, prefix.size(), prefix))
o1Name = o1Name.substr(4);
if (o2Name.length() > 4 && !o2Name.compare(0, prefix.size(), prefix))
o2Name = o2Name.substr(4);
}
bool ret = (o1Name.compare(o2Name) <= 0);
return ret;
}
Points of Interest
I created this post on blogger sharing another utility helping to play your music on the "dummy" car stereo hassle free. This project was essential for getting things working nicely in my car so hope others will appreciate too.
History
N/A