1 #include "otsdaq/Macros/StringMacros.h"
12 std::map<std::string ,
13 std::map<std::string , std::string >>
14 StringMacros::systemVariables_;
15 const std::string StringMacros::TBD =
"To-be-defined";
18 unsigned int StringMacros::getConcurrencyCount(
void)
22 return std::stoul(systemVariables_.at(
"System").at(
"logicalCores"));
29 if(sched_getaffinity(0,
sizeof(mask), &mask) == 0)
30 hw = CPU_COUNT(&mask);
31 systemVariables_[
"System"][
"logicalCores"] = std::to_string(hw);
39 std::string StringMacros::getPersistentSystemVariablesFilePath(
void)
41 return std::string(__ENV__(
"USER_DATA")) +
"/ServiceData/ArtdaqSystemVariables.dat";
55 static std::mutex loadMutex;
56 std::lock_guard<std::mutex> lock(loadMutex);
58 std::ifstream file(getPersistentSystemVariablesFilePath());
62 auto& ns = systemVariables_[
"artdaq"];
64 while(std::getline(file, line))
66 size_t eqPos = line.find(
'=');
67 if(eqPos == std::string::npos)
69 ns[line.substr(0, eqPos)] = line.substr(eqPos + 1);
78 #define TLVL_EscapeString 30
79 #define TLVL_EnvMath 49
80 #define TLVL_EnvSub 50
95 const std::string& haystack,
96 unsigned int* priorityIndex)
99 __COUTT__ <<
"\t\t wildCardMatch: " << needle <<
" =in= " << haystack <<
" ??? "
103 if(needle.size() == 0)
119 if(needle == haystack)
126 const bool hasWildcard = (needle.find(
'*') != std::string::npos);
135 if(needle[needle.size() - 1] ==
'*' &&
136 needle.substr(0, needle.size() - 1) == haystack.substr(0, needle.size() - 1))
144 if(needle[0] ==
'*' &&
145 needle.substr(1) == haystack.substr(haystack.size() - (needle.size() - 1)))
154 std::size_t patternPos = 0;
155 std::size_t textPos = 0;
156 std::size_t lastStarPattern = std::string::npos;
157 std::size_t lastStarTextPos = std::string::npos;
158 while(textPos < haystack.size())
160 if(patternPos < needle.size() && needle[patternPos] == haystack[textPos])
165 else if(patternPos < needle.size() && needle[patternPos] ==
'*')
167 lastStarPattern = patternPos++;
168 lastStarTextPos = textPos;
170 else if(lastStarPattern != std::string::npos)
172 patternPos = lastStarPattern + 1;
173 textPos = ++lastStarTextPos;
183 while(patternPos < needle.size() && needle[patternPos] ==
'*')
186 if(patternPos == needle.size())
210 const std::set<std::string>& haystack)
212 for(
const auto& haystackString : haystack)
215 if(haystackString.size() && haystackString[0] ==
'!')
232 std::string decodeURIString(data.size(), 0);
234 for(
unsigned int i = 0; i < data.size(); ++i, ++j)
239 if(data[i + 1] >
'9')
240 decodeURIString[j] += (data[i + 1] - 55) * 16;
242 decodeURIString[j] += (data[i + 1] - 48) * 16;
245 if(data[i + 2] >
'9')
246 decodeURIString[j] += (data[i + 2] - 55);
248 decodeURIString[j] += (data[i + 2] - 48);
253 decodeURIString[j] = data[i];
255 decodeURIString.resize(j);
256 return decodeURIString;
260 std::string StringMacros::encodeURIComponent(
const std::string& sourceStr)
262 std::string retStr =
"";
264 for(
const auto& c : sourceStr)
265 if((c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z') || (c >=
'0' && c <=
'9'))
269 sprintf(encodeStr,
"%%%2.2X", (uint8_t)c);
279 std::map<char, std::string> replacements = {
287 while(pos < str.size())
289 auto it = replacements.find(str[pos]);
290 if(it != replacements.end())
292 str.replace(pos, 1, it->second);
293 pos += it->second.size();
314 bool allowWhiteSpace ,
317 unsigned int ws = -1;
320 __COUTVS__(TLVL_EscapeString, allowWhiteSpace);
321 __COUTVS__(TLVL_EscapeString, forHtml);
323 for(
unsigned int i = 0; i < inString.length(); i++)
324 if(inString[i] !=
' ')
326 __COUTS__(TLVL_EscapeString)
327 << i <<
". " << inString[i] <<
":" << (int)inString[i] << std::endl;
330 if(inString[i] ==
'\r' || inString[i] ==
'\n' ||
331 inString[i] ==
'\t' ||
334 (inString[i] >
char(126) &&
335 inString[i] <
char(161)))
342 if(i + 2 < inString.size() && inString[i] == char(0xE2) &&
343 inString[i + 1] == char(0x80) &&
362 if(inString[i] ==
'\n')
366 sprintf(htmlTmp,
"&#%3.3d", inString[i]);
368 i, std::string(htmlTmp));
377 else if(inString[i] ==
'\t')
385 "        ");
387 i, std::string(htmlTmp));
395 sprintf(htmlTmp,
"	");
397 i, std::string(htmlTmp));
409 inString.erase(i, 1);
412 __COUTS__(31) << inString << std::endl;
416 __COUTS__(31) << inString << std::endl;
419 if(inString[i] ==
'\"' || inString[i] ==
'\'')
424 unsigned int backslashCount = 0;
425 for(
unsigned int j = i; j > 0 && inString[j - 1] ==
'\\'; --j)
428 if(backslashCount % 2 == 1)
431 inString.erase(i - 1, 1);
437 (inString[i] ==
'\'')
440 inString.replace(i + 5, 1, 1,
';');
444 else if(inString[i] ==
'&')
446 inString.insert(i,
"&");
447 inString.replace(i + 4, 1, 1,
';');
450 else if(inString[i] ==
'<' || inString[i] ==
'>')
475 else if(inString[i] >=
char(161) &&
476 inString[i] <=
char(255))
478 sprintf(htmlTmp,
"&#%3.3d", inString[i]);
479 inString.insert(i, std::string(htmlTmp));
480 inString.replace(i + 5, 1, 1,
';');
484 __COUTS__(TLVL_EscapeString) << inString << std::endl;
488 else if(allowWhiteSpace)
496 inString.insert(i,
" ");
499 inString.insert(i,
" ");
500 inString.replace(i + 5, 1, 1,
';');
505 __COUTS__(TLVL_EscapeString) << inString.size() <<
" " << ws << std::endl;
509 __COUTS__(TLVL_EscapeString) << inString.size() <<
" " << inString << std::endl;
515 if(ws == (
unsigned int)-1)
517 return inString.substr(0, ws + 1);
527 unsigned int sz = str.size();
531 std::string retStr =
"";
532 retStr.reserve(str.size() * 2);
533 for(
unsigned int i = 0; i < sz; ++i)
565 unsigned int sz = str.size();
569 std::string retStr =
"";
570 retStr.reserve(str.size());
572 for(; i < sz - 1; ++i)
604 retStr += str[sz - 1];
615 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](
unsigned char ch) {
616 return !std::isspace(ch);
620 s.erase(std::find_if(
621 s.rbegin(), s.rend(), [](
unsigned char ch) { return !std::isspace(ch); })
640 size_t begin = data.find(
"$");
641 if(begin != std::string::npos && begin + 1 < data.size())
644 std::string envVariable;
645 std::string converted = data;
646 bool usedBraces =
false;
648 while(begin && begin != std::string::npos &&
649 converted[begin - 1] ==
652 converted.replace(begin - 1, 1,
"");
653 begin = converted.find(
"$", begin + 1);
654 if(begin == std::string::npos)
656 __COUTS__(TLVL_EnvSub)
657 <<
"Only found escaped $'s that will not be converted: " << converted
666 if(begin + 2 < data.size() && data[begin + 1] ==
'(' && data[begin + 2] ==
'(')
668 end = data.find(
"))", begin + 3);
669 if(end == std::string::npos)
671 __SS__ <<
"Arithmetic expansion '$((...)),' at pos " << begin
672 <<
" in value, is missing closing '))'! Here was the value: "
677 std::string expression = data.substr(begin + 3, end - begin - 3);
678 __COUTVS__(TLVL_EnvMath, expression);
682 __COUTVS__(TLVL_EnvMath, expression);
689 __SS__ <<
"Arithmetic expansion '$((...)),' at pos " << begin
690 <<
" in value, does not evaluate to a number! Here was the value: "
695 __COUTS__(TLVL_EnvMath) <<
"Arithmetic result: " << result << __E__;
699 converted.replace(begin, end - begin + 2, std::to_string(result)));
701 else if(data[begin + 1] ==
'{')
703 end = data.find(
"}", begin + 2);
704 envVariable = data.substr(begin + 2, end - begin - 2);
711 for(end = begin + 1; end < data.size(); ++end)
712 if(!((data[end] >=
'0' && data[end] <=
'9') ||
713 (data[end] >=
'A' && data[end] <=
'Z') ||
714 (data[end] >=
'a' && data[end] <=
'z') || data[end] ==
'-' ||
715 data[end] ==
'_' || data[end] ==
'.' || data[end] ==
':'))
717 envVariable = data.substr(begin + 1, end - begin - 1);
720 __COUTVS__(TLVL_EnvSub, data);
721 __COUTVS__(TLVL_EnvSub, envVariable);
722 if(usedBraces && envVariable.starts_with(
"OTS."))
724 __COUTS__(TLVL_EnvSub) <<
"OTS system variable detected!" << __E__;
728 if(sysVarSplit.size() != 3 ||
729 systemVariables_.find(sysVarSplit[1]) == systemVariables_.end() ||
730 systemVariables_.at(sysVarSplit[1]).find(sysVarSplit[2]) ==
731 systemVariables_.at(sysVarSplit[1]).end())
734 <<
"System variable ${" << envVariable
735 <<
"} is not valid or was not found!"
737 <<
"If you were trying to access an ots System Variable, the correct "
739 <<
"${OTS.<variable>.<property>}, e.g. "
740 "${OTS.ActiveStateMachine.name}"
742 <<
"If you were trying to insert an arithmetic operation, the "
744 "syntax is $((4 - 3)) or $(($ENVVAR1 - $ENVVAR2))"
746 <<
"Available system variables:" << __E__;
749 for(
const auto& varPair : systemVariables_)
751 ss <<
"\n OTS." << varPair.first <<
".*";
752 for(
const auto& propPair : varPair.second)
753 ss <<
"\n - OTS." << varPair.first <<
"." << propPair.first;
763 systemVariables_.at(sysVarSplit[1]).at(sysVarSplit[2])));
767 char* envResult =
nullptr;
770 envResult = __ENV__(envVariable.c_str());
772 catch(
const std::runtime_error& e)
775 << (
"The environmental variable '" + envVariable +
776 "' is not set! Please make sure you set it before continuing!" +
778 "If you were trying to access an ots System Variable, the "
779 "correct syntax is " +
780 "${OTS.<variable>.<property>}, e.g. "
781 "${OTS.ActiveStateMachine.name}")
783 ss <<
"\n" << e.what() << __E__;
789 converted.replace(begin, end - begin, envResult));
793 __COUTS__(TLVL_EnvSub) <<
"Result: " << data << __E__;
805 std::vector<std::string> numbers;
806 std::vector<char> ops;
814 std::set<char>({
'+',
'-',
'*',
'/'}),
815 std::set<char>({
' ',
'\t',
'\n',
'\r'}),
821 for(
const auto& number : numbers)
823 if(number.size() == 0)
826 if(number.find(
"0x") == 0)
829 for(
unsigned int i = 2; i < number.size(); ++i)
831 if(!((number[i] >=
'0' && number[i] <=
'9') ||
832 (number[i] >=
'A' && number[i] <=
'F') ||
833 (number[i] >=
'a' && number[i] <=
'f')))
841 else if(number[0] ==
'b')
845 for(
unsigned int i = 1; i < number.size(); ++i)
847 if(!((number[i] >=
'0' && number[i] <=
'1')))
857 for(
unsigned int i = 0; i < number.size(); ++i)
858 if(!((number[i] >=
'0' && number[i] <=
'9') || number[i] ==
'.' ||
859 number[i] ==
'+' || number[i] ==
'-'))
882 std::vector<std::string> numbers;
883 std::vector<char> ops;
885 bool hasDecimal =
false;
890 std::set<char>({
'+',
'-',
'*',
'/'}),
891 std::set<char>({
' ',
'\t',
'\n',
'\r'}),
897 for(
const auto& number : numbers)
899 if(number.size() == 0)
902 if(number.find(
"0x") == 0)
905 for(
unsigned int i = 2; i < number.size(); ++i)
907 if(!((number[i] >=
'0' && number[i] <=
'9') ||
908 (number[i] >=
'A' && number[i] <=
'F') ||
909 (number[i] >=
'a' && number[i] <=
'f')))
917 else if(number[0] ==
'b')
921 for(
unsigned int i = 1; i < number.size(); ++i)
923 if(!((number[i] >=
'0' && number[i] <=
'1')))
933 for(
unsigned int i = 0; i < number.size(); ++i)
934 if(!((number[i] >=
'0' && number[i] <=
'9') || number[i] ==
'.' ||
935 number[i] ==
'+' || number[i] ==
'-'))
937 else if(number[i] ==
'.')
950 return "unsigned long long";
963 __COUT_ERR__ <<
"Invalid empty bool string " << s << __E__;
968 if(s.find(
"1") != std::string::npos || s ==
"true" || s ==
"True" || s ==
"TRUE")
975 if(s.find(
"0") != std::string::npos || s ==
"false" || s ==
"False" || s ==
"FALSE")
981 __COUT_ERR__ <<
"Invalid bool string " << s << __E__;
992 time_t timestamp(strtol(linuxTimeInSeconds.c_str(), 0, 10));
1012 std::stringstream ss;
1013 int days = t / 60 / 60 / 24;
1016 ss << days <<
" day" << (days > 1 ?
"s" :
"") <<
", ";
1017 t -= days * 60 * 60 * 24;
1021 ss << std::setw(2) << std::setfill(
'0') << (t / 60 / 60) <<
":" << std::setw(2)
1022 << std::setfill(
'0') << ((t % (60 * 60)) / 60) <<
":" << std::setw(2)
1023 << std::setfill(
'0') << (t % 60);
1028 uint64_t StringMacros::nowEpochMs()
1030 return std::chrono::duration_cast<std::chrono::milliseconds>(
1031 std::chrono::system_clock::now().time_since_epoch())
1039 const std::string& value,
bool doConvertEnvironmentVariables)
1042 return doConvertEnvironmentVariables
1046 catch(
const std::runtime_error& e)
1048 __SS__ <<
"Failed to validate value for default string data type. " << __E__
1049 << e.what() << __E__;
1058 std::set<std::string>& setToReturn,
1059 const std::set<char>& delimiter,
1060 const std::set<char>& whitespace)
1067 for(; j < inputString.size(); ++j)
1068 if((whitespace.find(inputString[j]) !=
1070 delimiter.find(inputString[j]) != delimiter.end()) &&
1073 else if((whitespace.find(inputString[j]) !=
1076 delimiter.find(inputString[j]) != delimiter.end()) &&
1082 setToReturn.emplace(inputString.substr(i, j - i));
1089 setToReturn.emplace(inputString.substr(i, j - i));
1105 std::vector<std::string>& listToReturn,
1106 const std::set<char>& delimiter,
1107 const std::set<char>& whitespace,
1108 std::vector<char>* listOfDelimiters,
1109 bool decodeURIComponents)
1114 std::set<char>::iterator delimeterSearchIt;
1115 char lastDelimiter = 0;
1124 for(; c < inputString.size(); ++c)
1128 delimeterSearchIt = delimiter.find(inputString[c]);
1129 isDelimiter = delimeterSearchIt != delimiter.end();
1134 if(whitespace.find(inputString[c]) !=
1143 else if(whitespace.find(inputString[c]) != whitespace.end() &&
1148 else if(isDelimiter)
1153 if(listOfDelimiters && listToReturn.size())
1160 listOfDelimiters->push_back(lastDelimiter);
1163 inputString.substr(i, j - i))
1164 : inputString.substr(i, j - i));
1174 lastDelimiter = *delimeterSearchIt;
1183 if(listOfDelimiters && listToReturn.size())
1189 listOfDelimiters->push_back(lastDelimiter);
1192 inputString.substr(i, j - i))
1193 : inputString.substr(i, j - i));
1197 if(listOfDelimiters && listToReturn.size() - 1 != listOfDelimiters->size() &&
1198 listToReturn.size() != listOfDelimiters->size())
1200 __SS__ <<
"There is a mismatch in delimiters to entries (should be equal or one "
1202 << listOfDelimiters->size() <<
" vs " << listToReturn.size() << __E__
1224 const std::string& inputString,
1225 const std::set<char>& delimiter,
1226 const std::set<char>& whitespace,
1227 std::vector<char>* listOfDelimiters,
1228 bool decodeURIComponents)
1230 std::vector<std::string> listToReturn;
1237 decodeURIComponents);
1238 return listToReturn;
1246 std::map<std::string, std::string>& mapToReturn,
1247 const std::set<char>& pairPairDelimiter,
1248 const std::set<char>& nameValueDelimiter,
1249 const std::set<char>& whitespace)
1255 bool needValue =
false;
1259 for(; j < inputString.size(); ++j)
1262 if((whitespace.find(inputString[j]) !=
1264 pairPairDelimiter.find(inputString[j]) != pairPairDelimiter.end()) &&
1267 else if((whitespace.find(inputString[j]) !=
1270 nameValueDelimiter.find(inputString[j]) !=
1271 nameValueDelimiter.end()) &&
1277 name = inputString.substr(i, j - i);
1287 if((whitespace.find(inputString[j]) !=
1289 nameValueDelimiter.find(inputString[j]) != nameValueDelimiter.end()) &&
1292 else if(whitespace.find(inputString[j]) !=
1295 pairPairDelimiter.find(inputString[j]) !=
1296 pairPairDelimiter.end())
1302 auto emplaceReturn =
1303 mapToReturn.emplace(std::pair<std::string, std::string>(
1306 inputString.substr(i, j - i))
1309 if(!emplaceReturn.second)
1311 __COUT__ <<
"Ignoring repetitive value ('"
1312 << inputString.substr(i, j - i)
1313 <<
"') and keeping current value ('"
1314 << emplaceReturn.first->second <<
"'). " << __E__;
1326 auto emplaceReturn =
1327 mapToReturn.emplace(std::pair<std::string, std::string>(
1330 inputString.substr(i, j - i))
1333 if(!emplaceReturn.second)
1335 __COUT__ <<
"Ignoring repetitive value ('" << inputString.substr(i, j - i)
1336 <<
"') and keeping current value ('" << emplaceReturn.first->second
1341 catch(
const std::runtime_error& e)
1343 __SS__ <<
"Error while extracting a map from the string '" << inputString
1344 <<
"'... is it a valid map?" << __E__ << e.what() << __E__;
1351 const std::string& primaryDelimeter,
1352 const std::string& secondaryDelimeter)
1354 std::stringstream ss;
1356 for(
auto& mapPair : mapToReturn)
1361 ss << primaryDelimeter;
1362 ss << mapPair.first << secondaryDelimeter << (
unsigned int)mapPair.second;
1370 const std::string& delimeter)
1372 std::stringstream ss;
1374 for(
auto& setValue : setToReturn)
1380 ss << (
unsigned int)setValue;
1388 const std::string& delimeter)
1390 std::stringstream ss;
1392 if(delimeter ==
"\n")
1394 for(
auto& setValue : setToReturn)
1400 ss << (
unsigned int)setValue;
1416 std::vector<std::string>& commonChunksToReturn,
1417 std::vector<std::string>& wildcardStringsToReturn,
1418 unsigned int& fixedWildcardLength)
1420 fixedWildcardLength = 0;
1445 std::pair<
unsigned int ,
unsigned int > wildcardBounds(
1446 std::make_pair(-1, 0));
1449 for(
unsigned int n = 1; n < haystack.size(); ++n)
1450 for(
unsigned int i = 0, j = 0;
1451 i < haystack[0].length() && j < haystack[n].length();
1454 if(i < wildcardBounds.first)
1456 if(haystack[0][i] != haystack[n][j])
1458 wildcardBounds.first = i;
1465 __COUTS__(3) <<
"Low side = " << wildcardBounds.first <<
" "
1466 << haystack[0].substr(0, wildcardBounds.first) << __E__;
1469 for(
unsigned int n = 1; n < haystack.size(); ++n)
1470 for(
int i = haystack[0].length() - 1, j = haystack[n].length() - 1;
1471 i >= (int)wildcardBounds.first && j >= (
int)wildcardBounds.first;
1474 if(i > (
int)wildcardBounds.second)
1476 if(haystack[0][i] != haystack[n][j])
1478 wildcardBounds.second = i + 1;
1486 __COUTS__(3) <<
"High side = " << wildcardBounds.second <<
" "
1487 << haystack[0].substr(wildcardBounds.second) << __E__;
1490 commonChunksToReturn.push_back(haystack[0].substr(0, wildcardBounds.first));
1492 if(wildcardBounds.first != (
unsigned int)-1)
1495 for(
int i = (wildcardBounds.first + wildcardBounds.second) / 2 + 1;
1496 i < (
int)wildcardBounds.second;
1498 if(haystack[0][wildcardBounds.first] == haystack[0][i] &&
1499 haystack[0].substr(wildcardBounds.first, wildcardBounds.second - i) ==
1500 haystack[0].substr(i, wildcardBounds.second - i))
1502 std::string multiWildcardString =
1503 haystack[0].substr(i, wildcardBounds.second - i);
1504 __COUT__ <<
"Potential multi-wildcard found: " << multiWildcardString
1505 <<
" at position i=" << i << __E__;
1507 std::vector<
unsigned int > wildCardInstances;
1509 wildCardInstances.push_back(wildcardBounds.first);
1511 unsigned int offset =
1512 wildCardInstances[0] + multiWildcardString.size() + 1;
1513 std::string middleString = haystack[0].substr(offset, (i - 1) - offset);
1514 __COUTV__(middleString);
1518 while((k = middleString.find(multiWildcardString)) != std::string::npos)
1520 __COUT__ <<
"Multi-wildcard found at " << k << __E__;
1522 wildCardInstances.push_back(offset + k);
1525 middleString.substr(k + multiWildcardString.size() + 1);
1526 offset += k + multiWildcardString.size() + 1;
1527 __COUTV__(middleString);
1531 wildCardInstances.push_back(i);
1533 for(
unsigned int w = 0; w < wildCardInstances.size() - 1; ++w)
1535 __COUTV__(wildCardInstances[w]);
1536 __COUTV__(wildCardInstances[w + 1]);
1537 __COUTV__(wildCardInstances.size());
1538 commonChunksToReturn.push_back(haystack[0].substr(
1539 wildCardInstances[w] + multiWildcardString.size(),
1540 wildCardInstances[w + 1] -
1541 (wildCardInstances[w] + multiWildcardString.size())));
1547 for(
unsigned int c = 1; c < commonChunksToReturn.size(); ++c)
1549 __COUT__ <<
"Checking [" << c <<
"]: " << commonChunksToReturn[c] << __E__;
1550 for(
unsigned int n = 1; n < haystack.size(); ++n)
1552 __COUT__ <<
"Checking chunks work with haystack [" << n
1553 <<
"]: " << haystack[n] << __E__;
1554 __COUTV__(commonChunksToReturn[0].size());
1555 std::string wildCardValue = haystack[n].substr(
1556 commonChunksToReturn[0].size(),
1557 haystack[n].find(commonChunksToReturn[1],
1558 commonChunksToReturn[0].size() + 1) -
1559 commonChunksToReturn[0].size());
1560 __COUTTV__(wildCardValue);
1562 std::string builtString =
"";
1563 for(
unsigned int cc = 0; cc < commonChunksToReturn.size(); ++cc)
1564 builtString += commonChunksToReturn[cc] + wildCardValue;
1565 __COUTTV__(builtString);
1566 __COUTTV__(wildCardValue);
1568 if(haystack[n].find(builtString) != 0)
1570 __COUT__ <<
"Dropping common chunk " << commonChunksToReturn[c]
1571 <<
", built '" << builtString <<
"' not found in "
1572 << haystack[n] << __E__;
1573 commonChunksToReturn.erase(commonChunksToReturn.begin() + c);
1578 __COUTT__ <<
"Found built '" << builtString <<
"' in " << haystack[n]
1584 __COUTTV__(commonChunksToReturn[0].size());
1586 __COUTTV__(fixedWildcardLength);
1588 for(
unsigned int i = 0; i < commonChunksToReturn[0].size(); ++i)
1589 if(commonChunksToReturn[0][commonChunksToReturn[0].size() - 1 - i] ==
'0')
1591 ++fixedWildcardLength;
1592 __COUTT__ <<
"Trying for added fixed length +1 to " << fixedWildcardLength
1599 for(
unsigned int c = 0; c < commonChunksToReturn.size(); ++c)
1601 unsigned int cnt = 0;
1602 for(
unsigned int i = 0; i < commonChunksToReturn[c].size(); ++i)
1603 if(commonChunksToReturn[c][commonChunksToReturn[c].size() - 1 - i] ==
'0')
1608 if(fixedWildcardLength < cnt)
1609 fixedWildcardLength = cnt;
1610 else if(fixedWildcardLength > cnt)
1612 __SS__ <<
"Invalid fixed length found, please simplify indexing between "
1613 "these common chunks: "
1618 __COUTTV__(fixedWildcardLength);
1620 if(fixedWildcardLength)
1621 for(
unsigned int c = 0; c < commonChunksToReturn.size(); ++c)
1622 commonChunksToReturn[c] = commonChunksToReturn[c].substr(
1623 0, commonChunksToReturn[c].size() - fixedWildcardLength);
1626 commonChunksToReturn.push_back(haystack[0].substr(wildcardBounds.second));
1633 unsigned int ioff = fixedWildcardLength;
1634 bool wildcardsNeeded =
false;
1635 bool someLeadingZeros =
false;
1636 bool allWildcardsSameSize =
true;
1638 for(
unsigned int n = 0; n < haystack.size(); ++n)
1640 std::string wildcard =
"";
1642 i = ioff + commonChunksToReturn[0].size();
1644 if(commonChunksToReturn.size() == 1)
1645 wildcard = haystack[n].substr(i);
1647 for(
unsigned int c = 1; c < commonChunksToReturn.size(); ++c)
1649 if(c == commonChunksToReturn.size() - 1)
1650 k = haystack[n].rfind(commonChunksToReturn[c]);
1652 k = haystack[n].find(commonChunksToReturn[c], i + 1);
1659 __COUTVS__(3, k - i);
1661 wildcard = haystack[n].substr(i, k - i);
1662 if(fixedWildcardLength && n == 0)
1663 fixedWildcardLength += wildcard.size();
1665 __COUTS__(3) <<
"name[" << n <<
"] = " << wildcard <<
" fixed @ "
1666 << fixedWildcardLength << __E__;
1671 wildcard != haystack[n].substr(i, k - i))
1673 __SS__ <<
"Invalid wildcard! for name[" << n <<
"] = " << haystack[n]
1674 <<
" - the extraction algorithm is confused, please simplify "
1675 "your naming convention."
1685 wildcardsNeeded =
true;
1688 if(wildcard[0] ==
'0' && !fixedWildcardLength)
1690 someLeadingZeros =
true;
1691 if(wildcardStringsToReturn.size() &&
1692 wildcard.size() != wildcardStringsToReturn[0].size())
1693 allWildcardsSameSize =
false;
1696 wildcardStringsToReturn.push_back(wildcard);
1703 if(someLeadingZeros && allWildcardsSameSize)
1705 __COUTTV__(fixedWildcardLength);
1706 fixedWildcardLength = wildcardStringsToReturn[0].size();
1707 __COUT__ <<
"Enforce wildcard size of " << fixedWildcardLength << __E__;
1710 if(wildcardStringsToReturn.size() != haystack.size())
1712 __SS__ <<
"There was a problem during common chunk extraction!" << __E__;
1716 return wildcardsNeeded;
1725 const std::string& rhs)
const
1731 for(
unsigned int i = 0; i < lhs.size() && i < rhs.size(); ++i)
1734 if((lhs[i] >=
'A' && lhs[i] <=
'Z' && rhs[i] >=
'A' && rhs[i] <=
'Z') ||
1735 (lhs[i] >=
'a' && lhs[i] <=
'z' && rhs[i] >=
'a' && rhs[i] <=
'z'))
1737 if(lhs[i] == rhs[i])
1739 return (lhs[i] < rhs[i]);
1742 else if(lhs[i] >=
'A' && lhs[i] <=
'Z')
1744 if(lhs[i] + 32 == rhs[i])
1746 return (lhs[i] + 32 < rhs[i]);
1748 else if(rhs[i] >=
'A' && rhs[i] <=
'Z')
1750 if(lhs[i] == rhs[i] + 32)
1752 return (lhs[i] < rhs[i] + 32);
1756 if(lhs[i] == rhs[i])
1758 return (lhs[i] < rhs[i]);
1763 return lhs.size() < rhs.size();
1773 std::array<char, 128> buffer;
1778 std::string cmdWithRedirect = std::string(cmd) +
" 2>&1";
1779 FILE* rawPipe = popen(cmdWithRedirect.c_str(),
"r");
1781 __THROW__(
"popen() failed!");
1786 std::unique_ptr<FILE, int (*)(FILE*)> pipe(rawPipe, pclose);
1788 while(fgets(buffer.data(), buffer.size(), pipe.get()) !=
nullptr)
1789 result += buffer.data();
1791 int status = pclose(pipe.release());
1793 __COUT_WARN__ <<
"pclose() failed for command: " << cmd << __E__;
1805 uintptr_t find_library_base(
const std::string& libname)
1807 std::ifstream maps(
"/proc/self/maps");
1810 while(std::getline(maps, line))
1812 if(line.find(libname) != std::string::npos &&
1813 line.find(
"r-xp") != std::string::npos)
1816 std::stringstream ss(line);
1817 ss >> std::hex >> base;
1825 void resolve_stack_entry(
const std::string& so_path,
1826 const std::string& real_name,
1827 const std::string& offset_begin,
1828 const std::string& offset_end
1833 const std::size_t pos0x = offset_end.find(
"0x");
1834 if(pos0x == std::string::npos)
1836 __COUTS__(52) <<
"resolve_stack_entry: could not find \"0x\" in offset_end: '"
1837 << offset_end <<
"'" << __E__;
1841 const std::size_t posBracket = offset_end.find(
']', pos0x);
1842 if(posBracket == std::string::npos || posBracket < pos0x + 3)
1844 __COUTS__(52) <<
"resolve_stack_entry: could not find closing ']' with at least "
1845 "one hex digit after \"0x\" "
1847 << offset_end <<
"'" << __E__;
1851 const std::string addr_str = offset_end.substr(pos0x, posBracket - pos0x);
1853 uintptr_t runtime_addr = 0;
1856 runtime_addr = std::stoull(addr_str,
nullptr, 16);
1858 catch(
const std::exception& e)
1860 __COUTS__(52) <<
"resolve_stack_entry: failed to parse runtime address from '"
1861 << addr_str <<
"': " << e.what() << __E__;
1865 std::string so_name = so_path.substr(so_path.find_last_of(
'/') + 1);
1867 uintptr_t base = find_library_base(so_name);
1870 std::cerr <<
"Could not find base for " << so_name <<
"\n";
1874 uintptr_t file_addr = runtime_addr - base;
1876 std::ostringstream cmd;
1877 cmd <<
"addr2line -f -C -e " << so_path <<
" 0x" << std::hex << file_addr;
1879 __COUT__ <<
"\nResolving:\n"
1880 << so_path <<
" : " << real_name << offset_begin <<
" [" << std::hex
1881 << runtime_addr <<
"]\n\n";
1892 #include <execinfo.h>
1896 __SS__ <<
"ots::stackTrace:\n";
1902 size = backtrace(array, 10);
1906 char** messages = backtrace_symbols(array, size);
1910 for(
unsigned int i = 1; i < size && messages != NULL; ++i)
1923 char *mangled_name = 0, *offset_begin = 0, *offset_end = 0;
1926 for(
char* p = messages[i]; *p; ++p)
1944 if(mangled_name && offset_begin && offset_end && mangled_name < offset_begin)
1946 *mangled_name++ =
'\0';
1947 *offset_begin++ =
'\0';
1948 *offset_end++ =
'\0';
1951 char* real_name = abi::__cxa_demangle(mangled_name, 0, 0, &status);
1956 ss <<
"[" << i <<
"] " << messages[i] <<
" : " << real_name <<
"+"
1957 << offset_begin << offset_end << std::endl;
1964 ss <<
"[" << i <<
"] " << messages[i] <<
" : " << mangled_name <<
"+"
1965 << offset_begin << offset_end << std::endl;
1972 ss <<
"[" << i <<
"] " << messages[i] << std::endl;
1992 const std::string& location,
1993 const unsigned int& line)
1995 char* environmentVariablePtr = getenv(name);
1996 if(!environmentVariablePtr)
1998 __SS__ <<
"Environment variable '$" << name <<
"' not defined at " << location
1999 <<
":" << line << __E__;
2003 return environmentVariablePtr;
2010 const std::string& field,
2011 uint32_t occurrence,
2013 size_t* returnFindPos ,
2014 const std::string& valueField ,
2015 const std::string& quoteType )
2018 *returnFindPos = std::string::npos;
2020 __COUTVS__(41, xml);
2022 size_t lo, findpos = after, hi;
2023 for(uint32_t i = 0; i <= occurrence; ++i)
2025 bool anyFound =
false;
2027 xml.find(
"<" + field,
2028 findpos)) != std::string::npos &&
2029 findpos + 1 + field.size() < xml.size())
2031 __COUTS__(40) <<
"find: ---- '<" << field <<
" findpos=" << findpos
2032 <<
"findpos " << findpos <<
" " << xml[findpos] <<
" "
2033 << xml[findpos + 1 + field.size()] <<
" "
2034 << (int)xml[findpos + 1 + field.size()] << __E__;
2042 if((quoteType ==
">" && xml[findpos] ==
'>') || xml[findpos] ==
' ' ||
2043 xml[findpos] ==
'\n' || xml[findpos] ==
'\t')
2052 __COUTS__(40) <<
"Field '" << field <<
"' not found" << __E__;
2057 lo = xml.find(valueField + quoteType, findpos) + valueField.size() + quoteType.size();
2059 if(TTEST(40) && quoteType.size())
2061 __COUTS__(40) <<
"Neighbors of field '" << field <<
"' and value '" << valueField
2062 <<
"' w/quote = " << quoteType << __E__;
2063 for(
size_t i = lo - valueField.size(); i < lo + 10 && i < xml.size(); ++i)
2064 __COUTS__(40) <<
"xml[" << i <<
"] " << xml[i] <<
" vs " << quoteType <<
" ? "
2065 << (int)xml[i] <<
" vs " << (
int)quoteType[0] << __E__;
2069 quoteType ==
">" ?
"<" : quoteType,
2070 lo)) == std::string::npos)
2072 __COUTS__(40) <<
"Value closing not found" << __E__;
2077 *returnFindPos = findpos - (1 + field.size());
2079 __COUTS__(40) <<
"after: " << after <<
", findpos: " << findpos <<
", hi/lo: " << hi
2080 <<
"/" << lo <<
", size: " << xml.size() << __E__;
2081 __COUTVS__(40, xml.substr(lo, hi - lo));
2082 return xml.substr(lo, hi - lo);
2089 const std::string& field,
2090 uint32_t occurrence,
2092 size_t* returnFindPos ,
2093 const std::string& valueField ,
2094 const std::string& quoteType )
2097 *returnFindPos = std::string::npos;
2099 __COUTVS__(41, xml);
2101 size_t lo = 0, hi, findpos = before;
2102 for(uint32_t i = 0; i <= occurrence; ++i)
2104 bool anyFound =
false;
2106 xml.rfind(
"<" + field,
2107 findpos)) != std::string::npos &&
2108 findpos + 1 + field.size() < xml.size())
2110 __COUTS__(40) <<
"rfind: ---- '<" << field <<
" findpos=" << findpos <<
" "
2111 << xml[findpos] <<
" " << xml[findpos + 1 + field.size()] <<
" "
2112 << (int)xml[findpos + 1 + field.size()] << __E__;
2114 findpos += 1 + field.size();
2117 if((quoteType ==
">" && xml[findpos] ==
'>') || xml[findpos] ==
' ' ||
2118 xml[findpos] ==
'\n' || xml[findpos] ==
'\t')
2124 findpos -= 1 + field.size() + 1;
2128 __COUTS__(40) <<
"Field '" << field <<
"' not found" << __E__;
2133 lo = xml.find(valueField + quoteType, findpos) + valueField.size() + quoteType.size();
2135 if(TTEST(40) && quoteType.size())
2137 __COUTS__(40) <<
"Neighbors?" << __E__;
2138 for(
size_t i = findpos; i < lo + 10 && i < xml.size(); ++i)
2139 __COUTS__(40) <<
"xml[" << i <<
"] " << xml[i] <<
" vs " << quoteType <<
" ? "
2140 << (int)xml[i] <<
" vs " << (
int)quoteType[0] << __E__;
2144 quoteType ==
">" ?
"<" : quoteType,
2145 lo)) == std::string::npos)
2147 __COUTS__(40) <<
"Value closing not found" << __E__;
2153 findpos - (1 + field.size());
2155 __COUTS__(40) <<
"before: " << before <<
", findpos: " << findpos <<
", hi/lo: " << hi
2156 <<
"/" << lo <<
", size: " << xml.size() << __E__;
2157 __COUTVS__(40, xml.substr(lo, hi - lo));
2158 return xml.substr(lo, hi - lo);
2166 const std::set<char>& delimiter )
2170 __COUTV__(splitArr.size());
2171 __COUTVS__(lvl, splitArr.size());
2172 for(
const auto& split : splitArr)
2173 __COUTS__(lvl) << split;
2188 std::unique_ptr<char, void (*)(
void*)> res{
2189 abi::__cxa_demangle(name, NULL, NULL, &status), std::free};
2191 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 loadPersistentSystemVariables(void)
loads persisted 'artdaq' namespace systemVariables_; returns false if no file found
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)