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
|
/*
* 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 <utility>
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<CXBTFFile>& 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<XBTFReaders::iterator, bool> 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();
}
}
|