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
|
// **************************************************************************
// * 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) ZenJu (zhnmju123 AT gmx DOT de) - All Rights Reserved *
// **************************************************************************
#ifndef SORTING_H_INCLUDED
#define SORTING_H_INCLUDED
#include <zen/assert_static.h>
#include "../file_hierarchy.h"
#include <zen/type_tools.h>
//#include "../synchronization.h"
namespace zen
{
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) != nullptr;
}
template <bool ascending, SelectedSide side> inline
bool lessShortFileName(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 LessFilename()(a.getRelativeName<side>(), b.getRelativeName<side>());
else
return false;
}
else
{
if (isDirectoryMapping(b))
return true;
else
return makeSortDirection(LessFilename(), Int2Type<ascending>())(a.getShortName<side>(), b.getShortName<side>());
}
}
template <bool ascending> //side currently unused!
bool lessRelativeName(const FileSystemObject& a, const FileSystemObject& b)
{
const bool isDirectoryA = isDirectoryMapping(a);
const Zstring& relDirNameA = isDirectoryA ?
a.getObjRelativeName() : //directory
beforeLast(a.getObjRelativeName(), FILE_NAME_SEPARATOR); //returns empty string if ch not found
const bool isDirectoryB = isDirectoryMapping(b);
const Zstring& relDirNameB = isDirectoryB ?
b.getObjRelativeName() : //directory
beforeLast(b.getObjRelativeName(), FILE_NAME_SEPARATOR); //returns empty string if ch not found
//compare relative names without filenames first
const int rv = cmpFileName(relDirNameA, relDirNameB);
if (rv != 0)
return makeSortDirection(std::less<int>(), Int2Type<ascending>())(rv, 0);
else //compare the filenames
{
if (isDirectoryB) //directories shall appear before files
return false;
else if (isDirectoryA)
return true;
return LessFilename()(a.getObjShortName(), b.getObjShortName());
}
}
template <bool ascending, SelectedSide side> inline
bool lessFilesize(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) != nullptr;
const bool isDirB = dynamic_cast<const DirMapping*>(&b) != nullptr;
//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)
return false;
else if (!fileObjB)
return true;
//return list beginning with largest files first
return makeSortDirection(std::less<UInt64>(), Int2Type<ascending>())(fileObjA->getFileSize<side>(), fileObjB->getFileSize<side>());
}
template <bool ascending, SelectedSide side> inline
bool lessFiletime(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
zen::Int64 dateA = fileObjA ? fileObjA->getLastWriteTime<side>() : linkObjA->getLastWriteTime<side>();
zen::Int64 dateB = fileObjB ? fileObjB->getLastWriteTime<side>() : linkObjB->getLastWriteTime<side>();
//return list beginning with newest files first
return makeSortDirection(std::less<Int64>(), Int2Type<ascending>())(dateA, dateB);
}
template <bool ascending, SelectedSide side> inline
bool lessExtension(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)
return false; //directories last
else if (!fileObjB)
return true; //directories last
return makeSortDirection(LessFilename(), Int2Type<ascending>())(fileObjA->getExtension<side>(), fileObjB->getExtension<side>());
}
template <bool ascending> inline
bool lessCmpResult(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 makeSortDirection(std::less<CompareFilesResult>(), Int2Type<ascending>())(a.getCategory(), b.getCategory());
}
template <bool ascending> inline
bool lessSyncDirection(const FileSystemObject& a, const FileSystemObject& b)
{
return makeSortDirection(std::less<SyncOperation>(), Int2Type<ascending>())(a.getSyncOperation(), b.getSyncOperation());
}
}
#endif // SORTING_H_INCLUDED
|