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

#include "../fileHierarchy.h"
#include "../shared/systemConstants.h"
#include "../synchronization.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
    }


    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 (dynamic_cast<const DirMapping*>(&a)) //sort directories by relative name
        {
            if (dynamic_cast<const DirMapping*>(&b))
                return stringSmallerThan(a.getRelativeName<side>(), b.getRelativeName<side>());
            else
                return false;
        }
        else
        {
            if (dynamic_cast<const DirMapping*>(&b))
                return true;
            else
            {
                return ascending ?
                       stringSmallerThan(a.getShortName<side>(), b.getShortName<side>()) :
                       stringSmallerThan(b.getShortName<side>(), a.getShortName<side>());
            }
        }
    }


    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 FileMapping* fileObjA = dynamic_cast<const FileMapping*>(&a);
        const Zstring relDirNameA = fileObjA != NULL ?
                                    a.getParentRelativeName<side>() : //file
                                    a.getRelativeName<side>();        //directory

        const FileMapping* fileObjB = dynamic_cast<const FileMapping*>(&b);
        const Zstring relDirNameB = fileObjB != NULL ?
                                    b.getParentRelativeName<side>() : //file
                                    b.getRelativeName<side>();        //directory

        //compare relative names without filenames first
        const int rv = compareString(relDirNameA, relDirNameB);
        if (rv != 0)
            return ascending ? rv < 0 : rv > 0;
        else //compare the filenames
        {
            if (fileObjB == NULL) //directories shall appear before files
                return false;
            else if (fileObjA == NULL)
                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

        return ascending ?
               fileObjA->getFileSize<side>() > fileObjB->getFileSize<side>() : //sortAscending shall result in list beginning with largest files first
               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 ascending ?
               fileObjA->getLastWriteTime<side>() > fileObjB->getLastWriteTime<side>() :
               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 ascending ?
               a.getCategory() < b.getCategory() :
               a.getCategory() > b.getCategory();
    }


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

#endif // SORTING_H_INCLUDED
bgstack15