-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsorting.cpp
312 lines (267 loc) · 8.05 KB
/
sorting.cpp
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
306
307
308
309
310
311
// Copyright (c) 2024 by Christopher Antos
// License: http://opensource.org/licenses/MIT
// vim: set et ts=4 sw=4 cino={0s:
#include "pch.h"
#include "flags.h"
#include "sorting.h"
#include "patterns.h"
static bool s_ascii_sort = false;
static bool s_reverse_all = false;
static bool s_explicit_extension = false;
// Reverses the combination of g_sort_order and s_reverse_all.
static bool s_reverse_sort_order = false;
WCHAR g_sort_order[10] = L"";
void SetSortOrder(const WCHAR* order, Error& e)
{
s_ascii_sort = false;
s_reverse_all = false;
s_explicit_extension = false;
ZeroMemory(g_sort_order, sizeof(g_sort_order));
WCHAR* set = g_sort_order;
SkipColonOrEqual(order);
if (wcscmp(order, L"-") == 0)
return;
if (!*order)
*(set++) = 'g';
else if (!wcsicmp(order, L"u"))
order++;
while (*order)
{
if (*order == '-')
{
*(set++) = '-';
}
else if (*order == 'a')
{
s_ascii_sort = true;
}
else if (*order == 'r')
{
s_reverse_all = true;
}
else
{
if (*order == 'e')
s_explicit_extension = true;
if (!wcschr(L"nesgdc", *order))
{
WCHAR tmp[2];
tmp[0] = *order;
tmp[1] = 0;
e.Set(L"Invalid sort order '%1'.") << tmp;
return;
}
if (!wcschr(g_sort_order, *order))
*(set++) = *order;
}
++order;
}
if (!wcscmp(g_sort_order, L"-") ||
(g_sort_order[0] && !wcschr(g_sort_order, 'n')))
{
*(set++) = 'n';
}
assert(!*set);
}
void SetReverseSort(bool reverse)
{
s_reverse_sort_order = reverse;
}
inline bool IsBeginNumeric(const WCHAR* p)
{
return (*p >= '0' && *p <= '9');
}
static LONG ParseNum(const WCHAR*& p)
{
LONG num = 0;
while (*p <= '9' && *p >= '0')
{
num *= 10;
num += (*p) - '0';
p++;
}
return num;
}
DWORD g_dwCmpStrFlags = 0;
int Sorting::CmpStrN(const WCHAR* p1, int len1, const WCHAR* p2, int len2)
{
const int n = CompareStringW(LOCALE_USER_DEFAULT, g_dwCmpStrFlags, p1, len1, p2, len2);
if (!n)
{
assert(false);
return 0;
}
return n - 2;
}
int Sorting::CmpStrNI(const WCHAR* p1, int len1, const WCHAR* p2, int len2)
{
const int n = CompareStringW(LOCALE_USER_DEFAULT, g_dwCmpStrFlags|NORM_IGNORECASE, p1, len1, p2, len2);
if (!n)
{
assert(false);
return 0;
}
return n - 2;
}
static int StrAlphaNumCompare(const WCHAR* p1, const WCHAR* p2)
{
while (true)
{
const WCHAR* end1 = p1;
const WCHAR* end2 = p2;
// Compare a run of text.
if (g_dwCmpStrFlags & SORT_STRINGSORT)
{
while (*end1 &&
*end2 &&
!(IsBeginNumeric(end1) && IsBeginNumeric(end2)))
{
end1 = CharNext(end1);
end2 = CharNext(end2);
}
if (!IsBeginNumeric(end1))
{
while (*end1)
end1 = CharNext(end1);
}
if (!IsBeginNumeric(end2))
{
while (*end2)
end2 = CharNext(end2);
}
}
else
{
while (*end1 && !IsBeginNumeric(end1))
end1 = CharNext(end1);
while (*end2 && !IsBeginNumeric(end2))
end2 = CharNext(end2);
}
assert(implies(*end1, IsBeginNumeric(end1)));
assert(implies(*end2, IsBeginNumeric(end2)));
const int result = Sorting::CmpStrNI(p1, int(end1 - p1), p2, int(end2 - p2));
if (result || !*end1)
return result;
p1 = end1;
p2 = end2;
// Compare a run of digits.
assert(!*p1 || IsBeginNumeric(p1));
assert(!*p2 || IsBeginNumeric(p2));
const LONG num1 = ParseNum(p1); // passed by reference
const LONG num2 = ParseNum(p2); // passed by reference
if (num1 < num2)
return -1;
if (num1 > num2)
return 1;
}
}
class NameSplitter
{
public:
NameSplitter(const WCHAR* p, unsigned index) : m_p(const_cast<WCHAR*>(p) + index), m_ch(p[index]) { *m_p = 0; }
~NameSplitter() { *m_p = m_ch; }
private:
WCHAR* const m_p;
const WCHAR m_ch;
};
bool CmpFileInfo(const std::unique_ptr<FileInfo>& fi1, const std::unique_ptr<FileInfo>& fi2)
{
assert(g_settings);
const FileInfo* const pfi1 = fi1.get();
const FileInfo* const pfi2 = fi2.get();
const bool is_file1 = !(pfi1->GetAttributes() & FILE_ATTRIBUTE_DIRECTORY);
const bool is_file2 = !(pfi2->GetAttributes() & FILE_ATTRIBUTE_DIRECTORY);
const WCHAR* const name1 = pfi1->GetLongName().Text();
const WCHAR* const name2 = pfi2->GetLongName().Text();
const WCHAR* const _ext1 = FindExtension(name1);
const WCHAR* const _ext2 = FindExtension(name2);
const unsigned name_len1 = _ext1 ? unsigned(_ext1 - name1) : unsigned(pfi1->GetLongName().Length());
const unsigned name_len2 = _ext2 ? unsigned(_ext2 - name2) : unsigned(pfi2->GetLongName().Length());
const WCHAR* const ext1 = _ext1 ? _ext1 : L"";
const WCHAR* const ext2 = _ext2 ? _ext2 : L"";
int n = 0;
for (const WCHAR* order = g_sort_order; !n && *order; order++)
{
bool reverse = (*order == '-');
if (reverse)
{
order++;
if (!*order)
break;
}
reverse = (reverse ^ s_reverse_all ^ s_reverse_sort_order);
switch (*order)
{
case 'g':
if (is_file1 != is_file2)
n = is_file1 ? 1 : -1;
break;
case 'n':
if (s_explicit_extension)
{
NameSplitter split1(name1, name_len1);
NameSplitter split2(name2, name_len2);
n = StrAlphaNumCompare(name1, name2);
if (!n)
{
// The lengths can differ when the strings are equal
// if they contain digits (e.g. "foo001" vs "foo1").
if (name_len1 < name_len2)
n = -1;
else if (name_len1 > name_len2)
n = 1;
else
n = Sorting::CmpStrI(ext1, ext2);
}
}
else
{
if (s_ascii_sort)
n = Sorting::CmpStrI(name1, name2);
else
n = StrAlphaNumCompare(name1, name2);
}
break;
case 'e':
if (s_ascii_sort)
n = Sorting::CmpStrI(ext1, ext2);
else
n = StrAlphaNumCompare(ext1, ext2);
break;
case 's':
{
const unsigned __int64 cb1 = pfi1->GetFileSize(g_settings->m_whichfilesize);
const unsigned __int64 cb2 = pfi2->GetFileSize(g_settings->m_whichfilesize);
if (cb1 < cb2)
n = -1;
else if (cb1 > cb2)
n = 1;
}
break;
case 'd':
{
const FILETIME* const pft1 = &pfi1->GetFileTime(g_settings->m_whichtimestamp);
const FILETIME* const pft2 = &pfi2->GetFileTime(g_settings->m_whichtimestamp);
n = CompareFileTime(pft1, pft2);
}
break;
case 'c':
{
const float ratio1 = pfi1->GetCompressionRatio();
const float ratio2 = pfi2->GetCompressionRatio();
if (ratio1 < ratio2)
n = -1;
else if (ratio1 > ratio2)
n = 1;
}
break;
}
if (reverse)
n = -n;
}
return n < 0;
}
bool CmpSubDirs(const std::unique_ptr<SubDir>& d1, const std::unique_ptr<SubDir>& d2)
{
return Sorting::CmpStrI(d1->dir.Text(), d2->dir.Text()) < 0;
}