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] ==
'\'')
358 (inString[i] ==
'\'')
361 inString.replace(i + 5, 1, 1,
';');
365 else if(inString[i] ==
'&')
367 inString.insert(i,
"&");
368 inString.replace(i + 4, 1, 1,
';');
371 else if(inString[i] ==
'<' || inString[i] ==
'>')
396 else if(inString[i] >=
char(161) &&
397 inString[i] <=
char(255))
399 sprintf(htmlTmp,
"&#%3.3d", inString[i]);
400 inString.insert(i, std::string(htmlTmp));
401 inString.replace(i + 5, 1, 1,
';');
405 __COUTS__(TLVL_EscapeString) << inString << std::endl;
409 else if(allowWhiteSpace)
417 inString.insert(i,
" ");
420 inString.insert(i,
" ");
421 inString.replace(i + 5, 1, 1,
';');
426 __COUTS__(TLVL_EscapeString) << inString.size() <<
" " << ws << std::endl;
430 __COUTS__(TLVL_EscapeString) << inString.size() <<
" " << inString << std::endl;
436 if(ws == (
unsigned int)-1)
438 return inString.substr(0, ws + 1);
448 unsigned int sz = str.size();
452 std::string retStr =
"";
453 retStr.reserve(str.size() * 2);
454 for(
unsigned int i = 0; i < sz; ++i)
486 unsigned int sz = str.size();
490 std::string retStr =
"";
491 retStr.reserve(str.size());
493 for(; i < sz - 1; ++i)
525 retStr += str[sz - 1];
536 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](
unsigned char ch) {
537 return !std::isspace(ch);
541 s.erase(std::find_if(
542 s.rbegin(), s.rend(), [](
unsigned char ch) { return !std::isspace(ch); })
561 size_t begin = data.find(
"$");
562 if(begin != std::string::npos && begin + 1 < data.size())
565 std::string envVariable;
566 std::string converted = data;
567 bool usedBraces =
false;
569 while(begin && begin != std::string::npos &&
570 converted[begin - 1] ==
573 converted.replace(begin - 1, 1,
"");
574 begin = converted.find(
"$", begin + 1);
575 if(begin == std::string::npos)
577 __COUTS__(TLVL_EnvSub)
578 <<
"Only found escaped $'s that will not be converted: " << converted
587 if(begin + 2 < data.size() && data[begin + 1] ==
'(' && data[begin + 2] ==
'(')
589 end = data.find(
"))", begin + 3);
590 if(end == std::string::npos)
592 __SS__ <<
"Arithmetic expansion '$((...)),' at pos " << begin
593 <<
" in value, is missing closing '))'! Here was the value: "
598 std::string expression = data.substr(begin + 3, end - begin - 3);
599 __COUTVS__(TLVL_EnvMath, expression);
603 __COUTVS__(TLVL_EnvMath, expression);
610 __SS__ <<
"Arithmetic expansion '$((...)),' at pos " << begin
611 <<
" in value, does not evaluate to a number! Here was the value: "
616 __COUTS__(TLVL_EnvMath) <<
"Arithmetic result: " << result << __E__;
620 converted.replace(begin, end - begin + 2, std::to_string(result)));
622 else if(data[begin + 1] ==
'{')
624 end = data.find(
"}", begin + 2);
625 envVariable = data.substr(begin + 2, end - begin - 2);
632 for(end = begin + 1; end < data.size(); ++end)
633 if(!((data[end] >=
'0' && data[end] <=
'9') ||
634 (data[end] >=
'A' && data[end] <=
'Z') ||
635 (data[end] >=
'a' && data[end] <=
'z') || data[end] ==
'-' ||
636 data[end] ==
'_' || data[end] ==
'.' || data[end] ==
':'))
638 envVariable = data.substr(begin + 1, end - begin - 1);
641 __COUTVS__(TLVL_EnvSub, data);
642 __COUTVS__(TLVL_EnvSub, envVariable);
643 if(usedBraces && envVariable.starts_with(
"OTS."))
645 __COUTS__(TLVL_EnvSub) <<
"OTS system variable detected!" << __E__;
649 if(sysVarSplit.size() != 3 ||
650 systemVariables_.find(sysVarSplit[1]) == systemVariables_.end() ||
651 systemVariables_.at(sysVarSplit[1]).find(sysVarSplit[2]) ==
652 systemVariables_.at(sysVarSplit[1]).end())
655 <<
"System variable ${" << envVariable
656 <<
"} is not valid or was not found!"
658 <<
"If you were trying to access an ots System Variable, the correct "
660 <<
"${OTS.<variable>.<property>}, e.g. "
661 "${OTS.ActiveStateMachine.name}"
663 <<
"If you were trying to insert an arithmetic operation, the "
665 "syntax is $((4 - 3)) or $(($ENVVAR1 - $ENVVAR2))"
667 <<
"Available system variables:" << __E__;
670 for(
const auto& varPair : systemVariables_)
672 ss <<
"\n OTS." << varPair.first <<
".*";
673 for(
const auto& propPair : varPair.second)
674 ss <<
"\n - OTS." << varPair.first <<
"." << propPair.first;
684 systemVariables_.at(sysVarSplit[1]).at(sysVarSplit[2])));
688 char* envResult =
nullptr;
691 envResult = __ENV__(envVariable.c_str());
693 catch(
const std::runtime_error& e)
696 << (
"The environmental variable '" + envVariable +
697 "' is not set! Please make sure you set it before continuing!" +
699 "If you were trying to access an ots System Variable, the "
700 "correct syntax is " +
701 "${OTS.<variable>.<property>}, e.g. "
702 "${OTS.ActiveStateMachine.name}")
704 ss <<
"\n" << e.what() << __E__;
710 converted.replace(begin, end - begin, envResult));
714 __COUTS__(TLVL_EnvSub) <<
"Result: " << data << __E__;
726 std::vector<std::string> numbers;
727 std::vector<char> ops;
735 std::set<char>({
'+',
'-',
'*',
'/'}),
736 std::set<char>({
' ',
'\t',
'\n',
'\r'}),
742 for(
const auto& number : numbers)
744 if(number.size() == 0)
747 if(number.find(
"0x") == 0)
750 for(
unsigned int i = 2; i < number.size(); ++i)
752 if(!((number[i] >=
'0' && number[i] <=
'9') ||
753 (number[i] >=
'A' && number[i] <=
'F') ||
754 (number[i] >=
'a' && number[i] <=
'f')))
762 else if(number[0] ==
'b')
766 for(
unsigned int i = 1; i < number.size(); ++i)
768 if(!((number[i] >=
'0' && number[i] <=
'1')))
778 for(
unsigned int i = 0; i < number.size(); ++i)
779 if(!((number[i] >=
'0' && number[i] <=
'9') || number[i] ==
'.' ||
780 number[i] ==
'+' || number[i] ==
'-'))
803 std::vector<std::string> numbers;
804 std::vector<char> ops;
806 bool hasDecimal =
false;
811 std::set<char>({
'+',
'-',
'*',
'/'}),
812 std::set<char>({
' ',
'\t',
'\n',
'\r'}),
818 for(
const auto& number : numbers)
820 if(number.size() == 0)
823 if(number.find(
"0x") == 0)
826 for(
unsigned int i = 2; i < number.size(); ++i)
828 if(!((number[i] >=
'0' && number[i] <=
'9') ||
829 (number[i] >=
'A' && number[i] <=
'F') ||
830 (number[i] >=
'a' && number[i] <=
'f')))
838 else if(number[0] ==
'b')
842 for(
unsigned int i = 1; i < number.size(); ++i)
844 if(!((number[i] >=
'0' && number[i] <=
'1')))
854 for(
unsigned int i = 0; i < number.size(); ++i)
855 if(!((number[i] >=
'0' && number[i] <=
'9') || number[i] ==
'.' ||
856 number[i] ==
'+' || number[i] ==
'-'))
858 else if(number[i] ==
'.')
871 return "unsigned long long";
884 __COUT_ERR__ <<
"Invalid empty bool string " << s << __E__;
889 if(s.find(
"1") != std::string::npos || s ==
"true" || s ==
"True" || s ==
"TRUE")
896 if(s.find(
"0") != std::string::npos || s ==
"false" || s ==
"False" || s ==
"FALSE")
902 __COUT_ERR__ <<
"Invalid bool string " << s << __E__;
913 time_t timestamp(strtol(linuxTimeInSeconds.c_str(), 0, 10));
933 std::stringstream ss;
934 int days = t / 60 / 60 / 24;
937 ss << days <<
" day" << (days > 1 ?
"s" :
"") <<
", ";
938 t -= days * 60 * 60 * 24;
942 ss << std::setw(2) << std::setfill(
'0') << (t / 60 / 60) <<
":" << std::setw(2)
943 << std::setfill(
'0') << ((t % (60 * 60)) / 60) <<
":" << std::setw(2)
944 << std::setfill(
'0') << (t % 60);
952 const std::string& value,
bool doConvertEnvironmentVariables)
955 return doConvertEnvironmentVariables
959 catch(
const std::runtime_error& e)
961 __SS__ <<
"Failed to validate value for default string data type. " << __E__
962 << e.what() << __E__;
971 std::set<std::string>& setToReturn,
972 const std::set<char>& delimiter,
973 const std::set<char>& whitespace)
980 for(; j < inputString.size(); ++j)
981 if((whitespace.find(inputString[j]) !=
983 delimiter.find(inputString[j]) != delimiter.end()) &&
986 else if((whitespace.find(inputString[j]) !=
989 delimiter.find(inputString[j]) != delimiter.end()) &&
995 setToReturn.emplace(inputString.substr(i, j - i));
1002 setToReturn.emplace(inputString.substr(i, j - i));
1018 std::vector<std::string>& listToReturn,
1019 const std::set<char>& delimiter,
1020 const std::set<char>& whitespace,
1021 std::vector<char>* listOfDelimiters,
1022 bool decodeURIComponents)
1027 std::set<char>::iterator delimeterSearchIt;
1028 char lastDelimiter = 0;
1037 for(; c < inputString.size(); ++c)
1041 delimeterSearchIt = delimiter.find(inputString[c]);
1042 isDelimiter = delimeterSearchIt != delimiter.end();
1047 if(whitespace.find(inputString[c]) !=
1056 else if(whitespace.find(inputString[c]) != whitespace.end() &&
1061 else if(isDelimiter)
1066 if(listOfDelimiters && listToReturn.size())
1073 listOfDelimiters->push_back(lastDelimiter);
1076 inputString.substr(i, j - i))
1077 : inputString.substr(i, j - i));
1087 lastDelimiter = *delimeterSearchIt;
1096 if(listOfDelimiters && listToReturn.size())
1102 listOfDelimiters->push_back(lastDelimiter);
1105 inputString.substr(i, j - i))
1106 : inputString.substr(i, j - i));
1110 if(listOfDelimiters && listToReturn.size() - 1 != listOfDelimiters->size() &&
1111 listToReturn.size() != listOfDelimiters->size())
1113 __SS__ <<
"There is a mismatch in delimiters to entries (should be equal or one "
1115 << listOfDelimiters->size() <<
" vs " << listToReturn.size() << __E__
1137 const std::string& inputString,
1138 const std::set<char>& delimiter,
1139 const std::set<char>& whitespace,
1140 std::vector<char>* listOfDelimiters,
1141 bool decodeURIComponents)
1143 std::vector<std::string> listToReturn;
1150 decodeURIComponents);
1151 return listToReturn;
1159 std::map<std::string, std::string>& mapToReturn,
1160 const std::set<char>& pairPairDelimiter,
1161 const std::set<char>& nameValueDelimiter,
1162 const std::set<char>& whitespace)
1168 bool needValue =
false;
1172 for(; j < inputString.size(); ++j)
1175 if((whitespace.find(inputString[j]) !=
1177 pairPairDelimiter.find(inputString[j]) != pairPairDelimiter.end()) &&
1180 else if((whitespace.find(inputString[j]) !=
1183 nameValueDelimiter.find(inputString[j]) !=
1184 nameValueDelimiter.end()) &&
1190 name = inputString.substr(i, j - i);
1200 if((whitespace.find(inputString[j]) !=
1202 nameValueDelimiter.find(inputString[j]) != nameValueDelimiter.end()) &&
1205 else if(whitespace.find(inputString[j]) !=
1208 pairPairDelimiter.find(inputString[j]) !=
1209 pairPairDelimiter.end())
1215 auto emplaceReturn =
1216 mapToReturn.emplace(std::pair<std::string, std::string>(
1219 inputString.substr(i, j - i))
1222 if(!emplaceReturn.second)
1224 __COUT__ <<
"Ignoring repetitive value ('"
1225 << inputString.substr(i, j - i)
1226 <<
"') and keeping current value ('"
1227 << emplaceReturn.first->second <<
"'). " << __E__;
1239 auto emplaceReturn =
1240 mapToReturn.emplace(std::pair<std::string, std::string>(
1243 inputString.substr(i, j - i))
1246 if(!emplaceReturn.second)
1248 __COUT__ <<
"Ignoring repetitive value ('" << inputString.substr(i, j - i)
1249 <<
"') and keeping current value ('" << emplaceReturn.first->second
1254 catch(
const std::runtime_error& e)
1256 __SS__ <<
"Error while extracting a map from the string '" << inputString
1257 <<
"'... is it a valid map?" << __E__ << e.what() << __E__;
1264 const std::string& primaryDelimeter,
1265 const std::string& secondaryDelimeter)
1267 std::stringstream ss;
1269 for(
auto& mapPair : mapToReturn)
1274 ss << primaryDelimeter;
1275 ss << mapPair.first << secondaryDelimeter << (
unsigned int)mapPair.second;
1283 const std::string& delimeter)
1285 std::stringstream ss;
1287 for(
auto& setValue : setToReturn)
1293 ss << (
unsigned int)setValue;
1301 const std::string& delimeter)
1303 std::stringstream ss;
1305 if(delimeter ==
"\n")
1307 for(
auto& setValue : setToReturn)
1313 ss << (
unsigned int)setValue;
1329 std::vector<std::string>& commonChunksToReturn,
1330 std::vector<std::string>& wildcardStringsToReturn,
1331 unsigned int& fixedWildcardLength)
1333 fixedWildcardLength = 0;
1358 std::pair<
unsigned int ,
unsigned int > wildcardBounds(
1359 std::make_pair(-1, 0));
1362 for(
unsigned int n = 1; n < haystack.size(); ++n)
1363 for(
unsigned int i = 0, j = 0;
1364 i < haystack[0].length() && j < haystack[n].length();
1367 if(i < wildcardBounds.first)
1369 if(haystack[0][i] != haystack[n][j])
1371 wildcardBounds.first = i;
1378 __COUTS__(3) <<
"Low side = " << wildcardBounds.first <<
" "
1379 << haystack[0].substr(0, wildcardBounds.first) << __E__;
1382 for(
unsigned int n = 1; n < haystack.size(); ++n)
1383 for(
int i = haystack[0].length() - 1, j = haystack[n].length() - 1;
1384 i >= (int)wildcardBounds.first && j >= (
int)wildcardBounds.first;
1387 if(i > (
int)wildcardBounds.second)
1389 if(haystack[0][i] != haystack[n][j])
1391 wildcardBounds.second = i + 1;
1399 __COUTS__(3) <<
"High side = " << wildcardBounds.second <<
" "
1400 << haystack[0].substr(wildcardBounds.second) << __E__;
1403 commonChunksToReturn.push_back(haystack[0].substr(0, wildcardBounds.first));
1405 if(wildcardBounds.first != (
unsigned int)-1)
1408 for(
int i = (wildcardBounds.first + wildcardBounds.second) / 2 + 1;
1409 i < (
int)wildcardBounds.second;
1411 if(haystack[0][wildcardBounds.first] == haystack[0][i] &&
1412 haystack[0].substr(wildcardBounds.first, wildcardBounds.second - i) ==
1413 haystack[0].substr(i, wildcardBounds.second - i))
1415 std::string multiWildcardString =
1416 haystack[0].substr(i, wildcardBounds.second - i);
1417 __COUT__ <<
"Potential multi-wildcard found: " << multiWildcardString
1418 <<
" at position i=" << i << __E__;
1420 std::vector<
unsigned int > wildCardInstances;
1422 wildCardInstances.push_back(wildcardBounds.first);
1424 unsigned int offset =
1425 wildCardInstances[0] + multiWildcardString.size() + 1;
1426 std::string middleString = haystack[0].substr(offset, (i - 1) - offset);
1427 __COUTV__(middleString);
1431 while((k = middleString.find(multiWildcardString)) != std::string::npos)
1433 __COUT__ <<
"Multi-wildcard found at " << k << __E__;
1435 wildCardInstances.push_back(offset + k);
1438 middleString.substr(k + multiWildcardString.size() + 1);
1439 offset += k + multiWildcardString.size() + 1;
1440 __COUTV__(middleString);
1444 wildCardInstances.push_back(i);
1446 for(
unsigned int w = 0; w < wildCardInstances.size() - 1; ++w)
1448 __COUTV__(wildCardInstances[w]);
1449 __COUTV__(wildCardInstances[w + 1]);
1450 __COUTV__(wildCardInstances.size());
1451 commonChunksToReturn.push_back(haystack[0].substr(
1452 wildCardInstances[w] + multiWildcardString.size(),
1453 wildCardInstances[w + 1] -
1454 (wildCardInstances[w] + multiWildcardString.size())));
1460 for(
unsigned int c = 1; c < commonChunksToReturn.size(); ++c)
1462 __COUT__ <<
"Checking [" << c <<
"]: " << commonChunksToReturn[c] << __E__;
1463 for(
unsigned int n = 1; n < haystack.size(); ++n)
1465 __COUT__ <<
"Checking chunks work with haystack [" << n
1466 <<
"]: " << haystack[n] << __E__;
1467 __COUTV__(commonChunksToReturn[0].size());
1468 std::string wildCardValue = haystack[n].substr(
1469 commonChunksToReturn[0].size(),
1470 haystack[n].find(commonChunksToReturn[1],
1471 commonChunksToReturn[0].size() + 1) -
1472 commonChunksToReturn[0].size());
1473 __COUTTV__(wildCardValue);
1475 std::string builtString =
"";
1476 for(
unsigned int cc = 0; cc < commonChunksToReturn.size(); ++cc)
1477 builtString += commonChunksToReturn[cc] + wildCardValue;
1478 __COUTTV__(builtString);
1479 __COUTTV__(wildCardValue);
1481 if(haystack[n].find(builtString) != 0)
1483 __COUT__ <<
"Dropping common chunk " << commonChunksToReturn[c]
1484 <<
", built '" << builtString <<
"' not found in "
1485 << haystack[n] << __E__;
1486 commonChunksToReturn.erase(commonChunksToReturn.begin() + c);
1491 __COUTT__ <<
"Found built '" << builtString <<
"' in " << haystack[n]
1497 __COUTTV__(commonChunksToReturn[0].size());
1499 __COUTTV__(fixedWildcardLength);
1501 for(
unsigned int i = 0; i < commonChunksToReturn[0].size(); ++i)
1502 if(commonChunksToReturn[0][commonChunksToReturn[0].size() - 1 - i] ==
'0')
1504 ++fixedWildcardLength;
1505 __COUTT__ <<
"Trying for added fixed length +1 to " << fixedWildcardLength
1512 for(
unsigned int c = 0; c < commonChunksToReturn.size(); ++c)
1514 unsigned int cnt = 0;
1515 for(
unsigned int i = 0; i < commonChunksToReturn[c].size(); ++i)
1516 if(commonChunksToReturn[c][commonChunksToReturn[c].size() - 1 - i] ==
'0')
1521 if(fixedWildcardLength < cnt)
1522 fixedWildcardLength = cnt;
1523 else if(fixedWildcardLength > cnt)
1525 __SS__ <<
"Invalid fixed length found, please simplify indexing between "
1526 "these common chunks: "
1531 __COUTTV__(fixedWildcardLength);
1533 if(fixedWildcardLength)
1534 for(
unsigned int c = 0; c < commonChunksToReturn.size(); ++c)
1535 commonChunksToReturn[c] = commonChunksToReturn[c].substr(
1536 0, commonChunksToReturn[c].size() - fixedWildcardLength);
1539 commonChunksToReturn.push_back(haystack[0].substr(wildcardBounds.second));
1546 unsigned int ioff = fixedWildcardLength;
1547 bool wildcardsNeeded =
false;
1548 bool someLeadingZeros =
false;
1549 bool allWildcardsSameSize =
true;
1551 for(
unsigned int n = 0; n < haystack.size(); ++n)
1553 std::string wildcard =
"";
1555 i = ioff + commonChunksToReturn[0].size();
1557 if(commonChunksToReturn.size() == 1)
1558 wildcard = haystack[n].substr(i);
1560 for(
unsigned int c = 1; c < commonChunksToReturn.size(); ++c)
1562 if(c == commonChunksToReturn.size() - 1)
1563 k = haystack[n].rfind(commonChunksToReturn[c]);
1565 k = haystack[n].find(commonChunksToReturn[c], i + 1);
1572 __COUTVS__(3, k - i);
1574 wildcard = haystack[n].substr(i, k - i);
1575 if(fixedWildcardLength && n == 0)
1576 fixedWildcardLength += wildcard.size();
1578 __COUTS__(3) <<
"name[" << n <<
"] = " << wildcard <<
" fixed @ "
1579 << fixedWildcardLength << __E__;
1584 wildcard != haystack[n].substr(i, k - i))
1586 __SS__ <<
"Invalid wildcard! for name[" << n <<
"] = " << haystack[n]
1587 <<
" - the extraction algorithm is confused, please simplify "
1588 "your naming convention."
1598 wildcardsNeeded =
true;
1601 if(wildcard[0] ==
'0' && !fixedWildcardLength)
1603 someLeadingZeros =
true;
1604 if(wildcardStringsToReturn.size() &&
1605 wildcard.size() != wildcardStringsToReturn[0].size())
1606 allWildcardsSameSize =
false;
1609 wildcardStringsToReturn.push_back(wildcard);
1616 if(someLeadingZeros && allWildcardsSameSize)
1618 __COUTTV__(fixedWildcardLength);
1619 fixedWildcardLength = wildcardStringsToReturn[0].size();
1620 __COUT__ <<
"Enforce wildcard size of " << fixedWildcardLength << __E__;
1623 if(wildcardStringsToReturn.size() != haystack.size())
1625 __SS__ <<
"There was a problem during common chunk extraction!" << __E__;
1629 return wildcardsNeeded;
1638 const std::string& rhs)
const
1644 for(
unsigned int i = 0; i < lhs.size() && i < rhs.size(); ++i)
1647 if((lhs[i] >=
'A' && lhs[i] <=
'Z' && rhs[i] >=
'A' && rhs[i] <=
'Z') ||
1648 (lhs[i] >=
'a' && lhs[i] <=
'z' && rhs[i] >=
'a' && rhs[i] <=
'z'))
1650 if(lhs[i] == rhs[i])
1652 return (lhs[i] < rhs[i]);
1655 else if(lhs[i] >=
'A' && lhs[i] <=
'Z')
1657 if(lhs[i] + 32 == rhs[i])
1659 return (lhs[i] + 32 < rhs[i]);
1661 else if(rhs[i] >=
'A' && rhs[i] <=
'Z')
1663 if(lhs[i] == rhs[i] + 32)
1665 return (lhs[i] < rhs[i] + 32);
1669 if(lhs[i] == rhs[i])
1671 return (lhs[i] < rhs[i]);
1676 return lhs.size() < rhs.size();
1686 std::array<char, 128> buffer;
1691 std::string cmdWithRedirect = std::string(cmd) +
" 2>&1";
1692 std::shared_ptr<FILE> pipe(popen(cmdWithRedirect.c_str(),
"r"), pclose);
1694 __THROW__(
"popen() failed!");
1697 while(!feof(pipe.get()))
1699 if(fgets(buffer.data(), 128, pipe.get()) !=
nullptr)
1700 result += buffer.data();
1713 uintptr_t find_library_base(
const std::string& libname)
1715 std::ifstream maps(
"/proc/self/maps");
1718 while(std::getline(maps, line))
1720 if(line.find(libname) != std::string::npos &&
1721 line.find(
"r-xp") != std::string::npos)
1724 std::stringstream ss(line);
1725 ss >> std::hex >> base;
1733 void resolve_stack_entry(
const std::string& so_path,
1734 const std::string& real_name,
1735 const std::string& offset_begin,
1736 const std::string& offset_end
1741 const std::size_t pos0x = offset_end.find(
"0x");
1742 if(pos0x == std::string::npos)
1744 __COUTS__(52) <<
"resolve_stack_entry: could not find \"0x\" in offset_end: '"
1745 << offset_end <<
"'" << __E__;
1749 const std::size_t posBracket = offset_end.find(
']', pos0x);
1750 if(posBracket == std::string::npos || posBracket < pos0x + 3)
1752 __COUTS__(52) <<
"resolve_stack_entry: could not find closing ']' with at least "
1753 "one hex digit after \"0x\" "
1755 << offset_end <<
"'" << __E__;
1759 const std::string addr_str = offset_end.substr(pos0x, posBracket - pos0x);
1761 uintptr_t runtime_addr = 0;
1764 runtime_addr = std::stoull(addr_str,
nullptr, 16);
1766 catch(
const std::exception& e)
1768 __COUTS__(52) <<
"resolve_stack_entry: failed to parse runtime address from '"
1769 << addr_str <<
"': " << e.what() << __E__;
1773 std::string so_name = so_path.substr(so_path.find_last_of(
'/') + 1);
1775 uintptr_t base = find_library_base(so_name);
1778 std::cerr <<
"Could not find base for " << so_name <<
"\n";
1782 uintptr_t file_addr = runtime_addr - base;
1784 std::ostringstream cmd;
1785 cmd <<
"addr2line -f -C -e " << so_path <<
" 0x" << std::hex << file_addr;
1787 __COUT__ <<
"\nResolving:\n"
1788 << so_path <<
" : " << real_name << offset_begin <<
" [" << std::hex
1789 << runtime_addr <<
"]\n\n";
1800 #include <execinfo.h>
1804 __SS__ <<
"ots::stackTrace:\n";
1810 size = backtrace(array, 10);
1814 char** messages = backtrace_symbols(array, size);
1818 for(
unsigned int i = 1; i < size && messages != NULL; ++i)
1831 char *mangled_name = 0, *offset_begin = 0, *offset_end = 0;
1834 for(
char* p = messages[i]; *p; ++p)
1852 if(mangled_name && offset_begin && offset_end && mangled_name < offset_begin)
1854 *mangled_name++ =
'\0';
1855 *offset_begin++ =
'\0';
1856 *offset_end++ =
'\0';
1859 char* real_name = abi::__cxa_demangle(mangled_name, 0, 0, &status);
1864 ss <<
"[" << i <<
"] " << messages[i] <<
" : " << real_name <<
"+"
1865 << offset_begin << offset_end << std::endl;
1872 ss <<
"[" << i <<
"] " << messages[i] <<
" : " << mangled_name <<
"+"
1873 << offset_begin << offset_end << std::endl;
1880 ss <<
"[" << i <<
"] " << messages[i] << std::endl;
1900 const std::string& location,
1901 const unsigned int& line)
1903 char* environmentVariablePtr = getenv(name);
1904 if(!environmentVariablePtr)
1906 __SS__ <<
"Environment variable '$" << name <<
"' not defined at " << location
1907 <<
":" << line << __E__;
1911 return environmentVariablePtr;
1918 const std::string& field,
1919 uint32_t occurrence,
1921 size_t* returnFindPos ,
1922 const std::string& valueField ,
1923 const std::string& quoteType )
1926 *returnFindPos = std::string::npos;
1928 __COUTVS__(41, xml);
1930 size_t lo, findpos = after, hi;
1931 for(uint32_t i = 0; i <= occurrence; ++i)
1933 bool anyFound =
false;
1935 xml.find(
"<" + field,
1936 findpos)) != std::string::npos &&
1937 findpos + 1 + field.size() < xml.size())
1939 __COUTS__(40) <<
"find: ---- '<" << field <<
" findpos=" << findpos
1940 <<
"findpos " << findpos <<
" " << xml[findpos] <<
" "
1941 << xml[findpos + 1 + field.size()] <<
" "
1942 << (int)xml[findpos + 1 + field.size()] << __E__;
1950 if((quoteType ==
">" && xml[findpos] ==
'>') || xml[findpos] ==
' ' ||
1951 xml[findpos] ==
'\n' || xml[findpos] ==
'\t')
1960 __COUTS__(40) <<
"Field '" << field <<
"' not found" << __E__;
1965 lo = xml.find(valueField + quoteType, findpos) + valueField.size() + quoteType.size();
1967 if(TTEST(40) && quoteType.size())
1969 __COUTS__(40) <<
"Neighbors of field '" << field <<
"' and value '" << valueField
1970 <<
"' w/quote = " << quoteType << __E__;
1971 for(
size_t i = lo - valueField.size(); i < lo + 10 && i < xml.size(); ++i)
1972 __COUTS__(40) <<
"xml[" << i <<
"] " << xml[i] <<
" vs " << quoteType <<
" ? "
1973 << (int)xml[i] <<
" vs " << (
int)quoteType[0] << __E__;
1977 quoteType ==
">" ?
"<" : quoteType,
1978 lo)) == std::string::npos)
1980 __COUTS__(40) <<
"Value closing not found" << __E__;
1985 *returnFindPos = findpos - (1 + field.size());
1987 __COUTS__(40) <<
"after: " << after <<
", findpos: " << findpos <<
", hi/lo: " << hi
1988 <<
"/" << lo <<
", size: " << xml.size() << __E__;
1989 __COUTVS__(40, xml.substr(lo, hi - lo));
1990 return xml.substr(lo, hi - lo);
1997 const std::string& field,
1998 uint32_t occurrence,
2000 size_t* returnFindPos ,
2001 const std::string& valueField ,
2002 const std::string& quoteType )
2005 *returnFindPos = std::string::npos;
2007 __COUTVS__(41, xml);
2009 size_t lo = 0, hi, findpos = before;
2010 for(uint32_t i = 0; i <= occurrence; ++i)
2012 bool anyFound =
false;
2014 xml.rfind(
"<" + field,
2015 findpos)) != std::string::npos &&
2016 findpos + 1 + field.size() < xml.size())
2018 __COUTS__(40) <<
"rfind: ---- '<" << field <<
" findpos=" << findpos <<
" "
2019 << xml[findpos] <<
" " << xml[findpos + 1 + field.size()] <<
" "
2020 << (int)xml[findpos + 1 + field.size()] << __E__;
2022 findpos += 1 + field.size();
2025 if((quoteType ==
">" && xml[findpos] ==
'>') || xml[findpos] ==
' ' ||
2026 xml[findpos] ==
'\n' || xml[findpos] ==
'\t')
2032 findpos -= 1 + field.size() + 1;
2036 __COUTS__(40) <<
"Field '" << field <<
"' not found" << __E__;
2041 lo = xml.find(valueField + quoteType, findpos) + valueField.size() + quoteType.size();
2043 if(TTEST(40) && quoteType.size())
2045 __COUTS__(40) <<
"Neighbors?" << __E__;
2046 for(
size_t i = findpos; i < lo + 10 && i < xml.size(); ++i)
2047 __COUTS__(40) <<
"xml[" << i <<
"] " << xml[i] <<
" vs " << quoteType <<
" ? "
2048 << (int)xml[i] <<
" vs " << (
int)quoteType[0] << __E__;
2052 quoteType ==
">" ?
"<" : quoteType,
2053 lo)) == std::string::npos)
2055 __COUTS__(40) <<
"Value closing not found" << __E__;
2061 findpos - (1 + field.size());
2063 __COUTS__(40) <<
"before: " << before <<
", findpos: " << findpos <<
", hi/lo: " << hi
2064 <<
"/" << lo <<
", size: " << xml.size() << __E__;
2065 __COUTVS__(40, xml.substr(lo, hi - lo));
2066 return xml.substr(lo, hi - lo);
2074 const std::set<char>& delimiter )
2078 __COUTV__(splitArr.size());
2079 __COUTVS__(lvl, splitArr.size());
2080 for(
const auto& split : splitArr)
2081 __COUTS__(lvl) << split;
2096 std::unique_ptr<char, void (*)(
void*)> res{
2097 abi::__cxa_demangle(name, NULL, NULL, &status), std::free};
2099 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)