Merge "init: Add support for ${x.y:-default} expansion"
This commit is contained in:
commit
4ab7548406
1 changed files with 13 additions and 3 deletions
|
|
@ -489,6 +489,7 @@ bool expand_props(const std::string& src, std::string* dst) {
|
||||||
* - will accept $$ as a literal $.
|
* - will accept $$ as a literal $.
|
||||||
* - no nested property expansion, i.e. ${foo.${bar}} is not supported,
|
* - no nested property expansion, i.e. ${foo.${bar}} is not supported,
|
||||||
* bad things will happen
|
* bad things will happen
|
||||||
|
* - ${x.y:-default} will return default value if property empty.
|
||||||
*/
|
*/
|
||||||
while (*src_ptr) {
|
while (*src_ptr) {
|
||||||
const char* c;
|
const char* c;
|
||||||
|
|
@ -511,6 +512,7 @@ bool expand_props(const std::string& src, std::string* dst) {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string prop_name;
|
std::string prop_name;
|
||||||
|
std::string def_val;
|
||||||
if (*c == '{') {
|
if (*c == '{') {
|
||||||
c++;
|
c++;
|
||||||
const char* end = strchr(c, '}');
|
const char* end = strchr(c, '}');
|
||||||
|
|
@ -521,6 +523,11 @@ bool expand_props(const std::string& src, std::string* dst) {
|
||||||
}
|
}
|
||||||
prop_name = std::string(c, end);
|
prop_name = std::string(c, end);
|
||||||
c = end + 1;
|
c = end + 1;
|
||||||
|
size_t def = prop_name.find(":-");
|
||||||
|
if (def < prop_name.size()) {
|
||||||
|
def_val = prop_name.substr(def + 2);
|
||||||
|
prop_name = prop_name.substr(0, def);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
prop_name = c;
|
prop_name = c;
|
||||||
ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
|
ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
|
||||||
|
|
@ -535,9 +542,12 @@ bool expand_props(const std::string& src, std::string* dst) {
|
||||||
|
|
||||||
std::string prop_val = property_get(prop_name.c_str());
|
std::string prop_val = property_get(prop_name.c_str());
|
||||||
if (prop_val.empty()) {
|
if (prop_val.empty()) {
|
||||||
ERROR("property '%s' doesn't exist while expanding '%s'\n",
|
if (def_val.empty()) {
|
||||||
prop_name.c_str(), src.c_str());
|
ERROR("property '%s' doesn't exist while expanding '%s'\n",
|
||||||
return false;
|
prop_name.c_str(), src.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
prop_val = def_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
dst->append(prop_val);
|
dst->append(prop_val);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue