From 1fa4ac0231019f128994de58fe30afdb9541f794 Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Sat, 2 Aug 2025 11:21:26 +0200 Subject: [PATCH] Avoid using boost::property_tree for ini parsing --- common/CMakeLists.txt | 1 + common/utils/ini.cpp | 92 ++++++++++++++++++++++++++++++++ common/utils/ini.h | 82 ++++++++++++++++++++++++++++ common/utils/xdg_icon_lookup.cpp | 31 +++++------ 4 files changed, 191 insertions(+), 15 deletions(-) create mode 100644 common/utils/ini.cpp create mode 100644 common/utils/ini.h diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index e7387510..c580a4fd 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -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) diff --git a/common/utils/ini.cpp b/common/utils/ini.cpp new file mode 100644 index 00000000..156176b4 --- /dev/null +++ b/common/utils/ini.cpp @@ -0,0 +1,92 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Guillaume Meunier + * + * 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 . + */ + +#include "ini.h" +#include + +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 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 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{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); + } +} diff --git a/common/utils/ini.h b/common/utils/ini.h new file mode 100644 index 00000000..2a63faab --- /dev/null +++ b/common/utils/ini.h @@ -0,0 +1,82 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Guillaume Meunier + * + * 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 . + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +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 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 get_optional(std::string_view section, std::string_view key) const; + + template + T get(std::string_view section, std::string_view key, const T & default_value = T{}) const + { + using return_type = std::decay_t; + + auto value = get_optional(section, key); + + if (not value) + return default_value; + + if constexpr (std::is_same_v) + return *value; + + if constexpr (std::is_same_v) + return std::string{*value}; + + if constexpr (std::is_integral_v) + { + return_type v; + if (std::from_chars(value->data(), value->data() + value->size(), v).ec != std::errc{}) + return default_value; + + return v; + } + } +}; +} // namespace wivrn diff --git a/common/utils/xdg_icon_lookup.cpp b/common/utils/xdg_icon_lookup.cpp index 8e4be14a..75186c9a 100644 --- a/common/utils/xdg_icon_lookup.cpp +++ b/common/utils/xdg_icon_lookup.cpp @@ -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 -#include #include +#include #include #include @@ -98,8 +99,9 @@ struct icon_theme_dir } }; -void find_icon_theme_dirs_helper(const std::vector & base_dirs, std::vector & dirs, std::vector & themes, const std::string & theme) +void find_icon_theme_dirs_helper(const std::vector & base_dirs, std::vector & dirs, std::vector & 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 & 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("Icon Theme.Inherits").value_or(""), ",")) + for (const auto & inherited_theme: utils::split(index.get("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("Icon Theme.Directories"), ",")) + for (auto directory: utils::split(index.get("Icon Theme", "Directories", ""), ",")) { - int size = index.get(directory + ".Size"); - int scale = index.get_optional(directory + ".Scale").value_or(1); - int min_size = index.get_optional(directory + ".MinSize").value_or(size); - int max_size = index.get_optional(directory + ".MaxSize").value_or(size); - int threshold = index.get_optional(directory + ".Threshold").value_or(2); + int size = index.get(directory, "Size"); + int scale = index.get(directory, "Scale", 1); + int min_size = index.get(directory, "MinSize", size); + int max_size = index.get(directory, "MaxSize", size); + int threshold = index.get(directory, "Threshold", 2); - std::string type_str = index.get_optional(directory + ".Type").value_or("Threshold"); + std::string type_str = index.get(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 & base } catch (std::exception & e) { - // std::cerr << e.what() << std::endl; } } }