The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

950 lines
39KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_FILE_JUCEHEADER__
  19. #define __JUCE_FILE_JUCEHEADER__
  20. #include "../containers/juce_Array.h"
  21. #include "../time/juce_Time.h"
  22. #include "../text/juce_StringArray.h"
  23. #include "../memory/juce_MemoryBlock.h"
  24. #include "../memory/juce_ScopedPointer.h"
  25. #include "../misc/juce_Result.h"
  26. class FileInputStream;
  27. class FileOutputStream;
  28. //==============================================================================
  29. /**
  30. Represents a local file or directory.
  31. This class encapsulates the absolute pathname of a file or directory, and
  32. has methods for finding out about the file and changing its properties.
  33. To read or write to the file, there are methods for returning an input or
  34. output stream.
  35. @see FileInputStream, FileOutputStream
  36. */
  37. class JUCE_API File
  38. {
  39. public:
  40. //==============================================================================
  41. /** Creates an (invalid) file object.
  42. The file is initially set to an empty path, so getFullPath() will return
  43. an empty string, and comparing the file to File::nonexistent will return
  44. true.
  45. You can use its operator= method to point it at a proper file.
  46. */
  47. File() noexcept {}
  48. /** Creates a file from an absolute path.
  49. If the path supplied is a relative path, it is taken to be relative
  50. to the current working directory (see File::getCurrentWorkingDirectory()),
  51. but this isn't a recommended way of creating a file, because you
  52. never know what the CWD is going to be.
  53. On the Mac/Linux, the path can include "~" notation for referring to
  54. user home directories.
  55. */
  56. File (const String& path);
  57. /** Creates a copy of another file object. */
  58. File (const File& other);
  59. /** Destructor. */
  60. ~File() noexcept {}
  61. /** Sets the file based on an absolute pathname.
  62. If the path supplied is a relative path, it is taken to be relative
  63. to the current working directory (see File::getCurrentWorkingDirectory()),
  64. but this isn't a recommended way of creating a file, because you
  65. never know what the CWD is going to be.
  66. On the Mac/Linux, the path can include "~" notation for referring to
  67. user home directories.
  68. */
  69. File& operator= (const String& newFilePath);
  70. /** Copies from another file object. */
  71. File& operator= (const File& otherFile);
  72. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  73. File (File&& otherFile) noexcept;
  74. File& operator= (File&& otherFile) noexcept;
  75. #endif
  76. //==============================================================================
  77. /** This static constant is used for referring to an 'invalid' file. */
  78. static const File nonexistent;
  79. //==============================================================================
  80. /** Checks whether the file actually exists.
  81. @returns true if the file exists, either as a file or a directory.
  82. @see existsAsFile, isDirectory
  83. */
  84. bool exists() const;
  85. /** Checks whether the file exists and is a file rather than a directory.
  86. @returns true only if this is a real file, false if it's a directory
  87. or doesn't exist
  88. @see exists, isDirectory
  89. */
  90. bool existsAsFile() const;
  91. /** Checks whether the file is a directory that exists.
  92. @returns true only if the file is a directory which actually exists, so
  93. false if it's a file or doesn't exist at all
  94. @see exists, existsAsFile
  95. */
  96. bool isDirectory() const;
  97. /** Returns the size of the file in bytes.
  98. @returns the number of bytes in the file, or 0 if it doesn't exist.
  99. */
  100. int64 getSize() const;
  101. /** Utility function to convert a file size in bytes to a neat string description.
  102. So for example 100 would return "100 bytes", 2000 would return "2 KB",
  103. 2000000 would produce "2 MB", etc.
  104. */
  105. static String descriptionOfSizeInBytes (int64 bytes);
  106. //==============================================================================
  107. /** Returns the complete, absolute path of this file.
  108. This includes the filename and all its parent folders. On Windows it'll
  109. also include the drive letter prefix; on Mac or Linux it'll be a complete
  110. path starting from the root folder.
  111. If you just want the file's name, you should use getFileName() or
  112. getFileNameWithoutExtension().
  113. @see getFileName, getRelativePathFrom
  114. */
  115. const String& getFullPathName() const noexcept { return fullPath; }
  116. /** Returns the last section of the pathname.
  117. Returns just the final part of the path - e.g. if the whole path
  118. is "/moose/fish/foo.txt" this will return "foo.txt".
  119. For a directory, it returns the final part of the path - e.g. for the
  120. directory "/moose/fish" it'll return "fish".
  121. If the filename begins with a dot, it'll return the whole filename, e.g. for
  122. "/moose/.fish", it'll return ".fish"
  123. @see getFullPathName, getFileNameWithoutExtension
  124. */
  125. String getFileName() const;
  126. /** Creates a relative path that refers to a file relatively to a given directory.
  127. e.g. File ("/moose/foo.txt").getRelativePathFrom (File ("/moose/fish/haddock"))
  128. would return "../../foo.txt".
  129. If it's not possible to navigate from one file to the other, an absolute
  130. path is returned. If the paths are invalid, an empty string may also be
  131. returned.
  132. @param directoryToBeRelativeTo the directory which the resultant string will
  133. be relative to. If this is actually a file rather than
  134. a directory, its parent directory will be used instead.
  135. If it doesn't exist, it's assumed to be a directory.
  136. @see getChildFile, isAbsolutePath
  137. */
  138. String getRelativePathFrom (const File& directoryToBeRelativeTo) const;
  139. //==============================================================================
  140. /** Returns the file's extension.
  141. Returns the file extension of this file, also including the dot.
  142. e.g. "/moose/fish/foo.txt" would return ".txt"
  143. @see hasFileExtension, withFileExtension, getFileNameWithoutExtension
  144. */
  145. String getFileExtension() const;
  146. /** Checks whether the file has a given extension.
  147. @param extensionToTest the extension to look for - it doesn't matter whether or
  148. not this string has a dot at the start, so ".wav" and "wav"
  149. will have the same effect. The comparison used is
  150. case-insensitve. To compare with multiple extensions, this
  151. parameter can contain multiple strings, separated by semi-colons -
  152. so, for example: hasFileExtension (".jpeg;png;gif") would return
  153. true if the file has any of those three extensions.
  154. @see getFileExtension, withFileExtension, getFileNameWithoutExtension
  155. */
  156. bool hasFileExtension (const String& extensionToTest) const;
  157. /** Returns a version of this file with a different file extension.
  158. e.g. File ("/moose/fish/foo.txt").withFileExtension ("html") returns "/moose/fish/foo.html"
  159. @param newExtension the new extension, either with or without a dot at the start (this
  160. doesn't make any difference). To get remove a file's extension altogether,
  161. pass an empty string into this function.
  162. @see getFileName, getFileExtension, hasFileExtension, getFileNameWithoutExtension
  163. */
  164. File withFileExtension (const String& newExtension) const;
  165. /** Returns the last part of the filename, without its file extension.
  166. e.g. for "/moose/fish/foo.txt" this will return "foo".
  167. @see getFileName, getFileExtension, hasFileExtension, withFileExtension
  168. */
  169. String getFileNameWithoutExtension() const;
  170. //==============================================================================
  171. /** Returns a 32-bit hash-code that identifies this file.
  172. This is based on the filename. Obviously it's possible, although unlikely, that
  173. two files will have the same hash-code.
  174. */
  175. int hashCode() const;
  176. /** Returns a 64-bit hash-code that identifies this file.
  177. This is based on the filename. Obviously it's possible, although unlikely, that
  178. two files will have the same hash-code.
  179. */
  180. int64 hashCode64() const;
  181. //==============================================================================
  182. /** Returns a file based on a relative path.
  183. This will find a child file or directory of the current object.
  184. e.g.
  185. File ("/moose/fish").getChildFile ("foo.txt") will produce "/moose/fish/foo.txt".
  186. File ("/moose/fish").getChildFile ("../foo.txt") will produce "/moose/foo.txt".
  187. If the string is actually an absolute path, it will be treated as such, e.g.
  188. File ("/moose/fish").getChildFile ("/foo.txt") will produce "/foo.txt"
  189. @see getSiblingFile, getParentDirectory, getRelativePathFrom, isAChildOf
  190. */
  191. File getChildFile (String relativePath) const;
  192. /** Returns a file which is in the same directory as this one.
  193. This is equivalent to getParentDirectory().getChildFile (name).
  194. @see getChildFile, getParentDirectory
  195. */
  196. File getSiblingFile (const String& siblingFileName) const;
  197. //==============================================================================
  198. /** Returns the directory that contains this file or directory.
  199. e.g. for "/moose/fish/foo.txt" this will return "/moose/fish".
  200. */
  201. File getParentDirectory() const;
  202. /** Checks whether a file is somewhere inside a directory.
  203. Returns true if this file is somewhere inside a subdirectory of the directory
  204. that is passed in. Neither file actually has to exist, because the function
  205. just checks the paths for similarities.
  206. e.g. File ("/moose/fish/foo.txt").isAChildOf ("/moose") is true.
  207. File ("/moose/fish/foo.txt").isAChildOf ("/moose/fish") is also true.
  208. */
  209. bool isAChildOf (const File& potentialParentDirectory) const;
  210. //==============================================================================
  211. /** Chooses a filename relative to this one that doesn't already exist.
  212. If this file is a directory, this will return a child file of this
  213. directory that doesn't exist, by adding numbers to a prefix and suffix until
  214. it finds one that isn't already there.
  215. If the prefix + the suffix doesn't exist, it won't bother adding a number.
  216. e.g. File ("/moose/fish").getNonexistentChildFile ("foo", ".txt", true) might
  217. return "/moose/fish/foo(2).txt" if there's already a file called "foo.txt".
  218. @param prefix the string to use for the filename before the number
  219. @param suffix the string to add to the filename after the number
  220. @param putNumbersInBrackets if true, this will create filenames in the
  221. format "prefix(number)suffix", if false, it will leave the
  222. brackets out.
  223. */
  224. File getNonexistentChildFile (const String& prefix,
  225. const String& suffix,
  226. bool putNumbersInBrackets = true) const;
  227. /** Chooses a filename for a sibling file to this one that doesn't already exist.
  228. If this file doesn't exist, this will just return itself, otherwise it
  229. will return an appropriate sibling that doesn't exist, e.g. if a file
  230. "/moose/fish/foo.txt" exists, this might return "/moose/fish/foo(2).txt".
  231. @param putNumbersInBrackets whether to add brackets around the numbers that
  232. get appended to the new filename.
  233. */
  234. File getNonexistentSibling (bool putNumbersInBrackets = true) const;
  235. //==============================================================================
  236. /** Compares the pathnames for two files. */
  237. bool operator== (const File& otherFile) const;
  238. /** Compares the pathnames for two files. */
  239. bool operator!= (const File& otherFile) const;
  240. /** Compares the pathnames for two files. */
  241. bool operator< (const File& otherFile) const;
  242. /** Compares the pathnames for two files. */
  243. bool operator> (const File& otherFile) const;
  244. //==============================================================================
  245. /** Checks whether a file can be created or written to.
  246. @returns true if it's possible to create and write to this file. If the file
  247. doesn't already exist, this will check its parent directory to
  248. see if writing is allowed.
  249. @see setReadOnly
  250. */
  251. bool hasWriteAccess() const;
  252. /** Changes the write-permission of a file or directory.
  253. @param shouldBeReadOnly whether to add or remove write-permission
  254. @param applyRecursively if the file is a directory and this is true, it will
  255. recurse through all the subfolders changing the permissions
  256. of all files
  257. @returns true if it manages to change the file's permissions.
  258. @see hasWriteAccess
  259. */
  260. bool setReadOnly (bool shouldBeReadOnly,
  261. bool applyRecursively = false) const;
  262. /** Returns true if this file is a hidden or system file.
  263. The criteria for deciding whether a file is hidden are platform-dependent.
  264. */
  265. bool isHidden() const;
  266. /** If this file is a link, this returns the file that it points to.
  267. If this file isn't actually link, it'll just return itself.
  268. */
  269. File getLinkedTarget() const;
  270. //==============================================================================
  271. /** Returns the last modification time of this file.
  272. @returns the time, or an invalid time if the file doesn't exist.
  273. @see setLastModificationTime, getLastAccessTime, getCreationTime
  274. */
  275. Time getLastModificationTime() const;
  276. /** Returns the last time this file was accessed.
  277. @returns the time, or an invalid time if the file doesn't exist.
  278. @see setLastAccessTime, getLastModificationTime, getCreationTime
  279. */
  280. Time getLastAccessTime() const;
  281. /** Returns the time that this file was created.
  282. @returns the time, or an invalid time if the file doesn't exist.
  283. @see getLastModificationTime, getLastAccessTime
  284. */
  285. Time getCreationTime() const;
  286. /** Changes the modification time for this file.
  287. @param newTime the time to apply to the file
  288. @returns true if it manages to change the file's time.
  289. @see getLastModificationTime, setLastAccessTime, setCreationTime
  290. */
  291. bool setLastModificationTime (const Time& newTime) const;
  292. /** Changes the last-access time for this file.
  293. @param newTime the time to apply to the file
  294. @returns true if it manages to change the file's time.
  295. @see getLastAccessTime, setLastModificationTime, setCreationTime
  296. */
  297. bool setLastAccessTime (const Time& newTime) const;
  298. /** Changes the creation date for this file.
  299. @param newTime the time to apply to the file
  300. @returns true if it manages to change the file's time.
  301. @see getCreationTime, setLastModificationTime, setLastAccessTime
  302. */
  303. bool setCreationTime (const Time& newTime) const;
  304. /** If possible, this will try to create a version string for the given file.
  305. The OS may be able to look at the file and give a version for it - e.g. with
  306. executables, bundles, dlls, etc. If no version is available, this will
  307. return an empty string.
  308. */
  309. String getVersion() const;
  310. //==============================================================================
  311. /** Creates an empty file if it doesn't already exist.
  312. If the file that this object refers to doesn't exist, this will create a file
  313. of zero size.
  314. If it already exists or is a directory, this method will do nothing.
  315. @returns true if the file has been created (or if it already existed).
  316. @see createDirectory
  317. */
  318. Result create() const;
  319. /** Creates a new directory for this filename.
  320. This will try to create the file as a directory, and fill also create
  321. any parent directories it needs in order to complete the operation.
  322. @returns a result to indicate whether the directory was created successfully, or
  323. an error message if it failed.
  324. @see create
  325. */
  326. Result createDirectory() const;
  327. /** Deletes a file.
  328. If this file is actually a directory, it may not be deleted correctly if it
  329. contains files. See deleteRecursively() as a better way of deleting directories.
  330. @returns true if the file has been successfully deleted (or if it didn't exist to
  331. begin with).
  332. @see deleteRecursively
  333. */
  334. bool deleteFile() const;
  335. /** Deletes a file or directory and all its subdirectories.
  336. If this file is a directory, this will try to delete it and all its subfolders. If
  337. it's just a file, it will just try to delete the file.
  338. @returns true if the file and all its subfolders have been successfully deleted
  339. (or if it didn't exist to begin with).
  340. @see deleteFile
  341. */
  342. bool deleteRecursively() const;
  343. /** Moves this file or folder to the trash.
  344. @returns true if the operation succeeded. It could fail if the trash is full, or
  345. if the file is write-protected, so you should check the return value
  346. and act appropriately.
  347. */
  348. bool moveToTrash() const;
  349. /** Moves or renames a file.
  350. Tries to move a file to a different location.
  351. If the target file already exists, this will attempt to delete it first, and
  352. will fail if this can't be done.
  353. Note that the destination file isn't the directory to put it in, it's the actual
  354. filename that you want the new file to have.
  355. @returns true if the operation succeeds
  356. */
  357. bool moveFileTo (const File& targetLocation) const;
  358. /** Copies a file.
  359. Tries to copy a file to a different location.
  360. If the target file already exists, this will attempt to delete it first, and
  361. will fail if this can't be done.
  362. @returns true if the operation succeeds
  363. */
  364. bool copyFileTo (const File& targetLocation) const;
  365. /** Copies a directory.
  366. Tries to copy an entire directory, recursively.
  367. If this file isn't a directory or if any target files can't be created, this
  368. will return false.
  369. @param newDirectory the directory that this one should be copied to. Note that this
  370. is the name of the actual directory to create, not the directory
  371. into which the new one should be placed, so there must be enough
  372. write privileges to create it if it doesn't exist. Any files inside
  373. it will be overwritten by similarly named ones that are copied.
  374. */
  375. bool copyDirectoryTo (const File& newDirectory) const;
  376. //==============================================================================
  377. /** Used in file searching, to specify whether to return files, directories, or both.
  378. */
  379. enum TypesOfFileToFind
  380. {
  381. findDirectories = 1, /**< Use this flag to indicate that you want to find directories. */
  382. findFiles = 2, /**< Use this flag to indicate that you want to find files. */
  383. findFilesAndDirectories = 3, /**< Use this flag to indicate that you want to find both files and directories. */
  384. ignoreHiddenFiles = 4 /**< Add this flag to avoid returning any hidden files in the results. */
  385. };
  386. /** Searches inside a directory for files matching a wildcard pattern.
  387. Assuming that this file is a directory, this method will search it
  388. for either files or subdirectories whose names match a filename pattern.
  389. @param results an array to which File objects will be added for the
  390. files that the search comes up with
  391. @param whatToLookFor a value from the TypesOfFileToFind enum, specifying whether to
  392. return files, directories, or both. If the ignoreHiddenFiles flag
  393. is also added to this value, hidden files won't be returned
  394. @param searchRecursively if true, all subdirectories will be recursed into to do
  395. an exhaustive search
  396. @param wildCardPattern the filename pattern to search for, e.g. "*.txt"
  397. @returns the number of results that have been found
  398. @see getNumberOfChildFiles, DirectoryIterator
  399. */
  400. int findChildFiles (Array<File>& results,
  401. int whatToLookFor,
  402. bool searchRecursively,
  403. const String& wildCardPattern = "*") const;
  404. /** Searches inside a directory and counts how many files match a wildcard pattern.
  405. Assuming that this file is a directory, this method will search it
  406. for either files or subdirectories whose names match a filename pattern,
  407. and will return the number of matches found.
  408. This isn't a recursive call, and will only search this directory, not
  409. its children.
  410. @param whatToLookFor a value from the TypesOfFileToFind enum, specifying whether to
  411. count files, directories, or both. If the ignoreHiddenFiles flag
  412. is also added to this value, hidden files won't be counted
  413. @param wildCardPattern the filename pattern to search for, e.g. "*.txt"
  414. @returns the number of matches found
  415. @see findChildFiles, DirectoryIterator
  416. */
  417. int getNumberOfChildFiles (int whatToLookFor,
  418. const String& wildCardPattern = "*") const;
  419. /** Returns true if this file is a directory that contains one or more subdirectories.
  420. @see isDirectory, findChildFiles
  421. */
  422. bool containsSubDirectories() const;
  423. //==============================================================================
  424. /** Creates a stream to read from this file.
  425. @returns a stream that will read from this file (initially positioned at the
  426. start of the file), or 0 if the file can't be opened for some reason
  427. @see createOutputStream, loadFileAsData
  428. */
  429. FileInputStream* createInputStream() const;
  430. /** Creates a stream to write to this file.
  431. If the file exists, the stream that is returned will be positioned ready for
  432. writing at the end of the file, so you might want to use deleteFile() first
  433. to write to an empty file.
  434. @returns a stream that will write to this file (initially positioned at the
  435. end of the file), or 0 if the file can't be opened for some reason
  436. @see createInputStream, appendData, appendText
  437. */
  438. FileOutputStream* createOutputStream (int bufferSize = 0x8000) const;
  439. //==============================================================================
  440. /** Loads a file's contents into memory as a block of binary data.
  441. Of course, trying to load a very large file into memory will blow up, so
  442. it's better to check first.
  443. @param result the data block to which the file's contents should be appended - note
  444. that if the memory block might already contain some data, you
  445. might want to clear it first
  446. @returns true if the file could all be read into memory
  447. */
  448. bool loadFileAsData (MemoryBlock& result) const;
  449. /** Reads a file into memory as a string.
  450. Attempts to load the entire file as a zero-terminated string.
  451. This makes use of InputStream::readEntireStreamAsString, which should
  452. automatically cope with unicode/acsii file formats.
  453. */
  454. String loadFileAsString() const;
  455. /** Reads the contents of this file as text and splits it into lines, which are
  456. appended to the given StringArray.
  457. */
  458. void readLines (StringArray& destLines) const;
  459. //==============================================================================
  460. /** Appends a block of binary data to the end of the file.
  461. This will try to write the given buffer to the end of the file.
  462. @returns false if it can't write to the file for some reason
  463. */
  464. bool appendData (const void* dataToAppend,
  465. int numberOfBytes) const;
  466. /** Replaces this file's contents with a given block of data.
  467. This will delete the file and replace it with the given data.
  468. A nice feature of this method is that it's safe - instead of deleting
  469. the file first and then re-writing it, it creates a new temporary file,
  470. writes the data to that, and then moves the new file to replace the existing
  471. file. This means that if the power gets pulled out or something crashes,
  472. you're a lot less likely to end up with a corrupted or unfinished file..
  473. Returns true if the operation succeeds, or false if it fails.
  474. @see appendText
  475. */
  476. bool replaceWithData (const void* dataToWrite,
  477. int numberOfBytes) const;
  478. /** Appends a string to the end of the file.
  479. This will try to append a text string to the file, as either 16-bit unicode
  480. or 8-bit characters in the default system encoding.
  481. It can also write the 'ff fe' unicode header bytes before the text to indicate
  482. the endianness of the file.
  483. Any single \\n characters in the string are replaced with \\r\\n before it is written.
  484. @see replaceWithText
  485. */
  486. bool appendText (const String& textToAppend,
  487. bool asUnicode = false,
  488. bool writeUnicodeHeaderBytes = false) const;
  489. /** Replaces this file's contents with a given text string.
  490. This will delete the file and replace it with the given text.
  491. A nice feature of this method is that it's safe - instead of deleting
  492. the file first and then re-writing it, it creates a new temporary file,
  493. writes the text to that, and then moves the new file to replace the existing
  494. file. This means that if the power gets pulled out or something crashes,
  495. you're a lot less likely to end up with an empty file..
  496. For an explanation of the parameters here, see the appendText() method.
  497. Returns true if the operation succeeds, or false if it fails.
  498. @see appendText
  499. */
  500. bool replaceWithText (const String& textToWrite,
  501. bool asUnicode = false,
  502. bool writeUnicodeHeaderBytes = false) const;
  503. /** Attempts to scan the contents of this file and compare it to another file, returning
  504. true if this is possible and they match byte-for-byte.
  505. */
  506. bool hasIdenticalContentTo (const File& other) const;
  507. //==============================================================================
  508. /** Creates a set of files to represent each file root.
  509. e.g. on Windows this will create files for "c:\", "d:\" etc according
  510. to which ones are available. On the Mac/Linux, this will probably
  511. just add a single entry for "/".
  512. */
  513. static void findFileSystemRoots (Array<File>& results);
  514. /** Finds the name of the drive on which this file lives.
  515. @returns the volume label of the drive, or an empty string if this isn't possible
  516. */
  517. String getVolumeLabel() const;
  518. /** Returns the serial number of the volume on which this file lives.
  519. @returns the serial number, or zero if there's a problem doing this
  520. */
  521. int getVolumeSerialNumber() const;
  522. /** Returns the number of bytes free on the drive that this file lives on.
  523. @returns the number of bytes free, or 0 if there's a problem finding this out
  524. @see getVolumeTotalSize
  525. */
  526. int64 getBytesFreeOnVolume() const;
  527. /** Returns the total size of the drive that contains this file.
  528. @returns the total number of bytes that the volume can hold
  529. @see getBytesFreeOnVolume
  530. */
  531. int64 getVolumeTotalSize() const;
  532. /** Returns true if this file is on a CD or DVD drive. */
  533. bool isOnCDRomDrive() const;
  534. /** Returns true if this file is on a hard disk.
  535. This will fail if it's a network drive, but will still be true for
  536. removable hard-disks.
  537. */
  538. bool isOnHardDisk() const;
  539. /** Returns true if this file is on a removable disk drive.
  540. This might be a usb-drive, a CD-rom, or maybe a network drive.
  541. */
  542. bool isOnRemovableDrive() const;
  543. //==============================================================================
  544. /** Launches the file as a process.
  545. - if the file is executable, this will run it.
  546. - if it's a document of some kind, it will launch the document with its
  547. default viewer application.
  548. - if it's a folder, it will be opened in Explorer, Finder, or equivalent.
  549. @see revealToUser
  550. */
  551. bool startAsProcess (const String& parameters = String::empty) const;
  552. /** Opens Finder, Explorer, or whatever the OS uses, to show the user this file's location.
  553. @see startAsProcess
  554. */
  555. void revealToUser() const;
  556. //==============================================================================
  557. /** A set of types of location that can be passed to the getSpecialLocation() method.
  558. */
  559. enum SpecialLocationType
  560. {
  561. /** The user's home folder. This is the same as using File ("~"). */
  562. userHomeDirectory,
  563. /** The user's default documents folder. On Windows, this might be the user's
  564. "My Documents" folder. On the Mac it'll be their "Documents" folder. Linux
  565. doesn't tend to have one of these, so it might just return their home folder.
  566. */
  567. userDocumentsDirectory,
  568. /** The folder that contains the user's desktop objects. */
  569. userDesktopDirectory,
  570. /** The folder in which applications store their persistent user-specific settings.
  571. On Windows, this might be "\Documents and Settings\username\Application Data".
  572. On the Mac, it might be "~/Library". If you're going to store your settings in here,
  573. always create your own sub-folder to put them in, to avoid making a mess.
  574. */
  575. userApplicationDataDirectory,
  576. /** An equivalent of the userApplicationDataDirectory folder that is shared by all users
  577. of the computer, rather than just the current user.
  578. On the Mac it'll be "/Library", on Windows, it could be something like
  579. "\Documents and Settings\All Users\Application Data".
  580. Depending on the setup, this folder may be read-only.
  581. */
  582. commonApplicationDataDirectory,
  583. /** The folder that should be used for temporary files.
  584. Always delete them when you're finished, to keep the user's computer tidy!
  585. */
  586. tempDirectory,
  587. /** Returns this application's executable file.
  588. If running as a plug-in or DLL, this will (where possible) be the DLL rather than the
  589. host app.
  590. On the mac this will return the unix binary, not the package folder - see
  591. currentApplicationFile for that.
  592. See also invokedExecutableFile, which is similar, but if the exe was launched from a
  593. file link, invokedExecutableFile will return the name of the link.
  594. */
  595. currentExecutableFile,
  596. /** Returns this application's location.
  597. If running as a plug-in or DLL, this will (where possible) be the DLL rather than the
  598. host app.
  599. On the mac this will return the package folder (if it's in one), not the unix binary
  600. that's inside it - compare with currentExecutableFile.
  601. */
  602. currentApplicationFile,
  603. /** Returns the file that was invoked to launch this executable.
  604. This may differ from currentExecutableFile if the app was started from e.g. a link - this
  605. will return the name of the link that was used, whereas currentExecutableFile will return
  606. the actual location of the target executable.
  607. */
  608. invokedExecutableFile,
  609. /** In a plugin, this will return the path of the host executable. */
  610. hostApplicationPath,
  611. /** The directory in which applications normally get installed.
  612. So on windows, this would be something like "c:\program files", on the
  613. Mac "/Applications", or "/usr" on linux.
  614. */
  615. globalApplicationsDirectory,
  616. /** The most likely place where a user might store their music files. */
  617. userMusicDirectory,
  618. /** The most likely place where a user might store their movie files. */
  619. userMoviesDirectory,
  620. };
  621. /** Finds the location of a special type of file or directory, such as a home folder or
  622. documents folder.
  623. @see SpecialLocationType
  624. */
  625. static File JUCE_CALLTYPE getSpecialLocation (const SpecialLocationType type);
  626. //==============================================================================
  627. /** Returns a temporary file in the system's temp directory.
  628. This will try to return the name of a non-existent temp file.
  629. To get the temp folder, you can use getSpecialLocation (File::tempDirectory).
  630. */
  631. static File createTempFile (const String& fileNameEnding);
  632. //==============================================================================
  633. /** Returns the current working directory.
  634. @see setAsCurrentWorkingDirectory
  635. */
  636. static File getCurrentWorkingDirectory();
  637. /** Sets the current working directory to be this file.
  638. For this to work the file must point to a valid directory.
  639. @returns true if the current directory has been changed.
  640. @see getCurrentWorkingDirectory
  641. */
  642. bool setAsCurrentWorkingDirectory() const;
  643. //==============================================================================
  644. /** The system-specific file separator character.
  645. On Windows, this will be '\', on Mac/Linux, it'll be '/'
  646. */
  647. static const juce_wchar separator;
  648. /** The system-specific file separator character, as a string.
  649. On Windows, this will be '\', on Mac/Linux, it'll be '/'
  650. */
  651. static const String separatorString;
  652. //==============================================================================
  653. /** Removes illegal characters from a filename.
  654. This will return a copy of the given string after removing characters
  655. that are not allowed in a legal filename, and possibly shortening the
  656. string if it's too long.
  657. Because this will remove slashes, don't use it on an absolute pathname.
  658. @see createLegalPathName
  659. */
  660. static String createLegalFileName (const String& fileNameToFix);
  661. /** Removes illegal characters from a pathname.
  662. Similar to createLegalFileName(), but this won't remove slashes, so can
  663. be used on a complete pathname.
  664. @see createLegalFileName
  665. */
  666. static String createLegalPathName (const String& pathNameToFix);
  667. /** Indicates whether filenames are case-sensitive on the current operating system. */
  668. static bool areFileNamesCaseSensitive();
  669. /** Returns true if the string seems to be a fully-specified absolute path. */
  670. static bool isAbsolutePath (const String& path);
  671. /** Creates a file that simply contains this string, without doing the sanity-checking
  672. that the normal constructors do.
  673. Best to avoid this unless you really know what you're doing.
  674. */
  675. static File createFileWithoutCheckingPath (const String& path) noexcept;
  676. /** Adds a separator character to the end of a path if it doesn't already have one. */
  677. static String addTrailingSeparator (const String& path);
  678. #if JUCE_MAC || JUCE_IOS || DOXYGEN
  679. //==============================================================================
  680. /** OSX ONLY - Finds the OSType of a file from the its resources. */
  681. OSType getMacOSType() const;
  682. /** OSX ONLY - Returns true if this file is actually a bundle. */
  683. bool isBundle() const;
  684. #endif
  685. #if JUCE_MAC || DOXYGEN
  686. /** OSX ONLY - Adds this file to the OSX dock */
  687. void addToDock() const;
  688. #endif
  689. private:
  690. //==============================================================================
  691. String fullPath;
  692. static String parseAbsolutePath (const String&);
  693. String getPathUpToLastSlash() const;
  694. Result createDirectoryInternal (const String&) const;
  695. bool copyInternal (const File&) const;
  696. bool moveInternal (const File&) const;
  697. bool setFileTimesInternal (int64 modificationTime, int64 accessTime, int64 creationTime) const;
  698. void getFileTimesInternal (int64& modificationTime, int64& accessTime, int64& creationTime) const;
  699. bool setFileReadOnlyInternal (bool shouldBeReadOnly) const;
  700. JUCE_LEAK_DETECTOR (File);
  701. };
  702. #endif // __JUCE_FILE_JUCEHEADER__