summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYifan Hong2017-12-18 18:27:24 -0600
committerYifan Hong2017-12-22 17:58:22 -0600
commiteff04664c85090494c60a2bf95cc23b0b24ae21c (patch)
tree7886d3854c611d9a875d40393892cc7986ee06b9 /assemble_vintf.cpp
parenta4721a6c7bc0757d3e55cffc6619463ebeb8c089 (diff)
downloadplatform-system-libvintf-eff04664c85090494c60a2bf95cc23b0b24ae21c.tar.gz
platform-system-libvintf-eff04664c85090494c60a2bf95cc23b0b24ae21c.tar.xz
platform-system-libvintf-eff04664c85090494c60a2bf95cc23b0b24ae21c.zip
assemble_vintf: getFlagIfUnset
getFlagIfUnset() gets an environment variable and write it to the compatibility matrix only if the compatibility matrix contains a dummy value. Test: libvintf_test Change-Id: I67b1a9936b9c67ddd37f3cb43499ae74e1a84985
Diffstat (limited to 'assemble_vintf.cpp')
-rw-r--r--assemble_vintf.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/assemble_vintf.cpp b/assemble_vintf.cpp
index 17f5d0c..fd8eb9f 100644
--- a/assemble_vintf.cpp
+++ b/assemble_vintf.cpp
@@ -79,6 +79,40 @@ class AssembleVintf {
79 return true; 79 return true;
80 } 80 }
81 81
82 /**
83 * Set *out to environment variable if *out is not a dummy value (i.e. default constructed).
84 */
85 template <typename T>
86 bool getFlagIfUnset(const std::string& envKey, T* out) {
87 bool hasExistingValue = !(*out == T{});
88
89 bool hasEnvValue = false;
90 T envValue;
91 const char* envCValue = getenv(envKey.c_str());
92 if (envCValue != nullptr) {
93 if (!parse(envCValue, &envValue)) {
94 std::cerr << "Cannot parse " << envCValue << "." << std::endl;
95 return false;
96 }
97 hasEnvValue = true;
98 }
99
100 if (hasExistingValue) {
101 if (hasEnvValue) {
102 std::cerr << "Warning: cannot override existing value " << *out << " with "
103 << envKey << " (which is " << envValue << ")." << std::endl;
104 }
105 return false;
106 }
107 if (!hasEnvValue) {
108 std::cerr << "Warning: " << envKey << " is not specified. Default to " << T{} << "."
109 << std::endl;
110 return false;
111 }
112 *out = envValue;
113 return true;
114 }
115
82 static bool getBooleanFlag(const char* key) { 116 static bool getBooleanFlag(const char* key) {
83 const char* envValue = getenv(key); 117 const char* envValue = getenv(key);
84 return envValue != nullptr && strcmp(envValue, "true") == 0; 118 return envValue != nullptr && strcmp(envValue, "true") == 0;