int File::listFiles(FileList &fileList) { fileList.clear(); if (isDirectory() == false) return 0; string dir = getName(); #if defined(WIN32) string findDir = dir + "\\*.*"; WIN32_FIND_DATA fd; HANDLE hFind; hFind = FindFirstFile(findDir.c_str(), &fd); if (hFind != INVALID_HANDLE_VALUE) { do{ string findFilename = fd.cFileName; if (findFilename.compare(".") != 0 && findFilename.compare("..") != 0) { File *file = new File(dir.c_str(), findFilename.c_str()); fileList.add(file); } } while(FindNextFile(hFind,&fd) != FALSE); } FindClose(hFind); #elif defined(BTRON) int fd = open(dir.c_str(), O_RDONLY); if( fd == -1 ) return fileList.size(); char buf[1024] ; int cnt; while(0 < (cnt = u_getdents(fd, (dirent *)buf, sizeof(buf)))) { for(struct dirent *p = (struct dirent *)buf ; (char *)p < &buf[cnt]; p=(struct dirent *) ((int)p+(p->d_reclen)) ) { File *file = new File(dir.c_str(), p->d_name); fileList.add(file); } } close(fd); #elif !defined(ITRON) && !defined(TENGINE) struct dirent **namelist; int n = scandir(dir.c_str(), &namelist, 0, alphasort); if (0 <= n) { while(n--) { string filename = namelist[n]->d_name; if (filename.compare(".") != 0 && filename.compare("..") != 0) { // Thanks for Pekka Virtanen <*****@*****.**> and Bastiaan Van Eeckhoudt <*****@*****.**> File *file = new File(dir.c_str(), filename.c_str()); fileList.add(file); } free(namelist[n]); } free(namelist); } #endif return fileList.size(); }
static ibool loaddirectories(const char *filename,FileList& fileList) /**************************************************************************** * * Function: loaddirectories * Parameters: filename - Name of directories to look for * fileList - Place to store the filenames * Returns: True on success, false on memory error * * Description: Loads a list of all the directories from the current * directory into the specified name list. * ****************************************************************************/ { PM_findData findData; void *hfile; ibool valid; valid = (hfile = PM_findFirstFile(filename,&findData)) != NULL; while (valid) { if ((findData.attrib & PM_FILE_DIRECTORY) && findData.name[0] != '.') { fileList.add(new TCDynStr(findData.name)); if (MV_lowMemory()) return false; } valid = PM_findNextFile(hfile,&findData); } if (hfile) PM_findClose(hfile); return true; }
int glob(const std::string& regex, FileList& result, bool recursive=true) { std::regex e(regex); if (dir && APR_SUCCESS == check_apr(apr_dir_open(&dir, dirname.c_str(), mPool))) { // iterate over directory: while (APR_SUCCESS == (apr_dir_read(&dirent, APR_FINFO_TYPE|APR_FINFO_NAME, dir))) { //printf("test %s %s\n", dirname.c_str(), dirent.name); if (dirent.filetype == APR_REG && dirent.name && std::regex_match(dirname+dirent.name,e) ) { FilePath res; res.file(dirent.name); res.path(dirname); result.add(res); } else if (recursive && dirent.filetype == APR_DIR && dirent.name && dirent.name[0] != '.') { Path path(dirname + dirent.name + AL_FILE_DELIMITER); path.glob(regex, result, true); } } } else { AL_WARN("couldn't open directory %s", dirname.c_str()); } return result.count(); }