1 #include "otsdaq/Macros/StringMacros.h"
10 std::map<std::string , std::string >>
11 StringMacros::systemVariables_;
12 const std::string StringMacros::TBD =
"To-be-defined";
14 #define TLVL_EscapeString 30
15 #define TLVL_EnvMath 49
16 #define TLVL_EnvSub 50
31 const std::string& haystack,
32 unsigned int* priorityIndex)
35 __COUTT__ <<
"\t\t wildCardMatch: " << needle <<
" =in= " << haystack <<
" ??? "
39 if(needle.size() == 0)
55 if(needle == haystack)
62 const bool hasWildcard = (needle.find(
'*') != std::string::npos);
71 if(needle[needle.size() - 1] ==
'*' &&
72 needle.substr(0, needle.size() - 1) == haystack.substr(0, needle.size() - 1))
80 if(needle[0] ==
'*' &&
81 needle.substr(1) == haystack.substr(haystack.size() - (needle.size() - 1)))
90 std::size_t patternPos = 0;
91 std::size_t textPos = 0;
92 std::size_t lastStarPattern = std::string::npos;
93 std::size_t lastStarTextPos = std::string::npos;
94 while(textPos < haystack.size())
96 if(patternPos < needle.size() && needle[patternPos] == haystack[textPos])
101 else if(patternPos < needle.size() && needle[patternPos] ==
'*')
103 lastStarPattern = patternPos++;
104 lastStarTextPos = textPos;
106 else if(lastStarPattern != std::string::npos)
108 patternPos = lastStarPattern + 1;
109 textPos = ++lastStarTextPos;
119 while(patternPos < needle.size() && needle[patternPos] ==
'*')
122 if(patternPos == needle.size())
146 const std::set<std::string>& haystack)
148 for(
const auto& haystackString : haystack)
151 if(haystackString.size() && haystackString[0] ==
'!')
168 std::string decodeURIString(data.size(), 0);
170 for(
unsigned int i = 0; i < data.size(); ++i, ++j)
175 if(data[i + 1] >
'9')
176 decodeURIString[j] += (data[i + 1] - 55) * 16;
178 decodeURIString[j] += (data[i + 1] - 48) * 16;
181 if(data[i + 2] >
'9')
182 decodeURIString[j] += (data[i + 2] - 55);
184 decodeURIString[j] += (data[i + 2] - 48);
189 decodeURIString[j] = data[i];
191 decodeURIString.resize(j);
192 return decodeURIString;
196 std::string StringMacros::encodeURIComponent(
const std::string& sourceStr)
198 std::string retStr =
"";
200 for(
const auto& c : sourceStr)
201 if((c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z') || (c >=
'0' && c <=
'9'))
205 sprintf(encodeStr,
"%%%2.2X", (uint8_t)c);
215 std::map<char, std::string> replacements = {
223 while(pos < str.size())
225 auto it = replacements.find(str[pos]);
226 if(it != replacements.end())
228 str.replace(pos, 1, it->second);
229 pos += it->second.size();
250 bool allowWhiteSpace ,
253 unsigned int ws = -1;
256 __COUTVS__(TLVL_EscapeString, allowWhiteSpace);
257 __COUTVS__(TLVL_EscapeString, forHtml);
259 for(
unsigned int i = 0; i < inString.length(); i++)
260 if(inString[i] !=
' ')
262 __COUTS__(TLVL_EscapeString)
263 << i <<
". " << inString[i] <<
":" << (int)inString[i] << std::endl;
266 if(inString[i] ==
'\r' || inString[i] ==
'\n' ||
267 inString[i] ==
'\t' ||
270 (inString[i] >
char(126) &&
271 inString[i] <
char(161)))
278 if(i + 2 < inString.size() && inString[i] == char(0xE2) &&
279 inString[i + 1] == char(0x80) &&
298 if(inString[i] ==
'\n')
302 sprintf(htmlTmp,
"&#%3.3d", inString[i]);
304 i, std::string(htmlTmp));
313 else if(inString[i] ==
'\t')
321 "        ");
323 i, std::string(htmlTmp));
331 sprintf(htmlTmp,
"	");
333 i, std::string(htmlTmp));
345 inString.erase(i, 1);
348 __COUTS__(31) << inString << std::endl;
352 __COUTS__(31) << inString << std::endl;
355 if(inString[i] ==
'\"' || inString[i] ==
'\'')
360 unsigned int backslashCount = 0;
361 for(
unsigned int j = i; j > 0 && inString[j - 1] ==
'\\'; --j)
364 if(backslashCount % 2 == 1)
367 inString.erase(i - 1, 1);
373 (inString[i] ==
'\'')
376 inString.replace(i + 5, 1, 1,
';');
380 else if(inString[i] ==
'&')
382 inString.insert(i,
"&");
383 inString.replace(i + 4, 1, 1,
';');
386 else if(inString[i] ==
'<' || inString[i] ==
'>')
411 else if(inString[i] >=
char(161) &&
412 inString[i] <=
char(255))
414 sprintf(htmlTmp,
"&#%3.3d", inString[i]);
415 inString.insert(i, std::string(htmlTmp));
416 inString.replace(i + 5, 1, 1,
';');
420 __COUTS__(TLVL_EscapeString) << inString << std::endl;
424 else if(allowWhiteSpace)
432 inString.insert(i,
" ");
435 inString.insert(i,
" ");
436 inString.replace(i + 5, 1, 1,
';');
441 __COUTS__(TLVL_EscapeString) << inString.size() <<
" " << ws << std::endl;
445 __COUTS__(TLVL_EscapeString) << inString.size() <<
" " << inString << std::endl;
451 if(ws == (
unsigned int)-1)
453 return inString.substr(0, ws + 1);
463 unsigned int sz = str.size();
467 std::string retStr =
"";
468 retStr.reserve(str.size() * 2);
469 for(
unsigned int i = 0; i < sz; ++i)
501 unsigned int sz = str.size();
505 std::string retStr =
"";
506 retStr.reserve(str.size());
508 for(; i < sz - 1; ++i)
540 retStr += str[sz - 1];
551 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](
unsigned char ch) {
552 return !std::isspace(ch);
556 s.erase(std::find_if(
557 s.rbegin(), s.rend(), [](
unsigned char ch) { return !std::isspace(ch); })
576 size_t begin = data.find(
"$");
577 if(begin != std::string::npos && begin + 1 < data.size())
580 std::string envVariable;
581 std::string converted = data;
582 bool usedBraces =
false;
584 while(begin && begin != std::string::npos &&
585 converted[begin - 1] ==
588 converted.replace(begin - 1, 1,
"");
589 begin = converted.find(
"$", begin + 1);
590 if(begin == std::string::npos)
592 __COUTS__(TLVL_EnvSub)
593 <<
"Only found escaped $'s that will not be converted: " << converted
602 if(begin + 2 < data.size() && data[begin + 1] ==
'(' && data[begin + 2] ==
'(')
604 end = data.find(
"))", begin + 3);
605 if(end == std::string::npos)
607 __SS__ <<
"Arithmetic expansion '$((...)),' at pos " << begin
608 <<
" in value, is missing closing '))'! Here was the value: "
613 std::string expression = data.substr(begin + 3, end - begin - 3);
614 __COUTVS__(TLVL_EnvMath, expression);
618 __COUTVS__(TLVL_EnvMath, expression);
625 __SS__ <<
"Arithmetic expansion '$((...)),' at pos " << begin
626 <<
" in value, does not evaluate to a number! Here was the value: "
631 __COUTS__(TLVL_EnvMath) <<
"Arithmetic result: " << result << __E__;
635 converted.replace(begin, end - begin + 2, std::to_string(result)));
637 else if(data[begin + 1] ==
'{')
639 end = data.find(
"}", begin + 2);
640 envVariable = data.substr(begin + 2, end - begin - 2);
647 for(end = begin + 1; end < data.size(); ++end)
648 if(!((data[end] >=
'0' && data[end] <=
'9') ||
649 (data[end] >=
'A' && data[end] <=
'Z') ||
650 (data[end] >=
'a' && data[end] <=
'z') || data[end] ==
'-' ||
651 data[end] ==
'_' || data[end] ==
'.' || data[end] ==
':'))
653 envVariable = data.substr(begin + 1, end - begin - 1);
656 __COUTVS__(TLVL_EnvSub, data);
657 __COUTVS__(TLVL_EnvSub, envVariable);
658 if(usedBraces && envVariable.starts_with(
"OTS."))
660 __COUTS__(TLVL_EnvSub) <<
"OTS system variable detected!" << __E__;
664 if(sysVarSplit.size() != 3 ||
665 systemVariables_.find(sysVarSplit[1]) == systemVariables_.end() ||
666 systemVariables_.at(sysVarSplit[1]).find(sysVarSplit[2]) ==
667 systemVariables_.at(sysVarSplit[1]).end())
670 <<
"System variable ${" << envVariable
671 <<
"} is not valid or was not found!"
673 <<
"If you were trying to access an ots System Variable, the correct "
675 <<
"${OTS.<variable>.<property>}, e.g. "
676 "${OTS.ActiveStateMachine.name}"
678 <<
"If you were trying to insert an arithmetic operation, the "
680 "syntax is $((4 - 3)) or $(($ENVVAR1 - $ENVVAR2))"
682 <<
"Available system variables:" << __E__;
685 for(
const auto& varPair : systemVariables_)
687 ss <<
"\n OTS." << varPair.first <<
".*";
688 for(
const auto& propPair : varPair.second)
689 ss <<
"\n - OTS." << varPair.first <<
"." << propPair.first;
699 systemVariables_.at(sysVarSplit[1]).at(sysVarSplit[2])));
703 char* envResult =
nullptr;
706 envResult = __ENV__(envVariable.c_str());
708 catch(
const std::runtime_error& e)
711 << (
"The environmental variable '" + envVariable +
712 "' is not set! Please make sure you set it before continuing!" +
714 "If you were trying to access an ots System Variable, the "
715 "correct syntax is " +
716 "${OTS.<variable>.<property>}, e.g. "
717 "${OTS.ActiveStateMachine.name}")
719 ss <<
"\n" << e.what() << __E__;
725 converted.replace(begin, end - begin, envResult));
729 __COUTS__(TLVL_EnvSub) <<
"Result: " << data << __E__;
741 std::vector<std::string> numbers;
742 std::vector<char> ops;
750 std::set<char>({
'+',
'-',
'*',
'/'}),
751 std::set<char>({
' ',
'\t',
'\n',
'\r'}),
757 for(
const auto& number : numbers)
759 if(number.size() == 0)
762 if(number.find(
"0x") == 0)
765 for(
unsigned int i = 2; i < number.size(); ++i)
767 if(!((number[i] >=
'0' && number[i] <=
'9') ||
768 (number[i] >=
'A' && number[i] <=
'F') ||
769 (number[i] >=
'a' && number[i] <=
'f')))
777 else if(number[0] ==
'b')
781 for(
unsigned int i = 1; i < number.size(); ++i)
783 if(!((number[i] >=
'0' && number[i] <=
'1')))
793 for(
unsigned int i = 0; i < number.size(); ++i)
794 if(!((number[i] >=
'0' && number[i] <=
'9') || number[i] ==
'.' ||
795 number[i] ==
'+' || number[i] ==
'-'))
818 std::vector<std::string> numbers;
819 std::vector<char> ops;
821 bool hasDecimal =
false;
826 std::set<char>({
'+',
'-',
'*',
'/'}),
827 std::set<char>({
' ',
'\t',
'\n',
'\r'}),
833 for(
const auto& number : numbers)
835 if(number.size() == 0)
838 if(number.find(
"0x") == 0)
841 for(
unsigned int i = 2; i < number.size(); ++i)
843 if(!((number[i] >=
'0' && number[i] <=
'9') ||
844 (number[i] >=
'A' && number[i] <=
'F') ||
845 (number[i] >=
'a' && number[i] <=
'f')))
853 else if(number[0] ==
'b')
857 for(
unsigned int i = 1; i < number.size(); ++i)
859 if(!((number[i] >=
'0' && number[i] <=
'1')))
869 for(
unsigned int i = 0; i < number.size(); ++i)
870 if(!((number[i] >=
'0' && number[i] <=
'9') || number[i] ==
'.' ||
871 number[i] ==
'+' || number[i] ==
'-'))
873 else if(number[i] ==
'.')
886 return "unsigned long long";
899 __COUT_ERR__ <<
"Invalid empty bool string " << s << __E__;
904 if(s.find(
"1") != std::string::npos || s ==
"true" || s ==
"True" || s ==
"TRUE")
911 if(s.find(
"0") != std::string::npos || s ==
"false" || s ==
"False" || s ==
"FALSE")
917 __COUT_ERR__ <<
"Invalid bool string " << s << __E__;
928 time_t timestamp(strtol(linuxTimeInSeconds.c_str(), 0, 10));
948 std::stringstream ss;
949 int days = t / 60 / 60 / 24;
952 ss << days <<
" day" << (days > 1 ?
"s" :
"") <<
", ";
953 t -= days * 60 * 60 * 24;
957 ss << std::setw(2) << std::setfill(
'0') << (t / 60 / 60) <<
":" << std::setw(2)
958 << std::setfill(
'0') << ((t % (60 * 60)) / 60) <<
":" << std::setw(2)
959 << std::setfill(
'0') << (t % 60);
967 const std::string& value,
bool doConvertEnvironmentVariables)
970 return doConvertEnvironmentVariables
974 catch(
const std::runtime_error& e)
976 __SS__ <<
"Failed to validate value for default string data type. " << __E__
977 << e.what() << __E__;
986 std::set<std::string>& setToReturn,
987 const std::set<char>& delimiter,
988 const std::set<char>& whitespace)
995 for(; j < inputString.size(); ++j)
996 if((whitespace.find(inputString[j]) !=
998 delimiter.find(inputString[j]) != delimiter.end()) &&
1001 else if((whitespace.find(inputString[j]) !=
1004 delimiter.find(inputString[j]) != delimiter.end()) &&
1010 setToReturn.emplace(inputString.substr(i, j - i));
1017 setToReturn.emplace(inputString.substr(i, j - i));
1033 std::vector<std::string>& listToReturn,
1034 const std::set<char>& delimiter,
1035 const std::set<char>& whitespace,
1036 std::vector<char>* listOfDelimiters,
1037 bool decodeURIComponents)
1042 std::set<char>::iterator delimeterSearchIt;
1043 char lastDelimiter = 0;
1052 for(; c < inputString.size(); ++c)
1056 delimeterSearchIt = delimiter.find(inputString[c]);
1057 isDelimiter = delimeterSearchIt != delimiter.end();
1062 if(whitespace.find(inputString[c]) !=
1071 else if(whitespace.find(inputString[c]) != whitespace.end() &&
1076 else if(isDelimiter)
1081 if(listOfDelimiters && listToReturn.size())
1088 listOfDelimiters->push_back(lastDelimiter);
1091 inputString.substr(i, j - i))
1092 : inputString.substr(i, j - i));
1102 lastDelimiter = *delimeterSearchIt;
1111 if(listOfDelimiters && listToReturn.size())
1117 listOfDelimiters->push_back(lastDelimiter);
1120 inputString.substr(i, j - i))
1121 : inputString.substr(i, j - i));
1125 if(listOfDelimiters && listToReturn.size() - 1 != listOfDelimiters->size() &&
1126 listToReturn.size() != listOfDelimiters->size())
1128 __SS__ <<
"There is a mismatch in delimiters to entries (should be equal or one "
1130 << listOfDelimiters->size() <<
" vs " << listToReturn.size() << __E__
1152 const std::string& inputString,
1153 const std::set<char>& delimiter,
1154 const std::set<char>& whitespace,
1155 std::vector<char>* listOfDelimiters,
1156 bool decodeURIComponents)
1158 std::vector<std::string> listToReturn;
1165 decodeURIComponents);
1166 return listToReturn;
1174 std::map<std::string, std::string>& mapToReturn,
1175 const std::set<char>& pairPairDelimiter,
1176 const std::set<char>& nameValueDelimiter,
1177 const std::set<char>& whitespace)
1183 bool needValue =
false;
1187 for(; j < inputString.size(); ++j)
1190 if((whitespace.find(inputString[j]) !=
1192 pairPairDelimiter.find(inputString[j]) != pairPairDelimiter.end()) &&
1195 else if((whitespace.find(inputString[j]) !=
1198 nameValueDelimiter.find(inputString[j]) !=
1199 nameValueDelimiter.end()) &&
1205 name = inputString.substr(i, j - i);
1215 if((whitespace.find(inputString[j]) !=
1217 nameValueDelimiter.find(inputString[j]) != nameValueDelimiter.end()) &&
1220 else if(whitespace.find(inputString[j]) !=
1223 pairPairDelimiter.find(inputString[j]) !=
1224 pairPairDelimiter.end())
1230 auto emplaceReturn =
1231 mapToReturn.emplace(std::pair<std::string, std::string>(
1234 inputString.substr(i, j - i))
1237 if(!emplaceReturn.second)
1239 __COUT__ <<
"Ignoring repetitive value ('"
1240 << inputString.substr(i, j - i)
1241 <<
"') and keeping current value ('"
1242 << emplaceReturn.first->second <<
"'). " << __E__;
1254 auto emplaceReturn =
1255 mapToReturn.emplace(std::pair<std::string, std::string>(
1258 inputString.substr(i, j - i))
1261 if(!emplaceReturn.second)
1263 __COUT__ <<
"Ignoring repetitive value ('" << inputString.substr(i, j - i)
1264 <<
"') and keeping current value ('" << emplaceReturn.first->second
1269 catch(
const std::runtime_error& e)
1271 __SS__ <<
"Error while extracting a map from the string '" << inputString
1272 <<
"'... is it a valid map?" << __E__ << e.what() << __E__;
1279 const std::string& primaryDelimeter,
1280 const std::string& secondaryDelimeter)
1282 std::stringstream ss;
1284 for(
auto& mapPair : mapToReturn)
1289 ss << primaryDelimeter;
1290 ss << mapPair.first << secondaryDelimeter << (
unsigned int)mapPair.second;
1298 const std::string& delimeter)
1300 std::stringstream ss;
1302 for(
auto& setValue : setToReturn)
1308 ss << (
unsigned int)setValue;
1316 const std::string& delimeter)
1318 std::stringstream ss;
1320 if(delimeter ==
"\n")
1322 for(
auto& setValue : setToReturn)
1328 ss << (
unsigned int)setValue;
1344 std::vector<std::string>& commonChunksToReturn,
1345 std::vector<std::string>& wildcardStringsToReturn,
1346 unsigned int& fixedWildcardLength)
1348 fixedWildcardLength = 0;
1373 std::pair<
unsigned int ,
unsigned int > wildcardBounds(
1374 std::make_pair(-1, 0));
1377 for(
unsigned int n = 1; n < haystack.size(); ++n)
1378 for(
unsigned int i = 0, j = 0;
1379 i < haystack[0].length() && j < haystack[n].length();
1382 if(i < wildcardBounds.first)
1384 if(haystack[0][i] != haystack[n][j])
1386 wildcardBounds.first = i;
1393 __COUTS__(3) <<
"Low side = " << wildcardBounds.first <<
" "
1394 << haystack[0].substr(0, wildcardBounds.first) << __E__;
1397 for(
unsigned int n = 1; n < haystack.size(); ++n)
1398 for(
int i = haystack[0].length() - 1, j = haystack[n].length() - 1;
1399 i >= (int)wildcardBounds.first && j >= (
int)wildcardBounds.first;
1402 if(i > (
int)wildcardBounds.second)
1404 if(haystack[0][i] != haystack[n][j])
1406 wildcardBounds.second = i + 1;
1414 __COUTS__(3) <<
"High side = " << wildcardBounds.second <<
" "
1415 << haystack[0].substr(wildcardBounds.second) << __E__;
1418 commonChunksToReturn.push_back(haystack[0].substr(0, wildcardBounds.first));
1420 if(wildcardBounds.first != (
unsigned int)-1)
1423 for(
int i = (wildcardBounds.first + wildcardBounds.second) / 2 + 1;
1424 i < (
int)wildcardBounds.second;
1426 if(haystack[0][wildcardBounds.first] == haystack[0][i] &&
1427 haystack[0].substr(wildcardBounds.first, wildcardBounds.second - i) ==
1428 haystack[0].substr(i, wildcardBounds.second - i))
1430 std::string multiWildcardString =
1431 haystack[0].substr(i, wildcardBounds.second - i);
1432 __COUT__ <<
"Potential multi-wildcard found: " << multiWildcardString
1433 <<
" at position i=" << i << __E__;
1435 std::vector<
unsigned int > wildCardInstances;
1437 wildCardInstances.push_back(wildcardBounds.first);
1439 unsigned int offset =
1440 wildCardInstances[0] + multiWildcardString.size() + 1;
1441 std::string middleString = haystack[0].substr(offset, (i - 1) - offset);
1442 __COUTV__(middleString);
1446 while((k = middleString.find(multiWildcardString)) != std::string::npos)
1448 __COUT__ <<
"Multi-wildcard found at " << k << __E__;
1450 wildCardInstances.push_back(offset + k);
1453 middleString.substr(k + multiWildcardString.size() + 1);
1454 offset += k + multiWildcardString.size() + 1;
1455 __COUTV__(middleString);
1459 wildCardInstances.push_back(i);
1461 for(
unsigned int w = 0; w < wildCardInstances.size() - 1; ++w)
1463 __COUTV__(wildCardInstances[w]);
1464 __COUTV__(wildCardInstances[w + 1]);
1465 __COUTV__(wildCardInstances.size());
1466 commonChunksToReturn.push_back(haystack[0].substr(
1467 wildCardInstances[w] + multiWildcardString.size(),
1468 wildCardInstances[w + 1] -
1469 (wildCardInstances[w] + multiWildcardString.size())));
1475 for(
unsigned int c = 1; c < commonChunksToReturn.size(); ++c)
1477 __COUT__ <<
"Checking [" << c <<
"]: " << commonChunksToReturn[c] << __E__;
1478 for(
unsigned int n = 1; n < haystack.size(); ++n)
1480 __COUT__ <<
"Checking chunks work with haystack [" << n
1481 <<
"]: " << haystack[n] << __E__;
1482 __COUTV__(commonChunksToReturn[0].size());
1483 std::string wildCardValue = haystack[n].substr(
1484 commonChunksToReturn[0].size(),
1485 haystack[n].find(commonChunksToReturn[1],
1486 commonChunksToReturn[0].size() + 1) -
1487 commonChunksToReturn[0].size());
1488 __COUTTV__(wildCardValue);
1490 std::string builtString =
"";
1491 for(
unsigned int cc = 0; cc < commonChunksToReturn.size(); ++cc)
1492 builtString += commonChunksToReturn[cc] + wildCardValue;
1493 __COUTTV__(builtString);
1494 __COUTTV__(wildCardValue);
1496 if(haystack[n].find(builtString) != 0)
1498 __COUT__ <<
"Dropping common chunk " << commonChunksToReturn[c]
1499 <<
", built '" << builtString <<
"' not found in "
1500 << haystack[n] << __E__;
1501 commonChunksToReturn.erase(commonChunksToReturn.begin() + c);
1506 __COUTT__ <<
"Found built '" << builtString <<
"' in " << haystack[n]
1512 __COUTTV__(commonChunksToReturn[0].size());
1514 __COUTTV__(fixedWildcardLength);
1516 for(
unsigned int i = 0; i < commonChunksToReturn[0].size(); ++i)
1517 if(commonChunksToReturn[0][commonChunksToReturn[0].size() - 1 - i] ==
'0')
1519 ++fixedWildcardLength;
1520 __COUTT__ <<
"Trying for added fixed length +1 to " << fixedWildcardLength
1527 for(
unsigned int c = 0; c < commonChunksToReturn.size(); ++c)
1529 unsigned int cnt = 0;
1530 for(
unsigned int i = 0; i < commonChunksToReturn[c].size(); ++i)
1531 if(commonChunksToReturn[c][commonChunksToReturn[c].size() - 1 - i] ==
'0')
1536 if(fixedWildcardLength < cnt)
1537 fixedWildcardLength = cnt;
1538 else if(fixedWildcardLength > cnt)
1540 __SS__ <<
"Invalid fixed length found, please simplify indexing between "
1541 "these common chunks: "
1546 __COUTTV__(fixedWildcardLength);
1548 if(fixedWildcardLength)
1549 for(
unsigned int c = 0; c < commonChunksToReturn.size(); ++c)
1550 commonChunksToReturn[c] = commonChunksToReturn[c].substr(
1551 0, commonChunksToReturn[c].size() - fixedWildcardLength);
1554 commonChunksToReturn.push_back(haystack[0].substr(wildcardBounds.second));
1561 unsigned int ioff = fixedWildcardLength;
1562 bool wildcardsNeeded =
false;
1563 bool someLeadingZeros =
false;
1564 bool allWildcardsSameSize =
true;
1566 for(
unsigned int n = 0; n < haystack.size(); ++n)
1568 std::string wildcard =
"";
1570 i = ioff + commonChunksToReturn[0].size();
1572 if(commonChunksToReturn.size() == 1)
1573 wildcard = haystack[n].substr(i);
1575 for(
unsigned int c = 1; c < commonChunksToReturn.size(); ++c)
1577 if(c == commonChunksToReturn.size() - 1)
1578 k = haystack[n].rfind(commonChunksToReturn[c]);
1580 k = haystack[n].find(commonChunksToReturn[c], i + 1);
1587 __COUTVS__(3, k - i);
1589 wildcard = haystack[n].substr(i, k - i);
1590 if(fixedWildcardLength && n == 0)
1591 fixedWildcardLength += wildcard.size();
1593 __COUTS__(3) <<
"name[" << n <<
"] = " << wildcard <<
" fixed @ "
1594 << fixedWildcardLength << __E__;
1599 wildcard != haystack[n].substr(i, k - i))
1601 __SS__ <<
"Invalid wildcard! for name[" << n <<
"] = " << haystack[n]
1602 <<
" - the extraction algorithm is confused, please simplify "
1603 "your naming convention."
1613 wildcardsNeeded =
true;
1616 if(wildcard[0] ==
'0' && !fixedWildcardLength)
1618 someLeadingZeros =
true;
1619 if(wildcardStringsToReturn.size() &&
1620 wildcard.size() != wildcardStringsToReturn[0].size())
1621 allWildcardsSameSize =
false;
1624 wildcardStringsToReturn.push_back(wildcard);
1631 if(someLeadingZeros && allWildcardsSameSize)
1633 __COUTTV__(fixedWildcardLength);
1634 fixedWildcardLength = wildcardStringsToReturn[0].size();
1635 __COUT__ <<
"Enforce wildcard size of " << fixedWildcardLength << __E__;
1638 if(wildcardStringsToReturn.size() != haystack.size())
1640 __SS__ <<
"There was a problem during common chunk extraction!" << __E__;
1644 return wildcardsNeeded;
1653 const std::string& rhs)
const
1659 for(
unsigned int i = 0; i < lhs.size() && i < rhs.size(); ++i)
1662 if((lhs[i] >=
'A' && lhs[i] <=
'Z' && rhs[i] >=
'A' && rhs[i] <=
'Z') ||
1663 (lhs[i] >=
'a' && lhs[i] <=
'z' && rhs[i] >=
'a' && rhs[i] <=
'z'))
1665 if(lhs[i] == rhs[i])
1667 return (lhs[i] < rhs[i]);
1670 else if(lhs[i] >=
'A' && lhs[i] <=
'Z')
1672 if(lhs[i] + 32 == rhs[i])
1674 return (lhs[i] + 32 < rhs[i]);
1676 else if(rhs[i] >=
'A' && rhs[i] <=
'Z')
1678 if(lhs[i] == rhs[i] + 32)
1680 return (lhs[i] < rhs[i] + 32);
1684 if(lhs[i] == rhs[i])
1686 return (lhs[i] < rhs[i]);
1691 return lhs.size() < rhs.size();
1701 std::array<char, 128> buffer;
1706 std::string cmdWithRedirect = std::string(cmd) +
" 2>&1";
1707 std::shared_ptr<FILE> pipe(popen(cmdWithRedirect.c_str(),
"r"), pclose);
1709 __THROW__(
"popen() failed!");
1712 while(!feof(pipe.get()))
1714 if(fgets(buffer.data(), 128, pipe.get()) !=
nullptr)
1715 result += buffer.data();
1728 uintptr_t find_library_base(
const std::string& libname)
1730 std::ifstream maps(
"/proc/self/maps");
1733 while(std::getline(maps, line))
1735 if(line.find(libname) != std::string::npos &&
1736 line.find(
"r-xp") != std::string::npos)
1739 std::stringstream ss(line);
1740 ss >> std::hex >> base;
1748 void resolve_stack_entry(
const std::string& so_path,
1749 const std::string& real_name,
1750 const std::string& offset_begin,
1751 const std::string& offset_end
1756 const std::size_t pos0x = offset_end.find(
"0x");
1757 if(pos0x == std::string::npos)
1759 __COUTS__(52) <<
"resolve_stack_entry: could not find \"0x\" in offset_end: '"
1760 << offset_end <<
"'" << __E__;
1764 const std::size_t posBracket = offset_end.find(
']', pos0x);
1765 if(posBracket == std::string::npos || posBracket < pos0x + 3)
1767 __COUTS__(52) <<
"resolve_stack_entry: could not find closing ']' with at least "
1768 "one hex digit after \"0x\" "
1770 << offset_end <<
"'" << __E__;
1774 const std::string addr_str = offset_end.substr(pos0x, posBracket - pos0x);
1776 uintptr_t runtime_addr = 0;
1779 runtime_addr = std::stoull(addr_str,
nullptr, 16);
1781 catch(
const std::exception& e)
1783 __COUTS__(52) <<
"resolve_stack_entry: failed to parse runtime address from '"
1784 << addr_str <<
"': " << e.what() << __E__;
1788 std::string so_name = so_path.substr(so_path.find_last_of(
'/') + 1);
1790 uintptr_t base = find_library_base(so_name);
1793 std::cerr <<
"Could not find base for " << so_name <<
"\n";
1797 uintptr_t file_addr = runtime_addr - base;
1799 std::ostringstream cmd;
1800 cmd <<
"addr2line -f -C -e " << so_path <<
" 0x" << std::hex << file_addr;
1802 __COUT__ <<
"\nResolving:\n"
1803 << so_path <<
" : " << real_name << offset_begin <<
" [" << std::hex
1804 << runtime_addr <<
"]\n\n";
1815 #include <execinfo.h>
1819 __SS__ <<
"ots::stackTrace:\n";
1825 size = backtrace(array, 10);
1829 char** messages = backtrace_symbols(array, size);
1833 for(
unsigned int i = 1; i < size && messages != NULL; ++i)
1846 char *mangled_name = 0, *offset_begin = 0, *offset_end = 0;
1849 for(
char* p = messages[i]; *p; ++p)
1867 if(mangled_name && offset_begin && offset_end && mangled_name < offset_begin)
1869 *mangled_name++ =
'\0';
1870 *offset_begin++ =
'\0';
1871 *offset_end++ =
'\0';
1874 char* real_name = abi::__cxa_demangle(mangled_name, 0, 0, &status);
1879 ss <<
"[" << i <<
"] " << messages[i] <<
" : " << real_name <<
"+"
1880 << offset_begin << offset_end << std::endl;
1887 ss <<
"[" << i <<
"] " << messages[i] <<
" : " << mangled_name <<
"+"
1888 << offset_begin << offset_end << std::endl;
1895 ss <<
"[" << i <<
"] " << messages[i] << std::endl;
1915 const std::string& location,
1916 const unsigned int& line)
1918 char* environmentVariablePtr = getenv(name);
1919 if(!environmentVariablePtr)
1921 __SS__ <<
"Environment variable '$" << name <<
"' not defined at " << location
1922 <<
":" << line << __E__;
1926 return environmentVariablePtr;
1933 const std::string& field,
1934 uint32_t occurrence,
1936 size_t* returnFindPos ,
1937 const std::string& valueField ,
1938 const std::string& quoteType )
1941 *returnFindPos = std::string::npos;
1943 __COUTVS__(41, xml);
1945 size_t lo, findpos = after, hi;
1946 for(uint32_t i = 0; i <= occurrence; ++i)
1948 bool anyFound =
false;
1950 xml.find(
"<" + field,
1951 findpos)) != std::string::npos &&
1952 findpos + 1 + field.size() < xml.size())
1954 __COUTS__(40) <<
"find: ---- '<" << field <<
" findpos=" << findpos
1955 <<
"findpos " << findpos <<
" " << xml[findpos] <<
" "
1956 << xml[findpos + 1 + field.size()] <<
" "
1957 << (int)xml[findpos + 1 + field.size()] << __E__;
1965 if((quoteType ==
">" && xml[findpos] ==
'>') || xml[findpos] ==
' ' ||
1966 xml[findpos] ==
'\n' || xml[findpos] ==
'\t')
1975 __COUTS__(40) <<
"Field '" << field <<
"' not found" << __E__;
1980 lo = xml.find(valueField + quoteType, findpos) + valueField.size() + quoteType.size();
1982 if(TTEST(40) && quoteType.size())
1984 __COUTS__(40) <<
"Neighbors of field '" << field <<
"' and value '" << valueField
1985 <<
"' w/quote = " << quoteType << __E__;
1986 for(
size_t i = lo - valueField.size(); i < lo + 10 && i < xml.size(); ++i)
1987 __COUTS__(40) <<
"xml[" << i <<
"] " << xml[i] <<
" vs " << quoteType <<
" ? "
1988 << (int)xml[i] <<
" vs " << (
int)quoteType[0] << __E__;
1992 quoteType ==
">" ?
"<" : quoteType,
1993 lo)) == std::string::npos)
1995 __COUTS__(40) <<
"Value closing not found" << __E__;
2000 *returnFindPos = findpos - (1 + field.size());
2002 __COUTS__(40) <<
"after: " << after <<
", findpos: " << findpos <<
", hi/lo: " << hi
2003 <<
"/" << lo <<
", size: " << xml.size() << __E__;
2004 __COUTVS__(40, xml.substr(lo, hi - lo));
2005 return xml.substr(lo, hi - lo);
2012 const std::string& field,
2013 uint32_t occurrence,
2015 size_t* returnFindPos ,
2016 const std::string& valueField ,
2017 const std::string& quoteType )
2020 *returnFindPos = std::string::npos;
2022 __COUTVS__(41, xml);
2024 size_t lo = 0, hi, findpos = before;
2025 for(uint32_t i = 0; i <= occurrence; ++i)
2027 bool anyFound =
false;
2029 xml.rfind(
"<" + field,
2030 findpos)) != std::string::npos &&
2031 findpos + 1 + field.size() < xml.size())
2033 __COUTS__(40) <<
"rfind: ---- '<" << field <<
" findpos=" << findpos <<
" "
2034 << xml[findpos] <<
" " << xml[findpos + 1 + field.size()] <<
" "
2035 << (int)xml[findpos + 1 + field.size()] << __E__;
2037 findpos += 1 + field.size();
2040 if((quoteType ==
">" && xml[findpos] ==
'>') || xml[findpos] ==
' ' ||
2041 xml[findpos] ==
'\n' || xml[findpos] ==
'\t')
2047 findpos -= 1 + field.size() + 1;
2051 __COUTS__(40) <<
"Field '" << field <<
"' not found" << __E__;
2056 lo = xml.find(valueField + quoteType, findpos) + valueField.size() + quoteType.size();
2058 if(TTEST(40) && quoteType.size())
2060 __COUTS__(40) <<
"Neighbors?" << __E__;
2061 for(
size_t i = findpos; i < lo + 10 && i < xml.size(); ++i)
2062 __COUTS__(40) <<
"xml[" << i <<
"] " << xml[i] <<
" vs " << quoteType <<
" ? "
2063 << (int)xml[i] <<
" vs " << (
int)quoteType[0] << __E__;
2067 quoteType ==
">" ?
"<" : quoteType,
2068 lo)) == std::string::npos)
2070 __COUTS__(40) <<
"Value closing not found" << __E__;
2076 findpos - (1 + field.size());
2078 __COUTS__(40) <<
"before: " << before <<
", findpos: " << findpos <<
", hi/lo: " << hi
2079 <<
"/" << lo <<
", size: " << xml.size() << __E__;
2080 __COUTVS__(40, xml.substr(lo, hi - lo));
2081 return xml.substr(lo, hi - lo);
2089 const std::set<char>& delimiter )
2093 __COUTV__(splitArr.size());
2094 __COUTVS__(lvl, splitArr.size());
2095 for(
const auto& split : splitArr)
2096 __COUTS__(lvl) << split;
2111 std::unique_ptr<char, void (*)(
void*)> res{
2112 abi::__cxa_demangle(name, NULL, NULL, &status), std::free};
2114 return (status == 0) ? res.get() : name;
defines used also by OtsConfigurationWizardSupervisor
bool operator()(const std::string &lhs, const std::string &rhs) const
<get string in order ignoring letter case
static std::string getTimestampString(const std::string &linuxTimeInSeconds)
static const std::string & trim(std::string &s)
static std::string extractXmlField(const std::string &xml, const std::string &field, uint32_t occurrence, size_t after, size_t *returnFindPos=nullptr, const std::string &valueField="value=", const std::string "eType="'")
static void getVectorFromString(const std::string &inputString, std::vector< std::string > &listToReturn, const std::set< char > &delimiter={',', '|', '&'}, const std::set< char > &whitespace={' ', '\t', '\n', '\r'}, std::vector< char > *listOfDelimiters=0, bool decodeURIComponents=false)
static std::string exec(const char *cmd)
static void getSetFromString(const std::string &inputString, std::set< std::string > &setToReturn, const std::set< char > &delimiter={',', '|', '&'}, const std::set< char > &whitespace={' ', '\t', '\n', '\r'})
static std::string setToString(const std::set< T > &setToReturn, const std::string &delimeter=", ")
setToString ~
static std::string escapeString(std::string inString, bool allowWhiteSpace=false, bool forHtml=false)
static T validateValueForDefaultStringDataType(const std::string &value, bool doConvertEnvironmentVariables=true)
static char * otsGetEnvironmentVarable(const char *name, const std::string &location, const unsigned int &line)
static void sanitizeForSQL(std::string &data)
StringMacros::sanitizeForSQL.
static void coutSplit(const std::string &str, uint8_t traceLevel=TLVL_DEBUG, const std::set< char > &delimiter={',', '\n', ';'})
static std::string vectorToString(const std::vector< T > &setToReturn, const std::string &delimeter=", ")
vectorToString ~
static std::string convertEnvironmentVariables(const std::string &data)
static std::string getNumberType(const std::string &stringToCheck)
Note: before call consider use of stringToCheck = StringMacros::convertEnvironmentVariables(stringToC...
static std::string escapeJSONStringEntities(const std::string &str)
static std::string demangleTypeName(const char *name)
static std::string rextractXmlField(const std::string &xml, const std::string &field, uint32_t occurrence, size_t before, size_t *returnFindPos=nullptr, const std::string &valueField="value=", const std::string "eType="'")
static bool extractCommonChunks(const std::vector< std::string > &haystack, std::vector< std::string > &commonChunksToReturn, std::vector< std::string > &wildcardStrings, unsigned int &fixedWildcardLength)
static bool inWildCardSet(const std::string &needle, const std::set< std::string > &haystack)
static bool isNumber(const std::string &stringToCheck)
Note: before call consider use of stringToCheck = StringMacros::convertEnvironmentVariables(stringToC...
static std::string mapToString(const std::map< std::string, T > &mapToReturn, const std::string &primaryDelimeter=", ", const std::string &secondaryDelimeter=": ")
static void getMapFromString(const std::string &inputString, std::map< S, T > &mapToReturn, const std::set< char > &pairPairDelimiter={',', '|', '&'}, const std::set< char > &nameValueDelimiter={'=', ':'}, const std::set< char > &whitespace={' ', '\t', '\n', '\r'})
getMapFromString ~
static std::string restoreJSONStringEntities(const std::string &str)
static std::string getTimeDurationString(const time_t durationInSeconds=time(0))
static bool wildCardMatch(const std::string &needle, const std::string &haystack, unsigned int *priorityIndex=0)
static std::string decodeURIComponent(const std::string &data)
static std::string stackTrace(void)
static bool getNumber(const std::string &s, T &retValue)