Skip to content

Commit e515506

Browse files
committed
Extract catalog loaders to their own files
1 parent 61c75a1 commit e515506

22 files changed

Lines changed: 600 additions & 385 deletions

src/celestia/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
set(CELESTIA_SOURCES
2+
catalogloader.h
23
celestiacore.cpp
34
celestiacore.h
45
celestiastate.cpp
@@ -15,9 +16,16 @@ set(CELESTIA_SOURCES
1516
helper.h
1617
hud.cpp
1718
hud.h
19+
loaddso.cpp
20+
loaddso.h
21+
loadsso.cpp
22+
loadsso.h
23+
loadstars.cpp
24+
loadstars.h
1825
moviecapture.h
1926
scriptmenu.cpp
2027
scriptmenu.h
28+
solarsystemloader.h
2129
textinput.cpp
2230
textinput.h
2331
textprintposition.cpp

src/celestia/catalogloader.h

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
void process(const fs::path &filepath)
49+
{
50+
if (DetermineFileType(filepath) != m_contentType)
51+
return;
52+
53+
if (std::find(std::begin(m_skipPaths), std::end(m_skipPaths), filepath)
54+
!= std::end(m_skipPaths))
55+
{
56+
util::GetLogger()->info(_("Skipping {} catalog: {}\n"), m_typeDesc, filepath);
57+
return;
58+
}
59+
util::GetLogger()->info(_("Loading {} catalog: {}\n"), m_typeDesc, filepath);
60+
if (m_notifier != nullptr)
61+
m_notifier->update(filepath.filename().string());
62+
63+
if (std::ifstream catalogFile(filepath); catalogFile.good())
64+
{
65+
if (!m_objDB->load(catalogFile, filepath.parent_path()))
66+
{
67+
util::GetLogger()->error(_("Error reading {} catalog file: {}\n"),
68+
m_typeDesc,
69+
filepath);
70+
}
71+
}
72+
}
73+
74+
void loadAll(util::array_view<fs::path> dirs)
75+
{
76+
std::vector<fs::path> entries;
77+
std::error_code ec;
78+
for (const auto &dir : dirs)
79+
{
80+
if (!util::IsValidDirectory(dir))
81+
continue;
82+
83+
entries.clear();
84+
85+
for (auto iter = fs::recursive_directory_iterator(dir, ec); iter != end(iter);
86+
iter.increment(ec))
87+
{
88+
if (ec)
89+
continue;
90+
if (!fs::is_directory(iter->path(), ec))
91+
entries.push_back(iter->path());
92+
}
93+
94+
std::sort(std::begin(entries), std::end(entries));
95+
96+
for (const auto &fn : entries)
97+
process(fn);
98+
}
99+
}
100+
};
101+
102+
} // namespace celestia

0 commit comments

Comments
 (0)