forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkSortFileNames.cxx
592 lines (506 loc) · 14.2 KB
/
vtkSortFileNames.cxx
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSortFileNames.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkSortFileNames.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include "vtkStringArray.h"
#include <vtkstd/string>
#include <vtkstd/vector>
#include <vtkstd/list>
#include <vtkstd/algorithm>
#include <vtksys/SystemTools.hxx>
#include <ctype.h>
vtkStandardNewMacro(vtkSortFileNames);
// a container for holding string arrays
class vtkStringArrayVector
{
public:
typedef vtkstd::vector<vtkSmartPointer<vtkStringArray> > VectorType;
static vtkStringArrayVector *New() {
return new vtkStringArrayVector; };
void Delete() {
delete this; };
void Reset() {
this->Container.clear(); };
void InsertNextStringArray(vtkStringArray *stringArray) {
this->Container.push_back(stringArray); };
vtkStringArray *GetStringArray(int i) {
return this->Container[static_cast<VectorType::size_type>(i)]; };
int GetNumberOfStringArrays() {
return static_cast<int>(this->Container.size()); };
private:
vtkStringArrayVector() : Container() {};
~vtkStringArrayVector() { this->Container.clear(); };
VectorType Container;
};
//----------------------------------------------------------------------------
vtkSortFileNames::vtkSortFileNames()
{
this->InputFileNames = 0;
this->NumericSort = 0;
this->IgnoreCase = 0;
this->Grouping = 0;
this->SkipDirectories = 0;
this->FileNames = vtkStringArray::New();
this->Groups = vtkStringArrayVector::New();
}
vtkSortFileNames::~vtkSortFileNames()
{
if (this->InputFileNames)
{
this->InputFileNames->Delete();
this->InputFileNames = 0;
}
if (this->FileNames)
{
this->FileNames->Delete();
this->FileNames = 0;
}
if (this->Groups)
{
this->Groups->Delete();
this->Groups = 0;
}
}
void vtkSortFileNames::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "InputFileNames: (" << this->GetInputFileNames() << ")\n";
os << indent << "NumericSort: " <<
(this->GetNumericSort() ? "On\n" : "Off\n");
os << indent << "IgnoreCase: " <<
(this->GetIgnoreCase() ? "On\n" : "Off\n");
os << indent << "Grouping: " <<
(this->GetGrouping() ? "On\n" : "Off\n");
os << indent << "SkipDirectories: " <<
(this->GetSkipDirectories() ? "On\n" : "Off\n");
os << indent << "NumberOfGroups: " << this->GetNumberOfGroups() << "\n";
if (this->GetGrouping())
{
for (int i = 0; i < this->GetNumberOfGroups(); i++)
{
os << indent.GetNextIndent() << "Group[" << i << "]: (" <<
this->GetNthGroup(i) << ")\n";
}
}
else
{
os << indent.GetNextIndent() << "FileNames: ("
<< this->GetFileNames() << ")\n";
}
}
void vtkSortFileNames::SetInputFileNames(vtkStringArray *input)
{
vtkSetObjectBodyMacro(InputFileNames, vtkStringArray, input);
}
int vtkSortFileNames::GetNumberOfGroups()
{
this->Update();
return this->Groups->GetNumberOfStringArrays();
}
vtkStringArray *vtkSortFileNames::GetNthGroup(int i)
{
this->Update();
if (!this->GetGrouping())
{
vtkErrorMacro(<< "GetNthGroup(): Grouping not on.");
return 0;
}
int n = this->Groups->GetNumberOfStringArrays();
if (i >= 0 && i < n)
{
return this->Groups->GetStringArray(i);
}
else
{
vtkErrorMacro(<< "GetNthGroup(i): index " << i << " is out of range");
return 0;
}
}
vtkStringArray *vtkSortFileNames::GetFileNames()
{
this->Update();
return this->FileNames;
}
void vtkSortFileNames::GroupFileNames(vtkStringArray *input,
vtkStringArrayVector *output)
{
vtkstd::string baseName;
vtkstd::string extension;
vtkstd::string fileNamePath;
vtkstd::string reducedName;
vtkstd::list<unsigned int> ungroupedFiles;
vtkstd::vector<vtkstd::string> reducedFileNames;
unsigned int numberOfStrings = input->GetNumberOfValues();
for (unsigned int i = 0; i < numberOfStrings; i++)
{
vtkstd::string& fileName = input->GetValue(i);
extension = vtksys::SystemTools::GetFilenameLastExtension(fileName);
fileNamePath = vtksys::SystemTools::GetFilenamePath(fileName);
baseName = vtksys::SystemTools::GetFilenameWithoutLastExtension(fileName);
// If extension is all digits, it is not a true extension, so
// add it back onto the filename. Note that the extension
// includes the leading dot.
int numericExtension = 1;
for (unsigned int j = 1; j < extension.length(); j++)
{
if (!(extension[j] >= '0' && extension[j] <= '9'))
{
numericExtension = 0;
}
}
if (numericExtension && extension.length() != 0)
{
baseName.append(extension);
extension = "";
}
// Create a reduced filename that replaces all digit sequences
// in the filename with a single digit "0". We begin by setting
// the reduced filename to the path.
reducedName = fileNamePath + "/";
int inDigitBlock = 0;
unsigned int charBlockStart = 0;
unsigned int stringLength = static_cast<unsigned int>(baseName.length());
for (unsigned int k = 0; k < stringLength; k++)
{
if (baseName[k] >= '0' && baseName[k] <= '9')
{
if (!inDigitBlock && k != 0)
{
reducedName.append(baseName.substr(charBlockStart,
k-charBlockStart));
reducedName.append("0");
}
inDigitBlock = 1;
}
else
{
if (inDigitBlock)
{
charBlockStart = k;
inDigitBlock = 0;
}
}
}
if (!inDigitBlock)
{
reducedName.append(baseName.substr(charBlockStart,
stringLength-charBlockStart));
}
// Add extension back to the filename.
reducedName.append(extension);
// If IgnoreCase is set, change to uppercase.
if (this->IgnoreCase)
{
unsigned int n = static_cast<unsigned int>(reducedName.length());
for (unsigned int j = 0; j < n; j++)
{
reducedName[j] = toupper(reducedName[j]);
}
}
// The reduced filename has each block of digits replaced with "0".
reducedFileNames.push_back(reducedName);
// push the index onto the "ungrouped" list
ungroupedFiles.push_back(i);
}
// now loop through all files and find all matches
while (!ungroupedFiles.empty())
{
// get the first element in the list
unsigned int fileIndex = ungroupedFiles.front();
vtkstd::string& reducedFileName = reducedFileNames[fileIndex];
vtkStringArray *newGroup = vtkStringArray::New();
// find all matches and move them into the group
vtkstd::list<unsigned int>::iterator p = ungroupedFiles.begin();
while (p != ungroupedFiles.end())
{
unsigned int tryIndex = *p;
if (reducedFileName == reducedFileNames[tryIndex])
{
newGroup->InsertNextValue(input->GetValue(tryIndex));
p = ungroupedFiles.erase(p);
}
else
{
p++;
}
}
// add the group to the output
output->InsertNextStringArray(newGroup);
newGroup->Delete();
}
}
// Sort filenames lexicographically, ignoring case.
bool vtkCompareFileNamesIgnoreCase(const vtkstd::string s1,
const vtkstd::string s2)
{
unsigned int n1 = static_cast<unsigned int>(s1.length());
unsigned int n2 = static_cast<unsigned int>(s2.length());
// find the minimum of the two lengths
unsigned int n = n1;
if (n > n2)
{
n = n2;
}
// compare the strings with no case
for (unsigned int i = 0; i < n; i++)
{
char c1 = toupper(s1[i]);
char c2 = toupper(s2[i]);
if (c1 < c2)
{
return 1;
}
if (c1 > c2)
{
return 0;
}
}
// if it is a tie, then the short string is "less"
if (n1 < n2)
{
return 1;
}
// if strings are equal, use case-sensitive comparison to break tie
if (n1 == n2)
{
return (s1 < s2);
}
// otherwise, if n1 > n2, then n1 wins
return 0;
}
// Sort filenames numerically
bool vtkCompareFileNamesNumeric(const vtkstd::string s1,
const vtkstd::string s2)
{
unsigned int n1 = static_cast<unsigned int>(s1.length());
unsigned int n2 = static_cast<unsigned int>(s2.length());
// compare the strings numerically
unsigned int i1 = 0;
unsigned int i2 = 0;
while (i1 < n1 && i2 < n2)
{
char c1 = s1[i1++];
char c2 = s2[i2++];
if (0 && (c1 >= '0' && c1 <= '9') && (c2 >= '0' && c2 <= '9'))
{
// convert decimal numeric sequence into an integer
unsigned int j1 = 0;
while (c1 >= '0' && c1 <= '9')
{
j1 = (j1 << 3) + (j1 << 1) + (c1 - '0');
if (i1 == n1)
{
break;
}
c1 = s1[i1++];
}
// convert decimal numeric sequence into an integer
unsigned int j2 = 0;
while (c2 >= '0' && c2 <= '9')
{
j2 = (j2 << 3) + (j2 << 1) + (c2 - '0');
if (i2 == n2)
{
break;
}
c2 = s2[i2++];
}
// perform the numeric comparison
if (j1 < j2)
{
return 1;
}
if (j1 > j2)
{
return 0;
}
}
// case-insensitive lexicographic comparison of non-digits
if ((c1 < '0' || c1 > '9') || (c2 < '0' || c2 > '9'))
{
if (c1 < c2)
{
return 1;
}
if (c1 > c2)
{
return 0;
}
}
}
// if it is a tie, then the shorter string is "less"
if ((n1 - i1) < (n2 - i2))
{
return 1;
}
// if strings are otherwise equal, fall back to default to break tie
if ((i1 == n1) && (i2 == n2))
{
return (s1 < s2);
}
// otherwise, return false
return 0;
}
// Sort filenames numerically
bool vtkCompareFileNamesNumericIgnoreCase(const vtkstd::string s1,
const vtkstd::string s2)
{
unsigned int n1 = static_cast<unsigned int>(s1.length());
unsigned int n2 = static_cast<unsigned int>(s2.length());
// compare the strings numerically
unsigned int i1 = 0;
unsigned int i2 = 0;
while (i1 < n1 && i2 < n2)
{
char c1 = s1[i1++];
char c2 = s2[i2++];
if ((c1 >= '0' && c1 <= '9') && (c2 >= '0' && c2 <= '9'))
{
// convert decimal numeric sequence into an integer
unsigned int j1 = 0;
while (c1 >= '0' && c1 <= '9')
{
j1 = (j1 << 3) + (j1 << 1) + (c1 - '0');
if (i1 == n1)
{
break;
}
c1 = s1[i1++];
}
// convert decimal numeric sequence into an integer
unsigned int j2 = 0;
while (c2 >= '0' && c2 <= '9')
{
j2 = (j2 << 3) + (j2 << 1) + (c2 - '0');
if (i2 == n2)
{
break;
}
c2 = s2[i2++];
}
// perform the numeric comparison
if (j1 < j2)
{
return 1;
}
if (j1 > j2)
{
return 0;
}
}
// case-insensitive lexicographic comparison of non-digits
if ((c1 < '0' || c1 > '9') || (c2 < '0' || c2 > '9'))
{
c1 = toupper(c1);
c2 = toupper(c2);
if (c1 < c2)
{
return 1;
}
if (c1 > c2)
{
return 0;
}
}
}
// if it is a tie, then the shorter string is "less"
if ((n1 - i1) < (n2 - i2))
{
return 1;
}
// if strings are otherwise equal, fall back to default to break tie
if ((i1 == n1) && (i2 == n2))
{
return vtkCompareFileNamesIgnoreCase(s1, s2);
}
// otherwise, return false
return 0;
}
void vtkSortFileNames::SortFileNames(vtkStringArray *input,
vtkStringArray *output)
{
// convert vtkStringArray into an STL vector
vtkstd::vector<vtkstd::string> fileNames;
unsigned int numberOfStrings = input->GetNumberOfValues();
for (unsigned int j = 0; j < numberOfStrings; j++)
{
vtkstd::string& fileName = input->GetValue(j);
// skip anything that is a directory
if (this->SkipDirectories &&
vtksys::SystemTools::FileIsDirectory(fileName.c_str()))
{
continue;
}
// build a new list
fileNames.push_back(fileName);
}
// perform the sort according to the options that are set
if (this->IgnoreCase)
{
if (this->NumericSort)
{
// numeric sort without case sensitivity
vtkstd::sort(fileNames.begin(), fileNames.end(),
vtkCompareFileNamesNumericIgnoreCase);
}
else
{
// lexicographic sort without case sensitivity
vtkstd::sort(fileNames.begin(), fileNames.end(),
vtkCompareFileNamesIgnoreCase);
}
}
else
{
if (this->NumericSort)
{
// numeric sort
vtkstd::sort(fileNames.begin(), fileNames.end(),
vtkCompareFileNamesNumeric);
}
else
{
// lexicographic sort (the default)
vtkstd::sort(fileNames.begin(), fileNames.end());
}
}
// build the output
vtkstd::vector<vtkstd::string>::iterator iter = fileNames.begin();
while (iter < fileNames.end())
{
output->InsertNextValue(*iter++);
}
}
void vtkSortFileNames::Execute()
{
// sort the input file names
this->FileNames->Reset();
this->SortFileNames(this->InputFileNames, this->FileNames);
// group the sorted files if grouping is on
this->Groups->Reset();
if (this->Grouping)
{
this->GroupFileNames(this->FileNames, this->Groups);
}
}
void vtkSortFileNames::Update()
{
if (this->InputFileNames != 0)
{
if (this->GetMTime() > this->UpdateTime.GetMTime() ||
this->InputFileNames->GetMTime() > this->UpdateTime.GetMTime())
{
this->Execute();
this->UpdateTime.Modified();
}
}
}