summaryrefslogtreecommitdiff
path: root/ui/sorting.h
blob: 22b9c39b0076b1163cb2a205495d0d3376d5f4d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#ifndef SORTING_H_INCLUDED
#define SORTING_H_INCLUDED

#include "../structures.h"
#include "../library/resources.h"
#include "../library/globalFunctions.h"


namespace FreeFileSync
{
    enum SideToSort
    {
        SORT_ON_LEFT,
        SORT_ON_RIGHT,
    };

    enum SortDirection
    {
        ASCENDING,
        DESCENDING,
    };


    template <SortDirection sortAscending>
    inline
    bool stringSmallerThan(const wxChar* stringA, const wxChar* 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;
#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
#endif
    }


    inline
    int compareString(const wxChar* stringA, const wxChar* stringB, const int lengthA, const int lengthB)
    {
#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)
        {
            if (stringA[i] != stringB[i])
                return stringA[i] - stringB[i];
        }
        return lengthA - lengthB;
#endif
    }


    template <SortDirection sortAscending, SideToSort side>
    inline
    bool sortByFileName(const FileCompareLine& a, const FileCompareLine& b)
    {
        const FileDescrLine* const descrLineA = side == SORT_ON_LEFT ? &a.fileDescrLeft : &a.fileDescrRight;
        const FileDescrLine* const descrLineB = side == SORT_ON_LEFT ? &b.fileDescrLeft : &b.fileDescrRight;

        //presort types: first files, then directories then empty rows
        if (descrLineA->objType == FileDescrLine::TYPE_NOTHING)
            return false;  //empty rows always last
        else if (descrLineB->objType == FileDescrLine::TYPE_NOTHING)
            return true;  //empty rows always last


        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());
            else
                return false;
        }
        else
        {
            if (descrLineB->objType == FileDescrLine::TYPE_DIRECTORY)
                return true;
            else
            {
                const wxChar* stringA = descrLineA->relativeName.c_str();
                const wxChar* stringB = descrLineB->relativeName.c_str();

                size_t pos = descrLineA->relativeName.findFromEnd(GlobalResources::FILE_NAME_SEPARATOR); //start search beginning from end
                if (pos != std::string::npos)
                    stringA += pos + 1;

                pos = descrLineB->relativeName.findFromEnd(GlobalResources::FILE_NAME_SEPARATOR); //start search beginning from end
                if (pos != std::string::npos)
                    stringB += pos + 1;

                return stringSmallerThan<sortAscending>(stringA, stringB);
            }
        }
    }


    template <SortDirection sortAscending, SideToSort side>
    bool sortByRelativeName(const FileCompareLine& a, const FileCompareLine& b)
    {
        const FileDescrLine* const descrLineA = side == SORT_ON_LEFT ? &a.fileDescrLeft : &a.fileDescrRight;
        const FileDescrLine* const descrLineB = side == SORT_ON_LEFT ? &b.fileDescrLeft : &b.fileDescrRight;

        //extract relative name and filename
        const wxChar* const relStringA  = descrLineA->relativeName.c_str(); //mustn't be NULL for CompareString() API to work correctly
        const wxChar* fileStringA = relStringA;
        int relLengthA  = 0;
        int fileLengthA = 0;

        if (descrLineA->objType == FileDescrLine::TYPE_DIRECTORY)
            relLengthA = descrLineA->relativeName.length();
        else if (descrLineA->objType == FileDescrLine::TYPE_FILE)
        {
            relLengthA = descrLineA->relativeName.findFromEnd(GlobalResources::FILE_NAME_SEPARATOR); //start search beginning from end
            if (relLengthA == wxNOT_FOUND)
            {
                relLengthA  = 0;
                fileLengthA = descrLineA->relativeName.length();
            }
            else
            {
                fileStringA += relLengthA + 1;
                fileLengthA = descrLineA->relativeName.length() - (relLengthA + 1);
            }
        }
        else
            return false; //empty rows should be on end of list


        const wxChar* const relStringB = descrLineB->relativeName.c_str(); //mustn't be NULL for CompareString() API to work correctly
        const wxChar* fileStringB = relStringB;
        int relLengthB  = 0;
        int fileLengthB = 0;

        if (descrLineB->objType == FileDescrLine::TYPE_DIRECTORY)
            relLengthB = descrLineB->relativeName.length();
        else if (descrLineB->objType == FileDescrLine::TYPE_FILE)
        {
            relLengthB = descrLineB->relativeName.findFromEnd(GlobalResources::FILE_NAME_SEPARATOR); //start search beginning from end
            if (relLengthB == wxNOT_FOUND)
            {
                relLengthB  = 0;
                fileLengthB = descrLineB->relativeName.length();
            }
            else
            {
                fileStringB += relLengthB + 1;
                fileLengthB = descrLineB->relativeName.length() - (relLengthB + 1);
            }
        }
        else
            return true; //empty rows should be on end of list

        //compare relative names without filenames first
        const int rv = compareString(relStringA, relStringB, relLengthA, relLengthB);
        if (rv != 0)
            return sortAscending == ASCENDING ? rv < 0 : rv > 0;
        else //compare the filenames
        {
            if (descrLineB->objType == FileDescrLine::TYPE_DIRECTORY)  //directories shall appear before files
                return false;
            else if (descrLineA->objType == FileDescrLine::TYPE_DIRECTORY)
                return true;

            return sortAscending == ASCENDING ?
                   compareString(fileStringA, fileStringB, fileLengthA, fileLengthB) < 0 :
                   compareString(fileStringA, fileStringB, fileLengthA, fileLengthB) > 0;
        }
    }


    template <SortDirection sortAscending, SideToSort side>
    inline
    bool sortByFullName(const FileCompareLine& a, const FileCompareLine& b)
    {
        const FileDescrLine* const descrLineA = side == SORT_ON_LEFT ? &a.fileDescrLeft : &a.fileDescrRight;
        const FileDescrLine* const descrLineB = side == SORT_ON_LEFT ? &b.fileDescrLeft : &b.fileDescrRight;

        //presort types: first files, then directories then empty rows
        if (descrLineA->objType == FileDescrLine::TYPE_NOTHING)
            return false;  //empty rows always last
        else if (descrLineB->objType == FileDescrLine::TYPE_NOTHING)
            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;
#else
            return sortAscending == ASCENDING ?
                   descrLineA->fullName.Cmp(descrLineB->fullName) < 0 :
                   descrLineA->fullName.Cmp(descrLineB->fullName) > 0;
#endif
    }


    template <SortDirection sortAscending, SideToSort side>
    inline
    bool sortByFileSize(const FileCompareLine& a, const FileCompareLine& b)
    {
        const FileDescrLine* const descrLineA = side == SORT_ON_LEFT ? &a.fileDescrLeft : &a.fileDescrRight;
        const FileDescrLine* const descrLineB = side == SORT_ON_LEFT ? &b.fileDescrLeft : &b.fileDescrRight;

        //presort types: first files, then directories then empty rows
        if (descrLineA->objType == FileDescrLine::TYPE_NOTHING)
            return false;  //empty rows always last
        else if (descrLineB->objType == FileDescrLine::TYPE_NOTHING)
            return true;  //empty rows always last


        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());
            else
                return false;
        }
        else
        {
            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;
        }
    }


    template <SortDirection sortAscending, SideToSort side>
    inline
    bool sortByDate(const FileCompareLine& a, const FileCompareLine& b)
    {
        const FileDescrLine* const descrLineA = side == SORT_ON_LEFT ? &a.fileDescrLeft : &a.fileDescrRight;
        const FileDescrLine* const descrLineB = side == SORT_ON_LEFT ? &b.fileDescrLeft : &b.fileDescrRight;

        //presort types: first files, then directories then empty rows
        if (descrLineA->objType == FileDescrLine::TYPE_NOTHING)
            return false;  //empty rows always last
        else if (descrLineB->objType == FileDescrLine::TYPE_NOTHING)
            return true;  //empty rows always last

        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());
            else
                return false;
        }
        else
        {
            if (descrLineB->objType == FileDescrLine::TYPE_DIRECTORY)
                return true;
            else
                return sortAscending == ASCENDING ?
                       descrLineA->lastWriteTimeRaw > descrLineB->lastWriteTimeRaw :
                       descrLineA->lastWriteTimeRaw < descrLineB->lastWriteTimeRaw;
        }
    }


    template <SortDirection sortAscending>
    inline
    bool sortByCmpResult(const FileCompareLine& a, const FileCompareLine& b)
    {
        //presort result: equal shall appear at end of list
        if (a.cmpResult == FILE_EQUAL)
            return false;
        if (b.cmpResult == FILE_EQUAL)
            return true;

        return sortAscending == ASCENDING ?
               a.cmpResult < b.cmpResult :
               a.cmpResult > b.cmpResult;
    }


    template <SortDirection sortAscending, 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;

#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;
#elif defined FFS_LINUX
            return sortAscending == ASCENDING ?
                   dirNameA->Cmp(*dirNameB) < 0 :
                   dirNameA->Cmp(*dirNameB) > 0;
#endif
    }
}

#endif // SORTING_H_INCLUDED
bgstack15