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.

TemporaryFile.h 7.1KB

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