filelist.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #pragma once
  2. #include <util/generic/buffer.h>
  3. #include <util/generic/string.h>
  4. #include <util/generic/strbuf.h>
  5. #include <util/generic/flags.h>
  6. class TFileEntitiesList {
  7. public:
  8. enum EMaskFlag {
  9. EM_FILES = 1,
  10. EM_DIRS = 2,
  11. EM_SLINKS = 4,
  12. EM_FILES_DIRS = EM_FILES | EM_DIRS,
  13. EM_FILES_SLINKS = EM_FILES | EM_SLINKS,
  14. EM_DIRS_SLINKS = EM_DIRS | EM_SLINKS,
  15. EM_FILES_DIRS_SLINKS = EM_FILES | EM_DIRS | EM_SLINKS
  16. };
  17. Y_DECLARE_FLAGS(EMask, EMaskFlag);
  18. TFileEntitiesList(EMask mask)
  19. : Mask(mask)
  20. {
  21. Clear();
  22. }
  23. void Clear() {
  24. Cur = nullptr;
  25. FileNamesSize = CurName = 0;
  26. FileNames.Clear();
  27. FileNames.Append("", 1);
  28. }
  29. const char* Next() {
  30. return Cur = (CurName++ < FileNamesSize ? strchr(Cur, 0) + 1 : nullptr);
  31. }
  32. size_t Size() {
  33. return FileNamesSize;
  34. }
  35. inline void Fill(const TString& dirname, bool sort = false) {
  36. Fill(dirname, TStringBuf(), sort);
  37. }
  38. inline void Fill(const TString& dirname, TStringBuf prefix, bool sort = false) {
  39. Fill(dirname, prefix, TStringBuf(), 1, sort);
  40. }
  41. void Fill(const TString& dirname, TStringBuf prefix, TStringBuf suffix, int depth, bool sort = false);
  42. void Restart() {
  43. Cur = FileNames.Data();
  44. CurName = 0;
  45. }
  46. protected:
  47. TBuffer FileNames;
  48. size_t FileNamesSize, CurName;
  49. const char* Cur;
  50. EMask Mask;
  51. };
  52. Y_DECLARE_OPERATORS_FOR_FLAGS(TFileEntitiesList::EMask);
  53. class TFileList: public TFileEntitiesList {
  54. public:
  55. TFileList()
  56. : TFileEntitiesList(TFileEntitiesList::EM_FILES)
  57. {
  58. }
  59. };
  60. class TDirsList: public TFileEntitiesList {
  61. public:
  62. TDirsList()
  63. : TFileEntitiesList(TFileEntitiesList::EM_DIRS)
  64. {
  65. }
  66. };