mirror of
https://github.com/amnezia-vpn/win-split-tunnel.git
synced 2026-05-17 08:16:00 +03:00
34 lines
726 B
C++
34 lines
726 B
C++
|
|
#include "settings.h"
|
||
|
|
#include <fstream>
|
||
|
|
#include <stdexcept>
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
//static
|
||
|
|
Settings Settings::FromFile(const std::filesystem::path &filename)
|
||
|
|
{
|
||
|
|
std::ifstream source(filename);
|
||
|
|
|
||
|
|
if (!source.is_open())
|
||
|
|
{
|
||
|
|
throw std::runtime_error("Failed to open settings file");
|
||
|
|
}
|
||
|
|
|
||
|
|
std::vector<std::wstring> intermediate;
|
||
|
|
|
||
|
|
for (std::string kvp; source >> kvp; intermediate.emplace_back(common::string::ToWide(kvp)));
|
||
|
|
|
||
|
|
return Settings(std::move(common::string::SplitKeyValuePairs(intermediate)));
|
||
|
|
}
|
||
|
|
|
||
|
|
const std::wstring &Settings::get(const std::wstring &key)
|
||
|
|
{
|
||
|
|
auto it = m_values.find(key);
|
||
|
|
|
||
|
|
if (it == m_values.end())
|
||
|
|
{
|
||
|
|
throw std::runtime_error("Settings key not present in settings file");
|
||
|
|
}
|
||
|
|
|
||
|
|
return it->second;
|
||
|
|
}
|