91 const std::string &date,
92 const std::string &searchTerm,
93 const std::vector<std::string> &fileFormats,
94 const std::string &dirHint) {
96 if (searchTerm ==
"_Definition")
97 fileType =
"instrument file";
98 else if (searchTerm ==
"_Parameters")
99 fileType =
"parameter file";
101 fileType = searchTerm +
" file";
103 g_log.
debug() <<
"Looking for " << fileType <<
" for " << instrumentName <<
" that is valid on '" << date <<
"'\n";
105 std::string instrument;
107 instrument = Kernel::ConfigService::Instance().getInstrument(instrumentName).name();
109 instrument = instrumentName;
115 const std::vector<std::string> &configDirs = Kernel::ConfigService::Instance().getInstrumentDirectories();
116 std::vector<std::string> directoryNames;
117 if (!dirHint.empty()) {
118 directoryNames.push_back(dirHint);
119 directoryNames.insert(directoryNames.end(), configDirs.begin(), configDirs.end());
121 directoryNames = configDirs;
125 const std::vector<std::string> matchingFiles =
127 std::string foundFile;
128 if (!matchingFiles.empty()) {
129 foundFile = matchingFiles[0];
130 g_log.
debug() <<
"The " << fileType <<
" selected is " << foundFile <<
'\n';
132 g_log.
debug() <<
"No " << fileType <<
" found\n";
218 const std::string ext =
".xml";
220 boost::algorithm::ierase_all(filename, ext);
222 const std::string suffixSeperator(
"_Definition");
227 if (
auto sepPos = boost::algorithm::ifind_first(filename, suffixSeperator)) {
228 prefix = std::string(filename.begin(), sepPos.begin());
229 suffix = std::string(sepPos.end(), filename.end());
234 std::filesystem::path directoryPath(dir);
237 std::string fullPathParamIDF = (directoryPath / (prefix +
"_Parameters" + suffix + ext)).string();
239 if (std::filesystem::exists(fullPathParamIDF)) {
240 return fullPathParamIDF;
243 fullPathParamIDF = (directoryPath / (prefix +
"_Parameters" + ext)).string();
244 if (std::filesystem::exists(fullPathParamIDF)) {
245 return fullPathParamIDF;
267 const std::vector<std::string> &fileFormats,
268 const std::vector<std::string> &directoryNames,
269 const std::string &date) {
273 g_log.
debug() <<
"No date specified, using current date and time.\n";
274 const std::string now = Types::Core::DateAndTime::getCurrentTime().toISO8601String();
280 std::stringstream ss;
282 for (
size_t i = 0; i < fileFormats.size(); ++i) {
285 ss << fileFormats[i];
288 const std::string allFileFormats = ss.str();
290 const boost::regex regex(prefix +
".*\\." + allFileFormats, boost::regex_constants::icase);
298 d = DateAndTime(normalisedDate);
299 }
catch (
const std::invalid_argument &) {
303 <<
"' as ISO8601; using current time for instrument file lookup.\n";
304 d = DateAndTime::getCurrentTime();
307 DateAndTime refDate(
"1899-01-01 23:59:00");
309 DateAndTime refDateGoodFile(
"1899-01-01 23:59:00");
311 struct MatchingFile {
312 DateAndTime validFrom;
313 size_t directoryIndex;
316 std::vector<MatchingFile> matchingFiles;
317 bool foundFile =
false;
318 std::string mostRecentFile;
319 for (
size_t directoryIndex = 0; directoryIndex < directoryNames.size(); ++directoryIndex) {
320 const auto &directoryName = directoryNames[directoryIndex];
323 for (
const auto &dir_entry : std::filesystem::directory_iterator(directoryName)) {
325 const auto &filePath = dir_entry.path();
326 if (!std::filesystem::is_regular_file(filePath))
329 const std::string l_filenamePart = filePath.filename().string();
330 if (regex_match(l_filenamePart, regex)) {
331 const std::string pathName = filePath.string();
332 g_log.
debug() <<
"Found file: '" << pathName <<
"'\n";
334 std::string validFrom, validTo;
336 g_log.
debug() <<
"File '" << pathName <<
" valid dates: from '" << validFrom <<
"' to '" << validTo <<
"'\n";
341 DateAndTime to, from;
343 if (validFrom.length() > 0) {
345 from.setFromISO8601(normFrom);
349 }
catch (
const std::invalid_argument &) {
350 g_log.
debug() <<
"Could not parse valid-from='" << validFrom <<
"' in '" << pathName
351 <<
"'; treating as lowest priority.\n";
355 if (validTo.length() > 0) {
357 to.setFromISO8601(normTo);
359 to.setFromISO8601(
"2100-01-01T00:00:00");
361 }
catch (
const std::invalid_argument &) {
362 g_log.
debug() <<
"Could not parse valid-to='" << validTo <<
"' in '" << pathName
363 <<
"'; treating as lowest priority.\n";
364 to.setFromISO8601(
"2100-01-01T00:00:00");
367 if (from <=
d &&
d <= to) {
369 matchingFiles.emplace_back(from, directoryIndex, pathName);
372 if (!foundFile && (from >= refDate)) {
374 mostRecentFile = pathName;
381 std::vector<std::string> pathNames;
382 if (!matchingFiles.empty()) {
383 std::sort(matchingFiles.begin(), matchingFiles.end(), [](
const auto &lhs,
const auto &
rhs) {
384 if (lhs.validFrom != rhs.validFrom)
385 return lhs.validFrom > rhs.validFrom;
386 if (lhs.directoryIndex != rhs.directoryIndex)
387 return lhs.directoryIndex < rhs.directoryIndex;
388 return lhs.path < rhs.path;
390 pathNames.reserve(matchingFiles.size());
392 std::transform(matchingFiles.begin(), matchingFiles.end(), std::back_inserter(pathNames),
393 [](
const auto &elem) { return elem.path; });
395 pathNames.emplace_back(std::move(mostRecentFile));
408 std::string &outValidTo) {
412 myContentHandler conHand;
413 pParser.setContentHandler(&conHand);
416 pParser.parse(IDFfilename);
417 }
catch (
const DummyException &e) {
418 outValidFrom = e.m_validFrom;
419 outValidTo = e.m_validTo;