From f0c65e6cf08012d10bde88b2caee39ad938b40fe Mon Sep 17 00:00:00 2001
From: Patrick Ziegler
Date: Mon, 1 Jul 2019 23:33:01 +0200
Subject: [PATCH] fix(config): Allow empty string as ref fallback (#1831)
---
include/components/config.hpp | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)
diff --git a/include/components/config.hpp b/include/components/config.hpp
index bb10d51a..84bf1094 100644
--- a/include/components/config.hpp
+++ b/include/components/config.hpp
@@ -268,17 +268,23 @@ class config {
T dereference_env(string var) const {
size_t pos;
string env_default;
+ /*
+ * This is needed because with only the string we cannot distinguish
+ * between an empty string as default and not default
+ */
+ bool has_default = false;
if ((pos = var.find(':')) != string::npos) {
env_default = var.substr(pos + 1);
+ has_default = true;
var.erase(pos);
}
- if (env_util::has(var.c_str())) {
- string env_value{env_util::get(var.c_str())};
+ if (env_util::has(var)) {
+ string env_value{env_util::get(var)};
m_log.info("Environment var reference ${%s} found (value=%s)", var, env_value);
return convert(move(env_value));
- } else if (!env_default.empty()) {
+ } else if (has_default) {
m_log.info("Environment var ${%s} is undefined, using defined fallback value \"%s\"", var, env_default);
return convert(move(env_default));
} else {
@@ -306,8 +312,10 @@ class config {
}
string fallback;
+ bool has_fallback = false;
if ((pos = var.find(':')) != string::npos) {
fallback = var.substr(pos + 1);
+ has_fallback = true;
var.erase(pos);
}
@@ -316,7 +324,7 @@ class config {
m_log.info("Found matching X resource \"%s\" (value=%s)", var, value);
return convert(move(value));
} catch (const xresource_error& err) {
- if (!fallback.empty()) {
+ if (has_fallback) {
m_log.warn("%s, using defined fallback value \"%s\"", err.what(), fallback);
return convert(move(fallback));
}
@@ -334,8 +342,10 @@ class config {
T dereference_file(string var) const {
size_t pos;
string fallback;
+ bool has_fallback = false;
if ((pos = var.find(':')) != string::npos) {
fallback = var.substr(pos + 1);
+ has_fallback = true;
var.erase(pos);
}
var = file_util::expand(var);
@@ -343,7 +353,7 @@ class config {
if (file_util::exists(var)) {
m_log.info("File reference \"%s\" found", var);
return convert(string_util::trim(file_util::contents(var), '\n'));
- } else if (!fallback.empty()) {
+ } else if (has_fallback) {
m_log.warn("File reference \"%s\" not found, using defined fallback value \"%s\"", var, fallback);
return convert(move(fallback));
} else {