Mantid
Loading...
Searching...
No Matches
Glob.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#include "MantidKernel/Glob.h"
9#include <Poco/Glob.h>
10
11namespace Mantid::Kernel {
12
35void Glob::glob(const std::string &pathPattern, std::set<std::string> &files, int options) {
36#ifdef _WIN32
37 // There appears to be a bug in the glob for windows.
38 // Putting case sensitive on then with reference to test
39 // testFindFileCaseSensitive()
40 // in FileFinderTest on Windows it is able to find "CSp78173.Raw" as it should
41 // even
42 // the case is wrong, but for some strange reason it then cannot find
43 // IDF_for_UNiT_TESTiNG.xMl!!!!
44 // Hence the reason to circumvent this by this #ifdef
45 Poco::Glob::glob(pathPattern, files, Poco::Glob::GLOB_CASELESS);
46#else
47 Poco::Glob::glob(pathPattern, files, options);
48#endif
49}
50
51std::string Glob::globToRegex(const std::string &globPattern) {
52 const std::string STAR("*");
53 const std::string STAR_ESCAPED("\\*");
54 const std::string QUESTION("?");
55 const std::string QUESTION_ESCAPED("\\?");
56
57 const std::string REGEX_MATCH_GLOB(".+");
58 const std::string REGEX_MATCH_CHAR(".");
59 const std::string REGEX_START_STR("^");
60 const std::string REGEX_END_STR("$");
61
62 // this is a variant of Strings::replace and Strings::replaceAll
63 std::string output = globPattern;
64
65 // replace stars
66 std::string::size_type pos = 0;
67 while ((pos = output.find(STAR, pos)) != std::string::npos) {
68 if ((pos > 0) && (pos + 1 != std::string::npos)) {
69 if (output.substr(pos - 1, STAR_ESCAPED.length()) == STAR_ESCAPED) {
70 pos += 1;
71 continue;
72 }
73 }
74 output.erase(pos, STAR.length());
75 output.insert(pos, REGEX_MATCH_GLOB);
76 pos += REGEX_MATCH_GLOB.length();
77 }
78
79 // now with question marks
80 pos = 0;
81 while ((pos = output.find(QUESTION, pos)) != std::string::npos) {
82 if ((pos > 0) && (pos + 1 != std::string::npos)) {
83 if (output.substr(pos - 1, QUESTION_ESCAPED.length()) == QUESTION_ESCAPED) {
84 pos += 1;
85 continue;
86 }
87 }
88 output.erase(pos, QUESTION.length());
89 output.insert(pos, REGEX_MATCH_CHAR);
90 pos += REGEX_MATCH_CHAR.length();
91 }
92
93 // now [! and don't worry about escaping
94 pos = 0;
95 while ((pos = output.find("[!", pos)) != std::string::npos) {
96 output.erase(pos, std::string("[!").length());
97 output.insert(pos, "![");
98 pos += std::string("![").length();
99 }
100
101 // replaced string with anchors
102 return REGEX_START_STR + output + REGEX_END_STR;
103 // return output;
104}
105
106} // namespace Mantid::Kernel
static std::string globToRegex(const std::string &globPattern)
Convert a glob pattern to an equivalent regular expression.
Definition Glob.cpp:51
static void glob(const std::string &pathPattern, std::set< std::string > &files, int options=0)
Creates a set of files that match the given pathPattern.
Definition Glob.cpp:35