43 #define PATH_SEP_STRING "/"
45 #include "build_config.h"
46 #define PATH_SEP_STRING "/\\"
58 #define GLOB_ERR 0x0001
59 #define GLOB_NOSORT 0x0002
60 #define GLOB_TILDE 0x0000
61 #define GLOB_NOSPACE 1
62 #define GLOB_ABORTED 2
63 #define GLOB_NOMATCH 3
76 static bool winMatchRE(
const std::string& pattern,
const std::string& test)
84 std::regex re(patternRE, std::regex::basic);
86 std::regex_search(test, m, re);
100 static void winGlob(
const char *pattern, std::list<std::string>& results)
102 WIN32_FIND_DATA findFileData;
104 std::string patternStr(pattern);
105 if (patternStr.empty())
107 std::string::size_type
pos = patternStr.find_first_of(
"*?["), pos2;
114 if (
pos != std::string::npos)
118 std::string pathSearch = patternStr.substr(0,pos2);
121 std::string matchPattern = patternStr.substr(
pos,pos2-
pos);
122 hFindFile = FindFirstFile(pathSearch.c_str(), &findFileData);
123 if (hFindFile != INVALID_HANDLE_VALUE)
130 if (winMatchRE(matchPattern, findFileData.cFileName))
134 patternStr.substr(0,
pos) + findFileData.cFileName);
136 std::string newPattern(
137 match + gnsstk::getFileSep() + patternStr.substr(pos2+1));
140 if ((pos2 != std::string::npos) &&
141 (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
144 winGlob(newPattern.c_str(), results);
146 else if (pos2 == std::string::npos)
149 results.push_back(match);
152 }
while(FindNextFile(hFindFile, &findFileData));
153 FindClose(hFindFile);
161 hFindFile = FindFirstFile(pattern, &findFileData);
162 if (hFindFile != INVALID_HANDLE_VALUE)
164 results.push_back(pattern);
178 static int glob(
const char *pattern,
int flags,
179 int (*errfunc) (
const char *epath,
int eerrno),
183 pglob->gl_pathv =
nullptr;
184 std::list<std::string> results;
185 winGlob(pattern, results);
188 pglob->gl_pathc = results.size();
189 pglob->gl_pathv = (
char**)malloc(pglob->gl_pathc *
sizeof(
char*));
190 if (pglob->gl_pathv ==
nullptr)
193 for (
const auto& i : results)
195 pglob->gl_pathv[j] = (
char*)malloc(i.length() + 1);
196 if (pglob->gl_pathv[j] ==
nullptr)
198 strcpy(pglob->gl_pathv[j], i.c_str());
210 static void globfree(glob_t *pglob)
212 for (
unsigned i = 0; i < pglob->gl_pathc; i++)
214 free(pglob->gl_pathv[i]);
216 free(pglob->gl_pathv);
217 pglob->gl_pathv =
nullptr;
224 list<string> FileSpecFind ::
225 find(
const std::string& fileSpecString,
239 string spec(fileSpecString);
240 unsigned textLen = dummyFSTS[fsType].length();
245 dummyFSTS[fsType] =
"Z";
250 unsigned selLen = dummyFSTS[fsSel].length();
255 dummyFSTS[fsSel] =
"Z";
270 if ((fst == FileSpec::fixed) || (fst == FileSpec::unknown))
272 if (dummyFSTS.find(fst) == dummyFSTS.end())
278 return findGlob(start, end, spec, dummyFSTS, filter);
282 std::list<std::string> FileSpecFind ::
283 find(
const std::string& fileSpec,
299 if ((fst == FileSpec::fixed) || (fst == FileSpec::unknown))
303 return findGlob(start, end, fileSpec, dummyFSTS, filter);
307 string FileSpecFind ::
308 transToken(
const string& token)
312 string::size_type spos = 0;
321 string::size_type ppos = token.find(
'%', spos);
322 if (ppos == string::npos)
324 rv += token.substr(spos, ppos);
327 rv += token.substr(spos, ppos-spos);
328 string::size_type fpos = token.find_first_not_of(
"0123456789", ppos+1);
329 unsigned long len = 0;
331 if ((fpos - ppos) > 0)
333 len = std::strtoul(&token[ppos+1], &end, 10);
341 if ((token[fpos] == textTok[0]) ||
342 (token[fpos] == selTok[0]) ||
343 (token[fpos] == stnTok[0]))
347 rv += string(len,
'?');
353 for (
unsigned long c = 0; c < len; c++)
473 list<string> FileSpecFind ::
479 const string& matched,
480 string::size_type
pos)
498 string::size_type stokpos = spec.find(
'%',
pos);
501 string::size_type stoppos =
min(
505 string::size_type srest =
506 (stokpos == string::npos
510 string thisSpec(spec.substr(0, srest));
515 string patternSpec(thisSpec);
516 patternSpec.replace(0,
pos, matched);
519 string pattern = transToken(patternSpec);
527 string currentSpec = thisSpec.substr(stoppos+1);
528 FileSpec currentSpecScanner(currentSpec);
538 string fromString = specScanner.
toString(fromTime, dummyFSTS);
539 string toString = specScanner.
toString(toTime, dummyFSTS);
544 if (toTimeMatch == fromTimeMatch)
550 int g = glob(pattern.c_str(), GLOB_ERR|GLOB_NOSORT|GLOB_TILDE,
nullptr,
552 for (
size_t i = 0; i < globbuf.gl_pathc; i++)
554 bool timeMatched =
true;
561 timeMatched = ((fromTimeMatch <= fileTime) &&
562 (fileTime < toTimeMatch));
568 bool matchedFilter =
true;
576 Filter::const_iterator fi = filter.begin();
581 std::string fieldVal;
585 while (fi != filter.end())
592 if ((fi->first == lastFST) ||
593 currentSpecScanner.
hasField(fi->first))
599 if (fi->first != lastFST)
603 fieldVal = specScanner.
extractField(globbuf.gl_pathv[i],
607 if (fi->second == fieldVal)
611 fi = filter.upper_bound(fi->first);
620 if ((fi == filter.end()) ||
621 (fi->first != lastFST))
623 matchedFilter =
false;
634 fi = filter.upper_bound(fi->first);
643 if (srest == string::npos)
645 rv.push_back(globbuf.gl_pathv[i]);
651 findGlob(fromTime, toTime, spec, dummyFSTS, filter,
652 globbuf.gl_pathv[i], thisSpec.length());
653 rv.splice(rv.end(), toAdd);