From 8a7002f5c50f91bdbc64b1e7383f939c8200fa4a Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Mon, 13 Apr 2020 21:24:03 +0200 Subject: [PATCH] Drop the VarMap stuff, use the new EnvFake class. --- src/chost.cpp | 2 +- src/gentoofunctions.cpp | 15 ++++++------- src/list_profiles.cpp | 6 +++--- src/main.cpp | 2 +- src/meson.build | 1 - src/replace_bash_vars.cpp | 4 ++-- src/syspaths.cpp | 44 ++++++--------------------------------- src/syspaths.hpp | 13 +++++------- src/to_var_map.hpp | 5 ++++- src/var_map.cpp | 39 ---------------------------------- src/var_map.hpp | 34 ------------------------------ 11 files changed, 30 insertions(+), 135 deletions(-) delete mode 100644 src/var_map.cpp delete mode 100644 src/var_map.hpp diff --git a/src/chost.cpp b/src/chost.cpp index fa1e43a..ab56337 100644 --- a/src/chost.cpp +++ b/src/chost.cpp @@ -71,7 +71,7 @@ namespace duck { { // If it's set in the env, trust the setting. If it's wrong, // then that's the caller's problem. - auto ret = sp.env("CHOST"); + auto ret = sp.env().get("CHOST"); if (ret and not ret->empty()) return string(*ret); } diff --git a/src/gentoofunctions.cpp b/src/gentoofunctions.cpp index 95929e6..542652e 100644 --- a/src/gentoofunctions.cpp +++ b/src/gentoofunctions.cpp @@ -82,15 +82,16 @@ namespace duck { std::string_view msg, const char* newline ) { - if (yesno(sp, sp[quiet_var])) { + if (yesno(sp, sp.env()[quiet_var])) { return; } else { - if (not yesno(sp, sp["RC_ENDCOL"]) and g_last_e_cmd == "ebegin") { + const auto& env = sp.env(); + if (not yesno(sp, env["RC_ENDCOL"]) and g_last_e_cmd == "ebegin") { stream << '\n'; } - stream << ' ' << sp[msg_prefix_var] << '*' - << sp["NORMAL"] << ' ' << sp["RC_INDENTATION"] + stream << ' ' << env[msg_prefix_var] << '*' + << env["NORMAL"] << ' ' << env["RC_INDENTATION"] << msg << newline; } @@ -100,7 +101,7 @@ namespace duck { } //unnamed namespace void vewarn (const SysPaths& sp, std::string_view msg) { - if (yesno(sp, sp["EINFO_VERBOSE"], true)) + if (yesno(sp, sp.env()["EINFO_VERBOSE"], true)) ewarn(sp, msg); } @@ -119,7 +120,7 @@ namespace duck { } void esyslog(const SysPaths& sp, std::string_view pri, std::string_view tag, std::string_view msg) { - if (not sp["EINFO_LOG"].empty() and not run_cmd_retcode("command -v logger > /dev/null 2>&1")) { + if (not sp.env()["EINFO_LOG"].empty() and not run_cmd_retcode("command -v logger > /dev/null 2>&1")) { if (msg.empty()) return; @@ -138,7 +139,7 @@ namespace duck { } { - auto val = sp[var]; + auto val = sp.env()[var]; if (val.empty()) return false; diff --git a/src/list_profiles.cpp b/src/list_profiles.cpp index eec26a1..e01e9ab 100644 --- a/src/list_profiles.cpp +++ b/src/list_profiles.cpp @@ -168,12 +168,12 @@ namespace duck { } std::cout << " [" << z << "] " << filename; - auto colours = sp.colours(); + const auto& env = sp.env(); if (filename == current_native) { - std::cout << ' ' << colours["GOOD"] << '*' << colours["NORMAL"]; + std::cout << ' ' << env["GOOD"] << '*' << env["NORMAL"]; } else if (fs::is_regular_file(sp.gcc_env_d() / ("config-" + target)) and filename == current) { - std::cout << ' ' << colours["HILITE"] << '*' << colours["NORMAL"]; + std::cout << ' ' << env["HILITE"] << '*' << env["NORMAL"]; } std::cout << '\n'; } diff --git a/src/main.cpp b/src/main.cpp index 2750b58..351b486 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -58,7 +58,7 @@ int main(int argc, char* argv[]) { } duck::SysPaths syspaths; - syspaths.set_colours(g_simple_colours); + syspaths.add_to_env(g_simple_colours); #if !defined(NDEBUG) std::cout << syspaths << '\n'; #endif diff --git a/src/meson.build b/src/meson.build index 2f547ed..4b2d6e1 100644 --- a/src/meson.build +++ b/src/meson.build @@ -18,7 +18,6 @@ executable(meson.project_name(), 'replace_bash_vars.cpp', 'to_var_map.cpp', 'load_file.cpp', - 'var_map.cpp', 'env_base.cpp', 'env_real.cpp', 'env_fake.cpp', diff --git a/src/replace_bash_vars.cpp b/src/replace_bash_vars.cpp index fb8b463..680f79e 100644 --- a/src/replace_bash_vars.cpp +++ b/src/replace_bash_vars.cpp @@ -59,7 +59,7 @@ namespace duck { std::string_view token(text.substr(2, end - 2)); std::string_view retval(token.empty() || token.front() != '!' ? token : token.substr(1)); if (not token.empty() and token.front() == '!') { - return {sp[retval], end + 1}; + return {sp.env()[retval], end + 1}; } else { return {retval, end + 1}; @@ -95,7 +95,7 @@ namespace duck { if (var.name == "argv0") rtext += PROJECT_NAME; else - rtext += sp[var.name]; + rtext += sp.env()[var.name]; pos += var.orig_len; prev_start = pos; } diff --git a/src/syspaths.cpp b/src/syspaths.cpp index ef8f848..7bf1a1c 100644 --- a/src/syspaths.cpp +++ b/src/syspaths.cpp @@ -34,54 +34,22 @@ namespace duck { const std::size_t pos = text.find_first_not_of('/'); return (text.npos == pos ? text : text.substr(pos)); } - - std::string_view val_or_def (const VarMap& map, std::string_view key, std::string_view def) noexcept { - try { - auto itm = map.find(key); - return (map.end() == itm ? def : itm->second); - } - catch (...) { - return def; - } - } } //unnamed namespace SysPaths::SysPaths() : - m_env(to_var_map_ignore_ownership(environ)), - m_root(val_or_def(m_env, "ROOT", "/")), - m_eprefix(fix_eprefix(val_or_def(m_env, "EPREFIX", ""))), + m_env(), + m_root(m_env.get("ROOT", "/")), + m_eprefix(fix_eprefix(m_env.get("EPREFIX", ""))), m_eroot(m_root / remove_front_slash(std::string(m_eprefix))), m_env_d(m_eroot / "etc/env.d"), m_gcc_env_d(m_env_d / "gcc") { } - std::string_view SysPaths::operator[] (std::string_view name) const noexcept { - assert(not name.empty()); - return val_or_def(m_env, name, ""); - } - - std::optional SysPaths::env (std::string_view name) const noexcept { - try { - assert(not name.empty()); - auto it_found = m_env.find(name); - if (m_env.end() == it_found) - return {}; - else - return std::make_optional(it_found->second); + void SysPaths::add_to_env (std::string_view colours) { + for (auto& entry : to_var_map_ignore_ownership(colours)) { + m_env.set(entry.first, entry.second); } - catch (...) { - return {}; - } - } - - std::string_view SysPaths::env (std::string_view name, std::string_view def) const noexcept { - assert(not name.empty()); - return val_or_def(m_env, name, def); - } - - void SysPaths::set_colours (std::string_view colours) { - m_colours = to_var_map_ignore_ownership(colours); } auto SysPaths::root() const -> const path_type& { diff --git a/src/syspaths.hpp b/src/syspaths.hpp index 41a992f..726074a 100644 --- a/src/syspaths.hpp +++ b/src/syspaths.hpp @@ -17,7 +17,7 @@ #pragma once -#include "var_map.hpp" +#include "env_fake.hpp" #include #include #if !defined(NDEBUG) @@ -35,12 +35,10 @@ namespace duck { SysPaths(); ~SysPaths() noexcept = default; - std::string_view operator[] (std::string_view name) const noexcept; - std::optional env (std::string_view name) const noexcept; - std::string_view env (std::string_view name, std::string_view def) const noexcept; + EnvFake& env() noexcept { return m_env; } + const EnvFake& env() const noexcept { return m_env; } - void set_colours (std::string_view colours); - VarMapView colours() const { return {&m_colours}; } + void add_to_env (std::string_view colours); const path_type& root() const; const path_type& eroot() const; @@ -49,8 +47,7 @@ namespace duck { const path_type& gcc_env_d() const; private: - VarMap m_env; - VarMap m_colours; + EnvFake m_env; path_type m_root; path_type m_eprefix; path_type m_eroot; diff --git a/src/to_var_map.hpp b/src/to_var_map.hpp index e48b521..d725d3b 100644 --- a/src/to_var_map.hpp +++ b/src/to_var_map.hpp @@ -17,10 +17,13 @@ #pragma once -#include "var_map.hpp" #include +#include +#include namespace duck { + typedef std::unordered_map VarMap; + //parameter is non-const so caller hopefully remembers to keep it alive //(ie: they can't just pass a temporary in) VarMap to_var_map (std::string& text); diff --git a/src/var_map.cpp b/src/var_map.cpp deleted file mode 100644 index 94e62ae..0000000 --- a/src/var_map.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* Copyright 2020, Michele Santullo - * This file is part of user-gcc. - * - * User-gcc 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. - * - * User-gcc 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 user-gcc. If not, see . - */ - -#include "var_map.hpp" -#include - -namespace duck { - VarMapView::VarMapView (const VarMap* vm) : - m_map(vm) - { - assert(m_map); - } - - std::string_view VarMapView::operator[] (std::string_view key) const { - if (key.empty()) - return ""; - - assert(m_map); - auto it_found = m_map->find(key); - if (m_map->end() != it_found) - return it_found->second; - else - return ""; - } -} //namespace duck diff --git a/src/var_map.hpp b/src/var_map.hpp deleted file mode 100644 index 5266564..0000000 --- a/src/var_map.hpp +++ /dev/null @@ -1,34 +0,0 @@ -/* Copyright 2020, Michele Santullo - * This file is part of user-gcc. - * - * User-gcc 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. - * - * User-gcc 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 user-gcc. If not, see . - */ - -#pragma once - -#include -#include - -namespace duck { - typedef std::unordered_map VarMap; - - class VarMapView { - public: - VarMapView (const VarMap* vm); - std::string_view operator[] (std::string_view key) const; - - private: - const VarMap* m_map; - }; -} //namespace duck