filelist.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "dirut.h"
  2. #include "filelist.h"
  3. #include "iterator.h"
  4. #include <util/system/defaults.h>
  5. void TFileEntitiesList::Fill(const TString& dirname, TStringBuf prefix, TStringBuf suffix, int depth, bool sort) {
  6. TDirIterator::TOptions opts;
  7. opts.SetMaxLevel(depth);
  8. if (sort) {
  9. opts.SetSortByName();
  10. }
  11. TDirIterator dir(dirname, opts);
  12. Clear();
  13. size_t dirNameLength = dirname.length();
  14. while (dirNameLength && (dirname[dirNameLength - 1] == '\\' || dirname[dirNameLength - 1] == '/')) {
  15. --dirNameLength;
  16. }
  17. for (auto file = dir.begin(); file != dir.end(); ++file) {
  18. if (file->fts_pathlen == file->fts_namelen || file->fts_pathlen <= dirNameLength) {
  19. continue;
  20. }
  21. TStringBuf filename = file->fts_path + dirNameLength + 1;
  22. if (filename.empty() || !filename.StartsWith(prefix) || !filename.EndsWith(suffix)) {
  23. continue;
  24. }
  25. if (((Mask & EM_FILES) && file->fts_info == FTS_F) || ((Mask & EM_DIRS) && file->fts_info == FTS_D) || ((Mask & EM_SLINKS) && file->fts_info == FTS_SL)) {
  26. ++FileNamesSize;
  27. FileNames.Append(filename.data(), filename.size() + 1);
  28. }
  29. }
  30. Restart();
  31. }