Drop the VarMap stuff, use the new EnvFake class.

This commit is contained in:
King_DuckZ 2020-04-13 21:24:03 +02:00
parent f696e1e3a0
commit 8a7002f5c5
11 changed files with 30 additions and 135 deletions

View file

@ -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);
}

View file

@ -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;

View file

@ -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';
}

View file

@ -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

View file

@ -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',

View file

@ -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;
}

View file

@ -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<std::string_view> 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& {

View file

@ -17,7 +17,7 @@
#pragma once
#include "var_map.hpp"
#include "env_fake.hpp"
#include <filesystem>
#include <string>
#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<std::string_view> 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;

View file

@ -17,10 +17,13 @@
#pragma once
#include "var_map.hpp"
#include <string>
#include <unordered_map>
#include <string_view>
namespace duck {
typedef std::unordered_map<std::string_view, std::string_view> 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);

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "var_map.hpp"
#include <cassert>
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

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <unordered_map>
#include <string_view>
namespace duck {
typedef std::unordered_map<std::string_view, std::string_view> VarMap;
class VarMapView {
public:
VarMapView (const VarMap* vm);
std::string_view operator[] (std::string_view key) const;
private:
const VarMap* m_map;
};
} //namespace duck