forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkGlobFileNames.cxx
155 lines (125 loc) · 4.12 KB
/
vtkGlobFileNames.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkGlobFileNames.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 "vtkGlobFileNames.h"
#include "vtkStringArray.h"
#include "vtkDebugLeaks.h"
#include "vtkObjectFactory.h"
#include <vtksys/Glob.hxx>
#include <vtksys/SystemTools.hxx>
#include <string>
#include <vector>
#include <algorithm>
//----------------------------------------------------------------------------
vtkGlobFileNames* vtkGlobFileNames::New()
{
VTK_STANDARD_NEW_BODY(vtkGlobFileNames)
}
//----------------------------------------------------------------------------
vtkGlobFileNames::vtkGlobFileNames()
{
this->Directory = 0;
this->Pattern = 0;
this->Recurse = 0;
this->FileNames = vtkStringArray::New();
}
//----------------------------------------------------------------------------
vtkGlobFileNames::~vtkGlobFileNames()
{
delete [] this->Directory;
delete [] this->Pattern;
this->FileNames->Delete();
this->FileNames = 0;
}
//----------------------------------------------------------------------------
void vtkGlobFileNames::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Directory: " <<
(this->GetDirectory() ? this->GetDirectory() : " none") << "\n";
os << indent << "Pattern: " <<
(this->GetPattern() ? this->GetPattern() : " none") << "\n";
os << indent << "Recurse: " << (this->GetRecurse() ? "On\n" : "Off\n");
os << indent << "FileNames: (" << this->GetFileNames() << ")\n";
indent = indent.GetNextIndent();
for(int i = 0; i < this->FileNames->GetNumberOfValues(); i++)
{
os << indent << this->FileNames->GetValue(i) << "\n";
}
}
//----------------------------------------------------------------------------
void vtkGlobFileNames::Reset()
{
this->FileNames->Reset();
}
//----------------------------------------------------------------------------
int vtkGlobFileNames::AddFileNames(const char* pattern)
{
this->SetPattern(pattern);
vtksys::Glob glob;
if (this->Recurse)
{
glob.RecurseOn();
}
else
{
glob.RecurseOff();
}
if (!this->Pattern)
{
vtkErrorMacro(<< "FindFileNames: pattern string is null.");
return 0;
}
std::string fullPattern = this->Pattern;
if (this->Directory && this->Directory[0] != '\0')
{
std::vector<std::string> components;
vtksys::SystemTools::SplitPath(fullPattern.c_str(), components);
// If Pattern is a relative path, prepend with Directory
if (components[0] == "")
{
components.insert(components.begin(), this->Directory);
fullPattern = vtksys::SystemTools::JoinPath(components);
}
}
if (!glob.FindFiles(fullPattern))
{
vtkErrorMacro(<< "FindFileNames: Glob action failed for \"" <<
fullPattern << "\"");
return 0;
}
// copy the filenames from glob
std::vector<std::string> files = glob.GetFiles();
// sort them lexicographically
std::sort(files.begin(), files.end());
// add them onto the list of filenames
for ( std::vector<std::string>::const_iterator iter = files.begin();
iter != files.end();
++iter)
{
this->FileNames->InsertNextValue(iter->c_str());
}
return 1;
}
//----------------------------------------------------------------------------
const char* vtkGlobFileNames::GetNthFileName(int index)
{
if(index >= this->FileNames->GetNumberOfValues() || index < 0)
{
vtkErrorMacro( << "Bad index for GetFileName on vtkGlobFileNames\n");
return 0;
}
return this->FileNames->GetValue(index).c_str();
}
//----------------------------------------------------------------------------
int vtkGlobFileNames::GetNumberOfFileNames()
{
return this->FileNames->GetNumberOfValues();
}