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.

784 lines
33KB

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