bool FileLister::isDirectory(const std::string &path) { return (MyIsDirectory(path) != FALSE); }
void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path, const std::set<std::string> &extra, const PathMatch& ignored) { const std::string cleanedPath = Path::toNativeSeparators(path); // basedir is the base directory which is used to form pathnames. // It always has a trailing backslash available for concatenation. std::string basedir; // searchPattern is the search string passed into FindFirst and FindNext. std::string searchPattern = cleanedPath; // The user wants to check all files in a dir const bool checkAllFilesInDir = (MyIsDirectory(cleanedPath) != FALSE); if (checkAllFilesInDir) { char c = cleanedPath.back(); switch (c) { case '\\': searchPattern += '*'; basedir = cleanedPath; break; case '*': basedir = cleanedPath.substr(0, cleanedPath.length() - 1); break; default: searchPattern += "\\*"; if (cleanedPath != ".") basedir = cleanedPath + '\\'; } } else { std::string::size_type pos = cleanedPath.find_last_of('\\'); if (std::string::npos != pos) { basedir = cleanedPath.substr(0, pos + 1); } } WIN32_FIND_DATAA ffd; HANDLE hFind = MyFindFirstFile(searchPattern, &ffd); if (INVALID_HANDLE_VALUE == hFind) return; do { if (ffd.cFileName[0] == '.' || ffd.cFileName[0] == '\0') continue; const char* ansiFfd = ffd.cFileName; if (std::strchr(ansiFfd,'?')) { ansiFfd = ffd.cAlternateFileName; } const std::string fname(basedir + ansiFfd); if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { // File if ((!checkAllFilesInDir || Path::acceptFile(fname, extra)) && !ignored.Match(fname)) { const std::string nativename = Path::fromNativeSeparators(fname); // Limitation: file sizes are assumed to fit in a 'size_t' #ifdef _WIN64 files[nativename] = (static_cast<std::size_t>(ffd.nFileSizeHigh) << 32) | ffd.nFileSizeLow; #else files[nativename] = ffd.nFileSizeLow; #endif } } else { // Directory if (!ignored.Match(fname)) FileLister::recursiveAddFiles(files, fname, extra, ignored); } } while (FindNextFileA(hFind, &ffd) != FALSE); FindClose(hFind); }
void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path) { // oss is the search string passed into FindFirst and FindNext. // bdir is the base directory which is used to form pathnames. // It always has a trailing backslash available for concatenation. std::ostringstream bdir, oss; std::string cleanedPath = Path::toNativeSeparators(path); oss << cleanedPath; if (MyIsDirectory(cleanedPath)) { char c = cleanedPath[ cleanedPath.size()-1 ]; switch (c) { case '\\': oss << '*'; bdir << cleanedPath; break; case '*': bdir << cleanedPath.substr(0, cleanedPath.length() - 1); break; default: oss << "\\*"; if (cleanedPath != ".") bdir << cleanedPath << '\\'; } } else { std::string::size_type pos = cleanedPath.find_last_of('\\'); if (std::string::npos != pos) { bdir << cleanedPath.substr(0, pos + 1); } } WIN32_FIND_DATAA ffd; HANDLE hFind = MyFindFirstFile(oss.str(), &ffd); if (INVALID_HANDLE_VALUE == hFind) return; do { if (ffd.cFileName[0] == '.' || ffd.cFileName[0] == '\0') continue; const char* ansiFfd = ffd.cFileName; if (strchr(ansiFfd,'?')) { ansiFfd = ffd.cAlternateFileName; } std::ostringstream fname; fname << bdir.str() << ansiFfd; if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { // File // If recursive is not used, accept all files given by user if (Path::sameFileName(path, ansiFfd) || Path::acceptFile(ansiFfd)) { const std::string nativename = Path::fromNativeSeparators(fname.str()); // Limitation: file sizes are assumed to fit in a 'size_t' #ifdef _WIN64 files[nativename] = (static_cast<std::size_t>(ffd.nFileSizeHigh) << 32) | ffd.nFileSizeLow; #else files[nativename] = ffd.nFileSizeLow; #endif } } else { // Directory FileLister::recursiveAddFiles(files, fname.str()); } } while (FindNextFileA(hFind, &ffd) != FALSE); if (INVALID_HANDLE_VALUE != hFind) { FindClose(hFind); } }