--------------------------------------------------------------------------- MN: WARNING, THIS FUNCTION HAS BEEN OBSERVED TO GENERATE FILESYSTEM EXCEPTIONS IN THE WILD.
| 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 |
no test coverage detected