|
| 1 | +// catalogloader.h |
| 2 | +// |
| 3 | +// Copyright (C) 2001-2023, the Celestia Development Team |
| 4 | +// |
| 5 | +// This program is free software; you can redistribute it and/or |
| 6 | +// modify it under the terms of the GNU General Public License |
| 7 | +// as published by the Free Software Foundation; either version 2 |
| 8 | +// of the License, or (at your option) any later version. |
| 9 | + |
| 10 | +#pragma once |
| 11 | + |
| 12 | +#include <algorithm> |
| 13 | +#include <fstream> |
| 14 | +#include <string> |
| 15 | + |
| 16 | +#include <celestia/progressnotifier.h> |
| 17 | +#include <celutil/array_view.h> |
| 18 | +#include <celutil/filetype.h> |
| 19 | +#include <celutil/fsutils.h> |
| 20 | +#include <celutil/gettext.h> |
| 21 | +#include <celutil/logger.h> |
| 22 | + |
| 23 | +namespace celestia |
| 24 | +{ |
| 25 | + |
| 26 | +template<class OBJDB> class CatalogLoader |
| 27 | +{ |
| 28 | + OBJDB *m_objDB; |
| 29 | + std::string m_typeDesc; |
| 30 | + ContentType m_contentType; |
| 31 | + ProgressNotifier *m_notifier; |
| 32 | + util::array_view<fs::path> m_skipPaths; |
| 33 | + |
| 34 | +public: |
| 35 | + CatalogLoader(OBJDB *db, |
| 36 | + const std::string &typeDesc, |
| 37 | + const ContentType &contentType, |
| 38 | + ProgressNotifier *notifier, |
| 39 | + util::array_view<fs::path> skipPaths) : |
| 40 | + m_objDB(db), |
| 41 | + m_typeDesc(typeDesc), |
| 42 | + m_contentType(contentType), |
| 43 | + m_notifier(notifier), |
| 44 | + m_skipPaths(skipPaths) |
| 45 | + { |
| 46 | + } |
| 47 | + |
| 48 | + bool load(std::istream &in, const fs::path &dir) |
| 49 | + { |
| 50 | + return m_objDB->load(in, dir); |
| 51 | + } |
| 52 | + |
| 53 | + void process(const fs::path &filePath, const fs::path &parentPath) |
| 54 | + { |
| 55 | + if (DetermineFileType(filePath) != m_contentType) |
| 56 | + return; |
| 57 | + |
| 58 | + if (std::find(std::begin(m_skipPaths), std::end(m_skipPaths), filePath) |
| 59 | + != std::end(m_skipPaths)) |
| 60 | + { |
| 61 | + util::GetLogger()->info(_("Skipping {} catalog: {}\n"), m_typeDesc, filePath); |
| 62 | + return; |
| 63 | + } |
| 64 | + util::GetLogger()->info(_("Loading {} catalog: {}\n"), m_typeDesc, filePath); |
| 65 | + if (m_notifier != nullptr) |
| 66 | + m_notifier->update(filePath.filename().string()); |
| 67 | + |
| 68 | + if (std::ifstream catalogFile(filePath); |
| 69 | + !catalogFile.good() || !load(catalogFile, parentPath)) |
| 70 | + { |
| 71 | + util::GetLogger()->error(_("Error reading {} catalog file: {}\n"), |
| 72 | + m_typeDesc, |
| 73 | + filePath); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + void loadExtras(util::array_view<fs::path> dirs) |
| 78 | + { |
| 79 | + std::vector<fs::path> entries; |
| 80 | + std::error_code ec; |
| 81 | + for (const auto &dir : dirs) |
| 82 | + { |
| 83 | + if (!util::IsValidDirectory(dir)) |
| 84 | + continue; |
| 85 | + |
| 86 | + entries.clear(); |
| 87 | + |
| 88 | + for (auto iter = fs::recursive_directory_iterator(dir, ec); iter != end(iter); |
| 89 | + iter.increment(ec)) |
| 90 | + { |
| 91 | + if (ec) |
| 92 | + continue; |
| 93 | + if (!fs::is_directory(iter->path(), ec)) |
| 94 | + entries.push_back(iter->path()); |
| 95 | + } |
| 96 | + |
| 97 | + std::sort(std::begin(entries), std::end(entries)); |
| 98 | + |
| 99 | + for (const auto &fn : entries) |
| 100 | + process(fn, fn.parent_path()); |
| 101 | + } |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +} // namespace celestia |
0 commit comments