This implementation is in no-way a complete or full-featured, but is a prototype to showcase the potential of FileSystem abstraction.
| 555 | // This implementation is in no-way a complete or full-featured, |
| 556 | // but is a prototype to showcase the potential of FileSystem abstraction. |
| 557 | class InMemoryFileSystem : public TestFileSystem { |
| 558 | public: |
| 559 | using Files = std::map<fs::path, std::string>; |
| 560 | using Directories = std::set<fs::path>; |
| 561 | using OpenOutputFiles = std::map<const std::ostream *, fs::path>; |
| 562 | |
| 563 | explicit InMemoryFileSystem(const fs::path &cwd) : TestFileSystem(cwd) { |
| 564 | FileSystem::setInstance(this); |
| 565 | |
| 566 | fs::path d1 = cwd; |
| 567 | fs::path d2; |
| 568 | while (d1 != d2) { |
| 569 | m_directories.emplace(d1); |
| 570 | d2 = d1; |
| 571 | d1 = d1.parent_path(); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | std::istream &openInput(const fs::path &filepath, |
| 576 | std::ios_base::openmode mode) override { |
| 577 | if (!filepath.is_absolute()) { |
| 578 | return m_nullInputStream; |
| 579 | } |
| 580 | |
| 581 | std::scoped_lock<std::mutex> lock(m_inputStreamsMutex); |
| 582 | Files::const_iterator it1 = m_files.find(filepath); |
| 583 | if (it1 == m_files.end()) { |
| 584 | return m_nullInputStream; |
| 585 | } |
| 586 | |
| 587 | std::pair<InputStreams::iterator, bool> it2 = |
| 588 | m_inputStreams.emplace(new std::istringstream(it1->second)); |
| 589 | |
| 590 | return **it2.first; |
| 591 | } |
| 592 | |
| 593 | bool close(std::istream &strm) override { |
| 594 | std::scoped_lock<std::mutex> lock(m_inputStreamsMutex); |
| 595 | |
| 596 | InputStreams::const_iterator it = |
| 597 | m_inputStreams.find((std::istringstream *)&strm); |
| 598 | if (it != m_inputStreams.end()) { |
| 599 | m_inputStreams.erase(it); |
| 600 | return true; |
| 601 | } |
| 602 | |
| 603 | return false; |
| 604 | } |
| 605 | |
| 606 | std::ostream &openOutput(const fs::path &filepath, |
| 607 | std::ios_base::openmode mode) override { |
| 608 | if (!filepath.is_absolute()) { |
| 609 | return m_nullOutputStream; |
| 610 | } |
| 611 | |
| 612 | std::scoped_lock<std::mutex> lock(m_outputStreamsMutex); |
| 613 | |
| 614 | std::string content; |