1 #include "otsdaq/Macros/StringMacros.h"
10 std::map<std::string ,
11 std::map<std::string , std::string >>
12 StringMacros::systemVariables_;
13 const std::string StringMacros::TBD =
"To-be-defined";
16 unsigned int StringMacros::getConcurrencyCount(
void)
20 return std::stoul(systemVariables_.at(
"System").at(
"logicalCores"));
27 if(sched_getaffinity(0,
sizeof(mask), &mask) == 0)
28 hw = CPU_COUNT(&mask);
29 systemVariables_[
"System"][
"logicalCores"] = std::to_string(hw);
33 #define TLVL_EscapeString 30
34 #define TLVL_EnvMath 49
35 #define TLVL_EnvSub 50
50 const std::string& haystack,
51 unsigned int* priorityIndex)
54 __COUTT__ <<
"\t\t wildCardMatch: " << needle <<
" =in= " << haystack <<
" ??? "
58 if(needle.size() == 0)
74 if(needle == haystack)
81 const bool hasWildcard = (needle.find(
'*') != std::string::npos);
90 if(needle[needle.size() - 1] ==
'*' &&
91 needle.substr(0, needle.size() - 1) == haystack.substr(0, needle.size() - 1))
99 if(needle[0] ==
'*' &&
100 needle.substr(1) == haystack.substr(haystack.size() - (needle.size() - 1)))
109 std::size_t patternPos = 0;
110 std::size_t textPos = 0;
111 std::size_t lastStarPattern = std::string::npos;
112 std::size_t lastStarTextPos = std::string::npos;
113 while(textPos < haystack.size())
115 if(patternPos < needle.size() && needle[patternPos] == haystack[textPos])
120 else if(patternPos < needle.size() && needle[patternPos] ==
'*')
122 lastStarPattern = patternPos++;
123 lastStarTextPos = textPos;
125 else if(lastStarPattern != std::string::npos)
127 patternPos = lastStarPattern + 1;
128 textPos = ++lastStarTextPos;
138 while(patternPos < needle.size() && needle[patternPos] ==
'*')
141 if(patternPos == needle.size())
165 const std::set<std::string>& haystack)
167 for(
const auto& haystackString : haystack)
170 if(haystackString.size() && haystackString[0] ==
'!')
187 std::string decodeURIString(data.size(), 0);
189 for(
unsigned int i = 0; i < data.size(); ++i, ++j)
194 if(data[i + 1] >
'9')
195 decodeURIString[j] += (data[i + 1] - 55) * 16;
197 decodeURIString[j] += (data[i + 1] - 48) * 16;
200 if(data[i + 2] >
'9')
201 decodeURIString[j] += (data[i + 2] - 55);
203 decodeURIString[j] += (data[i + 2] - 48);
208 decodeURIString[j] = data[i];
210 decodeURIString.resize(j);
211 return decodeURIString;
215 std::string StringMacros::encodeURIComponent(
const std::string& sourceStr)
217 std::string retStr =
"";
219 for(
const auto& c : sourceStr)
220 if((c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z') || (c >=
'0' && c <=
'9'))
224 sprintf(encodeStr,
"%%%2.2X", (uint8_t)c);
234 std::map<char, std::string> replacements = {
242 while(pos < str.size())
244 auto it = replacements.find(str[pos]);
245 if(it != replacements.end())
247 str.replace(pos, 1, it->second);
248 pos += it->second.size();
269 bool allowWhiteSpace ,
272 unsigned int ws = -1;
275 __COUTVS__(TLVL_EscapeString, allowWhiteSpace);
276 __COUTVS__(TLVL_EscapeString, forHtml);
278 for(
unsigned int i = 0; i < inString.length(); i++)
279 if(inString[i] !=
' ')
281 __COUTS__(TLVL_EscapeString)
282 << i <<
". " << inString[i] <<
":" << (int)inString[i] << std::endl;
285 if(inString[i] ==
'\r' || inString[i] ==
'\n' ||
286 inString[i] ==
'\t' ||
289 (inString[i] >
char(126) &&
290 inString[i] <
char(161)))
297 if(i + 2 < inString.size() && inString[i] == char(0xE2) &&
298 inString[i + 1] == char(0x80) &&
317 if(inString[i] ==
'\n')
321 sprintf(htmlTmp,
"&#%3.3d", inString[i]);
323 i, std::string(htmlTmp));
332 else if(inString[i] ==
'\t')
340 "        ");
342 i, std::string(htmlTmp));
350 sprintf(htmlTmp,
"	");
352 i, std::string(htmlTmp));
364 inString.erase(i, 1);
367 __COUTS__(31) << inString << std::endl;
371 __COUTS__(31) << inString << std::endl;
374 if(inString[i] ==
'\"' || inString[i] ==
'\'')
379 unsigned int backslashCount = 0;
380 for(
unsigned int j = i; j > 0 && inString[j - 1] ==
'\\'; --j)
383 if(backslashCount % 2 == 1)
386 inString.erase(i - 1, 1);
392 (inString[i] ==
'\'')
395 inString.replace(i + 5, 1, 1,
';');
399 else if(inString[i] ==
'&')
401 inString.insert(i,
"&");
402 inString.replace(i + 4, 1, 1,
';');
405 else if(inString[i] ==
'<' || inString[i] ==
'>')
430 else if(inString[i] >=
char(161) &&
431 inString[i] <=
char(255))
433 sprintf(htmlTmp,
"&#%3.3d", inString[i]);
434 inString.insert(i, std::string(htmlTmp));
435 inString.replace(i + 5, 1, 1,
';');
439 __COUTS__(TLVL_EscapeString) << inString << std::endl;
443 else if(allowWhiteSpace)
451 inString.insert(i,
" ");
454 inString.insert(i,
" ");
455 inString.replace(i + 5, 1, 1,
';');
460 __COUTS__(TLVL_EscapeString) << inString.size() <<
" " << ws << std::endl;
464 __COUTS__(TLVL_EscapeString) << inString.size() <<
" " << inString << std::endl;
470 if(ws == (
unsigned int)-1)
472 return inString.substr(0, ws + 1);
482 unsigned int sz = str.size();
486 std::string retStr =
"";
487 retStr.reserve(str.size() * 2);
488 for(
unsigned int i = 0; i < sz; ++i)
520 unsigned int sz = str.size();
524 std::string retStr =
"";
525 retStr.reserve(str.size());
527 for(; i < sz - 1; ++i)
559 retStr += str[sz - 1];
570 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](
unsigned char ch) {
571 return !std::isspace(ch);
575 s.erase(std::find_if(
576 s.rbegin(), s.rend(), [](
unsigned char ch) { return !std::isspace(ch); })
595 size_t begin = data.find(
"$");
596 if(begin != std::string::npos && begin + 1 < data.size())
599 std::string envVariable;
600 std::string converted = data;
601 bool usedBraces =
false;
603 while(begin && begin != std::string::npos &&
604 converted[begin - 1] ==
607 converted.replace(begin - 1, 1,
"");
608 begin = converted.find(
"$", begin + 1);
609 if(begin == std::string::npos)
611 __COUTS__(TLVL_EnvSub)
612 <<
"Only found escaped $'s that will not be converted: " << converted
621 if(begin + 2 < data.size() && data[begin + 1] ==
'(' && data[begin + 2] ==
'(')
623 end = data.find(
"))", begin + 3);
624 if(end == std::string::npos)
626 __SS__ <<
"Arithmetic expansion '$((...)),' at pos " << begin
627 <<
" in value, is missing closing '))'! Here was the value: "
632 std::string expression = data.substr(begin + 3, end - begin - 3);
633 __COUTVS__(TLVL_EnvMath, expression);
637 __COUTVS__(TLVL_EnvMath, expression);
644 __SS__ <<
"Arithmetic expansion '$((...)),' at pos " << begin
645 <<
" in value, does not evaluate to a number! Here was the value: "
650 __COUTS__(TLVL_EnvMath) <<
"Arithmetic result: " << result << __E__;
654 converted.replace(begin, end - begin + 2, std::to_string(result)));
656 else if(data[begin + 1] ==
'{')
658 end = data.find(
"}", begin + 2);
659 envVariable = data.substr(begin + 2, end - begin - 2);
666 for(end = begin + 1; end < data.size(); ++end)
667 if(!((data[end] >=
'0' && data[end] <=
'9') ||
668 (data[end] >=
'A' && data[end] <=
'Z') ||
669 (data[end] >=
'a' && data[end] <=
'z') || data[end] ==
'-' ||
670 data[end] ==
'_' || data[end] ==
'.' || data[end] ==
':'))
672 envVariable = data.substr(begin + 1, end - begin - 1);
675 __COUTVS__(TLVL_EnvSub, data);
676 __COUTVS__(TLVL_EnvSub, envVariable);
677 if(usedBraces && envVariable.starts_with(
"OTS."))
679 __COUTS__(TLVL_EnvSub) <<
"OTS system variable detected!" << __E__;
683 if(sysVarSplit.size() != 3 ||
684 systemVariables_.find(sysVarSplit[1]) == systemVariables_.end() ||
685 systemVariables_.at(sysVarSplit[1]).find(sysVarSplit[2]) ==
686 systemVariables_.at(sysVarSplit[1]).end())
689 <<
"System variable ${" << envVariable
690 <<
"} is not valid or was not found!"
692 <<
"If you were trying to access an ots System Variable, the correct "
694 <<
"${OTS.<variable>.<property>}, e.g. "
695 "${OTS.ActiveStateMachine.name}"
697 <<
"If you were trying to insert an arithmetic operation, the "
699 "syntax is $((4 - 3)) or $(($ENVVAR1 - $ENVVAR2))"
701 <<
"Available system variables:" << __E__;
704 for(
const auto& varPair : systemVariables_)
706 ss <<
"\n OTS." << varPair.first <<
".*";
707 for(
const auto& propPair : varPair.second)
708 ss <<
"\n - OTS." << varPair.first <<
"." << propPair.first;
718 systemVariables_.at(sysVarSplit[1]).at(sysVarSplit[2])));
722 char* envResult =
nullptr;
725 envResult = __ENV__(envVariable.c_str());
727 catch(
const std::runtime_error& e)
730 << (
"The environmental variable '" + envVariable +
731 "' is not set! Please make sure you set it before continuing!" +
733 "If you were trying to access an ots System Variable, the "
734 "correct syntax is " +
735 "${OTS.<variable>.<property>}, e.g. "
736 "${OTS.ActiveStateMachine.name}")
738 ss <<
"\n" << e.what() << __E__;
744 converted.replace(begin, end - begin, envResult));
748 __COUTS__(TLVL_EnvSub) <<
"Result: " << data << __E__;
760 std::vector<std::string> numbers;
761 std::vector<char> ops;
769 std::set<char>({
'+',
'-',
'*',
'/'}),
770 std::set<char>({
' ',
'\t',
'\n',
'\r'}),
776 for(
const auto& number : numbers)
778 if(number.size() == 0)
781 if(number.find(
"0x") == 0)
784 for(
unsigned int i = 2; i < number.size(); ++i)
786 if(!((number[i] >=
'0' && number[i] <=
'9') ||
787 (number[i] >=
'A' && number[i] <=
'F') ||
788 (number[i] >=
'a' && number[i] <=
'f')))
796 else if(number[0] ==
'b')
800 for(
unsigned int i = 1; i < number.size(); ++i)
802 if(!((number[i] >=
'0' && number[i] <=
'1')))
812 for(
unsigned int i = 0; i < number.size(); ++i)
813 if(!((number[i] >=
'0' && number[i] <=
'9') || number[i] ==
'.' ||
814 number[i] ==
'+' || number[i] ==
'-'))
837 std::vector<std::string> numbers;
838 std::vector<char> ops;
840 bool hasDecimal =
false;
845 std::set<char>({
'+',
'-',
'*',
'/'}),
846 std::set<char>({
' ',
'\t',
'\n',
'\r'}),
852 for(
const auto& number : numbers)
854 if(number.size() == 0)
857 if(number.find(
"0x") == 0)
860 for(
unsigned int i = 2; i < number.size(); ++i)
862 if(!((number[i] >=
'0' && number[i] <=
'9') ||
863 (number[i] >=
'A' && number[i] <=
'F') ||
864 (number[i] >=
'a' && number[i] <=
'f')))
872 else if(number[0] ==
'b')
876 for(
unsigned int i = 1; i < number.size(); ++i)
878 if(!((number[i] >=
'0' && number[i] <=
'1')))
888 for(
unsigned int i = 0; i < number.size(); ++i)
889 if(!((number[i] >=
'0' && number[i] <=
'9') || number[i] ==
'.' ||
890 number[i] ==
'+' || number[i] ==
'-'))
892 else if(number[i] ==
'.')
905 return "unsigned long long";
918 __COUT_ERR__ <<
"Invalid empty bool string " << s << __E__;
923 if(s.find(
"1") != std::string::npos || s ==
"true" || s ==
"True" || s ==
"TRUE")
930 if(s.find(
"0") != std::string::npos || s ==
"false" || s ==
"False" || s ==
"FALSE")
936 __COUT_ERR__ <<
"Invalid bool string " << s << __E__;
947 time_t timestamp(strtol(linuxTimeInSeconds.c_str(), 0, 10));
967 std::stringstream ss;
968 int days = t / 60 / 60 / 24;
971 ss << days <<
" day" << (days > 1 ?
"s" :
"") <<
", ";
972 t -= days * 60 * 60 * 24;
976 ss << std::setw(2) << std::setfill(
'0') << (t / 60 / 60) <<
":" << std::setw(2)
977 << std::setfill(
'0') << ((t % (60 * 60)) / 60) <<
":" << std::setw(2)
978 << std::setfill(
'0') << (t % 60);
986 const std::string& value,
bool doConvertEnvironmentVariables)
989 return doConvertEnvironmentVariables
993 catch(
const std::runtime_error& e)
995 __SS__ <<
"Failed to validate value for default string data type. " << __E__
996 << e.what() << __E__;
1005 std::set<std::string>& setToReturn,
1006 const std::set<char>& delimiter,
1007 const std::set<char>& whitespace)
1014 for(; j < inputString.size(); ++j)
1015 if((whitespace.find(inputString[j]) !=
1017 delimiter.find(inputString[j]) != delimiter.end()) &&
1020 else if((whitespace.find(inputString[j]) !=
1023 delimiter.find(inputString[j]) != delimiter.end()) &&
1029 setToReturn.emplace(inputString.substr(i, j - i));
1036 setToReturn.emplace(inputString.substr(i, j - i));
1052 std::vector<std::string>& listToReturn,
1053 const std::set<char>& delimiter,
1054 const std::set<char>& whitespace,
1055 std::vector<char>* listOfDelimiters,
1056 bool decodeURIComponents)
1061 std::set<char>::iterator delimeterSearchIt;
1062 char lastDelimiter = 0;
1071 for(; c < inputString.size(); ++c)
1075 delimeterSearchIt = delimiter.find(inputString[c]);
1076 isDelimiter = delimeterSearchIt != delimiter.end();
1081 if(whitespace.find(inputString[c]) !=
1090 else if(whitespace.find(inputString[c]) != whitespace.end() &&
1095 else if(isDelimiter)
1100 if(listOfDelimiters && listToReturn.size())
1107 listOfDelimiters->push_back(lastDelimiter);
1110 inputString.substr(i, j - i))
1111 : inputString.substr(i, j - i));
1121 lastDelimiter = *delimeterSearchIt;
1130 if(listOfDelimiters && listToReturn.size())
1136 listOfDelimiters->push_back(lastDelimiter);
1139 inputString.substr(i, j - i))
1140 : inputString.substr(i, j - i));
1144 if(listOfDelimiters && listToReturn.size() - 1 != listOfDelimiters->size() &&
1145 listToReturn.size() != listOfDelimiters->size())
1147 __SS__ <<
"There is a mismatch in delimiters to entries (should be equal or one "
1149 << listOfDelimiters->size() <<
" vs " << listToReturn.size() << __E__
1171 const std::string& inputString,
1172 const std::set<char>& delimiter,
1173 const std::set<char>& whitespace,
1174 std::vector<char>* listOfDelimiters,
1175 bool decodeURIComponents)
1177 std::vector<std::string> listToReturn;
1184 decodeURIComponents);
1185 return listToReturn;
1193 std::map<std::string, std::string>& mapToReturn,
1194 const std::set<char>& pairPairDelimiter,
1195 const std::set<char>& nameValueDelimiter,
1196 const std::set<char>& whitespace)
1202 bool needValue =
false;
1206 for(; j < inputString.size(); ++j)
1209 if((whitespace.find(inputString[j]) !=
1211 pairPairDelimiter.find(inputString[j]) != pairPairDelimiter.end()) &&
1214 else if((whitespace.find(inputString[j]) !=
1217 nameValueDelimiter.find(inputString[j]) !=
1218 nameValueDelimiter.end()) &&
1224 name = inputString.substr(i, j - i);
1234 if((whitespace.find(inputString[j]) !=
1236 nameValueDelimiter.find(inputString[j]) != nameValueDelimiter.end()) &&
1239 else if(whitespace.find(inputString[j]) !=
1242 pairPairDelimiter.find(inputString[j]) !=
1243 pairPairDelimiter.end())
1249 auto emplaceReturn =
1250 mapToReturn.emplace(std::pair<std::string, std::string>(
1253 inputString.substr(i, j - i))
1256 if(!emplaceReturn.second)
1258 __COUT__ <<
"Ignoring repetitive value ('"
1259 << inputString.substr(i, j - i)
1260 <<
"') and keeping current value ('"
1261 << emplaceReturn.first->second <<
"'). " << __E__;
1273 auto emplaceReturn =
1274 mapToReturn.emplace(std::pair<std::string, std::string>(
1277 inputString.substr(i, j - i))
1280 if(!emplaceReturn.second)
1282 __COUT__ <<
"Ignoring repetitive value ('" << inputString.substr(i, j - i)
1283 <<
"') and keeping current value ('" << emplaceReturn.first->second
1288 catch(
const std::runtime_error& e)
1290 __SS__ <<
"Error while extracting a map from the string '" << inputString
1291 <<
"'... is it a valid map?" << __E__ << e.what() << __E__;
1298 const std::string& primaryDelimeter,
1299 const std::string& secondaryDelimeter)
1301 std::stringstream ss;
1303 for(
auto& mapPair : mapToReturn)
1308 ss << primaryDelimeter;
1309 ss << mapPair.first << secondaryDelimeter << (
unsigned int)mapPair.second;
1317 const std::string& delimeter)
1319 std::stringstream ss;
1321 for(
auto& setValue : setToReturn)
1327 ss << (
unsigned int)setValue;
1335 const std::string& delimeter)
1337 std::stringstream ss;
1339 if(delimeter ==
"\n")
1341 for(
auto& setValue : setToReturn)
1347 ss << (
unsigned int)setValue;
1363 std::vector<std::string>& commonChunksToReturn,
1364 std::vector<std::string>& wildcardStringsToReturn,
1365 unsigned int& fixedWildcardLength)
1367 fixedWildcardLength = 0;
1392 std::pair<
unsigned int ,
unsigned int > wildcardBounds(
1393 std::make_pair(-1, 0));
1396 for(
unsigned int n = 1; n < haystack.size(); ++n)
1397 for(
unsigned int i = 0, j = 0;
1398 i < haystack[0].length() && j < haystack[n].length();
1401 if(i < wildcardBounds.first)
1403 if(haystack[0][i] != haystack[n][j])
1405 wildcardBounds.first = i;
1412 __COUTS__(3) <<
"Low side = " << wildcardBounds.first <<
" "
1413 << haystack[0].substr(0, wildcardBounds.first) << __E__;
1416 for(
unsigned int n = 1; n < haystack.size(); ++n)
1417 for(
int i = haystack[0].length() - 1, j = haystack[n].length() - 1;
1418 i >= (int)wildcardBounds.first && j >= (
int)wildcardBounds.first;
1421 if(i > (
int)wildcardBounds.second)
1423 if(haystack[0][i] != haystack[n][j])
1425 wildcardBounds.second = i + 1;
1433 __COUTS__(3) <<
"High side = " << wildcardBounds.second <<
" "
1434 << haystack[0].substr(wildcardBounds.second) << __E__;
1437 commonChunksToReturn.push_back(haystack[0].substr(0, wildcardBounds.first));
1439 if(wildcardBounds.first != (
unsigned int)-1)
1442 for(
int i = (wildcardBounds.first + wildcardBounds.second) / 2 + 1;
1443 i < (
int)wildcardBounds.second;
1445 if(haystack[0][wildcardBounds.first] == haystack[0][i] &&
1446 haystack[0].substr(wildcardBounds.first, wildcardBounds.second - i) ==
1447 haystack[0].substr(i, wildcardBounds.second - i))
1449 std::string multiWildcardString =
1450 haystack[0].substr(i, wildcardBounds.second - i);
1451 __COUT__ <<
"Potential multi-wildcard found: " << multiWildcardString
1452 <<
" at position i=" << i << __E__;
1454 std::vector<
unsigned int > wildCardInstances;
1456 wildCardInstances.push_back(wildcardBounds.first);
1458 unsigned int offset =
1459 wildCardInstances[0] + multiWildcardString.size() + 1;
1460 std::string middleString = haystack[0].substr(offset, (i - 1) - offset);
1461 __COUTV__(middleString);
1465 while((k = middleString.find(multiWildcardString)) != std::string::npos)
1467 __COUT__ <<
"Multi-wildcard found at " << k << __E__;
1469 wildCardInstances.push_back(offset + k);
1472 middleString.substr(k + multiWildcardString.size() + 1);
1473 offset += k + multiWildcardString.size() + 1;
1474 __COUTV__(middleString);
1478 wildCardInstances.push_back(i);
1480 for(
unsigned int w = 0; w < wildCardInstances.size() - 1; ++w)
1482 __COUTV__(wildCardInstances[w]);
1483 __COUTV__(wildCardInstances[w + 1]);
1484 __COUTV__(wildCardInstances.size());
1485 commonChunksToReturn.push_back(haystack[0].substr(
1486 wildCardInstances[w] + multiWildcardString.size(),
1487 wildCardInstances[w + 1] -
1488 (wildCardInstances[w] + multiWildcardString.size())));
1494 for(
unsigned int c = 1; c < commonChunksToReturn.size(); ++c)
1496 __COUT__ <<
"Checking [" << c <<
"]: " << commonChunksToReturn[c] << __E__;
1497 for(
unsigned int n = 1; n < haystack.size(); ++n)
1499 __COUT__ <<
"Checking chunks work with haystack [" << n
1500 <<
"]: " << haystack[n] << __E__;
1501 __COUTV__(commonChunksToReturn[0].size());
1502 std::string wildCardValue = haystack[n].substr(
1503 commonChunksToReturn[0].size(),
1504 haystack[n].find(commonChunksToReturn[1],
1505 commonChunksToReturn[0].size() + 1) -
1506 commonChunksToReturn[0].size());
1507 __COUTTV__(wildCardValue);
1509 std::string builtString =
"";
1510 for(
unsigned int cc = 0; cc < commonChunksToReturn.size(); ++cc)
1511 builtString += commonChunksToReturn[cc] + wildCardValue;
1512 __COUTTV__(builtString);
1513 __COUTTV__(wildCardValue);
1515 if(haystack[n].find(builtString) != 0)
1517 __COUT__ <<
"Dropping common chunk " << commonChunksToReturn[c]
1518 <<
", built '" << builtString <<
"' not found in "
1519 << haystack[n] << __E__;
1520 commonChunksToReturn.erase(commonChunksToReturn.begin() + c);
1525 __COUTT__ <<
"Found built '" << builtString <<
"' in " << haystack[n]
1531 __COUTTV__(commonChunksToReturn[0].size());
1533 __COUTTV__(fixedWildcardLength);
1535 for(
unsigned int i = 0; i < commonChunksToReturn[0].size(); ++i)
1536 if(commonChunksToReturn[0][commonChunksToReturn[0].size() - 1 - i] ==
'0')
1538 ++fixedWildcardLength;
1539 __COUTT__ <<
"Trying for added fixed length +1 to " << fixedWildcardLength
1546 for(
unsigned int c = 0; c < commonChunksToReturn.size(); ++c)
1548 unsigned int cnt = 0;
1549 for(
unsigned int i = 0; i < commonChunksToReturn[c].size(); ++i)
1550 if(commonChunksToReturn[c][commonChunksToReturn[c].size() - 1 - i] ==
'0')
1555 if(fixedWildcardLength < cnt)
1556 fixedWildcardLength = cnt;
1557 else if(fixedWildcardLength > cnt)
1559 __SS__ <<
"Invalid fixed length found, please simplify indexing between "
1560 "these common chunks: "
1565 __COUTTV__(fixedWildcardLength);
1567 if(fixedWildcardLength)
1568 for(
unsigned int c = 0; c < commonChunksToReturn.size(); ++c)
1569 commonChunksToReturn[c] = commonChunksToReturn[c].substr(
1570 0, commonChunksToReturn[c].size() - fixedWildcardLength);
1573 commonChunksToReturn.push_back(haystack[0].substr(wildcardBounds.second));
1580 unsigned int ioff = fixedWildcardLength;
1581 bool wildcardsNeeded =
false;
1582 bool someLeadingZeros =
false;
1583 bool allWildcardsSameSize =
true;
1585 for(
unsigned int n = 0; n < haystack.size(); ++n)
1587 std::string wildcard =
"";
1589 i = ioff + commonChunksToReturn[0].size();
1591 if(commonChunksToReturn.size() == 1)
1592 wildcard = haystack[n].substr(i);
1594 for(
unsigned int c = 1; c < commonChunksToReturn.size(); ++c)
1596 if(c == commonChunksToReturn.size() - 1)
1597 k = haystack[n].rfind(commonChunksToReturn[c]);
1599 k = haystack[n].find(commonChunksToReturn[c], i + 1);
1606 __COUTVS__(3, k - i);
1608 wildcard = haystack[n].substr(i, k - i);
1609 if(fixedWildcardLength && n == 0)
1610 fixedWildcardLength += wildcard.size();
1612 __COUTS__(3) <<
"name[" << n <<
"] = " << wildcard <<
" fixed @ "
1613 << fixedWildcardLength << __E__;
1618 wildcard != haystack[n].substr(i, k - i))
1620 __SS__ <<
"Invalid wildcard! for name[" << n <<
"] = " << haystack[n]
1621 <<
" - the extraction algorithm is confused, please simplify "
1622 "your naming convention."
1632 wildcardsNeeded =
true;
1635 if(wildcard[0] ==
'0' && !fixedWildcardLength)
1637 someLeadingZeros =
true;
1638 if(wildcardStringsToReturn.size() &&
1639 wildcard.size() != wildcardStringsToReturn[0].size())
1640 allWildcardsSameSize =
false;
1643 wildcardStringsToReturn.push_back(wildcard);
1650 if(someLeadingZeros && allWildcardsSameSize)
1652 __COUTTV__(fixedWildcardLength);
1653 fixedWildcardLength = wildcardStringsToReturn[0].size();
1654 __COUT__ <<
"Enforce wildcard size of " << fixedWildcardLength << __E__;
1657 if(wildcardStringsToReturn.size() != haystack.size())
1659 __SS__ <<
"There was a problem during common chunk extraction!" << __E__;
1663 return wildcardsNeeded;
1672 const std::string& rhs)
const
1678 for(
unsigned int i = 0; i < lhs.size() && i < rhs.size(); ++i)
1681 if((lhs[i] >=
'A' && lhs[i] <=
'Z' && rhs[i] >=
'A' && rhs[i] <=
'Z') ||
1682 (lhs[i] >=
'a' && lhs[i] <=
'z' && rhs[i] >=
'a' && rhs[i] <=
'z'))
1684 if(lhs[i] == rhs[i])
1686 return (lhs[i] < rhs[i]);
1689 else if(lhs[i] >=
'A' && lhs[i] <=
'Z')
1691 if(lhs[i] + 32 == rhs[i])
1693 return (lhs[i] + 32 < rhs[i]);
1695 else if(rhs[i] >=
'A' && rhs[i] <=
'Z')
1697 if(lhs[i] == rhs[i] + 32)
1699 return (lhs[i] < rhs[i] + 32);
1703 if(lhs[i] == rhs[i])
1705 return (lhs[i] < rhs[i]);
1710 return lhs.size() < rhs.size();
1720 std::array<char, 128> buffer;
1725 std::string cmdWithRedirect = std::string(cmd) +
" 2>&1";
1726 std::shared_ptr<FILE> pipe(popen(cmdWithRedirect.c_str(),
"r"), pclose);
1728 __THROW__(
"popen() failed!");
1731 while(!feof(pipe.get()))
1733 if(fgets(buffer.data(), 128, pipe.get()) !=
nullptr)
1734 result += buffer.data();
1747 uintptr_t find_library_base(
const std::string& libname)
1749 std::ifstream maps(
"/proc/self/maps");
1752 while(std::getline(maps, line))
1754 if(line.find(libname) != std::string::npos &&
1755 line.find(
"r-xp") != std::string::npos)
1758 std::stringstream ss(line);
1759 ss >> std::hex >> base;
1767 void resolve_stack_entry(
const std::string& so_path,
1768 const std::string& real_name,
1769 const std::string& offset_begin,
1770 const std::string& offset_end
1775 const std::size_t pos0x = offset_end.find(
"0x");
1776 if(pos0x == std::string::npos)
1778 __COUTS__(52) <<
"resolve_stack_entry: could not find \"0x\" in offset_end: '"
1779 << offset_end <<
"'" << __E__;
1783 const std::size_t posBracket = offset_end.find(
']', pos0x);
1784 if(posBracket == std::string::npos || posBracket < pos0x + 3)
1786 __COUTS__(52) <<
"resolve_stack_entry: could not find closing ']' with at least "
1787 "one hex digit after \"0x\" "
1789 << offset_end <<
"'" << __E__;
1793 const std::string addr_str = offset_end.substr(pos0x, posBracket - pos0x);
1795 uintptr_t runtime_addr = 0;
1798 runtime_addr = std::stoull(addr_str,
nullptr, 16);
1800 catch(
const std::exception& e)
1802 __COUTS__(52) <<
"resolve_stack_entry: failed to parse runtime address from '"
1803 << addr_str <<
"': " << e.what() << __E__;
1807 std::string so_name = so_path.substr(so_path.find_last_of(
'/') + 1);
1809 uintptr_t base = find_library_base(so_name);
1812 std::cerr <<
"Could not find base for " << so_name <<
"\n";
1816 uintptr_t file_addr = runtime_addr - base;
1818 std::ostringstream cmd;
1819 cmd <<
"addr2line -f -C -e " << so_path <<
" 0x" << std::hex << file_addr;
1821 __COUT__ <<
"\nResolving:\n"
1822 << so_path <<
" : " << real_name << offset_begin <<
" [" << std::hex
1823 << runtime_addr <<
"]\n\n";
1834 #include <execinfo.h>
1838 __SS__ <<
"ots::stackTrace:\n";
1844 size = backtrace(array, 10);
1848 char** messages = backtrace_symbols(array, size);
1852 for(
unsigned int i = 1; i < size && messages != NULL; ++i)
1865 char *mangled_name = 0, *offset_begin = 0, *offset_end = 0;
1868 for(
char* p = messages[i]; *p; ++p)
1886 if(mangled_name && offset_begin && offset_end && mangled_name < offset_begin)
1888 *mangled_name++ =
'\0';
1889 *offset_begin++ =
'\0';
1890 *offset_end++ =
'\0';
1893 char* real_name = abi::__cxa_demangle(mangled_name, 0, 0, &status);
1898 ss <<
"[" << i <<
"] " << messages[i] <<
" : " << real_name <<
"+"
1899 << offset_begin << offset_end << std::endl;
1906 ss <<
"[" << i <<
"] " << messages[i] <<
" : " << mangled_name <<
"+"
1907 << offset_begin << offset_end << std::endl;
1914 ss <<
"[" << i <<
"] " << messages[i] << std::endl;
1934 const std::string& location,
1935 const unsigned int& line)
1937 char* environmentVariablePtr = getenv(name);
1938 if(!environmentVariablePtr)
1940 __SS__ <<
"Environment variable '$" << name <<
"' not defined at " << location
1941 <<
":" << line << __E__;
1945 return environmentVariablePtr;
1952 const std::string& field,
1953 uint32_t occurrence,
1955 size_t* returnFindPos ,
1956 const std::string& valueField ,
1957 const std::string& quoteType )
1960 *returnFindPos = std::string::npos;
1962 __COUTVS__(41, xml);
1964 size_t lo, findpos = after, hi;
1965 for(uint32_t i = 0; i <= occurrence; ++i)
1967 bool anyFound =
false;
1969 xml.find(
"<" + field,
1970 findpos)) != std::string::npos &&
1971 findpos + 1 + field.size() < xml.size())
1973 __COUTS__(40) <<
"find: ---- '<" << field <<
" findpos=" << findpos
1974 <<
"findpos " << findpos <<
" " << xml[findpos] <<
" "
1975 << xml[findpos + 1 + field.size()] <<
" "
1976 << (int)xml[findpos + 1 + field.size()] << __E__;
1984 if((quoteType ==
">" && xml[findpos] ==
'>') || xml[findpos] ==
' ' ||
1985 xml[findpos] ==
'\n' || xml[findpos] ==
'\t')
1994 __COUTS__(40) <<
"Field '" << field <<
"' not found" << __E__;
1999 lo = xml.find(valueField + quoteType, findpos) + valueField.size() + quoteType.size();
2001 if(TTEST(40) && quoteType.size())
2003 __COUTS__(40) <<
"Neighbors of field '" << field <<
"' and value '" << valueField
2004 <<
"' w/quote = " << quoteType << __E__;
2005 for(
size_t i = lo - valueField.size(); i < lo + 10 && i < xml.size(); ++i)
2006 __COUTS__(40) <<
"xml[" << i <<
"] " << xml[i] <<
" vs " << quoteType <<
" ? "
2007 << (int)xml[i] <<
" vs " << (
int)quoteType[0] << __E__;
2011 quoteType ==
">" ?
"<" : quoteType,
2012 lo)) == std::string::npos)
2014 __COUTS__(40) <<
"Value closing not found" << __E__;
2019 *returnFindPos = findpos - (1 + field.size());
2021 __COUTS__(40) <<
"after: " << after <<
", findpos: " << findpos <<
", hi/lo: " << hi
2022 <<
"/" << lo <<
", size: " << xml.size() << __E__;
2023 __COUTVS__(40, xml.substr(lo, hi - lo));
2024 return xml.substr(lo, hi - lo);
2031 const std::string& field,
2032 uint32_t occurrence,
2034 size_t* returnFindPos ,
2035 const std::string& valueField ,
2036 const std::string& quoteType )
2039 *returnFindPos = std::string::npos;
2041 __COUTVS__(41, xml);
2043 size_t lo = 0, hi, findpos = before;
2044 for(uint32_t i = 0; i <= occurrence; ++i)
2046 bool anyFound =
false;
2048 xml.rfind(
"<" + field,
2049 findpos)) != std::string::npos &&
2050 findpos + 1 + field.size() < xml.size())
2052 __COUTS__(40) <<
"rfind: ---- '<" << field <<
" findpos=" << findpos <<
" "
2053 << xml[findpos] <<
" " << xml[findpos + 1 + field.size()] <<
" "
2054 << (int)xml[findpos + 1 + field.size()] << __E__;
2056 findpos += 1 + field.size();
2059 if((quoteType ==
">" && xml[findpos] ==
'>') || xml[findpos] ==
' ' ||
2060 xml[findpos] ==
'\n' || xml[findpos] ==
'\t')
2066 findpos -= 1 + field.size() + 1;
2070 __COUTS__(40) <<
"Field '" << field <<
"' not found" << __E__;
2075 lo = xml.find(valueField + quoteType, findpos) + valueField.size() + quoteType.size();
2077 if(TTEST(40) && quoteType.size())
2079 __COUTS__(40) <<
"Neighbors?" << __E__;
2080 for(
size_t i = findpos; i < lo + 10 && i < xml.size(); ++i)
2081 __COUTS__(40) <<
"xml[" << i <<
"] " << xml[i] <<
" vs " << quoteType <<
" ? "
2082 << (int)xml[i] <<
" vs " << (
int)quoteType[0] << __E__;
2086 quoteType ==
">" ?
"<" : quoteType,
2087 lo)) == std::string::npos)
2089 __COUTS__(40) <<
"Value closing not found" << __E__;
2095 findpos - (1 + field.size());
2097 __COUTS__(40) <<
"before: " << before <<
", findpos: " << findpos <<
", hi/lo: " << hi
2098 <<
"/" << lo <<
", size: " << xml.size() << __E__;
2099 __COUTVS__(40, xml.substr(lo, hi - lo));
2100 return xml.substr(lo, hi - lo);
2108 const std::set<char>& delimiter )
2112 __COUTV__(splitArr.size());
2113 __COUTVS__(lvl, splitArr.size());
2114 for(
const auto& split : splitArr)
2115 __COUTS__(lvl) << split;
2130 std::unique_ptr<char, void (*)(
void*)> res{
2131 abi::__cxa_demangle(name, NULL, NULL, &status), std::free};
2133 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)