dir.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/dir.h
  3. // Purpose: wxDir is a class for enumerating the files in a directory
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 08.12.99
  7. // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_DIR_H_
  11. #define _WX_DIR_H_
  12. #include "wx/longlong.h"
  13. #include "wx/string.h"
  14. #include "wx/filefn.h" // for wxS_DIR_DEFAULT
  15. class WXDLLIMPEXP_FWD_BASE wxArrayString;
  16. // ----------------------------------------------------------------------------
  17. // constants
  18. // ----------------------------------------------------------------------------
  19. // These flags affect the behaviour of GetFirst/GetNext() and Traverse().
  20. // They define what types are included in the list of items they produce.
  21. // Note that wxDIR_NO_FOLLOW is relevant only on Unix and ignored under systems
  22. // not supporting symbolic links.
  23. enum wxDirFlags
  24. {
  25. wxDIR_FILES = 0x0001, // include files
  26. wxDIR_DIRS = 0x0002, // include directories
  27. wxDIR_HIDDEN = 0x0004, // include hidden files
  28. wxDIR_DOTDOT = 0x0008, // include '.' and '..'
  29. wxDIR_NO_FOLLOW = 0x0010, // don't dereference any symlink
  30. // by default, enumerate everything except '.' and '..'
  31. wxDIR_DEFAULT = wxDIR_FILES | wxDIR_DIRS | wxDIR_HIDDEN
  32. };
  33. // these constants are possible return value of wxDirTraverser::OnDir()
  34. enum wxDirTraverseResult
  35. {
  36. wxDIR_IGNORE = -1, // ignore this directory but continue with others
  37. wxDIR_STOP, // stop traversing
  38. wxDIR_CONTINUE // continue into this directory
  39. };
  40. // ----------------------------------------------------------------------------
  41. // wxDirTraverser: helper class for wxDir::Traverse()
  42. // ----------------------------------------------------------------------------
  43. class WXDLLIMPEXP_BASE wxDirTraverser
  44. {
  45. public:
  46. /// a virtual dtor has been provided since this class has virtual members
  47. virtual ~wxDirTraverser() { }
  48. // called for each file found by wxDir::Traverse()
  49. //
  50. // return wxDIR_STOP or wxDIR_CONTINUE from here (wxDIR_IGNORE doesn't
  51. // make sense)
  52. virtual wxDirTraverseResult OnFile(const wxString& filename) = 0;
  53. // called for each directory found by wxDir::Traverse()
  54. //
  55. // return one of the enum elements defined above
  56. virtual wxDirTraverseResult OnDir(const wxString& dirname) = 0;
  57. // called for each directory which we couldn't open during our traversal
  58. // of the directory tree
  59. //
  60. // this method can also return either wxDIR_STOP, wxDIR_IGNORE or
  61. // wxDIR_CONTINUE but the latter is treated specially: it means to retry
  62. // opening the directory and so may lead to infinite loop if it is
  63. // returned unconditionally, be careful with this!
  64. //
  65. // the base class version always returns wxDIR_IGNORE
  66. virtual wxDirTraverseResult OnOpenError(const wxString& dirname);
  67. };
  68. // ----------------------------------------------------------------------------
  69. // wxDir: portable equivalent of {open/read/close}dir functions
  70. // ----------------------------------------------------------------------------
  71. class WXDLLIMPEXP_FWD_BASE wxDirData;
  72. class WXDLLIMPEXP_BASE wxDir
  73. {
  74. public:
  75. // ctors
  76. // -----
  77. // default, use Open()
  78. wxDir() { m_data = NULL; }
  79. // opens the directory for enumeration, use IsOpened() to test success
  80. wxDir(const wxString& dir);
  81. // dtor calls Close() automatically
  82. ~wxDir() { Close(); }
  83. // open the directory for enumerating
  84. bool Open(const wxString& dir);
  85. // close the directory, Open() can be called again later
  86. void Close();
  87. // returns true if the directory was successfully opened
  88. bool IsOpened() const;
  89. // get the full name of the directory (without '/' at the end)
  90. wxString GetName() const;
  91. // Same as GetName() but does include the trailing separator, unless the
  92. // string is empty (only for invalid directories).
  93. wxString GetNameWithSep() const;
  94. // file enumeration routines
  95. // -------------------------
  96. // start enumerating all files matching filespec (or all files if it is
  97. // empty) and flags, return true on success
  98. bool GetFirst(wxString *filename,
  99. const wxString& filespec = wxEmptyString,
  100. int flags = wxDIR_DEFAULT) const;
  101. // get next file in the enumeration started with GetFirst()
  102. bool GetNext(wxString *filename) const;
  103. // return true if this directory has any files in it
  104. bool HasFiles(const wxString& spec = wxEmptyString) const;
  105. // return true if this directory has any subdirectories
  106. bool HasSubDirs(const wxString& spec = wxEmptyString) const;
  107. // enumerate all files in this directory and its subdirectories
  108. //
  109. // return the number of files found
  110. size_t Traverse(wxDirTraverser& sink,
  111. const wxString& filespec = wxEmptyString,
  112. int flags = wxDIR_DEFAULT) const;
  113. // simplest version of Traverse(): get the names of all files under this
  114. // directory into filenames array, return the number of files
  115. static size_t GetAllFiles(const wxString& dirname,
  116. wxArrayString *files,
  117. const wxString& filespec = wxEmptyString,
  118. int flags = wxDIR_DEFAULT);
  119. // check if there any files matching the given filespec under the given
  120. // directory (i.e. searches recursively), return the file path if found or
  121. // empty string otherwise
  122. static wxString FindFirst(const wxString& dirname,
  123. const wxString& filespec,
  124. int flags = wxDIR_DEFAULT);
  125. #if wxUSE_LONGLONG
  126. // returns the size of all directories recursively found in given path
  127. static wxULongLong GetTotalSize(const wxString &dir, wxArrayString *filesSkipped = NULL);
  128. #endif // wxUSE_LONGLONG
  129. // static utilities for directory management
  130. // (alias to wxFileName's functions for dirs)
  131. // -----------------------------------------
  132. // test for existence of a directory with the given name
  133. static bool Exists(const wxString& dir);
  134. static bool Make(const wxString &dir, int perm = wxS_DIR_DEFAULT,
  135. int flags = 0);
  136. static bool Remove(const wxString &dir, int flags = 0);
  137. private:
  138. friend class wxDirData;
  139. wxDirData *m_data;
  140. wxDECLARE_NO_COPY_CLASS(wxDir);
  141. };
  142. #endif // _WX_DIR_H_