MCPcopy Create free account
hub / github.com/computationalpathologygroup/ASAP / getFiles

Function getFiles

core/filetools.cpp:575–666  ·  view source on GitHub ↗

--------------------------------------------------------------------------- MN: WARNING, THIS FUNCTION HAS BEEN OBSERVED TO GENERATE FILESYSTEM EXCEPTIONS IN THE WILD.

Source from the content-addressed store, hash-verified

573//MN: WARNING, THIS FUNCTION HAS BEEN OBSERVED TO GENERATE FILESYSTEM EXCEPTIONS
574// IN THE WILD.
575 void getFiles(
576 const std::string &thepath,
577 const std::string &name,
578 std::vector<std::string> &v,
579 bool recurse)
580 {
581 v.clear();
582 std::string pa(thepath);
583 if (pa.empty()) pa = ".";
584 cleanDirName(pa);
585#ifdef WIN32
586 replaceAll(pa, "/", "\\");
587 if (pa.empty()) pa = ".";
588 if (pa[pa.size()-1] != '\\') pa += "\\";
589#else
590 replaceAll(pa, "\\", "/");
591 if (pa.empty()) pa = ".";
592 if (pa[pa.size()-1] != '/') pa += "/";
593#endif
594 if (!dirExists(pa)) return;
595
596 std::vector<fs::path> vp;
597 vp.push_back(fs::path(pa));
598 unsigned int i = 0;
599
600 //to create a proper regular expression, any non literal should be preceded by \:
601 //All characters are literals except: ".", "|", "*", "?", "+", "(", ")", "{", "}",
602 //"[", "]", "^", "$" and "\".
603 //some of these characters are not allowed in filenames, ...
604 //and * should be replaced by .* and ? should be replaced by .?
605 std::string filename = name;
606
607 replaceAll(filename, ".", "\\.");
608 replaceAll(filename, "|", "\\|");
609 replaceAll(filename, "|", "\\|");
610 replaceAll(filename, "+", "\\+");
611 replaceAll(filename, "(", "\\(");
612 replaceAll(filename, ")", "\\)");
613 replaceAll(filename, "[", "\\[");
614 replaceAll(filename, "]", "\\]");
615 replaceAll(filename, "{", "\\{");
616 replaceAll(filename, "}", "\\}");
617 replaceAll(filename, "$", "\\$");
618 replaceAll(filename, "^", "\\^");
619 replaceAll(filename, "*", ".*");
620 replaceAll(filename, "?", ".?");
621 std::regex e(filename, std::regex::icase);
622
623 while (i < vp.size())
624 {
625 //now we collect all files in the directory and check if they match the pattern
626 fs::directory_iterator end_itr; // default construction yields past-the-end
627 std::string p = vp[i].string();
628#ifdef WIN32
629 if (p[p.size()-1] != '\\') p += "\\";
630#else
631 if (p[p.size()-1] != '/') p += "/";
632#endif

Callers 5

copyDirectoryFunction · 0.85
initializeTypeMethod · 0.85
initializeTypeMethod · 0.85
mainFunction · 0.85

Calls 6

cleanDirNameFunction · 0.85
replaceAllFunction · 0.85
dirExistsFunction · 0.85
emptyMethod · 0.80
sizeMethod · 0.80
clearMethod · 0.45

Tested by

no test coverage detected