summaryrefslogtreecommitdiff
path: root/ui/sorting.h
blob: 5771d7db0e7a3278dc68f7fad5f22b575b3d82cf (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
#ifndef SORTING_H_INCLUDED
#define SORTING_H_INCLUDED

#include "../fileHierarchy.h"
#include "../shared/systemConstants.h"
#include "../synchronization.h"
#include "../shared/staticAssert.h"


namespace FreeFileSync
{
inline
int compareString(const Zstring& stringA, const Zstring& stringB)
{
#ifdef FFS_WIN //Windows does NOT distinguish between upper/lower-case
    return stringA.CmpNoCase(stringB);
#elif defined FFS_LINUX //Linux DOES distinguish between upper/lower-case
    return stringA.Cmp(stringB);
#endif
}


template <bool ascending>
struct Compare
{
    template <class T>
    bool isSmallerThan(T a, T b)
    {
        assert_static(sizeof(T) <= 2 * sizeof(int)); //use for comparing (small) INTEGRAL types only!
        return a < b;
    }
};
template <>
struct Compare<false>
{
    template <class T>
    bool isSmallerThan(T a, T b)
    {
        assert_static(sizeof(T) <= 2 * sizeof(int)); //use for comparing (small) INTEGRAL types only!
        return a > b;
    }
};


inline
bool stringSmallerThan(const Zstring& stringA, const Zstring& stringB)
{
    return compareString(stringA, stringB) < 0;
}


template <bool ascending, SelectedSide side>
inline
bool sortByFileName(const FileSystemObject& a, const FileSystemObject& b)
{
    //presort types: first files, then directories then empty rows
    if (a.isEmpty<side>())
        return false;  //empty rows always last
    else if (b.isEmpty<side>())
        return true;  //empty rows always last


    if (isDirectoryMapping(a)) //sort directories by relative name
    {
        if (isDirectoryMapping(b))
            return stringSmallerThan(a.getRelativeName<side>(), b.getRelativeName<side>());
        else
            return false;
    }
    else
    {
        if (isDirectoryMapping(b))
            return true;
        else
            return Compare<ascending>().isSmallerThan(
                       compareString(a.getShortName<side>(), b.getShortName<side>()), 0);
    }
}


template <bool ascending, SelectedSide side>
bool sortByRelativeName(const FileSystemObject& a, const FileSystemObject& b)
{
    if (a.isEmpty<side>())
        return false;  //empty rows always last
    else if (b.isEmpty<side>())
        return true;  //empty rows always last

    const bool isDirectoryA = isDirectoryMapping(a);
    const Zstring relDirNameA = isDirectoryA ?
                                a.getRelativeName<side>() : //directory
                                a.getParentRelativeName();  //file

    const bool isDirectoryB = isDirectoryMapping(b);
    const Zstring relDirNameB = isDirectoryB ?
                                b.getRelativeName<side>() : //directory
                                b.getParentRelativeName();  //file


    //compare relative names without filenames first
    const int rv = compareString(relDirNameA, relDirNameB);
    if (rv != 0)
        return Compare<ascending>().isSmallerThan(rv, 0);
    else //compare the filenames
    {
        if (isDirectoryB) //directories shall appear before files
            return false;
        else if (isDirectoryA)
            return true;

        return stringSmallerThan(a.getShortName<side>(), b.getShortName<side>());
    }
}


template <bool ascending, SelectedSide side>
inline
bool sortByFileSize(const FileSystemObject& a, const FileSystemObject& b)
{
    if (a.isEmpty<side>())
        return false;  //empty rows always last
    else if (b.isEmpty<side>())
        return true;  //empty rows always last


    const FileMapping* fileObjA = dynamic_cast<const FileMapping*>(&a);
    const FileMapping* fileObjB = dynamic_cast<const FileMapping*>(&b);

    if (fileObjA == NULL)
        return false; //directories last
    else if (fileObjB == NULL)
        return true;  //directories last

    //sortAscending shall result in list beginning with largest files first
    return Compare<!ascending>().isSmallerThan(fileObjA->getFileSize<side>(), fileObjB->getFileSize<side>());
}


template <bool ascending, SelectedSide side>
inline
bool sortByDate(const FileSystemObject& a, const FileSystemObject& b)
{
    if (a.isEmpty<side>())
        return false;  //empty rows always last
    else if (b.isEmpty<side>())
        return true;  //empty rows always last


    const FileMapping* fileObjA = dynamic_cast<const FileMapping*>(&a);
    const FileMapping* fileObjB = dynamic_cast<const FileMapping*>(&b);

    if (fileObjA == NULL)
        return false; //directories last
    else if (fileObjB == NULL)
        return true;  //directories last

    return Compare<!ascending>().isSmallerThan(fileObjA->getLastWriteTime<side>(), fileObjB->getLastWriteTime<side>());
}


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

    return Compare<ascending>().isSmallerThan(a.getCategory(), b.getCategory());
}


template <bool ascending>
inline
bool sortBySyncDirection(const FileSystemObject& a, const FileSystemObject& b)
{
    return Compare<ascending>().isSmallerThan(a.getSyncOperation(), b.getSyncOperation());
}
}

#endif // SORTING_H_INCLUDED
bgstack15