Blue Brain BioExplorer
LoaderRegistry.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2024, EPFL/Blue Brain Project
3  * All rights reserved. Do not distribute without permission.
4  * Responsible Author: Daniel.Nachbaur <daniel.nachbaur@epfl.ch>
5  *
6  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License version 3.0 as published
10  * by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #include "LoaderRegistry.h"
23 
26 
27 namespace core
28 {
29 void LoaderRegistry::registerLoader(std::unique_ptr<Loader> loader)
30 {
31  _loaderInfos.push_back({loader->getName(), loader->getSupportedStorage(), loader->getProperties()});
32  _loaders.push_back(std::move(loader));
33 }
34 
35 const std::vector<LoaderInfo>& LoaderRegistry::getLoaderInfos() const
36 {
37  return _loaderInfos;
38 }
39 
40 bool LoaderRegistry::isSupportedFile(const std::string& filename) const
41 {
42  if (fs::is_directory(filename))
43  return false;
44 
45  const auto extension = extractExtension(filename);
46  if (_archiveSupported(filename, extension))
47  return true;
48  for (const auto& loader : _loaders)
49  if (loader->isSupported(filename, extension))
50  return true;
51  return false;
52 }
53 
54 bool LoaderRegistry::isSupportedType(const std::string& type) const
55 {
56  if (_archiveSupported("", type))
57  return true;
58  for (const auto& loader : _loaders)
59  if (loader->isSupported("", type))
60  return true;
61  return false;
62 }
63 
64 const Loader& LoaderRegistry::getSuitableLoader(const std::string& filename, const std::string& filetype,
65  const std::string& loaderName) const
66 {
67  if (fs::is_directory(filename))
68  throw std::runtime_error("'" + filename + "' is a directory");
69 
70  const auto extension = filetype.empty() ? extractExtension(filename) : filetype;
71 
72  // If we have an archive we always use the archive loader even if a specific
73  // loader is specified
74  if (_archiveSupported(filename, extension))
75  return *_archiveLoader;
76 
77  // Find specific loader
78  if (!loaderName.empty())
79  {
80  for (const auto& loader : _loaders)
81  if (loader->getName() == loaderName)
82  return *loader.get();
83 
84  throw std::runtime_error("No loader found with name '" + loaderName + "'");
85  }
86 
87  for (const auto& loader : _loaders)
88  if (loader->isSupported(filename, extension))
89  return *loader;
90 
91  throw std::runtime_error("No loader found for filename '" + filename + "' and filetype '" + filetype + "'");
92 }
93 
95 {
96  _loaders.clear();
97  _archiveLoader.reset();
98  _loaderInfos.clear();
99 }
100 
101 void LoaderRegistry::registerArchiveLoader(std::unique_ptr<Loader> loader)
102 {
103  _archiveLoader = std::move(loader);
104 }
105 
106 bool LoaderRegistry::_archiveSupported(const std::string& filename, const std::string& filetype) const
107 {
108  return _archiveLoader && _archiveLoader->isSupported(filename, filetype);
109 }
110 } // namespace core
bool isSupportedType(const std::string &type) const
const std::vector< LoaderInfo > & getLoaderInfos() const
void registerLoader(std::unique_ptr< Loader > loader)
bool isSupportedFile(const std::string &filename) const
const Loader & getSuitableLoader(const std::string &filename, const std::string &filetype, const std::string &loaderName) const
void registerArchiveLoader(std::unique_ptr< Loader > loader)
std::string extractExtension(const std::string &filename)
Definition: Utils.cpp:64