Avoid using boost::property_tree for ini parsing
This commit is contained in:
@@ -47,6 +47,7 @@ if (WIVRN_BUILD_DASHBOARD OR WIVRN_BUILD_SERVER)
|
||||
utils/xdg_icon_lookup.cpp
|
||||
utils/steam_app_info.cpp
|
||||
utils/load_icon.cpp
|
||||
utils/ini.cpp
|
||||
application.cpp
|
||||
)
|
||||
target_link_libraries(wivrn-common-server PRIVATE nlohmann_json::nlohmann_json PNG::PNG PkgConfig::librsvg-2.0 PUBLIC wivrn-common-base)
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2025 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* 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.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* 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
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "ini.h"
|
||||
#include <ranges>
|
||||
|
||||
static constexpr std::string_view strip(std::string_view sv)
|
||||
{
|
||||
size_t prefix_size = sv.find_first_not_of(' ');
|
||||
|
||||
if (prefix_size == std::string_view::npos)
|
||||
return {};
|
||||
|
||||
sv.remove_prefix(prefix_size);
|
||||
|
||||
size_t suffix_size = sv.find_last_not_of(' ');
|
||||
if (suffix_size == std::string_view::npos)
|
||||
return {};
|
||||
|
||||
sv.remove_suffix(sv.size() - suffix_size - 1);
|
||||
|
||||
return sv;
|
||||
}
|
||||
|
||||
static constexpr std::optional<wivrn::ini::key_value> parse_key_value(std::string_view section, std::string_view line)
|
||||
{
|
||||
size_t equal_sign = line.find('=');
|
||||
if (equal_sign == std::string_view::npos)
|
||||
return {};
|
||||
|
||||
auto key = strip(line.substr(0, equal_sign)); // TODO locale
|
||||
auto value = strip(line.substr(equal_sign + 1));
|
||||
|
||||
return wivrn::ini::key_value{
|
||||
.section = section,
|
||||
.key = key,
|
||||
.locale = "",
|
||||
.value = value,
|
||||
};
|
||||
}
|
||||
|
||||
std::optional<std::string_view> wivrn::ini::get_optional(std::string_view section, std::string_view key) const
|
||||
{
|
||||
for (const auto & line: lines)
|
||||
{
|
||||
if (line.section == section and line.key == key)
|
||||
return line.value;
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
wivrn::ini::ini(std::istream & file)
|
||||
{
|
||||
contents = {std::istreambuf_iterator<char>{file}, {}};
|
||||
|
||||
std::string_view current_section;
|
||||
|
||||
for (auto bounds: contents | std::views::split('\n'))
|
||||
{
|
||||
auto line = strip({bounds.begin(), bounds.end()});
|
||||
|
||||
if (line.starts_with('#'))
|
||||
continue;
|
||||
|
||||
if (line.starts_with('[') and line.ends_with(']'))
|
||||
{
|
||||
current_section = line.substr(1, line.size() - 2);
|
||||
continue;
|
||||
}
|
||||
|
||||
auto kv = parse_key_value(current_section, line);
|
||||
|
||||
if (kv)
|
||||
lines.push_back(*kv);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2025 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* 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.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* 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
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <charconv>
|
||||
#include <istream>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
class ini
|
||||
{
|
||||
public:
|
||||
struct key_value
|
||||
{
|
||||
std::string_view section;
|
||||
std::string_view key;
|
||||
std::string_view locale;
|
||||
std::string_view value;
|
||||
};
|
||||
|
||||
private:
|
||||
std::string contents;
|
||||
std::vector<key_value> lines;
|
||||
|
||||
public:
|
||||
ini() = default;
|
||||
ini(ini &&) = default;
|
||||
ini(const ini &) = delete;
|
||||
ini & operator=(ini &&) = default;
|
||||
ini & operator=(const ini &) = delete;
|
||||
|
||||
ini(std::istream & file);
|
||||
|
||||
std::optional<std::string_view> get_optional(std::string_view section, std::string_view key) const;
|
||||
|
||||
template <typename T>
|
||||
T get(std::string_view section, std::string_view key, const T & default_value = T{}) const
|
||||
{
|
||||
using return_type = std::decay_t<T>;
|
||||
|
||||
auto value = get_optional(section, key);
|
||||
|
||||
if (not value)
|
||||
return default_value;
|
||||
|
||||
if constexpr (std::is_same_v<return_type, std::string_view>)
|
||||
return *value;
|
||||
|
||||
if constexpr (std::is_same_v<return_type, std::string>)
|
||||
return std::string{*value};
|
||||
|
||||
if constexpr (std::is_integral_v<return_type>)
|
||||
{
|
||||
return_type v;
|
||||
if (std::from_chars(value->data(), value->data() + value->size(), v).ec != std::errc{})
|
||||
return default_value;
|
||||
|
||||
return v;
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace wivrn
|
||||
@@ -18,13 +18,14 @@
|
||||
|
||||
#include "xdg_icon_lookup.h"
|
||||
|
||||
#include "ini.h"
|
||||
#include "strings.h"
|
||||
#include "utils/flatpak.h"
|
||||
#include "xdg_base_directory.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <boost/property_tree/ini_parser.hpp>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
|
||||
@@ -98,8 +99,9 @@ struct icon_theme_dir
|
||||
}
|
||||
};
|
||||
|
||||
void find_icon_theme_dirs_helper(const std::vector<std::filesystem::path> & base_dirs, std::vector<icon_theme_dir> & dirs, std::vector<std::string> & themes, const std::string & theme)
|
||||
void find_icon_theme_dirs_helper(const std::vector<std::filesystem::path> & base_dirs, std::vector<icon_theme_dir> & dirs, std::vector<std::string> & themes, std::string theme)
|
||||
{
|
||||
// Theme is passed by copy because the themes vector might be resized
|
||||
for (const auto & base_dir: base_dirs)
|
||||
{
|
||||
const auto theme_dir = base_dir / "icons" / theme;
|
||||
@@ -110,24 +112,24 @@ void find_icon_theme_dirs_helper(const std::vector<std::filesystem::path> & base
|
||||
|
||||
try
|
||||
{
|
||||
boost::property_tree::ptree index;
|
||||
boost::property_tree::ini_parser::read_ini(index_theme, index);
|
||||
std::ifstream file{index_theme};
|
||||
wivrn::ini index{file};
|
||||
|
||||
for (const auto & inherited_theme: utils::split(index.get_optional<std::string>("Icon Theme.Inherits").value_or(""), ","))
|
||||
for (const auto & inherited_theme: utils::split(index.get<std::string>("Icon Theme", "Inherits", ""), ","))
|
||||
{
|
||||
// if (not std::ranges::contains(themes, inherited_theme))
|
||||
// themes.push_back(inherited_theme);
|
||||
if (inherited_theme != "" and not std::ranges::contains(themes, inherited_theme))
|
||||
themes.push_back(inherited_theme);
|
||||
}
|
||||
|
||||
for (auto directory: utils::split(index.get<std::string>("Icon Theme.Directories"), ","))
|
||||
for (auto directory: utils::split(index.get<std::string>("Icon Theme", "Directories", ""), ","))
|
||||
{
|
||||
int size = index.get<int>(directory + ".Size");
|
||||
int scale = index.get_optional<int>(directory + ".Scale").value_or(1);
|
||||
int min_size = index.get_optional<int>(directory + ".MinSize").value_or(size);
|
||||
int max_size = index.get_optional<int>(directory + ".MaxSize").value_or(size);
|
||||
int threshold = index.get_optional<int>(directory + ".Threshold").value_or(2);
|
||||
int size = index.get<int>(directory, "Size");
|
||||
int scale = index.get<int>(directory, "Scale", 1);
|
||||
int min_size = index.get<int>(directory, "MinSize", size);
|
||||
int max_size = index.get<int>(directory, "MaxSize", size);
|
||||
int threshold = index.get<int>(directory, "Threshold", 2);
|
||||
|
||||
std::string type_str = index.get_optional<std::string>(directory + ".Type").value_or("Threshold");
|
||||
std::string type_str = index.get<std::string>(directory, "Type", "Threshold");
|
||||
|
||||
icon_theme_type type = icon_theme_type::threshold;
|
||||
if (type_str == "Fixed")
|
||||
@@ -150,7 +152,6 @@ void find_icon_theme_dirs_helper(const std::vector<std::filesystem::path> & base
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
// std::cerr << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user