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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkIOSSFilesScanner.h"
#include "vtkObjectFactory.h"
#include <vtksys/Directory.hxx>
#include <vtksys/FStream.hxx>
#include <vtksys/RegularExpression.hxx>
#include <vtksys/SystemTools.hxx>
#include <algorithm>
#include <cctype>
namespace
{
void rtrim(std::string& s)
{
s.erase(
std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(),
s.end());
}
}
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkIOSSFilesScanner);
//----------------------------------------------------------------------------
vtkIOSSFilesScanner::vtkIOSSFilesScanner() = default;
//----------------------------------------------------------------------------
vtkIOSSFilesScanner::~vtkIOSSFilesScanner() = default;
//----------------------------------------------------------------------------
bool vtkIOSSFilesScanner::IsMetaFile(const std::string& filename)
{
vtksys::ifstream metafile(filename.c_str());
if (!metafile.good())
{
return false;
}
std::string s;
std::getline(metafile, s);
rtrim(s); // strip trailing white-spaces
if (s.empty() ||
s.size() !=
static_cast<size_t>(std::count_if(
s.begin(), s.end(), [](unsigned char c) { return std::isprint(c) || std::isspace(c); })))
{
return false;
}
// let's just check that the first value is a valid filename.
auto metafile_path =
vtksys::SystemTools::GetFilenamePath(vtksys::SystemTools::CollapseFullPath(filename));
auto fpath = vtksys::SystemTools::CollapseFullPath(s, metafile_path);
return (vtksys::SystemTools::FileExists(fpath, /*isFile=*/true));
}
//----------------------------------------------------------------------------
std::set<std::string> vtkIOSSFilesScanner::GetFilesFromMetaFile(const std::string& filename)
{
if (!vtkIOSSFilesScanner::IsMetaFile(filename))
{
return { filename };
}
std::set<std::string> result;
const auto metafile_path =
vtksys::SystemTools::GetFilenamePath(vtksys::SystemTools::CollapseFullPath(filename));
vtksys::ifstream metafile(filename.c_str());
while (metafile.good() && !metafile.eof())
{
std::string fname;
std::getline(metafile, fname);
rtrim(fname);
if (!fname.empty())
{
fname = vtksys::SystemTools::CollapseFullPath(fname, metafile_path);
result.insert(fname);
}
}
return result;
}
//----------------------------------------------------------------------------
std::set<std::string> vtkIOSSFilesScanner::GetRelatedFiles(
const std::set<std::string>& originalSet, const std::vector<std::string>& directoryListing)
{
if (originalSet.empty())
{
return originalSet;
}
// Matches paths `{NAME}.e-s{RS}.{NUMRANKS}.{RANK}` or {NAME}.g-s{RS}.{NUMRANKS}.{RANK}`
// where `-s{RS}` and/or `.{NUMRANKS}.{RANK}` is optional.
vtksys::RegularExpression extensionRegexExodus(
R"(^(.*\.[eg][^-.]*)(-s.[0-9]+)?(\.[0-9]+(\.[0-9]+)?)?$)");
vtksys::RegularExpression extensionRegexExodus2(
R"(^(.*\.exo[^-.]*)(-s.[0-9]+)?(\.[0-9]+(\.[0-9]+)?)?$)");
vtksys::RegularExpression extensionRegexCGNS(
R"(^(.*\.cgns[^-.]*)(-s.[0-9]+)?(\.[0-9]+(\.[0-9]+)?)?$)");
// extract process count from the filename, if any, otherwise -1.
auto getProcessCount = [](const std::string& fname)
{
vtksys::RegularExpression procRegEx(R"(^.*\.([0-9]+)\.[0-9]+$)");
if (procRegEx.find(fname))
{
return std::atoi(procRegEx.match(1).c_str());
}
return -1;
};
// get the files per directory
std::map<std::string /*directory*/, std::set<std::string> /* files*/> uniqueFilesPerDirectory;
for (const auto& filename : originalSet)
{
std::string unix_fname = filename;
vtksys::SystemTools::ConvertToUnixSlashes(unix_fname);
auto prefix = vtksys::SystemTools::GetFilenamePath(unix_fname);
if (!prefix.empty())
{
// add dir separator to make `join` easier later on.
prefix += "/";
}
uniqueFilesPerDirectory[prefix].emplace(unix_fname);
}
// get the prefixes per directory that are used to find other files related to this one.
std::map<std::string /*directory*/, std::map<std::string /*prefix*/, int>> prefixesPerDirectory;
for (const auto& uniqueFiles : uniqueFilesPerDirectory)
{
for (const auto& unix_fname : uniqueFiles.second)
{
const auto& directoryName = uniqueFiles.first;
// prefixes are used to find other files related to this one.
auto fname_wo_path = vtksys::SystemTools::GetFilenameName(unix_fname);
if (fname_wo_path.empty())
{
// this happens if the unix_fname was not a full path.
fname_wo_path = unix_fname;
}
if (extensionRegexExodus2.find(fname_wo_path))
{
prefixesPerDirectory[directoryName].insert(
std::make_pair(extensionRegexExodus2.match(1), getProcessCount(fname_wo_path)));
}
else if (extensionRegexExodus.find(fname_wo_path))
{
prefixesPerDirectory[directoryName].insert(
std::make_pair(extensionRegexExodus.match(1), getProcessCount(fname_wo_path)));
}
else if (extensionRegexCGNS.find(fname_wo_path))
{
prefixesPerDirectory[directoryName].insert(
std::make_pair(extensionRegexCGNS.match(1), getProcessCount(fname_wo_path)));
}
}
}
// combine all provided unique files in all directories.
std::set<std::string> result;
for (const auto& uniqueFiles : uniqueFilesPerDirectory)
{
for (const auto& fname : uniqueFiles.second)
{
result.insert(fname);
}
}
// query the files in the directories.
std::map<std::string /*directory*/, std::vector<std::string> /* files*/> dirlist;
if (directoryListing.empty())
{
for (const auto& prefix : prefixesPerDirectory)
{
const auto& directoryName = prefix.first;
vtksys::Directory directory;
if (!directory.Load(directoryName))
{
return result;
}
for (unsigned long cc = 0, max = directory.GetNumberOfFiles(); cc < max; ++cc)
{
dirlist[directoryName].emplace_back(directory.GetFile(cc));
}
}
}
else
{
if (!prefixesPerDirectory.empty())
{
// For now, we only check the directory for the first file. Not sure if we
// should scan all directories for all files in the original set.
dirlist[prefixesPerDirectory.begin()->first] = directoryListing;
}
}
// get the files in the directories that match the pattern and have the same prefix.
for (const auto& filesInDir : dirlist)
{
const auto& directoryName = filesInDir.first;
const auto& prefixes = prefixesPerDirectory[directoryName];
for (const auto& filename : filesInDir.second)
{
std::string dbaseName;
if (extensionRegexExodus2.find(filename))
{
dbaseName = extensionRegexExodus2.match(1);
}
else if (extensionRegexExodus.find(filename))
{
dbaseName = extensionRegexExodus.match(1);
}
else if (extensionRegexCGNS.find(filename))
{
dbaseName = extensionRegexCGNS.match(1);
}
else
{
continue;
}
const int procCount = getProcessCount(filename);
auto piter = prefixes.find(dbaseName);
if (piter != prefixes.end() && piter->second == procCount)
{
result.insert(directoryName + filename);
}
}
}
return result;
}
//----------------------------------------------------------------------------
bool vtkIOSSFilesScanner::DoTestFilePatternMatching()
{
auto verify = [](const std::set<std::string>& original,
const std::vector<std::string>& dir_listing,
const std::set<std::string>& expected)
{ return (vtkIOSSFilesScanner::GetRelatedFiles(original, dir_listing) == expected); };
if (!verify({ "mysimoutput.e-s.000" },
{ "mysimoutput.e-s.000", "mysimoutput.e-s.001", "mysimoutput.e-s.002" },
{ "mysimoutput.e-s.000", "mysimoutput.e-s.001", "mysimoutput.e-s.002" }))
{
return false;
}
if (!verify({ "mysimoutput.exo-s.000" },
{ "mysimoutput.exo-s.000", "mysimoutput.exo-s.001", "mysimoutput.exo-s.002" },
{ "mysimoutput.exo-s.000", "mysimoutput.exo-s.001", "mysimoutput.exo-s.002" }))
{
return false;
}
if (!verify({ "mysimoutput.exo-s00" },
{ "mysimoutput.exo-s00", "mysimoutput.exo-s01", "mysimoutput.exo-s02" },
{ "mysimoutput.exo-s00", "mysimoutput.exo-s01", "mysimoutput.exo-s02" }))
{
return false;
}
if (!verify({ "/tmp/mysimoutput.e-s.000" },
{ "mysimoutput.e-s.000", "mysimoutput.e-s.001", "mysimoutput.e-s.002" },
{ "/tmp/mysimoutput.e-s.000", "/tmp/mysimoutput.e-s.001", "/tmp/mysimoutput.e-s.002" }))
{
return false;
}
if (!verify({ "C:\\Directory\\mysimoutput.e-s.000" },
{ "mysimoutput.e-s.000", "mysimoutput.e-s.001", "mysimoutput.e-s.002" },
{ "C:/Directory/mysimoutput.e-s.000", "C:/Directory/mysimoutput.e-s.001",
"C:/Directory/mysimoutput.e-s.002" }))
{
return false;
}
if (!verify({ "/tmp space/mysimoutput.e-s.000" },
{ "mysimoutput.e-s.000", "mysimoutput.e-s.001", "mysimoutput.e-s.002" },
{ "/tmp space/mysimoutput.e-s.000", "/tmp space/mysimoutput.e-s.001",
"/tmp space/mysimoutput.e-s.002" }))
{
return false;
}
if (!verify({ "C:\\Directory space\\mysimoutput.e-s.000" },
{ "mysimoutput.e-s.000", "mysimoutput.e-s.001", "mysimoutput.e-s.002" },
{ "C:/Directory space/mysimoutput.e-s.000", "C:/Directory space/mysimoutput.e-s.001",
"C:/Directory space/mysimoutput.e-s.002" }))
{
return false;
}
if (!verify({ "/tmp/can.e.4.0" },
{ "can.e.4.0", "can.e.4.1", "can.e.4.2", "can.e.4.3", "can.e.2.0", "can.e.2.1" },
{ "/tmp/can.e.4.0", "/tmp/can.e.4.1", "/tmp/can.e.4.2", "/tmp/can.e.4.3" }))
{
return false;
}
return true;
}
//----------------------------------------------------------------------------
void vtkIOSSFilesScanner::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
VTK_ABI_NAMESPACE_END
|