layer: fix env var delimiters

This commit is contained in:
Christophe 2023-05-25 12:28:36 +02:00 committed by Christophe
parent 335e80ff1c
commit faa46b0633
3 changed files with 16 additions and 5 deletions

View file

@ -112,13 +112,19 @@ std::string GetEnvSettingName(const char *layer_key, const char *setting_key, Tr
return result.str();
}
char GetEnvDelimiter() {
#ifdef WIN32 // a define is necessary because ':' is used for disk drives on Windows path
return ';';
#else
return ':';
#endif
}
char FindDelimiter(const std::string& s) {
if (s.find(',') != std::string::npos) {
return ',';
} else if (s.find(':') != std::string::npos) { // Typically Unix env variables
return ':';
} else if (s.find(';') != std::string::npos) { // Typically Win32 env variables
return ';';
} else if (s.find(GetEnvDelimiter()) != std::string::npos) {
return GetEnvDelimiter();
} else {
return ',';
}

View file

@ -50,6 +50,9 @@ namespace vl {
// Find the delimiter (, ; :) in a string made of tokens. Return ',' by default
char FindDelimiter(const std::string &s);
// ';' on WIN32 and ':' on Unix
char GetEnvDelimiter();
// Remove whitespaces at the beginning of the end
std::string TrimWhitespace(const std::string &s);

View file

@ -68,11 +68,13 @@ TEST(test_layer_settings_util, FindDelimiter) {
char A = vl::FindDelimiter("VALUE_A,VALUE_B");
EXPECT_EQ(',', A);
#ifdef WIN32
char B = vl::FindDelimiter("VALUE_A;VALUE_B");
EXPECT_EQ(';', B);
#else
char C = vl::FindDelimiter("VALUE_A:VALUE_B");
EXPECT_EQ(':', C);
#endif
EXPECT_EQ(',', vl::FindDelimiter("VALUE_A"));