summaryrefslogtreecommitdiff
path: root/ui/sorting.h
diff options
context:
space:
mode:
authorDaniel Wilhelm <daniel@wili.li>2014-04-18 17:00:17 +0200
committerDaniel Wilhelm <daniel@wili.li>2014-04-18 17:00:17 +0200
commitfd0853d2623dd278b08288331ed42e3be59252fb (patch)
treea7645daeaef8bdbed064faf4eb88e72cee58726c /ui/sorting.h
parent2.1 (diff)
downloadFreeFileSync-fd0853d2623dd278b08288331ed42e3be59252fb.tar.gz
FreeFileSync-fd0853d2623dd278b08288331ed42e3be59252fb.tar.bz2
FreeFileSync-fd0853d2623dd278b08288331ed42e3be59252fb.zip
2.2
Diffstat (limited to 'ui/sorting.h')
-rw-r--r--ui/sorting.h108
1 files changed, 38 insertions, 70 deletions
diff --git a/ui/sorting.h b/ui/sorting.h
index 2716eceb..c3cf8f97 100644
--- a/ui/sorting.h
+++ b/ui/sorting.h
@@ -2,8 +2,7 @@
#define SORTING_H_INCLUDED
#include "../structures.h"
-#include "../library/resources.h"
-#include "../library/globalFunctions.h"
+#include "../shared/globalFunctions.h"
namespace FreeFileSync
@@ -14,31 +13,15 @@ namespace FreeFileSync
SORT_ON_RIGHT,
};
- enum SortDirection
- {
- ASCENDING,
- DESCENDING,
- };
-
- template <SortDirection sortAscending>
inline
- bool stringSmallerThan(const wxChar* stringA, const wxChar* stringB)
+ bool stringSmallerThan(const DefaultChar* stringA, const DefaultChar* stringB)
{
-#ifdef FFS_WIN //case-insensitive comparison!
- return sortAscending == ASCENDING ?
- FreeFileSync::compareStringsWin32(stringA, stringB) < 0 : //way faster than wxString::CmpNoCase() in windows build!!!
- FreeFileSync::compareStringsWin32(stringA, stringB) > 0;
+#ifdef FFS_WIN
+ //case-insensitive comparison!
+ return FreeFileSync::compareStringsWin32(stringA, stringB) < 0; //way faster than wxString::CmpNoCase() in windows build!!!
#else
- while (*stringA == *stringB)
- {
- if (*stringA == wxChar(0)) //strings are equal
- return false;
-
- ++stringA;
- ++stringB;
- }
- return sortAscending == ASCENDING ? *stringA < *stringB : *stringA > *stringB; //wxChar(0) is handled correctly
+ return defaultCompare(stringA, stringB) < 0;
#endif
}
@@ -46,7 +29,8 @@ namespace FreeFileSync
inline
int compareString(const wxChar* stringA, const wxChar* stringB, const int lengthA, const int lengthB)
{
-#ifdef FFS_WIN //case-insensitive comparison!
+#ifdef FFS_WIN
+ //case-insensitive comparison!
return FreeFileSync::compareStringsWin32(stringA, stringB, lengthA, lengthB); //way faster than wxString::CmpNoCase() in the windows build!!!
#else
for (int i = 0; i < std::min(lengthA, lengthB); ++i)
@@ -55,11 +39,15 @@ namespace FreeFileSync
return stringA[i] - stringB[i];
}
return lengthA - lengthB;
+
+ //equivalent:
+ //const int rv = strncmp(stringA, stringB, std::min(lengthA, lengthB));
+ //return rv != 0 ? rv : lengthA - lengthB;
#endif
}
- template <SortDirection sortAscending, SideToSort side>
+ template <SideToSort side>
inline
bool sortByFileName(const FileCompareLine& a, const FileCompareLine& b)
{
@@ -76,7 +64,7 @@ namespace FreeFileSync
if (descrLineA->objType == FileDescrLine::TYPE_DIRECTORY) //sort directories by relative name
{
if (descrLineB->objType == FileDescrLine::TYPE_DIRECTORY)
- return stringSmallerThan<sortAscending>(descrLineA->relativeName.c_str(), descrLineB->relativeName.c_str());
+ return stringSmallerThan(descrLineA->relativeName.c_str(), descrLineB->relativeName.c_str());
else
return false;
}
@@ -89,21 +77,21 @@ namespace FreeFileSync
const wxChar* stringA = descrLineA->relativeName.c_str();
const wxChar* stringB = descrLineB->relativeName.c_str();
- size_t pos = descrLineA->relativeName.findFromEnd(FreeFileSync::FILE_NAME_SEPARATOR); //start search beginning from end
+ size_t pos = descrLineA->relativeName.findFromEnd(globalFunctions::FILE_NAME_SEPARATOR); //start search beginning from end
if (pos != std::string::npos)
stringA += pos + 1;
- pos = descrLineB->relativeName.findFromEnd(FreeFileSync::FILE_NAME_SEPARATOR); //start search beginning from end
+ pos = descrLineB->relativeName.findFromEnd(globalFunctions::FILE_NAME_SEPARATOR); //start search beginning from end
if (pos != std::string::npos)
stringB += pos + 1;
- return stringSmallerThan<sortAscending>(stringA, stringB);
+ return stringSmallerThan(stringA, stringB);
}
}
}
- template <SortDirection sortAscending, SideToSort side>
+ template <SideToSort side>
bool sortByRelativeName(const FileCompareLine& a, const FileCompareLine& b)
{
const FileDescrLine* const descrLineA = side == SORT_ON_LEFT ? &a.fileDescrLeft : &a.fileDescrRight;
@@ -119,7 +107,7 @@ namespace FreeFileSync
relLengthA = descrLineA->relativeName.length();
else if (descrLineA->objType == FileDescrLine::TYPE_FILE)
{
- relLengthA = descrLineA->relativeName.findFromEnd(FreeFileSync::FILE_NAME_SEPARATOR); //start search beginning from end
+ relLengthA = descrLineA->relativeName.findFromEnd(globalFunctions::FILE_NAME_SEPARATOR); //start search beginning from end
if (relLengthA == wxNOT_FOUND)
{
relLengthA = 0;
@@ -144,7 +132,7 @@ namespace FreeFileSync
relLengthB = descrLineB->relativeName.length();
else if (descrLineB->objType == FileDescrLine::TYPE_FILE)
{
- relLengthB = descrLineB->relativeName.findFromEnd(FreeFileSync::FILE_NAME_SEPARATOR); //start search beginning from end
+ relLengthB = descrLineB->relativeName.findFromEnd(globalFunctions::FILE_NAME_SEPARATOR); //start search beginning from end
if (relLengthB == wxNOT_FOUND)
{
relLengthB = 0;
@@ -162,7 +150,7 @@ namespace FreeFileSync
//compare relative names without filenames first
const int rv = compareString(relStringA, relStringB, relLengthA, relLengthB);
if (rv != 0)
- return sortAscending == ASCENDING ? rv < 0 : rv > 0;
+ return rv < 0;
else //compare the filenames
{
if (descrLineB->objType == FileDescrLine::TYPE_DIRECTORY) //directories shall appear before files
@@ -170,14 +158,12 @@ namespace FreeFileSync
else if (descrLineA->objType == FileDescrLine::TYPE_DIRECTORY)
return true;
- return sortAscending == ASCENDING ?
- compareString(fileStringA, fileStringB, fileLengthA, fileLengthB) < 0 :
- compareString(fileStringA, fileStringB, fileLengthA, fileLengthB) > 0;
+ return compareString(fileStringA, fileStringB, fileLengthA, fileLengthB) < 0;
}
}
- template <SortDirection sortAscending, SideToSort side>
+ template <SideToSort side>
inline
bool sortByFullName(const FileCompareLine& a, const FileCompareLine& b)
{
@@ -191,18 +177,14 @@ namespace FreeFileSync
return true; //empty rows always last
else
#ifdef FFS_WIN //case-insensitive comparison!
- return sortAscending == ASCENDING ?
- FreeFileSync::compareStringsWin32(descrLineA->fullName.c_str(), descrLineB->fullName.c_str()) < 0 : //way faster than wxString::CmpNoCase() in windows build!!!
- FreeFileSync::compareStringsWin32(descrLineA->fullName.c_str(), descrLineB->fullName.c_str()) > 0;
+ return FreeFileSync::compareStringsWin32(descrLineA->fullName.c_str(), descrLineB->fullName.c_str()) < 0; //way faster than wxString::CmpNoCase() in windows build!!!
#else
- return sortAscending == ASCENDING ?
- descrLineA->fullName.Cmp(descrLineB->fullName) < 0 :
- descrLineA->fullName.Cmp(descrLineB->fullName) > 0;
+ return descrLineA->fullName.Cmp(descrLineB->fullName) < 0;
#endif
}
- template <SortDirection sortAscending, SideToSort side>
+ template <SideToSort side>
inline
bool sortByFileSize(const FileCompareLine& a, const FileCompareLine& b)
{
@@ -219,7 +201,7 @@ namespace FreeFileSync
if (descrLineA->objType == FileDescrLine::TYPE_DIRECTORY) //sort directories by relative name
{
if (descrLineB->objType == FileDescrLine::TYPE_DIRECTORY)
- return stringSmallerThan<sortAscending>(descrLineA->relativeName.c_str(), descrLineB->relativeName.c_str());
+ return stringSmallerThan(descrLineA->relativeName.c_str(), descrLineB->relativeName.c_str());
else
return false;
}
@@ -228,14 +210,12 @@ namespace FreeFileSync
if (descrLineB->objType == FileDescrLine::TYPE_DIRECTORY)
return true;
else
- return sortAscending == ASCENDING ?
- descrLineA->fileSize > descrLineB->fileSize : //sortAscending shall result in list beginning with largest files first
- descrLineA->fileSize < descrLineB->fileSize;
+ return descrLineA->fileSize > descrLineB->fileSize; //sortAscending shall result in list beginning with largest files first
}
}
- template <SortDirection sortAscending, SideToSort side>
+ template <SideToSort side>
inline
bool sortByDate(const FileCompareLine& a, const FileCompareLine& b)
{
@@ -251,7 +231,7 @@ namespace FreeFileSync
if (descrLineA->objType == FileDescrLine::TYPE_DIRECTORY) //sort directories by relative name
{
if (descrLineB->objType == FileDescrLine::TYPE_DIRECTORY)
- return stringSmallerThan<sortAscending>(descrLineA->relativeName.c_str(), descrLineB->relativeName.c_str());
+ return stringSmallerThan(descrLineA->relativeName.c_str(), descrLineB->relativeName.c_str());
else
return false;
}
@@ -260,14 +240,11 @@ namespace FreeFileSync
if (descrLineB->objType == FileDescrLine::TYPE_DIRECTORY)
return true;
else
- return sortAscending == ASCENDING ?
- descrLineA->lastWriteTimeRaw > descrLineB->lastWriteTimeRaw :
- descrLineA->lastWriteTimeRaw < descrLineB->lastWriteTimeRaw;
+ return descrLineA->lastWriteTimeRaw > descrLineB->lastWriteTimeRaw;
}
}
- template <SortDirection sortAscending>
inline
bool sortByCmpResult(const FileCompareLine& a, const FileCompareLine& b)
{
@@ -277,37 +254,28 @@ namespace FreeFileSync
if (b.cmpResult == FILE_EQUAL)
return true;
- return sortAscending == ASCENDING ?
- a.cmpResult < b.cmpResult :
- a.cmpResult > b.cmpResult;
+ return a.cmpResult < b.cmpResult;
}
- template <SortDirection sortAscending>
inline
bool sortBySyncDirection(const FileCompareLine& a, const FileCompareLine& b)
{
- return sortAscending == ASCENDING ?
- a.direction < b.direction :
- a.direction > b.direction;
+ return a.syncDir < b.syncDir;
}
- template <SortDirection sortAscending, SideToSort side>
+ template <SideToSort side>
inline
bool sortByDirectory(const FolderCompareLine& a, const FolderCompareLine& b)
{
- const Zstring* const dirNameA = side == SORT_ON_LEFT ? &a.syncPair.leftDirectory : &a.syncPair.rightDirectory;
- const Zstring* const dirNameB = side == SORT_ON_LEFT ? &b.syncPair.leftDirectory : &b.syncPair.rightDirectory;
+ const Zstring& dirNameA = side == SORT_ON_LEFT ? a.syncPair.leftDirectory : a.syncPair.rightDirectory;
+ const Zstring& dirNameB = side == SORT_ON_LEFT ? b.syncPair.leftDirectory : b.syncPair.rightDirectory;
#ifdef FFS_WIN //case-insensitive comparison!
- return sortAscending == ASCENDING ?
- FreeFileSync::compareStringsWin32(dirNameA->c_str(), dirNameB->c_str()) < 0 : //way faster than wxString::CmpNoCase() in windows build!!!
- FreeFileSync::compareStringsWin32(dirNameA->c_str(), dirNameB->c_str()) > 0;
+ return FreeFileSync::compareStringsWin32(dirNameA.c_str(), dirNameB.c_str()) < 0; //way faster than wxString::CmpNoCase() in windows build!!!
#elif defined FFS_LINUX
- return sortAscending == ASCENDING ?
- dirNameA->Cmp(*dirNameB) < 0 :
- dirNameA->Cmp(*dirNameB) > 0;
+ return dirNameA.Cmp(dirNameB) < 0;
#endif
}
}
bgstack15