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.

177 lines
7.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_TEMPORARYFILE_H_INCLUDED
  24. #define JUCE_TEMPORARYFILE_H_INCLUDED
  25. #include "juce_File.h"
  26. namespace water {
  27. //==============================================================================
  28. /**
  29. Manages a temporary file, which will be deleted when this object is deleted.
  30. This object is intended to be used as a stack based object, using its scope
  31. to make sure the temporary file isn't left lying around.
  32. For example:
  33. @code
  34. {
  35. File myTargetFile ("~/myfile.txt");
  36. // this will choose a file called something like "~/myfile_temp239348.txt"
  37. // which definitely doesn't exist at the time the constructor is called.
  38. TemporaryFile temp (myTargetFile);
  39. // create a stream to the temporary file, and write some data to it...
  40. ScopedPointer<FileOutputStream> out (temp.getFile().createOutputStream());
  41. if (out != nullptr)
  42. {
  43. out->write ( ...etc )
  44. out = nullptr; // (deletes the stream)
  45. // ..now we've finished writing, this will rename the temp file to
  46. // make it replace the target file we specified above.
  47. bool succeeded = temp.overwriteTargetFileWithTemporary();
  48. }
  49. // ..and even if something went wrong and our overwrite failed,
  50. // as the TemporaryFile object goes out of scope here, it'll make sure
  51. // that the temp file gets deleted.
  52. }
  53. @endcode
  54. @see File, FileOutputStream
  55. */
  56. class TemporaryFile
  57. {
  58. public:
  59. //==============================================================================
  60. enum OptionFlags
  61. {
  62. useHiddenFile = 1, /**< Indicates that the temporary file should be hidden -
  63. i.e. its name should start with a dot. */
  64. putNumbersInBrackets = 2 /**< Indicates that when numbers are appended to make sure
  65. the file is unique, they should go in brackets rather
  66. than just being appended (see File::getNonexistentSibling() )*/
  67. };
  68. //==============================================================================
  69. /** Creates a randomly-named temporary file in the default temp directory.
  70. @param suffix a file suffix to use for the file
  71. @param optionFlags a combination of the values listed in the OptionFlags enum
  72. The file will not be created until you write to it. And remember that when
  73. this object is deleted, the file will also be deleted!
  74. */
  75. TemporaryFile (const String& suffix = String(),
  76. int optionFlags = 0);
  77. /** Creates a temporary file in the same directory as a specified file.
  78. This is useful if you have a file that you want to overwrite, but don't
  79. want to harm the original file if the write operation fails. You can
  80. use this to create a temporary file next to the target file, then
  81. write to the temporary file, and finally use overwriteTargetFileWithTemporary()
  82. to replace the target file with the one you've just written.
  83. This class won't create any files until you actually write to them. And remember
  84. that when this object is deleted, the temporary file will also be deleted!
  85. @param targetFile the file that you intend to overwrite - the temporary
  86. file will be created in the same directory as this
  87. @param optionFlags a combination of the values listed in the OptionFlags enum
  88. */
  89. TemporaryFile (const File& targetFile,
  90. int optionFlags = 0);
  91. /** Creates a temporary file using an explicit filename.
  92. The other constructors are a better choice than this one, unless for some reason
  93. you need to explicitly specify the temporary file you want to use.
  94. @param targetFile the file that you intend to overwrite
  95. @param temporaryFile the temporary file to be used
  96. */
  97. TemporaryFile (const File& targetFile,
  98. const File& temporaryFile);
  99. /** Destructor.
  100. When this object is deleted it will make sure that its temporary file is
  101. also deleted! If the operation fails, it'll throw an assertion in debug
  102. mode.
  103. */
  104. ~TemporaryFile();
  105. //==============================================================================
  106. /** Returns the temporary file. */
  107. const File& getFile() const noexcept { return temporaryFile; }
  108. /** Returns the target file that was specified in the constructor. */
  109. const File& getTargetFile() const noexcept { return targetFile; }
  110. /** Tries to move the temporary file to overwrite the target file that was
  111. specified in the constructor.
  112. If you used the constructor that specified a target file, this will attempt
  113. to replace that file with the temporary one.
  114. Before calling this, make sure:
  115. - that you've actually written to the temporary file
  116. - that you've closed any open streams that you were using to write to it
  117. - and that you don't have any streams open to the target file, which would
  118. prevent it being overwritten
  119. If the file move succeeds, this returns false, and the temporary file will
  120. have disappeared. If it fails, the temporary file will probably still exist,
  121. but will be deleted when this object is destroyed.
  122. */
  123. bool overwriteTargetFileWithTemporary() const;
  124. /** Attempts to delete the temporary file, if it exists.
  125. @returns true if the file is successfully deleted (or if it didn't exist).
  126. */
  127. bool deleteTemporaryFile() const;
  128. private:
  129. //==============================================================================
  130. const File temporaryFile, targetFile;
  131. JUCE_DECLARE_NON_COPYABLE (TemporaryFile)
  132. };
  133. }
  134. #endif // JUCE_TEMPORARYFILE_H_INCLUDED