filefn.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: filefn.h
  3. // Purpose: interface of wxPathList and file functions
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxPathList
  9. The path list is a convenient way of storing a number of directories, and
  10. when presented with a filename without a directory, searching for an
  11. existing file in those directories.
  12. Be sure to look also at wxStandardPaths if you only want to search files in
  13. some standard paths.
  14. @library{wxbase}
  15. @category{file}
  16. @see wxArrayString, wxStandardPaths, wxFileName
  17. */
  18. class wxPathList : public wxArrayString
  19. {
  20. public:
  21. /**
  22. Standard constructor.
  23. */
  24. wxPathList();
  25. /**
  26. Constructs the object calling the Add() function.
  27. */
  28. wxPathList(const wxArrayString& arr);
  29. /**
  30. Adds the given directory to the path list, if the @a path is not already in the list.
  31. If the path cannot be normalized for some reason, it returns @false.
  32. The @a path is always considered to be a directory but no existence checks will be
  33. done on it (because if it doesn't exist, it could be created later and thus result a
  34. valid path when FindValidPath() is called).
  35. @note if the given path is relative, it won't be made absolute before adding it
  36. (this is why FindValidPath() may return relative paths).
  37. */
  38. bool Add(const wxString& path);
  39. /**
  40. Adds all elements of the given array as paths.
  41. */
  42. void Add(const wxArrayString& arr);
  43. /**
  44. Finds the value of the given environment variable, and adds all paths
  45. to the path list.
  46. Useful for finding files in the @c PATH variable, for example.
  47. */
  48. void AddEnvList(const wxString& env_variable);
  49. /**
  50. Given a full filename (with path), calls Add() with the path of the file.
  51. */
  52. bool EnsureFileAccessible(const wxString& filename);
  53. /**
  54. Like FindValidPath() but this function always returns an absolute path
  55. (eventually prepending the current working directory to the value returned
  56. wxPathList::FindValidPath()) or an empty string.
  57. */
  58. wxString FindAbsoluteValidPath(const wxString& file) const;
  59. /**
  60. Searches the given file in all paths stored in this class.
  61. The first path which concatenated to the given string points to an existing
  62. file (see wxFileExists()) is returned.
  63. If the file wasn't found in any of the stored paths, an empty string is returned.
  64. The given string must be a file name, eventually with a path prefix (if the path
  65. prefix is absolute, only its name will be searched); i.e. it must not end with
  66. a directory separator (see wxFileName::GetPathSeparator) otherwise an assertion
  67. will fail.
  68. The returned path may be relative to the current working directory.
  69. Note in fact that wxPathList can be used to store both relative and absolute
  70. paths so that if you added relative paths, then the current working directory
  71. (see wxGetCwd() and wxSetWorkingDirectory()) may affect the value returned
  72. by this function!
  73. */
  74. wxString FindValidPath(const wxString& file) const;
  75. };
  76. // ============================================================================
  77. // Global functions/macros
  78. // ============================================================================
  79. /** @addtogroup group_funcmacro_file */
  80. //@{
  81. /**
  82. A special return value of many wxWidgets classes to indicate that
  83. an invalid offset was given.
  84. */
  85. const int wxInvalidOffset = -1;
  86. /**
  87. The type used to store and provide byte offsets or byte sizes for files or streams.
  88. This type is usually just a synonym for @c off_t but can be defined as
  89. @c wxLongLong_t if @c wxHAS_HUGE_FILES is defined but @c off_t is only 32
  90. bits.
  91. */
  92. typedef off_t wxFileOffset;
  93. /**
  94. Under Unix this macro changes the current process umask to the given value,
  95. unless it is equal to -1 in which case nothing is done, and restores it to
  96. the original value on scope exit. It works by declaring a variable which
  97. sets umask to @a mask in its constructor and restores it in its destructor.
  98. Under other platforms this macro expands to nothing.
  99. @header{wx/filefn.h}
  100. */
  101. #define wxCHANGE_UMASK(mask)
  102. /**
  103. This function returns the total number of bytes and number of free bytes on
  104. the disk containing the directory @a path (it should exist). Both @a total
  105. and @a free parameters may be @NULL if the corresponding information is not
  106. needed.
  107. @since 2.3.2
  108. @note The generic Unix implementation depends on the system having the
  109. @c statfs() or @c statvfs() function.
  110. @return @true on success, @false if an error occurred (for example, the
  111. directory doesn’t exist).
  112. @header{wx/filefn.h}
  113. */
  114. bool wxGetDiskSpace(const wxString& path,
  115. wxLongLong total = NULL,
  116. wxLongLong free = NULL);
  117. /**
  118. Returns the Windows directory under Windows; other platforms return an
  119. empty string.
  120. @header{wx/filefn.h}
  121. */
  122. wxString wxGetOSDirectory();
  123. /**
  124. Parses the @a wildCard, returning the number of filters. Returns 0 if none
  125. or if there's a problem.
  126. The arrays will contain an equal number of items found before the error. On
  127. platforms where native dialogs handle only one filter per entry, entries in
  128. arrays are automatically adjusted. @a wildCard is in the form:
  129. @code
  130. "All files (*)|*|Image Files (*.jpeg *.png)|*.jpg;*.png"
  131. @endcode
  132. @header{wx/filefn.h}
  133. */
  134. int wxParseCommonDialogsFilter(const wxString& wildCard,
  135. wxArrayString& descriptions,
  136. wxArrayString& filters);
  137. /**
  138. Converts a DOS to a Unix filename by replacing backslashes with forward
  139. slashes.
  140. @deprecated
  141. Construct a wxFileName with wxPATH_DOS and then use
  142. wxFileName::GetFullPath(wxPATH_UNIX) instead.
  143. @header{wx/filefn.h}
  144. */
  145. void wxDos2UnixFilename(wxChar* s);
  146. /**
  147. Converts a Unix to a DOS filename by replacing forward slashes with
  148. backslashes.
  149. @deprecated
  150. Construct a wxFileName with wxPATH_UNIX and then use
  151. wxFileName::GetFullPath(wxPATH_DOS) instead.
  152. @header{wx/filefn.h}
  153. */
  154. void wxUnix2DosFilename(wxChar* s);
  155. /**
  156. Returns @true if @a dirname exists and is a directory.
  157. @header{wx/filefn.h}
  158. */
  159. bool wxDirExists(const wxString& dirname);
  160. /**
  161. @deprecated
  162. This function is obsolete, please use wxFileName::SplitPath() instead.
  163. This function splits a full file name into components: the path (including
  164. possible disk/drive specification under Windows), the base name, and the
  165. extension. Any of the output parameters (@a path, @a name or @a ext) may be
  166. @NULL if you are not interested in the value of a particular component.
  167. wxSplitPath() will correctly handle filenames with both DOS and Unix path
  168. separators under Windows, however it will not consider backslashes as path
  169. separators under Unix (where backslash is a valid character in a filename).
  170. On entry, @a fullname should be non-@NULL (it may be empty though).
  171. On return, @a path contains the file path (without the trailing separator),
  172. @a name contains the file name and @c ext contains the file extension
  173. without leading dot. All three of them may be empty if the corresponding
  174. component is. The old contents of the strings pointed to by these
  175. parameters will be overwritten in any case (if the pointers are not @NULL).
  176. @header{wx/filefn.h}
  177. */
  178. void wxSplitPath(const wxString& fullname,
  179. wxString* path,
  180. wxString* name,
  181. wxString* ext);
  182. /**
  183. Returns time of last modification of given file.
  184. The function returns <tt>(time_t)-1</tt> if an error occurred (e.g. file
  185. not found).
  186. @header{wx/filefn.h}
  187. */
  188. time_t wxFileModificationTime(const wxString& filename);
  189. /**
  190. Renames @a file1 to @e file2, returning @true if successful.
  191. If @a file2 is a directory, @a file1 is moved into it (@a overwrite is
  192. ignored in this case). Otherwise, if @a file2 is an existing file, it is
  193. overwritten if @a overwrite is @true (default) and the function fails if @a
  194. overwrite is @false.
  195. @header{wx/filefn.h}
  196. */
  197. bool wxRenameFile(const wxString& file1,
  198. const wxString& file2,
  199. bool overwrite = true);
  200. /**
  201. Copies @a file1 to @e file2, returning @true if successful. If @a overwrite
  202. parameter is @true (default), the destination file is overwritten if it
  203. exists, but if @a overwrite is @false, the functions fails in this case.
  204. This function supports resources forks under Mac OS.
  205. @header{wx/filefn.h}
  206. */
  207. bool wxCopyFile(const wxString& file1,
  208. const wxString& file2,
  209. bool overwrite = true);
  210. /**
  211. Returns @true if the file exists and is a plain file.
  212. @header{wx/filefn.h}
  213. */
  214. bool wxFileExists(const wxString& filename);
  215. /**
  216. Returns @true if the @a pattern matches the @e text; if @a dot_special is
  217. @true, filenames beginning with a dot are not matched with wildcard
  218. characters.
  219. @see wxIsWild()
  220. @header{wx/filefn.h}
  221. */
  222. bool wxMatchWild(const wxString& pattern,
  223. const wxString& text,
  224. bool dot_special);
  225. /**
  226. @deprecated This function is deprecated, use wxGetCwd() instead.
  227. Copies the current working directory into the buffer if supplied, or copies
  228. the working directory into new storage (which you must delete yourself) if
  229. the buffer is @NULL.
  230. @a sz is the size of the buffer if supplied.
  231. @header{wx/filefn.h}
  232. */
  233. wxString wxGetWorkingDirectory(char* buf = NULL, int sz = 1000);
  234. /**
  235. Returns the directory part of the filename.
  236. @header{wx/filefn.h}
  237. */
  238. wxString wxPathOnly(const wxString& path);
  239. /**
  240. Returns @true if the pattern contains wildcards.
  241. @see wxMatchWild()
  242. @header{wx/filefn.h}
  243. */
  244. bool wxIsWild(const wxString& pattern);
  245. /**
  246. Returns @true if the argument is an absolute filename, i.e.\ with a slash
  247. or drive name at the beginning.
  248. @header{wx/filefn.h}
  249. */
  250. bool wxIsAbsolutePath(const wxString& filename);
  251. /**
  252. Returns a string containing the current (or working) directory.
  253. @header{wx/filefn.h}
  254. */
  255. wxString wxGetCwd();
  256. /**
  257. Sets the current working directory, returning @true if the operation
  258. succeeded. Under MS Windows, the current drive is also changed if @a dir
  259. contains a drive specification.
  260. @header{wx/filefn.h}
  261. */
  262. bool wxSetWorkingDirectory(const wxString& dir);
  263. /**
  264. Concatenates @a file1 and @a file2 to @e file3, returning @true if
  265. successful.
  266. @header{wx/filefn.h}
  267. */
  268. bool wxConcatFiles(const wxString& file1,
  269. const wxString& file2,
  270. const wxString& file3);
  271. /**
  272. Removes @e file, returning @true if successful.
  273. @header{wx/filefn.h}
  274. */
  275. bool wxRemoveFile(const wxString& file);
  276. /**
  277. File permission bit names.
  278. We define these constants in wxWidgets because S_IREAD &c are not standard.
  279. However, we do assume that the values correspond to the Unix umask bits.
  280. */
  281. enum wxPosixPermissions
  282. {
  283. /// Standard POSIX names for these permission flags with "wx" prefix.
  284. //@{
  285. wxS_IRUSR = 00400,
  286. wxS_IWUSR = 00200,
  287. wxS_IXUSR = 00100,
  288. wxS_IRGRP = 00040,
  289. wxS_IWGRP = 00020,
  290. wxS_IXGRP = 00010,
  291. wxS_IROTH = 00004,
  292. wxS_IWOTH = 00002,
  293. wxS_IXOTH = 00001,
  294. //@}
  295. /// Longer but more readable synonyms for the constants above.
  296. //@{
  297. wxPOSIX_USER_READ = wxS_IRUSR,
  298. wxPOSIX_USER_WRITE = wxS_IWUSR,
  299. wxPOSIX_USER_EXECUTE = wxS_IXUSR,
  300. wxPOSIX_GROUP_READ = wxS_IRGRP,
  301. wxPOSIX_GROUP_WRITE = wxS_IWGRP,
  302. wxPOSIX_GROUP_EXECUTE = wxS_IXGRP,
  303. wxPOSIX_OTHERS_READ = wxS_IROTH,
  304. wxPOSIX_OTHERS_WRITE = wxS_IWOTH,
  305. wxPOSIX_OTHERS_EXECUTE = wxS_IXOTH,
  306. //@}
  307. /// Default mode for the new files: allow reading/writing them to everybody but
  308. /// the effective file mode will be set after ANDing this value with umask and
  309. /// so won't include wxS_IW{GRP,OTH} for the default 022 umask value
  310. wxS_DEFAULT = (wxPOSIX_USER_READ | wxPOSIX_USER_WRITE | \
  311. wxPOSIX_GROUP_READ | wxPOSIX_GROUP_WRITE | \
  312. wxPOSIX_OTHERS_READ | wxPOSIX_OTHERS_WRITE),
  313. /// Default mode for the new directories (see wxFileName::Mkdir): allow
  314. /// reading/writing/executing them to everybody, but just like wxS_DEFAULT
  315. /// the effective directory mode will be set after ANDing this value with umask
  316. wxS_DIR_DEFAULT = (wxPOSIX_USER_READ | wxPOSIX_USER_WRITE | wxPOSIX_USER_EXECUTE | \
  317. wxPOSIX_GROUP_READ | wxPOSIX_GROUP_WRITE | wxPOSIX_GROUP_EXECUTE | \
  318. wxPOSIX_OTHERS_READ | wxPOSIX_OTHERS_WRITE | wxPOSIX_OTHERS_EXECUTE)
  319. };
  320. /**
  321. Makes the directory @a dir, returning @true if successful.
  322. @a perm is the access mask for the directory for the systems on which it is
  323. supported (Unix) and doesn't have any effect on the other ones.
  324. @header{wx/filefn.h}
  325. */
  326. bool wxMkdir(const wxString& dir, int perm = wxS_DIR_DEFAULT);
  327. /**
  328. Removes the directory @a dir, returning @true if successful. Does not work
  329. under VMS.
  330. The @a flags parameter is reserved for future use.
  331. @note There is also a wxRmDir() function which simply wraps the standard
  332. POSIX @c rmdir() function and so return an integer error code instead
  333. of a boolean value (but otherwise is currently identical to
  334. wxRmdir()), don't confuse these two functions.
  335. @header{wx/filefn.h}
  336. */
  337. bool wxRmdir(const wxString& dir, int flags = 0);
  338. /**
  339. Returns the next file that matches the path passed to wxFindFirstFile().
  340. See wxFindFirstFile() for an example.
  341. @header{wx/filefn.h}
  342. */
  343. wxString wxFindNextFile();
  344. /**
  345. This function does directory searching; returns the first file that matches
  346. the path @a spec, or the empty string. Use wxFindNextFile() to get the next
  347. matching file. Neither will report the current directory "." or the parent
  348. directory "..".
  349. @warning As of 2.5.2, these functions are not thread-safe! (they use static
  350. variables). You probably want to use wxDir::GetFirst() or
  351. wxDirTraverser instead.
  352. @a spec may contain wildcards.
  353. @a flags may be wxDIR for restricting the query to directories, wxFILE for
  354. files or zero for either.
  355. For example:
  356. @code
  357. wxString f = wxFindFirstFile("/home/project/*.*");
  358. while ( !f.empty() )
  359. {
  360. ...
  361. f = wxFindNextFile();
  362. }
  363. @endcode
  364. @header{wx/filefn.h}
  365. */
  366. wxString wxFindFirstFile(const wxString& spec, int flags = 0);
  367. /**
  368. Parameter indicating how file offset should be interpreted.
  369. This is used by wxFFile::Seek() and wxFile::Seek().
  370. @header{wx/filefn.h}
  371. */
  372. enum wxSeekMode
  373. {
  374. wxFromStart, ///< Seek from the file beginning.
  375. wxFromCurrent, ///< Seek from the current position.
  376. wxFromEnd ///< Seek from end of the file.
  377. };
  378. /**
  379. File kind enumerations returned from wxGetFileKind().
  380. Also used by wxFFile::GetKind() and wxFile::GetKind().
  381. @header{wx/filefn.h}
  382. */
  383. enum wxFileKind
  384. {
  385. wxFILE_KIND_UNKNOWN, ///< Unknown file kind, or unable to determine
  386. wxFILE_KIND_DISK, ///< A file supporting seeking to arbitrary offsets
  387. wxFILE_KIND_TERMINAL, ///< A tty
  388. wxFILE_KIND_PIPE ///< A pipe
  389. };
  390. //@}
  391. /** @addtogroup group_funcmacro_file */
  392. //@{
  393. /**
  394. Returns the type of an open file. Possible return values are enumerations
  395. of ::wxFileKind.
  396. @header{wx/filefn.h}
  397. */
  398. wxFileKind wxGetFileKind(int fd);
  399. wxFileKind wxGetFileKind(FILE* fp);
  400. //@}
  401. /** @addtogroup group_funcmacro_file */
  402. //@{
  403. /**
  404. @deprecated
  405. This function is obsolete, please use wxFileName::SplitPath() instead.
  406. Returns the filename for a full path. The second form returns a pointer to
  407. temporary storage that should not be deallocated.
  408. @header{wx/filefn.h}
  409. */
  410. wxString wxFileNameFromPath(const wxString& path);
  411. char* wxFileNameFromPath(char* path);
  412. //@}
  413. /** @addtogroup group_funcmacro_file */
  414. //@{
  415. /**
  416. @deprecated
  417. This function is obsolete, please use wxFileName::CreateTempFileName() instead.
  418. @header{wx/filefn.h}
  419. */
  420. char* wxGetTempFileName(const wxString& prefix, char* buf = NULL);
  421. bool wxGetTempFileName(const wxString& prefix, wxString& buf);
  422. //@}