static void add_directory(std::string directory, bool recursively = false){ push_directory(directory); if(recursively){ add_recursively(directory); } }
void files::exclude( const std::string &path, const std::string &mask, bool scan_subdirs ) // subtraction: beware duplicates (set!) { files target, to_remove; add_recursively( to_remove, path, mask, scan_subdirs ); for( const_iterator it = this->begin(); it != this->end(); ++it ) if( to_remove.find( file( it->name() ) ) == to_remove.end() ) target.insert( file( it->name() ) ); std::swap( *this, target ); }
static void add_recursively(std::string parent) { if(is_dir(parent)){ push_directory(parent); if (DIR *dp = opendir(parent.c_str())) { while (struct dirent *ep = readdir(dp)) if (ep->d_name[0] != '.') add_recursively(parent + "/" + ep->d_name); closedir(dp); } else throw std::runtime_error(parent + std::string(": Couldn't open the directory.")); } }
bool files::add_recursively( files &self, const std::string &sDir, const std::string &mask, bool recursive ) { #if defined(_WIN32) WIN32_FIND_DATAA fdFile; HANDLE hFind = NULL; std::string sPath = sDir + "\\" + mask; if((hFind = FindFirstFileA(sPath.c_str(), &fdFile)) == INVALID_HANDLE_VALUE) { std::cout << "Path not found: " << sDir << std::endl; return false; } do { // Ignore . and .. folders if(strcmp(fdFile.cFileName, ".") != 0 && strcmp(fdFile.cFileName, "..") != 0) { // Rebuild path sPath = std::string(sDir) + "\\" + std::string(fdFile.cFileName); // Scan recursively if needed if( fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { if( recursive ) add_recursively( self, sPath, mask, recursive ); else self.insert( file( sPath ) ); } else { self.insert( file( sPath ) ); } } } while( FindNextFileA( hFind, &fdFile ) ); FindClose( hFind ); return true; #else moon9::iostring out; FILE *fp; if( recursive) fp = popen( moon9::iostring( "find \1 -type d -or -type f -name '\2'", sDir, mask ).c_str(), "r" ); else fp = popen( moon9::iostring( "find \1 -maxdepth 1 -type d -or -type f -name '\2'", sDir, mask ).c_str(), "r" ); if( fp ) { while( !feof(fp) ) out << (unsigned char)(fgetc(fp)); fclose(fp); } moon9::iostrings found = out.tokenize("\n\r"); for( size_t i = 0; i < found.size(); ++i ) { file entry( found[i] ); if( entry.exist() ) self.insert( entry ); } return true; #endif }
void files::include( const std::string &path, const std::string &mask, bool scan_subdirs ) // addition: beware duplicates (set!) { add_recursively( *this, path, mask, scan_subdirs ); }