2017-04-21 22:10:16 +00:00
|
|
|
/* Copyright 2017, Michele Santullo
|
2017-06-12 21:57:40 +00:00
|
|
|
* This file is part of "kamokan".
|
2017-04-21 22:10:16 +00:00
|
|
|
*
|
2017-06-12 21:57:40 +00:00
|
|
|
* "kamokan" is free software: you can redistribute it and/or modify
|
2017-04-21 22:10:16 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2017-06-12 21:57:40 +00:00
|
|
|
* "kamokan" is distributed in the hope that it will be useful,
|
2017-04-21 22:10:16 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-06-12 21:57:40 +00:00
|
|
|
* along with "kamokan". If not, see <http://www.gnu.org/licenses/>.
|
2017-04-21 22:10:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "settings_bag.hpp"
|
2017-04-24 17:55:04 +00:00
|
|
|
#include "duckhandy/lexical_cast.hpp"
|
2017-06-06 23:19:53 +00:00
|
|
|
#include "spdlog.hpp"
|
2017-04-24 17:55:04 +00:00
|
|
|
#include <ciso646>
|
2017-04-21 22:10:16 +00:00
|
|
|
#include <cassert>
|
2017-04-24 17:55:04 +00:00
|
|
|
#include <cstdint>
|
2017-06-02 20:53:27 +00:00
|
|
|
#include <iostream>
|
2017-04-21 22:10:16 +00:00
|
|
|
|
2017-06-12 21:57:40 +00:00
|
|
|
namespace kamokan {
|
2017-05-04 09:00:49 +00:00
|
|
|
namespace {
|
2017-06-12 21:57:40 +00:00
|
|
|
const IniFile::KeyValueMapType* get_kamokan_node (const IniFile& parIni, boost::string_view parSectionName) {
|
2017-06-02 20:53:27 +00:00
|
|
|
auto it_found = parIni.parsed().find(parSectionName);
|
2017-05-04 09:00:49 +00:00
|
|
|
if (parIni.parsed().end() != it_found) {
|
|
|
|
return &it_found->second;
|
|
|
|
}
|
|
|
|
else {
|
2017-06-02 20:53:27 +00:00
|
|
|
std::cerr << "Couldn't find section [" << parSectionName << "] in the settings file\n";
|
2017-05-04 09:00:49 +00:00
|
|
|
static const IniFile::KeyValueMapType empty_key_values;
|
|
|
|
return &empty_key_values;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} //unnamed namespace
|
|
|
|
|
2017-06-06 22:04:40 +00:00
|
|
|
SettingsBag::SettingsBag (const Kakoune::SafePtr<IniFile>& parIni, boost::string_view parSectionName) :
|
2017-04-21 22:10:16 +00:00
|
|
|
m_ini(parIni),
|
2017-06-12 21:57:40 +00:00
|
|
|
m_values(get_kamokan_node(*parIni, parSectionName))
|
2017-04-21 22:10:16 +00:00
|
|
|
{
|
|
|
|
assert(m_values);
|
|
|
|
}
|
|
|
|
|
|
|
|
SettingsBag::~SettingsBag() noexcept = default;
|
|
|
|
|
2017-06-06 22:04:40 +00:00
|
|
|
const boost::string_view& SettingsBag::operator[] (boost::string_view parIndex) const {
|
2017-04-21 22:10:16 +00:00
|
|
|
auto it_found = m_values->find(parIndex);
|
|
|
|
if (m_values->end() != it_found)
|
|
|
|
return it_found->second;
|
|
|
|
else
|
|
|
|
return m_defaults.at(parIndex);
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:04:40 +00:00
|
|
|
void SettingsBag::add_default (boost::string_view parKey, boost::string_view parValue) {
|
2017-04-24 08:25:04 +00:00
|
|
|
assert(m_defaults.find(parKey) == m_defaults.end());
|
|
|
|
m_defaults[parKey] = parValue;
|
2017-04-23 12:54:06 +00:00
|
|
|
}
|
2017-06-12 21:57:40 +00:00
|
|
|
} //namespace kamokan
|