bool_t match_mask(const char* mask, const char* str) { if (mask && str) { bool_t matched = 0; bool_t done = 0; while (!done) { if (*mask == '*') // 0 or more characters { ++mask; if (*mask == 0) { matched = 1; } else { matched = match_mask(mask, str); while ((!matched) && (*str != 0)) { ++str; matched = match_mask(mask, str); } } done = 1; } else if (*mask == 0) // mask is over { matched = (*str == 0) ? 1 : 0; done = 1; } else { if ( (*mask == *str) || // exact match, case-sensitive ((*mask == '?') && (*str != 0)) ) // any character { ++mask; ++str; } else { matched = 0; done = 1; } } } return matched; } return 0; }
bool OSDir::linuxAddFileStack(std::string pathName, std::string fileMask, bool bRecursive) { #ifdef _WIN32 // quell warnings if (!bRecursive) { fileMask.size(); pathName.size(); } return false; #else DIR *directory; dirent *fileInfo; struct stat statbuf; char searchstr[1024]; std::string FilePath; strcpy(searchstr, pathName.c_str()); if (searchstr[strlen(searchstr) - 1] != '/') strcat(searchstr, "/"); directory = opendir(searchstr); if (!directory) return false; // TODO: make it use the filemask while ((fileInfo = readdir(directory))) { if (!((strcmp(fileInfo->d_name, ".") == 0) || (strcmp(fileInfo->d_name, "..") == 0))) { FilePath = searchstr; FilePath += fileInfo->d_name; strcpy(fileInfo->d_name, TextUtils::toupper(fileInfo->d_name).c_str()); stat(FilePath.c_str(), &statbuf); if (S_ISDIR(statbuf.st_mode) && bRecursive) linuxAddFileStack(FilePath,fileMask,bRecursive); else if (match_mask(fileMask.c_str(), fileInfo->d_name)) info->nameList.push_back(FilePath); } } closedir(directory); return true; #endif// !Win32 }
bool LinuxAddFileStack(const char *szPathName, const char* fileMask, bool bRecursive, std::vector<std::string> &list, bool justDirs = false) { DIR *directory; dirent *fileInfo; struct stat statbuf; char searchstr[1024]; std::string FilePath; strcpy(searchstr, szPathName); if (searchstr[strlen(searchstr) - 1] != '/') strcat(searchstr, "/"); directory = opendir(searchstr); if (!directory) return false; // TODO: make it use the filemask while ((fileInfo = readdir(directory))) { if (!((strcmp(fileInfo->d_name, ".") == 0) || (strcmp(fileInfo->d_name, "..") == 0))) { FilePath = searchstr; FilePath += fileInfo->d_name; stat(FilePath.c_str(), &statbuf); if (justDirs && S_ISDIR(statbuf.st_mode)) { // we never do just dirs recrusively list.push_back(FilePath); } else if (!justDirs) { if (S_ISDIR(statbuf.st_mode) && bRecursive) LinuxAddFileStack(FilePath.c_str(),fileMask,bRecursive, list); else if (match_mask (fileMask, fileInfo->d_name)) list.push_back(FilePath); } } } closedir(directory); return true; }