-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved FileSettingsStorage to a separate file, clang-format
- Loading branch information
1 parent
d9c0950
commit a37e2e9
Showing
17 changed files
with
168 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
Source/Viewer/include/Viewer/Highlighting/FileSettingsStorage.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (C) 2024 Michael Kazakov. Subject to GNU General Public License version 3. | ||
#pragma once | ||
#include "SettingsStorage.h" | ||
#include <Base/RobinHoodUtil.h> | ||
#include <Utility/FileMask.h> | ||
#include <filesystem> | ||
#include <vector> | ||
|
||
namespace nc::viewer::hl { | ||
|
||
class FileSettingsStorage : public SettingsStorage | ||
{ | ||
public: | ||
FileSettingsStorage(const std::filesystem::path &_base_dir, const std::filesystem::path &_overrides_dir); | ||
|
||
std::string Language(std::string_view _filename) override; | ||
|
||
std::shared_ptr<const std::string> Settings(std::string_view _lang) override; | ||
|
||
private: | ||
struct Lang { | ||
std::string name; | ||
std::string settings_filename; | ||
nc::utility::FileMask mask; | ||
}; | ||
|
||
void LoadLangs(); | ||
|
||
std::filesystem::path m_BaseDir; | ||
std::vector<Lang> m_Langs; | ||
robin_hood::unordered_flat_map<std::string, | ||
std::shared_ptr<const std::string>, | ||
RHTransparentStringHashEqual, | ||
RHTransparentStringHashEqual> | ||
m_Settings; | ||
}; | ||
|
||
} // namespace nc::viewer::hl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (C) 2024 Michael Kazakov. Subject to GNU General Public License version 3. | ||
#include <Viewer/Highlighting/FileSettingsStorage.h> | ||
#include <fstream> | ||
#include <nlohmann/json.hpp> | ||
#include <ranges> | ||
#include <fmt/format.h> | ||
|
||
using json = nlohmann::json; | ||
|
||
namespace nc::viewer::hl { | ||
|
||
// TODO: logging | ||
// TODO: error handling | ||
|
||
FileSettingsStorage::FileSettingsStorage(const std::filesystem::path &_base_dir, | ||
const std::filesystem::path &_overrides_dir) | ||
: m_BaseDir(_base_dir) | ||
{ | ||
(void)_overrides_dir; | ||
LoadLangs(); | ||
} | ||
|
||
void FileSettingsStorage::LoadLangs() | ||
{ | ||
std::ifstream f(m_BaseDir / "Main.json"); | ||
json data = json::parse(f); | ||
|
||
m_Langs.clear(); | ||
if( data.contains("langs") ) { | ||
auto &langs = data.at("langs"); | ||
for( auto it = langs.begin(); it != langs.end(); ++it ) { | ||
std::string name = it->at("name"); | ||
std::string settings = it->at("settings"); | ||
std::string filemask = it->at("filemask"); | ||
if( name.empty() || settings.empty() || filemask.empty() ) { | ||
continue; | ||
} | ||
|
||
// TODO: check that the lang is unique | ||
|
||
Lang lang; | ||
lang.name = name; | ||
lang.settings_filename = settings; | ||
lang.mask = utility::FileMask(filemask, utility::FileMask::Type::Mask); | ||
m_Langs.push_back(std::move(lang)); | ||
} | ||
} | ||
} | ||
|
||
std::string FileSettingsStorage::Language(std::string_view _filename) | ||
{ | ||
for( auto &lang : m_Langs ) { | ||
if( lang.mask.MatchName(_filename) ) { | ||
return lang.name; | ||
} | ||
} | ||
return {}; | ||
} | ||
|
||
std::shared_ptr<const std::string> FileSettingsStorage::Settings(std::string_view _lang) | ||
{ | ||
if( auto sett_it = m_Settings.find(_lang); sett_it != m_Settings.end() ) { | ||
return sett_it->second; | ||
} | ||
|
||
auto lang_it = std::ranges::find_if(m_Langs, [&](auto &lang) { return lang.name == _lang; }); | ||
if( lang_it == m_Langs.end() ) { | ||
return {}; | ||
} | ||
|
||
const std::filesystem::path settings_path = m_BaseDir / lang_it->settings_filename; | ||
std::ifstream ifs{settings_path}; | ||
if( ifs ) { | ||
std::string text((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>()); | ||
auto shared = std::make_shared<std::string>(std::move(text)); | ||
m_Settings.emplace(_lang, shared); | ||
return shared; | ||
} | ||
else { | ||
m_Settings.emplace(_lang, nullptr); | ||
return {}; | ||
} | ||
} | ||
|
||
} // namespace nc::viewer::hl |
Oops, something went wrong.