|
13 | 13 | #include <Byteswap.h> |
14 | 14 | #include <TError.h> |
15 | 15 | #include <TFile.h> |
| 16 | +#include <TIterator.h> |
16 | 17 | #include <TKey.h> |
| 18 | +#include <TList.h> |
17 | 19 | #include <TROOT.h> |
18 | 20 |
|
19 | 21 | #include <algorithm> |
@@ -184,10 +186,6 @@ static std::string ValidateAndNormalizePath(std::string &path) |
184 | 186 |
|
185 | 187 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
186 | 188 |
|
187 | | -RFile::RFile(std::unique_ptr<TFile> file) : fFile(std::move(file)) {} |
188 | | - |
189 | | -RFile::~RFile() = default; |
190 | | - |
191 | 189 | std::unique_ptr<RFile> RFile::Open(std::string_view path) |
192 | 190 | { |
193 | 191 | CheckExtension(path); |
@@ -227,6 +225,10 @@ std::unique_ptr<RFile> RFile::Recreate(std::string_view path) |
227 | 225 | return rfile; |
228 | 226 | } |
229 | 227 |
|
| 228 | +RFile::RFile(std::unique_ptr<TFile> file) : fFile(std::move(file)) {} |
| 229 | + |
| 230 | +RFile::~RFile() = default; |
| 231 | + |
230 | 232 | TKey *RFile::GetTKey(std::string_view path) const |
231 | 233 | { |
232 | 234 | // In RFile, differently from TFile, when dealing with a path like "a/b/c", we always consider it to mean |
@@ -373,6 +375,125 @@ void RFile::PutUntyped(std::string_view pathSV, const std::type_info &type, cons |
373 | 375 | } |
374 | 376 | } |
375 | 377 |
|
| 378 | +ROOT::Experimental::RFileKeyIterable::RIterator::RIterStackElem::RIterStackElem(TIterator *it, const std::string &path) |
| 379 | + : fIter(it), fDirPath(path) |
| 380 | +{ |
| 381 | +} |
| 382 | + |
| 383 | +ROOT::Experimental::RFileKeyIterable::RIterator::RIterStackElem::~RIterStackElem() = default; |
| 384 | + |
| 385 | +ROOT::Experimental::RFileKeyIterable::RIterator::RIterator(TIterator *iter, Pattern_t pattern, std::uint32_t flags) |
| 386 | + : fPattern(pattern), fFlags(flags) |
| 387 | +{ |
| 388 | + if (iter) { |
| 389 | + fIterStack.emplace_back(iter); |
| 390 | + |
| 391 | + if (!pattern.empty()) { |
| 392 | + fRootDirNesting = std::count(pattern.begin(), pattern.end(), '/'); |
| 393 | + // `pattern` may or may not end with '/', but we consider it a directory regardless. |
| 394 | + // In other words, like in virtually all filesystem operations, "dir" and "dir/" are equivalent. |
| 395 | + fRootDirNesting += pattern.back() != '/'; |
| 396 | + } |
| 397 | + |
| 398 | + // Advance the iterator to skip the first key, which is always the TFile key. |
| 399 | + // This will also skip keys until we reach the first correct key we want to return. |
| 400 | + Advance(); |
| 401 | + } |
| 402 | +} |
| 403 | + |
| 404 | +ROOT::Experimental::RFileKeyIterable::RIterator ROOT::Experimental::RFileKeyIterable::begin() const |
| 405 | +{ |
| 406 | + return {fFile->GetListOfKeys()->MakeIterator(), fPattern, fFlags}; |
| 407 | +} |
| 408 | + |
| 409 | +ROOT::Experimental::RFileKeyIterable::RIterator ROOT::Experimental::RFileKeyIterable::end() const |
| 410 | +{ |
| 411 | + return {nullptr, fPattern, fFlags}; |
| 412 | +} |
| 413 | + |
| 414 | +void ROOT::Experimental::RFileKeyIterable::RIterator::Advance() |
| 415 | +{ |
| 416 | + fCurKey = nullptr; |
| 417 | + |
| 418 | + const bool recursive = fFlags & kRecursive; |
| 419 | + |
| 420 | + // We only want to return keys that refer to user objects, not internal ones, therefore we skip |
| 421 | + // all keys that have internal class names. |
| 422 | + while (!fIterStack.empty()) { |
| 423 | + auto &[iter, dirPath] = fIterStack.back(); |
| 424 | + assert(iter); |
| 425 | + TObject *keyObj = iter->Next(); |
| 426 | + if (!keyObj) { |
| 427 | + // reached end of the iteration |
| 428 | + fIterStack.pop_back(); |
| 429 | + continue; |
| 430 | + } |
| 431 | + |
| 432 | + assert(keyObj->IsA() == TClass::GetClass<TKey>()); |
| 433 | + auto key = static_cast<TKey *>(keyObj); |
| 434 | + |
| 435 | + const auto dirSep = (dirPath.empty() ? "" : "/"); |
| 436 | + |
| 437 | + if (strcmp(key->GetClassName(), "TDirectory") == 0 || strcmp(key->GetClassName(), "TDirectoryFile") == 0) { |
| 438 | + TDirectory *dir = key->ReadObject<TDirectory>(); |
| 439 | + TIterator *innerIter = dir->GetListOfKeys()->MakeIterator(); |
| 440 | + assert(innerIter); |
| 441 | + fIterStack.emplace_back(innerIter, dirPath + dirSep + dir->GetName()); |
| 442 | + continue; |
| 443 | + } |
| 444 | + |
| 445 | + // Reconstruct the full path of the key |
| 446 | + const auto &fullPath = dirPath + dirSep + key->GetName(); |
| 447 | + const auto nesting = fIterStack.size() - 1; |
| 448 | + |
| 449 | + // skip key if it's not a child of root dir |
| 450 | + if (!ROOT::StartsWith(fullPath, fPattern)) |
| 451 | + continue; |
| 452 | + |
| 453 | + // check that we are in the same directory as "rootDir". |
| 454 | + if (!recursive && nesting != fRootDirNesting) |
| 455 | + continue; |
| 456 | + |
| 457 | + // All checks passed: return this key. |
| 458 | + assert(!fullPath.empty()); |
| 459 | + fCurKey = key; |
| 460 | + break; |
| 461 | + } |
| 462 | +} |
| 463 | + |
| 464 | +ROOT::Experimental::RFileKeyInfo ROOT::Experimental::RFileKeyIterable::RIterator::operator*() |
| 465 | +{ |
| 466 | + if (fIterStack.empty()) |
| 467 | + throw ROOT::RException(R__FAIL("tried to dereference an invalid iterator")); |
| 468 | + |
| 469 | + const TKey *key = fCurKey; |
| 470 | + if (!key) |
| 471 | + throw ROOT::RException(R__FAIL("tried to dereference an invalid iterator")); |
| 472 | + |
| 473 | + const auto &dirPath = fIterStack.back().fDirPath; |
| 474 | + |
| 475 | + RFileKeyInfo keyInfo; |
| 476 | + keyInfo.fName = dirPath + (dirPath.empty() ? "" : "/") + key->GetName(); |
| 477 | + keyInfo.fClassName = key->GetClassName(); |
| 478 | + keyInfo.fCycle = key->GetCycle(); |
| 479 | + keyInfo.fTitle = key->GetTitle(); |
| 480 | + return keyInfo; |
| 481 | +} |
| 482 | + |
| 483 | +void RFile::Print(std::ostream &out) const |
| 484 | +{ |
| 485 | + std::vector<RFileKeyInfo> keys; |
| 486 | + auto keysIter = GetKeys(); |
| 487 | + for (const auto &key : keysIter) { |
| 488 | + keys.emplace_back(key); |
| 489 | + } |
| 490 | + |
| 491 | + std::sort(keys.begin(), keys.end(), [](const auto &a, const auto &b) { return a.fName < b.fName; }); |
| 492 | + for (const auto &key : keys) { |
| 493 | + out << key.fClassName << " " << key.fName << ";" << key.fCycle << ": \"" << key.fTitle << "\"\n"; |
| 494 | + } |
| 495 | +} |
| 496 | + |
376 | 497 | size_t RFile::Flush() |
377 | 498 | { |
378 | 499 | return fFile->Write(); |
|
0 commit comments