Audio plugin host https://kx.studio/carla
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.

File.h 33KB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2018 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_FILE_H_INCLUDED
  21. #define WATER_FILE_H_INCLUDED
  22. #include "../containers/Array.h"
  23. #include "../misc/Result.h"
  24. namespace water {
  25. //==============================================================================
  26. /**
  27. Represents a local file or directory.
  28. This class encapsulates the absolute pathname of a file or directory, and
  29. has methods for finding out about the file and changing its properties.
  30. To read or write to the file, there are methods for returning an input or
  31. output stream.
  32. @see FileInputStream, FileOutputStream
  33. */
  34. class File
  35. {
  36. public:
  37. //==============================================================================
  38. /** Creates an (invalid) file object.
  39. The file is initially set to an empty path, so getFullPathName() will return
  40. an empty string.
  41. You can use its operator= method to point it at a proper file.
  42. */
  43. File() noexcept;
  44. /** Creates a file from an absolute path.
  45. If the path supplied is a relative path, it is taken to be relative
  46. to the current working directory (see File::getCurrentWorkingDirectory()),
  47. but this isn't a recommended way of creating a file, because you
  48. never know what the CWD is going to be.
  49. On the Mac/Linux, the path can include "~" notation for referring to
  50. user home directories.
  51. */
  52. File (const String& absolutePath);
  53. /** Creates a copy of another file object. */
  54. File (const File&);
  55. /** Destructor. */
  56. ~File() noexcept {}
  57. /** Sets the file based on an absolute pathname.
  58. If the path supplied is a relative path, it is taken to be relative
  59. to the current working directory (see File::getCurrentWorkingDirectory()),
  60. but this isn't a recommended way of creating a file, because you
  61. never know what the CWD is going to be.
  62. On the Mac/Linux, the path can include "~" notation for referring to
  63. user home directories.
  64. */
  65. File& operator= (const String& newAbsolutePath);
  66. /** Copies from another file object. */
  67. File& operator= (const File& otherFile);
  68. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  69. File (File&&) noexcept;
  70. File& operator= (File&&) noexcept;
  71. #endif
  72. //==============================================================================
  73. /** Checks whether the file actually exists.
  74. @returns true if the file exists, either as a file or a directory.
  75. @see existsAsFile, isDirectory
  76. */
  77. bool exists() const;
  78. /** Checks whether the file exists and is a file rather than a directory.
  79. @returns true only if this is a real file, false if it's a directory
  80. or doesn't exist
  81. @see exists, isDirectory
  82. */
  83. bool existsAsFile() const;
  84. /** Checks whether the file is a directory that exists.
  85. @returns true only if the file is a directory which actually exists, so
  86. false if it's a file or doesn't exist at all
  87. @see exists, existsAsFile
  88. */
  89. bool isDirectory() const;
  90. /** Returns the size of the file in bytes.
  91. @returns the number of bytes in the file, or 0 if it doesn't exist.
  92. */
  93. int64 getSize() const;
  94. /** Utility function to convert a file size in bytes to a neat string description.
  95. So for example 100 would return "100 bytes", 2000 would return "2 KB",
  96. 2000000 would produce "2 MB", etc.
  97. */
  98. static String descriptionOfSizeInBytes (int64 bytes);
  99. //==============================================================================
  100. /** Returns the complete, absolute path of this file.
  101. This includes the filename and all its parent folders. On Windows it'll
  102. also include the drive letter prefix; on Mac or Linux it'll be a complete
  103. path starting from the root folder.
  104. If you just want the file's name, you should use getFileName() or
  105. getFileNameWithoutExtension().
  106. @see getFileName, getRelativePathFrom
  107. */
  108. const String& getFullPathName() const noexcept { return fullPath; }
  109. /** Returns the last section of the pathname.
  110. Returns just the final part of the path - e.g. if the whole path
  111. is "/moose/fish/foo.txt" this will return "foo.txt".
  112. For a directory, it returns the final part of the path - e.g. for the
  113. directory "/moose/fish" it'll return "fish".
  114. If the filename begins with a dot, it'll return the whole filename, e.g. for
  115. "/moose/.fish", it'll return ".fish"
  116. @see getFullPathName, getFileNameWithoutExtension
  117. */
  118. String getFileName() const;
  119. /** Creates a relative path that refers to a file relatively to a given directory.
  120. e.g. File ("/moose/foo.txt").getRelativePathFrom (File ("/moose/fish/haddock"))
  121. would return "../../foo.txt".
  122. If it's not possible to navigate from one file to the other, an absolute
  123. path is returned. If the paths are invalid, an empty string may also be
  124. returned.
  125. @param directoryToBeRelativeTo the directory which the resultant string will
  126. be relative to. If this is actually a file rather than
  127. a directory, its parent directory will be used instead.
  128. If it doesn't exist, it's assumed to be a directory.
  129. @see getChildFile, isAbsolutePath
  130. */
  131. String getRelativePathFrom (const File& directoryToBeRelativeTo) const;
  132. //==============================================================================
  133. /** Returns the file's extension.
  134. Returns the file extension of this file, also including the dot.
  135. e.g. "/moose/fish/foo.txt" would return ".txt"
  136. @see hasFileExtension, withFileExtension, getFileNameWithoutExtension
  137. */
  138. String getFileExtension() const;
  139. /** Checks whether the file has a given extension.
  140. @param extensionToTest the extension to look for - it doesn't matter whether or
  141. not this string has a dot at the start, so ".wav" and "wav"
  142. will have the same effect. To compare with multiple extensions, this
  143. parameter can contain multiple strings, separated by semi-colons -
  144. so, for example: hasFileExtension (".jpeg;png;gif") would return
  145. true if the file has any of those three extensions.
  146. @see getFileExtension, withFileExtension, getFileNameWithoutExtension
  147. */
  148. bool hasFileExtension (StringRef extensionToTest) const;
  149. /** Returns a version of this file with a different file extension.
  150. e.g. File ("/moose/fish/foo.txt").withFileExtension ("html") returns "/moose/fish/foo.html"
  151. @param newExtension the new extension, either with or without a dot at the start (this
  152. doesn't make any difference). To get remove a file's extension altogether,
  153. pass an empty string into this function.
  154. @see getFileName, getFileExtension, hasFileExtension, getFileNameWithoutExtension
  155. */
  156. File withFileExtension (StringRef newExtension) const;
  157. /** Returns the last part of the filename, without its file extension.
  158. e.g. for "/moose/fish/foo.txt" this will return "foo".
  159. @see getFileName, getFileExtension, hasFileExtension, withFileExtension
  160. */
  161. String getFileNameWithoutExtension() const;
  162. //==============================================================================
  163. /** Returns a file that represents a relative (or absolute) sub-path of the current one.
  164. This will find a child file or directory of the current object.
  165. e.g.
  166. File ("/moose/fish").getChildFile ("foo.txt") will produce "/moose/fish/foo.txt".
  167. File ("/moose/fish").getChildFile ("haddock/foo.txt") will produce "/moose/fish/haddock/foo.txt".
  168. File ("/moose/fish").getChildFile ("../foo.txt") will produce "/moose/foo.txt".
  169. If the string is actually an absolute path, it will be treated as such, e.g.
  170. File ("/moose/fish").getChildFile ("/foo.txt") will produce "/foo.txt"
  171. @see getSiblingFile, getParentDirectory, getRelativePathFrom, isAChildOf
  172. */
  173. File getChildFile (StringRef relativeOrAbsolutePath) const;
  174. /** Returns a file which is in the same directory as this one.
  175. This is equivalent to getParentDirectory().getChildFile (name).
  176. @see getChildFile, getParentDirectory
  177. */
  178. File getSiblingFile (StringRef siblingFileName) const;
  179. //==============================================================================
  180. /** Returns the directory that contains this file or directory.
  181. e.g. for "/moose/fish/foo.txt" this will return "/moose/fish".
  182. */
  183. File getParentDirectory() const;
  184. /** Checks whether a file is somewhere inside a directory.
  185. Returns true if this file is somewhere inside a subdirectory of the directory
  186. that is passed in. Neither file actually has to exist, because the function
  187. just checks the paths for similarities.
  188. e.g. File ("/moose/fish/foo.txt").isAChildOf ("/moose") is true.
  189. File ("/moose/fish/foo.txt").isAChildOf ("/moose/fish") is also true.
  190. */
  191. bool isAChildOf (const File& potentialParentDirectory) const;
  192. //==============================================================================
  193. /** Chooses a filename relative to this one that doesn't already exist.
  194. If this file is a directory, this will return a child file of this
  195. directory that doesn't exist, by adding numbers to a prefix and suffix until
  196. it finds one that isn't already there.
  197. If the prefix + the suffix doesn't exist, it won't bother adding a number.
  198. e.g. File ("/moose/fish").getNonexistentChildFile ("foo", ".txt", true) might
  199. return "/moose/fish/foo(2).txt" if there's already a file called "foo.txt".
  200. @param prefix the string to use for the filename before the number
  201. @param suffix the string to add to the filename after the number
  202. @param putNumbersInBrackets if true, this will create filenames in the
  203. format "prefix(number)suffix", if false, it will leave the
  204. brackets out.
  205. */
  206. File getNonexistentChildFile (const String& prefix,
  207. const String& suffix,
  208. bool putNumbersInBrackets = true) const;
  209. /** Chooses a filename for a sibling file to this one that doesn't already exist.
  210. If this file doesn't exist, this will just return itself, otherwise it
  211. will return an appropriate sibling that doesn't exist, e.g. if a file
  212. "/moose/fish/foo.txt" exists, this might return "/moose/fish/foo(2).txt".
  213. @param putNumbersInBrackets whether to add brackets around the numbers that
  214. get appended to the new filename.
  215. */
  216. File getNonexistentSibling (bool putNumbersInBrackets = true) const;
  217. //==============================================================================
  218. /** Compares the pathnames for two files. */
  219. bool operator== (const File&) const;
  220. /** Compares the pathnames for two files. */
  221. bool operator!= (const File&) const;
  222. /** Compares the pathnames for two files. */
  223. bool operator< (const File&) const;
  224. /** Compares the pathnames for two files. */
  225. bool operator> (const File&) const;
  226. //==============================================================================
  227. /** Checks whether a file can be created or written to.
  228. @returns true if it's possible to create and write to this file. If the file
  229. doesn't already exist, this will check its parent directory to
  230. see if writing is allowed.
  231. @see setReadOnly
  232. */
  233. bool hasWriteAccess() const;
  234. /** Changes the write-permission of a file or directory.
  235. @param shouldBeReadOnly whether to add or remove write-permission
  236. @param applyRecursively if the file is a directory and this is true, it will
  237. recurse through all the subfolders changing the permissions
  238. of all files
  239. @returns true if it manages to change the file's permissions.
  240. @see hasWriteAccess
  241. */
  242. bool setReadOnly (bool shouldBeReadOnly,
  243. bool applyRecursively = false) const;
  244. /** Changes the execute-permissions of a file.
  245. @param shouldBeExecutable whether to add or remove execute-permission
  246. @returns true if it manages to change the file's permissions.
  247. */
  248. bool setExecutePermission (bool shouldBeExecutable) const;
  249. /** Returns true if this file is a hidden or system file.
  250. The criteria for deciding whether a file is hidden are platform-dependent.
  251. */
  252. bool isHidden() const;
  253. /** Returns a unique identifier for the file, if one is available.
  254. Depending on the OS and file-system, this may be a unix inode number or
  255. a win32 file identifier, or 0 if it fails to find one. The number will
  256. be unique on the filesystem, but not globally.
  257. */
  258. uint64 getFileIdentifier() const;
  259. /** If possible, this will try to create a version string for the given file.
  260. The OS may be able to look at the file and give a version for it - e.g. with
  261. executables, bundles, dlls, etc. If no version is available, this will
  262. return an empty string.
  263. */
  264. String getVersion() const;
  265. //==============================================================================
  266. /** Creates an empty file if it doesn't already exist.
  267. If the file that this object refers to doesn't exist, this will create a file
  268. of zero size.
  269. If it already exists or is a directory, this method will do nothing.
  270. If the parent directories of the File do not exist then this method will
  271. recursively create the parent directories.
  272. @returns a result to indicate whether the file was created successfully,
  273. or an error message if it failed.
  274. @see createDirectory
  275. */
  276. Result create() const;
  277. /** Creates a new directory for this filename.
  278. This will try to create the file as a directory, and will also create
  279. any parent directories it needs in order to complete the operation.
  280. @returns a result to indicate whether the directory was created successfully, or
  281. an error message if it failed.
  282. @see create
  283. */
  284. Result createDirectory() const;
  285. /** Deletes a file.
  286. If this file is actually a directory, it may not be deleted correctly if it
  287. contains files. See deleteRecursively() as a better way of deleting directories.
  288. @returns true if the file has been successfully deleted (or if it didn't exist to
  289. begin with).
  290. @see deleteRecursively
  291. */
  292. bool deleteFile() const;
  293. /** Deletes a file or directory and all its subdirectories.
  294. If this file is a directory, this will try to delete it and all its subfolders. If
  295. it's just a file, it will just try to delete the file.
  296. @returns true if the file and all its subfolders have been successfully deleted
  297. (or if it didn't exist to begin with).
  298. @see deleteFile
  299. */
  300. bool deleteRecursively() const;
  301. /** Moves or renames a file.
  302. Tries to move a file to a different location.
  303. If the target file already exists, this will attempt to delete it first, and
  304. will fail if this can't be done.
  305. Note that the destination file isn't the directory to put it in, it's the actual
  306. filename that you want the new file to have.
  307. Also note that on some OSes (e.g. Windows), moving files between different
  308. volumes may not be possible.
  309. @returns true if the operation succeeds
  310. */
  311. bool moveFileTo (const File& targetLocation) const;
  312. /** Copies a file.
  313. Tries to copy a file to a different location.
  314. If the target file already exists, this will attempt to delete it first, and
  315. will fail if this can't be done.
  316. @returns true if the operation succeeds
  317. */
  318. bool copyFileTo (const File& targetLocation) const;
  319. /** Replaces a file.
  320. Replace the file in the given location, assuming the replaced files identity.
  321. Depending on the file system this will preserve file attributes such as
  322. creation date, short file name, etc.
  323. If replacement succeeds the original file is deleted.
  324. @returns true if the operation succeeds
  325. */
  326. bool replaceFileIn (const File& targetLocation) const;
  327. /** Copies a directory.
  328. Tries to copy an entire directory, recursively.
  329. If this file isn't a directory or if any target files can't be created, this
  330. will return false.
  331. @param newDirectory the directory that this one should be copied to. Note that this
  332. is the name of the actual directory to create, not the directory
  333. into which the new one should be placed, so there must be enough
  334. write privileges to create it if it doesn't exist. Any files inside
  335. it will be overwritten by similarly named ones that are copied.
  336. */
  337. bool copyDirectoryTo (const File& newDirectory) const;
  338. //==============================================================================
  339. /** Used in file searching, to specify whether to return files, directories, or both.
  340. */
  341. enum TypesOfFileToFind
  342. {
  343. findDirectories = 1, /**< Use this flag to indicate that you want to find directories. */
  344. findFiles = 2, /**< Use this flag to indicate that you want to find files. */
  345. findFilesAndDirectories = 3, /**< Use this flag to indicate that you want to find both files and directories. */
  346. ignoreHiddenFiles = 4 /**< Add this flag to avoid returning any hidden files in the results. */
  347. };
  348. /** Searches inside a directory for files matching a wildcard pattern.
  349. Assuming that this file is a directory, this method will search it
  350. for either files or subdirectories whose names match a filename pattern.
  351. @param results an array to which File objects will be added for the
  352. files that the search comes up with
  353. @param whatToLookFor a value from the TypesOfFileToFind enum, specifying whether to
  354. return files, directories, or both. If the ignoreHiddenFiles flag
  355. is also added to this value, hidden files won't be returned
  356. @param searchRecursively if true, all subdirectories will be recursed into to do
  357. an exhaustive search
  358. @param wildCardPattern the filename pattern to search for, e.g. "*.txt"
  359. @returns the number of results that have been found
  360. @see getNumberOfChildFiles, DirectoryIterator
  361. */
  362. int findChildFiles (Array<File>& results,
  363. int whatToLookFor,
  364. bool searchRecursively,
  365. const String& wildCardPattern = "*") const;
  366. /** Searches inside a directory and counts how many files match a wildcard pattern.
  367. Assuming that this file is a directory, this method will search it
  368. for either files or subdirectories whose names match a filename pattern,
  369. and will return the number of matches found.
  370. This isn't a recursive call, and will only search this directory, not
  371. its children.
  372. @param whatToLookFor a value from the TypesOfFileToFind enum, specifying whether to
  373. count files, directories, or both. If the ignoreHiddenFiles flag
  374. is also added to this value, hidden files won't be counted
  375. @param wildCardPattern the filename pattern to search for, e.g. "*.txt"
  376. @returns the number of matches found
  377. @see findChildFiles, DirectoryIterator
  378. */
  379. int getNumberOfChildFiles (int whatToLookFor,
  380. const String& wildCardPattern = "*") const;
  381. /** Returns true if this file is a directory that contains one or more subdirectories.
  382. @see isDirectory, findChildFiles
  383. */
  384. bool containsSubDirectories() const;
  385. //==============================================================================
  386. /** Creates a stream to read from this file.
  387. @returns a stream that will read from this file (initially positioned at the
  388. start of the file), or nullptr if the file can't be opened for some reason
  389. @see createOutputStream, loadFileAsData
  390. */
  391. FileInputStream* createInputStream() const;
  392. /** Creates a stream to write to this file.
  393. If the file exists, the stream that is returned will be positioned ready for
  394. writing at the end of the file, so you might want to use deleteFile() first
  395. to write to an empty file.
  396. @returns a stream that will write to this file (initially positioned at the
  397. end of the file), or nullptr if the file can't be opened for some reason
  398. @see createInputStream, appendData, appendText
  399. */
  400. FileOutputStream* createOutputStream (size_t bufferSize = 0x8000) const;
  401. //==============================================================================
  402. /** Loads a file's contents into memory as a block of binary data.
  403. Of course, trying to load a very large file into memory will blow up, so
  404. it's better to check first.
  405. @param result the data block to which the file's contents should be appended - note
  406. that if the memory block might already contain some data, you
  407. might want to clear it first
  408. @returns true if the file could all be read into memory
  409. */
  410. bool loadFileAsData (MemoryBlock& result) const;
  411. /** Reads a file into memory as a string.
  412. Attempts to load the entire file as a zero-terminated string.
  413. This makes use of InputStream::readEntireStreamAsString, which can
  414. read either UTF-16 or UTF-8 file formats.
  415. */
  416. String loadFileAsString() const;
  417. /** Reads the contents of this file as text and splits it into lines, which are
  418. appended to the given StringArray.
  419. */
  420. void readLines (StringArray& destLines) const;
  421. //==============================================================================
  422. /** Appends a block of binary data to the end of the file.
  423. This will try to write the given buffer to the end of the file.
  424. @returns false if it can't write to the file for some reason
  425. */
  426. bool appendData (const void* dataToAppend,
  427. size_t numberOfBytes) const;
  428. /** Replaces this file's contents with a given block of data.
  429. This will delete the file and replace it with the given data.
  430. A nice feature of this method is that it's safe - instead of deleting
  431. the file first and then re-writing it, it creates a new temporary file,
  432. writes the data to that, and then moves the new file to replace the existing
  433. file. This means that if the power gets pulled out or something crashes,
  434. you're a lot less likely to end up with a corrupted or unfinished file..
  435. Returns true if the operation succeeds, or false if it fails.
  436. @see appendText
  437. */
  438. bool replaceWithData (const void* dataToWrite,
  439. size_t numberOfBytes) const;
  440. /** Appends a string to the end of the file.
  441. This will try to append a text string to the file, as either 16-bit unicode
  442. or 8-bit characters in the default system encoding.
  443. It can also write the 'ff fe' unicode header bytes before the text to indicate
  444. the endianness of the file.
  445. Any single \\n characters in the string are replaced with \\r\\n before it is written.
  446. @see replaceWithText
  447. */
  448. bool appendText (const String& textToAppend,
  449. bool asUnicode = false,
  450. bool writeUnicodeHeaderBytes = false) const;
  451. /** Replaces this file's contents with a given text string.
  452. This will delete the file and replace it with the given text.
  453. A nice feature of this method is that it's safe - instead of deleting
  454. the file first and then re-writing it, it creates a new temporary file,
  455. writes the text to that, and then moves the new file to replace the existing
  456. file. This means that if the power gets pulled out or something crashes,
  457. you're a lot less likely to end up with an empty file..
  458. For an explanation of the parameters here, see the appendText() method.
  459. Returns true if the operation succeeds, or false if it fails.
  460. @see appendText
  461. */
  462. bool replaceWithText (const String& textToWrite,
  463. bool asUnicode = false,
  464. bool writeUnicodeHeaderBytes = false) const;
  465. /** Attempts to scan the contents of this file and compare it to another file, returning
  466. true if this is possible and they match byte-for-byte.
  467. */
  468. bool hasIdenticalContentTo (const File& other) const;
  469. //==============================================================================
  470. /** A set of types of location that can be passed to the getSpecialLocation() method.
  471. */
  472. enum SpecialLocationType
  473. {
  474. /** The user's home folder. This is the same as using File ("~"). */
  475. userHomeDirectory,
  476. /** The folder that should be used for temporary files.
  477. Always delete them when you're finished, to keep the user's computer tidy!
  478. */
  479. tempDirectory,
  480. /** Returns this application's executable file.
  481. If running as a plug-in or DLL, this will (where possible) be the DLL rather than the
  482. host app.
  483. On the mac this will return the unix binary, not the package folder.
  484. */
  485. currentExecutableFile,
  486. /** In a plugin, this will return the path of the host executable. */
  487. hostApplicationPath
  488. };
  489. /** Finds the location of a special type of file or directory, such as a home folder or
  490. documents folder.
  491. @see SpecialLocationType
  492. */
  493. static File getSpecialLocation (const SpecialLocationType type);
  494. //==============================================================================
  495. /** Returns a temporary file in the system's temp directory.
  496. This will try to return the name of a non-existent temp file.
  497. To get the temp folder, you can use getSpecialLocation (File::tempDirectory).
  498. */
  499. static File createTempFile (StringRef fileNameEnding);
  500. //==============================================================================
  501. /** Returns the current working directory.
  502. @see setAsCurrentWorkingDirectory
  503. */
  504. static File getCurrentWorkingDirectory();
  505. /** Sets the current working directory to be this file.
  506. For this to work the file must point to a valid directory.
  507. @returns true if the current directory has been changed.
  508. @see getCurrentWorkingDirectory
  509. */
  510. bool setAsCurrentWorkingDirectory() const;
  511. //==============================================================================
  512. /** The system-specific file separator character.
  513. On Windows, this will be '\', on Mac/Linux, it'll be '/'
  514. */
  515. static const water_uchar separator;
  516. /** The system-specific file separator character, as a string.
  517. On Windows, this will be '\', on Mac/Linux, it'll be '/'
  518. */
  519. static const String separatorString;
  520. //==============================================================================
  521. /** Returns a version of a filename with any illegal characters removed.
  522. This will return a copy of the given string after removing characters
  523. that are not allowed in a legal filename, and possibly shortening the
  524. string if it's too long.
  525. Because this will remove slashes, don't use it on an absolute pathname - use
  526. createLegalPathName() for that.
  527. @see createLegalPathName
  528. */
  529. static String createLegalFileName (const String& fileNameToFix);
  530. /** Returns a version of a path with any illegal characters removed.
  531. Similar to createLegalFileName(), but this won't remove slashes, so can
  532. be used on a complete pathname.
  533. @see createLegalFileName
  534. */
  535. static String createLegalPathName (const String& pathNameToFix);
  536. /** Indicates whether filenames are case-sensitive on the current operating system. */
  537. static bool areFileNamesCaseSensitive();
  538. /** Returns true if the string seems to be a fully-specified absolute path. */
  539. static bool isAbsolutePath (StringRef path);
  540. /** Creates a file that simply contains this string, without doing the sanity-checking
  541. that the normal constructors do.
  542. Best to avoid this unless you really know what you're doing.
  543. */
  544. static File createFileWithoutCheckingPath (const String& absolutePath) noexcept;
  545. /** Adds a separator character to the end of a path if it doesn't already have one. */
  546. static String addTrailingSeparator (const String& path);
  547. //==============================================================================
  548. /** Tries to create a symbolic link and returns a boolean to indicate success */
  549. bool createSymbolicLink (const File& linkFileToCreate, bool overwriteExisting) const;
  550. /** Returns true if this file is a link or alias that can be followed using getLinkedTarget(). */
  551. bool isSymbolicLink() const;
  552. /** If this file is a link or alias, this returns the file that it points to.
  553. If the file isn't actually link, it'll just return itself.
  554. */
  555. File getLinkedTarget() const;
  556. //==============================================================================
  557. struct NaturalFileComparator
  558. {
  559. NaturalFileComparator (bool shouldPutFoldersFirst) noexcept : foldersFirst (shouldPutFoldersFirst) {}
  560. int compareElements (const File& firstFile, const File& secondFile) const
  561. {
  562. if (foldersFirst && (firstFile.isDirectory() != secondFile.isDirectory()))
  563. return firstFile.isDirectory() ? -1 : 1;
  564. return firstFile.getFullPathName().compareNatural (secondFile.getFullPathName(), File::areFileNamesCaseSensitive());
  565. }
  566. bool foldersFirst;
  567. };
  568. private:
  569. //==============================================================================
  570. String fullPath;
  571. static String parseAbsolutePath (const String&);
  572. String getPathUpToLastSlash() const;
  573. Result createDirectoryInternal (const String&) const;
  574. bool copyInternal (const File&) const;
  575. bool moveInternal (const File&) const;
  576. bool replaceInternal (const File&) const;
  577. bool setFileTimesInternal (int64 m, int64 a, int64 c) const;
  578. void getFileTimesInternal (int64& m, int64& a, int64& c) const;
  579. bool setFileReadOnlyInternal (bool) const;
  580. bool setFileExecutableInternal (bool) const;
  581. };
  582. }
  583. #endif // WATER_FILE_H_INCLUDED