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
|
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
// * Copyright (C) 2008-2010 ZenJu (zhnmju123 AT gmx.de) *
// **************************************************************************
//
#ifndef SORTING_H_INCLUDED
#define SORTING_H_INCLUDED
#include "../file_hierarchy.h"
#include "../shared/system_constants.h"
#include "../synchronization.h"
#include "../shared/assert_static.h"
namespace ffs3
{
namespace
{
struct CompileTimeReminder : public FSObjectVisitor
{
virtual void visit(const FileMapping& fileObj) {}
virtual void visit(const SymLinkMapping& linkObj) {}
virtual void visit(const DirMapping& dirObj) {}
} checkDymanicCasts; //just a compile-time reminder to check dynamic casts in this file
}
inline
bool isDirectoryMapping(const FileSystemObject& fsObj)
{
return dynamic_cast<const DirMapping*>(&fsObj) != NULL;
}
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;
}
};
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 cmpFileName(a.getRelativeName<side>(), b.getRelativeName<side>()) < 0;
else
return false;
}
else
{
if (isDirectoryMapping(b))
return true;
else
return Compare<ascending>().isSmallerThan(
cmpFileName(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 or symlink
const bool isDirectoryB = isDirectoryMapping(b);
const Zstring relDirNameB = isDirectoryB ?
b.getRelativeName<side>() : //directory
b.getParentRelativeName(); //file or symlink
//compare relative names without filenames first
const int rv = cmpFileName(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 cmpFileName(a.getShortName<side>(), b.getShortName<side>()) < 0;
}
}
template <bool ascending, SelectedSide side>
inline
bool sortByFileSize(const FileSystemObject& a, const FileSystemObject& b)
{
//empty rows always last
if (a.isEmpty<side>())
return false;
else if (b.isEmpty<side>())
return true;
const bool isDirA = dynamic_cast<const DirMapping*>(&a) != NULL;
const bool isDirB = dynamic_cast<const DirMapping*>(&b) != NULL;
//directories second last
if (isDirA)
return false;
else if (isDirB)
return true;
const FileMapping* fileObjA = dynamic_cast<const FileMapping*>(&a);
const FileMapping* fileObjB = dynamic_cast<const FileMapping*>(&b);
//then symlinks
if (fileObjA == NULL)
return false;
else if (fileObjB == NULL)
return true;
//return 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);
const SymLinkMapping* linkObjA = dynamic_cast<const SymLinkMapping*>(&a);
const SymLinkMapping* linkObjB = dynamic_cast<const SymLinkMapping*>(&b);
if (!fileObjA && !linkObjA)
return false; //directories last
else if (!fileObjB && !linkObjB)
return true; //directories last
const wxLongLong& dateA = fileObjA ? fileObjA->getLastWriteTime<side>() : linkObjA->getLastWriteTime<side>();
const wxLongLong& dateB = fileObjB ? fileObjB->getLastWriteTime<side>() : linkObjB->getLastWriteTime<side>();
//return list beginning with newest files first
return Compare<ascending>().isSmallerThan(dateA, dateB);
}
template <bool ascending, SelectedSide side>
inline
bool sortByExtension(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->getExtension<side>(), fileObjB->getExtension<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
|