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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkGlobFileNames.h"
#include "vtkStringArray.h"
#include "vtkDebugLeaks.h"
#include "vtkObjectFactory.h"
#include <algorithm>
#include <string>
#include <vector>
#include <vtksys/Glob.hxx>
#include <vtksys/SystemTools.hxx>
//------------------------------------------------------------------------------
VTK_ABI_NAMESPACE_BEGIN
vtkGlobFileNames* vtkGlobFileNames::New()
{
VTK_STANDARD_NEW_BODY(vtkGlobFileNames);
}
//------------------------------------------------------------------------------
vtkGlobFileNames::vtkGlobFileNames()
{
this->Directory = nullptr;
this->Pattern = nullptr;
this->Recurse = 0;
this->FileNames = vtkStringArray::New();
}
//------------------------------------------------------------------------------
vtkGlobFileNames::~vtkGlobFileNames()
{
delete[] this->Directory;
delete[] this->Pattern;
this->FileNames->Delete();
this->FileNames = nullptr;
}
//------------------------------------------------------------------------------
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, components);
// If Pattern is a relative path, prepend with Directory
if (components[0].empty())
{
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 nullptr;
}
return this->FileNames->GetValue(index).c_str();
}
//------------------------------------------------------------------------------
int vtkGlobFileNames::GetNumberOfFileNames()
{
return this->FileNames->GetNumberOfValues();
}
VTK_ABI_NAMESPACE_END
|