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.

175 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. namespace water {
  26. //==============================================================================
  27. /**
  28. Manages a temporary file, which will be deleted when this object is deleted.
  29. This object is intended to be used as a stack based object, using its scope
  30. to make sure the temporary file isn't left lying around.
  31. For example:
  32. @code
  33. {
  34. File myTargetFile ("~/myfile.txt");
  35. // this will choose a file called something like "~/myfile_temp239348.txt"
  36. // which definitely doesn't exist at the time the constructor is called.
  37. TemporaryFile temp (myTargetFile);
  38. // create a stream to the temporary file, and write some data to it...
  39. ScopedPointer<FileOutputStream> out (temp.getFile().createOutputStream());
  40. if (out != nullptr)
  41. {
  42. out->write ( ...etc )
  43. out = nullptr; // (deletes the stream)
  44. // ..now we've finished writing, this will rename the temp file to
  45. // make it replace the target file we specified above.
  46. bool succeeded = temp.overwriteTargetFileWithTemporary();
  47. }
  48. // ..and even if something went wrong and our overwrite failed,
  49. // as the TemporaryFile object goes out of scope here, it'll make sure
  50. // that the temp file gets deleted.
  51. }
  52. @endcode
  53. @see File, FileOutputStream
  54. */
  55. class TemporaryFile
  56. {
  57. public:
  58. //==============================================================================
  59. enum OptionFlags
  60. {
  61. useHiddenFile = 1, /**< Indicates that the temporary file should be hidden -
  62. i.e. its name should start with a dot. */
  63. putNumbersInBrackets = 2 /**< Indicates that when numbers are appended to make sure
  64. the file is unique, they should go in brackets rather
  65. than just being appended (see File::getNonexistentSibling() )*/
  66. };
  67. //==============================================================================
  68. /** Creates a randomly-named temporary file in the default temp directory.
  69. @param suffix a file suffix to use for the file
  70. @param optionFlags a combination of the values listed in the OptionFlags enum
  71. The file will not be created until you write to it. And remember that when
  72. this object is deleted, the file will also be deleted!
  73. */
  74. TemporaryFile (const String& suffix = String(),
  75. int optionFlags = 0);
  76. /** Creates a temporary file in the same directory as a specified file.
  77. This is useful if you have a file that you want to overwrite, but don't
  78. want to harm the original file if the write operation fails. You can
  79. use this to create a temporary file next to the target file, then
  80. write to the temporary file, and finally use overwriteTargetFileWithTemporary()
  81. to replace the target file with the one you've just written.
  82. This class won't create any files until you actually write to them. And remember
  83. that when this object is deleted, the temporary file will also be deleted!
  84. @param targetFile the file that you intend to overwrite - the temporary
  85. file will be created in the same directory as this
  86. @param optionFlags a combination of the values listed in the OptionFlags enum
  87. */
  88. TemporaryFile (const File& targetFile,
  89. int optionFlags = 0);
  90. /** Creates a temporary file using an explicit filename.
  91. The other constructors are a better choice than this one, unless for some reason
  92. you need to explicitly specify the temporary file you want to use.
  93. @param targetFile the file that you intend to overwrite
  94. @param temporaryFile the temporary file to be used
  95. */
  96. TemporaryFile (const File& targetFile,
  97. const File& temporaryFile);
  98. /** Destructor.
  99. When this object is deleted it will make sure that its temporary file is
  100. also deleted! If the operation fails, it'll throw an assertion in debug
  101. mode.
  102. */
  103. ~TemporaryFile();
  104. //==============================================================================
  105. /** Returns the temporary file. */
  106. const File& getFile() const noexcept { return temporaryFile; }
  107. /** Returns the target file that was specified in the constructor. */
  108. const File& getTargetFile() const noexcept { return targetFile; }
  109. /** Tries to move the temporary file to overwrite the target file that was
  110. specified in the constructor.
  111. If you used the constructor that specified a target file, this will attempt
  112. to replace that file with the temporary one.
  113. Before calling this, make sure:
  114. - that you've actually written to the temporary file
  115. - that you've closed any open streams that you were using to write to it
  116. - and that you don't have any streams open to the target file, which would
  117. prevent it being overwritten
  118. If the file move succeeds, this returns false, and the temporary file will
  119. have disappeared. If it fails, the temporary file will probably still exist,
  120. but will be deleted when this object is destroyed.
  121. */
  122. bool overwriteTargetFileWithTemporary() const;
  123. /** Attempts to delete the temporary file, if it exists.
  124. @returns true if the file is successfully deleted (or if it didn't exist).
  125. */
  126. bool deleteTemporaryFile() const;
  127. private:
  128. //==============================================================================
  129. const File temporaryFile, targetFile;
  130. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TemporaryFile)
  131. };
  132. }
  133. #endif // JUCE_TEMPORARYFILE_H_INCLUDED