From c04dcc2e7d834218ef2d4194331e383402495ae1 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 10 Apr 2024 20:07:22 +0200 Subject: Adding upstream version 2:20.4+dfsg. Signed-off-by: Daniel Baumann --- xbmc/filesystem/XbtManager.cpp | 127 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 xbmc/filesystem/XbtManager.cpp (limited to 'xbmc/filesystem/XbtManager.cpp') diff --git a/xbmc/filesystem/XbtManager.cpp b/xbmc/filesystem/XbtManager.cpp new file mode 100644 index 0000000..bb091f1 --- /dev/null +++ b/xbmc/filesystem/XbtManager.cpp @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2015-2018 Team Kodi + * This file is part of Kodi - https://kodi.tv + * + * SPDX-License-Identifier: GPL-2.0-or-later + * See LICENSES/README.md for more information. + */ + +#include "XbtManager.h" + +#include "URL.h" +#include "guilib/XBTF.h" +#include "guilib/XBTFReader.h" + +#include + +namespace XFILE +{ + +CXbtManager::CXbtManager() = default; + +CXbtManager::~CXbtManager() = default; + +CXbtManager& CXbtManager::GetInstance() +{ + static CXbtManager xbtManager; + return xbtManager; +} + +bool CXbtManager::HasFiles(const CURL& path) const +{ + return ProcessFile(path) != m_readers.end(); +} + +bool CXbtManager::GetFiles(const CURL& path, std::vector& files) const +{ + const auto& reader = ProcessFile(path); + if (reader == m_readers.end()) + return false; + + files = reader->second.reader->GetFiles(); + return true; +} + +bool CXbtManager::GetReader(const CURL& path, CXBTFReaderPtr& reader) const +{ + const auto& it = ProcessFile(path); + if (it == m_readers.end()) + return false; + + reader = it->second.reader; + return true; +} + +void CXbtManager::Release(const CURL& path) +{ + const auto& it = GetReader(path); + if (it == m_readers.end()) + return; + + RemoveReader(it); +} + +CXbtManager::XBTFReaders::iterator CXbtManager::GetReader(const CURL& path) const +{ + return GetReader(NormalizePath(path)); +} + +CXbtManager::XBTFReaders::iterator CXbtManager::GetReader(const std::string& path) const +{ + if (path.empty()) + return m_readers.end(); + + return m_readers.find(path); +} + +void CXbtManager::RemoveReader(XBTFReaders::iterator readerIterator) const +{ + if (readerIterator == m_readers.end()) + return; + + // close the reader + readerIterator->second.reader->Close(); + + // and remove it from the map + m_readers.erase(readerIterator); +} + +CXbtManager::XBTFReaders::const_iterator CXbtManager::ProcessFile(const CURL& path) const +{ + std::string filePath = NormalizePath(path); + + // check if the file is known + auto it = GetReader(filePath); + if (it != m_readers.end()) + { + // check if the XBT file has been modified + if (it->second.reader->GetLastModificationTimestamp() <= it->second.lastModification) + return it; + + // the XBT file has been modified so close and remove it from the cache + // it will be re-opened by the following logic + RemoveReader(it); + } + + // try to read the file + CXBTFReaderPtr reader(new CXBTFReader()); + if (!reader->Open(filePath)) + return m_readers.end(); + + XBTFReader xbtfReader = { + reader, + reader->GetLastModificationTimestamp() + }; + std::pair result = m_readers.insert(std::make_pair(filePath, xbtfReader)); + return result.first; +} + +std::string CXbtManager::NormalizePath(const CURL& path) +{ + if (path.IsProtocol("xbt")) + return path.GetHostName(); + + return path.Get(); +} + +} -- cgit v1.2.3