dir.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: dir.h
  3. // Purpose: interface of wxDir and wxDirTraverser
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. Possible return values of wxDirTraverser callback functions.
  9. */
  10. enum wxDirTraverseResult
  11. {
  12. wxDIR_IGNORE = -1, ///< Ignore this directory but continue with others.
  13. wxDIR_STOP, ///< Stop traversing.
  14. wxDIR_CONTINUE ///< Continue into this directory.
  15. };
  16. /**
  17. @class wxDirTraverser
  18. wxDirTraverser is an abstract interface which must be implemented by
  19. objects passed to wxDir::Traverse() function.
  20. Example of use (this works almost like wxDir::GetAllFiles()):
  21. @code
  22. class wxDirTraverserSimple : public wxDirTraverser
  23. {
  24. public:
  25. wxDirTraverserSimple(wxArrayString& files) : m_files(files) { }
  26. virtual wxDirTraverseResult OnFile(const wxString& filename)
  27. {
  28. m_files.Add(filename);
  29. return wxDIR_CONTINUE;
  30. }
  31. virtual wxDirTraverseResult OnDir(const wxString& WXUNUSED(dirname))
  32. {
  33. return wxDIR_CONTINUE;
  34. }
  35. private:
  36. wxArrayString& m_files;
  37. };
  38. // get the names of all files in the array
  39. wxArrayString files;
  40. wxDirTraverserSimple traverser(files);
  41. wxDir dir(dirname);
  42. dir.Traverse(traverser);
  43. @endcode
  44. @library{wxbase}
  45. @category{file}
  46. */
  47. class wxDirTraverser
  48. {
  49. public:
  50. /**
  51. This function is called for each directory. It may return ::wxDIR_STOP
  52. to abort traversing completely, ::wxDIR_IGNORE to skip this directory
  53. but continue with others or ::wxDIR_CONTINUE to enumerate all files and
  54. subdirectories in this directory.
  55. This is a pure virtual function and must be implemented in the derived
  56. class.
  57. */
  58. virtual wxDirTraverseResult OnDir(const wxString& dirname) = 0;
  59. /**
  60. This function is called for each file. It may return ::wxDIR_STOP to
  61. abort traversing (for example, if the file being searched is found) or
  62. ::wxDIR_CONTINUE to proceed.
  63. This is a pure virtual function and must be implemented in the derived
  64. class.
  65. */
  66. virtual wxDirTraverseResult OnFile(const wxString& filename) = 0;
  67. /**
  68. This function is called for each directory which we failed to open for
  69. enumerating. It may return ::wxDIR_STOP to abort traversing completely,
  70. ::wxDIR_IGNORE to skip this directory but continue with others or
  71. ::wxDIR_CONTINUE to retry opening this directory once again.
  72. The base class version always returns ::wxDIR_IGNORE.
  73. */
  74. virtual wxDirTraverseResult OnOpenError(const wxString& openerrorname);
  75. };
  76. /**
  77. These flags affect the behaviour of GetFirst/GetNext() and Traverse(),
  78. determining what types are included in the list of items they produce.
  79. */
  80. enum wxDirFlags
  81. {
  82. wxDIR_FILES = 0x0001, ///< Includes files.
  83. wxDIR_DIRS = 0x0002, ///< Includes directories.
  84. wxDIR_HIDDEN = 0x0004, ///< Includes hidden files.
  85. wxDIR_DOTDOT = 0x0008, ///< Includes "." and "..".
  86. /**
  87. Don't follow symbolic links during the directory traversal.
  88. This flag is ignored under systems not supporting symbolic links (i.e.
  89. non-Unix ones).
  90. Notice that this flag is @e not included in wxDIR_DEFAULT and so the
  91. default behaviour of wxDir::Traverse() is to follow symbolic links,
  92. even if they lead outside of the directory being traversed.
  93. @since 2.9.5
  94. */
  95. wxDIR_NO_FOLLOW = 0x0010,
  96. /**
  97. Default directory traversal flags include both files and directories,
  98. even hidden.
  99. Notice that by default wxDIR_NO_FOLLOW is @e not included, meaning that
  100. symbolic links are followed by default. If this is not desired, you
  101. must pass that flag explicitly.
  102. */
  103. wxDIR_DEFAULT = wxDIR_FILES | wxDIR_DIRS | wxDIR_HIDDEN
  104. };
  105. /**
  106. @class wxDir
  107. wxDir is a portable equivalent of Unix open/read/closedir functions which
  108. allow enumerating of the files in a directory. wxDir allows to enumerate
  109. files as well as directories.
  110. wxDir also provides a flexible way to enumerate files recursively using
  111. Traverse() or a simpler GetAllFiles() function.
  112. Example of use:
  113. @code
  114. wxDir dir(wxGetCwd());
  115. if ( !dir.IsOpened() )
  116. {
  117. // deal with the error here - wxDir would already log an error message
  118. // explaining the exact reason of the failure
  119. return;
  120. }
  121. puts("Enumerating object files in current directory:");
  122. wxString filename;
  123. bool cont = dir.GetFirst(&filename, filespec, flags);
  124. while ( cont )
  125. {
  126. printf("%s\n", filename.c_str());
  127. cont = dir.GetNext(&filename);
  128. }
  129. @endcode
  130. @library{wxbase}
  131. @category{file}
  132. */
  133. class wxDir
  134. {
  135. public:
  136. /**
  137. Default constructor, use Open() afterwards.
  138. */
  139. wxDir();
  140. /**
  141. Opens the directory for enumeration, use IsOpened() to test for errors.
  142. */
  143. wxDir(const wxString& dir);
  144. /**
  145. Destructor cleans up the associated resources by calling Close().
  146. It is not virtual and so this class is not meant to be used
  147. polymorphically.
  148. */
  149. ~wxDir();
  150. /**
  151. Close the directory.
  152. The object can't be used after closing it, but Open() may be called
  153. again to reopen it later.
  154. @since 2.9.5
  155. */
  156. void Close();
  157. /**
  158. Test for existence of a directory with the given name.
  159. */
  160. static bool Exists(const wxString& dir);
  161. /**
  162. The function returns the path of the first file matching the given
  163. @a filespec or an empty string if there are no files matching it.
  164. The @a flags parameter may or may not include ::wxDIR_FILES, the
  165. function always behaves as if it were specified. By default, @a flags
  166. includes ::wxDIR_DIRS and so the function recurses into the
  167. subdirectories but if this flag is not specified, the function
  168. restricts the search only to the directory @a dirname itself.
  169. See ::wxDirFlags for the list of the possible flags.
  170. @see Traverse()
  171. */
  172. static wxString FindFirst(const wxString& dirname,
  173. const wxString& filespec,
  174. int flags = wxDIR_DEFAULT);
  175. /**
  176. The function appends the names of all the files under directory
  177. @a dirname to the array @a files (note that its old content is
  178. preserved). Only files matching the @a filespec are taken, with empty
  179. spec matching all the files.
  180. The @a flags parameter should always include ::wxDIR_FILES or the array
  181. would be unchanged and should include ::wxDIR_DIRS flag to recurse into
  182. subdirectories (both flags are included in the value by default).
  183. See ::wxDirFlags for the list of the possible flags.
  184. @return Returns the total number of files found while traversing
  185. the directory @a dirname (i.e. the number of entries appended
  186. to the @a files array).
  187. @see Traverse()
  188. */
  189. static size_t GetAllFiles(const wxString& dirname, wxArrayString* files,
  190. const wxString& filespec = wxEmptyString,
  191. int flags = wxDIR_DEFAULT);
  192. /**
  193. Start enumerating all files matching @a filespec (or all files if it is
  194. empty) and @e flags, return @true on success.
  195. See ::wxDirFlags for the list of the possible flags.
  196. */
  197. bool GetFirst(wxString* filename,
  198. const wxString& filespec = wxEmptyString,
  199. int flags = wxDIR_DEFAULT) const;
  200. /**
  201. Returns the name of the directory itself.
  202. The returned string does not have the trailing path separator (slash or
  203. backslash).
  204. Notice that in spite of this the last character of the returned string
  205. can still be the path separator if this directory is the root one.
  206. Because of this, don't append @c wxFILE_SEP_PATH to the returned value
  207. if you do need a slash-terminated directory name but use
  208. GetNameWithSep() instead to avoid having duplicate consecutive slashes.
  209. */
  210. wxString GetName() const;
  211. /**
  212. Returns the name of the directory with the path separator appended.
  213. The last character of the returned string is always @c wxFILE_SEP_PATH
  214. unless the string is empty, indicating that this directory is invalid.
  215. @see GetName()
  216. @since 2.9.4
  217. */
  218. wxString GetNameWithSep() const;
  219. /**
  220. Continue enumerating files which satisfy the criteria specified by the
  221. last call to GetFirst().
  222. */
  223. bool GetNext(wxString* filename) const;
  224. /**
  225. Returns the size (in bytes) of all files recursively found in @c dir or
  226. @c wxInvalidSize in case of error.
  227. In case it happens that while traversing folders a file's size cannot
  228. be read, that file is added to the @a filesSkipped array, if not @NULL,
  229. and then skipped. This usually happens with some special folders which
  230. are locked by the operating system or by another process. Remember that
  231. when the size of @a filesSkipped is not zero, then the returned value
  232. is not 100% accurate and, if the skipped files were big, it could be
  233. far from real size of the directory.
  234. @see wxFileName::GetHumanReadableSize(), wxGetDiskSpace()
  235. */
  236. static wxULongLong GetTotalSize(const wxString& dir,
  237. wxArrayString* filesSkipped = NULL);
  238. /**
  239. Returns @true if the directory contains any files matching the given
  240. @a filespec. If @a filespec is empty, look for any files at all. In any
  241. case, even hidden files are taken into account.
  242. */
  243. bool HasFiles(const wxString& filespec = wxEmptyString) const;
  244. /**
  245. Returns @true if the directory contains any subdirectories (if a non
  246. empty @a filespec is given, only check for directories matching it).
  247. The hidden subdirectories are taken into account as well.
  248. */
  249. bool HasSubDirs(const wxString& dirspec = wxEmptyString) const;
  250. /**
  251. Returns @true if the directory was successfully opened by a previous
  252. call to Open().
  253. */
  254. bool IsOpened() const;
  255. /**
  256. Creates a directory.
  257. This is just an alias for wxFileName::Mkdir(); refer to that function
  258. for more info.
  259. */
  260. static bool Make(const wxString &dir, int perm = wxS_DIR_DEFAULT,
  261. int flags = 0);
  262. /**
  263. Open the directory for enumerating, returns @true on success or @false
  264. if an error occurred.
  265. */
  266. bool Open(const wxString& dir);
  267. /**
  268. Removes a directory.
  269. This is just an alias for wxFileName::Rmdir(); refer to that function
  270. for more info.
  271. */
  272. static bool Remove(const wxString &dir, int flags = 0);
  273. /**
  274. Enumerate all files and directories under the given directory.
  275. If @a flags contains ::wxDIR_DIRS this enumeration is recursive, i.e.
  276. all the subdirectories of the given one and the files inside them will
  277. be traversed. Otherwise only the files in this directory itself are.
  278. If @a flags doesn't contain ::wxDIR_FILES then only subdirectories are
  279. examined but not normal files. It doesn't make sense to not specify
  280. either ::wxDIR_DIRS or ::wxDIR_FILES and usually both of them should be
  281. specified, as is the case by default.
  282. For each directory found, @ref wxDirTraverser::OnDir() "sink.OnDir()"
  283. is called and @ref wxDirTraverser::OnFile() "sink.OnFile()" is called
  284. for every file. Depending on the return value, the enumeration may
  285. continue or stop. If entering a subdirectory fails, @ref
  286. wxDirTraverser::OnOpenError() "sink.OnOpenError()" is called.
  287. The function returns the total number of files found or @c "(size_t)-1"
  288. on error.
  289. See ::wxDirFlags for the full list of the possible flags.
  290. @see GetAllFiles()
  291. */
  292. size_t Traverse(wxDirTraverser& sink,
  293. const wxString& filespec = wxEmptyString,
  294. int flags = wxDIR_DEFAULT) const;
  295. };