Skip to content

Commit 833c6a2

Browse files
committed
COMMON: add a new KeyValue parsing function which can deal e.g. with seperators in the 'path=' key - fixes EOS-6119
1 parent c9394db commit 833c6a2

3 files changed

Lines changed: 93 additions & 1 deletion

File tree

common/StringConversion.cc

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,76 @@ StringConversion::GetKeyValueMap(const char* mapstring,
558558
return true;
559559
}
560560

561+
562+
//------------------------------------------------------------------------------
563+
// Split a comma separated key:val list and fill it into a map with special path treatment
564+
//------------------------------------------------------------------------------
565+
bool
566+
StringConversion::GetSpecialKeyValueMap(const char* mapstring,
567+
std::map<std::string, std::string>& map,
568+
const char* split,
569+
const char* sdelimiter,
570+
std::vector<std::string>* keyvector,
571+
const char* pathKey,
572+
const char* stopKey)
573+
{
574+
if (!mapstring || !split || !sdelimiter || !pathKey || !stopKey)
575+
return false;
576+
std::string input(mapstring);
577+
std::string delimiter(sdelimiter);
578+
std::string splitter(split);
579+
std::string pathPrefix = std::string(pathKey) + splitter;
580+
std::string stopPrefix = std::string(stopKey) + splitter;
581+
size_t pos = 0;
582+
size_t start = 0;
583+
while (start < input.length()) {
584+
pos = input.find(delimiter, start);
585+
std::string token;
586+
if (pos == std::string::npos) {
587+
token = input.substr(start);
588+
start = input.length();
589+
} else {
590+
token = input.substr(start, pos - start);
591+
start = pos + delimiter.length();
592+
}
593+
if (token.find(pathPrefix) == 0) {
594+
std::string key = pathKey;
595+
std::string value = token.substr(pathPrefix.length());
596+
while (start < input.length()) {
597+
pos = input.find(delimiter, start);
598+
std::string next_token;
599+
if (pos == std::string::npos) {
600+
next_token = input.substr(start);
601+
start = input.length();
602+
} else {
603+
next_token = input.substr(start, pos - start);
604+
start = pos + delimiter.length();
605+
}
606+
if (next_token.find(stopPrefix) == 0) {
607+
start -= (next_token.length() + delimiter.length());
608+
break;
609+
} else {
610+
value += delimiter + next_token;
611+
}
612+
}
613+
map[key] = value;
614+
if (keyvector)
615+
keyvector->push_back(key);
616+
} else {
617+
size_t split_pos = token.find(splitter);
618+
if (split_pos == std::string::npos)
619+
continue;
620+
std::string key = token.substr(0, split_pos);
621+
std::string value = token.substr(split_pos + splitter.length());
622+
map[key] = value;
623+
if (keyvector)
624+
keyvector->push_back(key);
625+
}
626+
}
627+
return true;
628+
}
629+
630+
561631
//------------------------------------------------------------------------------
562632
// Specialized splitting function returning the host part out of a queue name
563633
//------------------------------------------------------------------------------

common/StringConversion.hh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,28 @@ public:
386386
std::vector<std::string>* keyvector = 0);
387387

388388

389+
// ---------------------------------------------------------------------------
390+
/**
391+
* Split a comma separated key:val list and fill it into a map
392+
*
393+
* @param mapstring map string to parse
394+
* @param map return map after parsing if ok
395+
* @param split separator used to separate key from value default ":"
396+
* @param delimiter separator used to separate individual key value pairs
397+
* @param keyvector returns optional the order of the keys in a vector
398+
* @param pathkey returns the name which indicates the beginning of a path, after which everything is read a s part of the path until stopKey
399+
* @param stopKey marks the end of the previous pathkey
400+
* @return true if format ok, otherwise false
401+
*/
402+
// ---------------------------------------------------------------------------
403+
static bool GetSpecialKeyValueMap(const char* mapstring,
404+
std::map<std::string, std::string>& map,
405+
const char* split,
406+
const char* sdelimiter,
407+
std::vector<std::string>* keyvector,
408+
const char* pathKey = "path",
409+
const char* stopKey = "fstpath");
410+
389411
// ---------------------------------------------------------------------------
390412
/**
391413
* Replace a key in a string,string map

console/commands/com_report.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ com_report(char* arg1)
216216

217217
std::map<std::string, std::string> map;
218218

219-
if (eos::common::StringConversion::GetKeyValueMap(line.c_str(),
219+
if (eos::common::StringConversion::GetSpecialKeyValueMap(line.c_str(),
220220
map,
221221
"=",
222222
"&",

0 commit comments

Comments
 (0)