The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

165 lines
6.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_TEMPORARYFILE_JUCEHEADER__
  19. #define __JUCE_TEMPORARYFILE_JUCEHEADER__
  20. #include "juce_FileOutputStream.h"
  21. //==============================================================================
  22. /**
  23. Manages a temporary file, which will be deleted when this object is deleted.
  24. This object is intended to be used as a stack based object, using its scope
  25. to make sure the temporary file isn't left lying around.
  26. For example:
  27. @code
  28. {
  29. File myTargetFile ("~/myfile.txt");
  30. // this will choose a file called something like "~/myfile_temp239348.txt"
  31. // which definitely doesn't exist at the time the constructor is called.
  32. TemporaryFile temp (myTargetFile);
  33. // create a stream to the temporary file, and write some data to it...
  34. ScopedPointer <FileOutputStream> out (temp.getFile().createOutputStream());
  35. if (out != 0)
  36. {
  37. out->write ( ...etc )
  38. out->flush();
  39. out = 0; // (deletes the stream)
  40. // ..now we've finished writing, this will rename the temp file to
  41. // make it replace the target file we specified above.
  42. bool succeeded = temp.overwriteTargetFileWithTemporary();
  43. }
  44. // ..and even if something went wrong and our overwrite failed,
  45. // as the TemporaryFile object goes out of scope here, it'll make sure
  46. // that the temp file gets deleted.
  47. }
  48. @endcode
  49. @see File, FileOutputStream
  50. */
  51. class JUCE_API TemporaryFile
  52. {
  53. public:
  54. //==============================================================================
  55. enum OptionFlags
  56. {
  57. useHiddenFile = 1, /**< Indicates that the temporary file should be hidden -
  58. i.e. its name should start with a dot. */
  59. putNumbersInBrackets = 2 /**< Indicates that when numbers are appended to make sure
  60. the file is unique, they should go in brackets rather
  61. than just being appended (see File::getNonexistentSibling() )*/
  62. };
  63. //==============================================================================
  64. /** Creates a randomly-named temporary file in the default temp directory.
  65. @param suffix a file suffix to use for the file
  66. @param optionFlags a combination of the values listed in the OptionFlags enum
  67. The file will not be created until you write to it. And remember that when
  68. this object is deleted, the file will also be deleted!
  69. */
  70. TemporaryFile (const String& suffix = String::empty,
  71. int optionFlags = 0);
  72. /** Creates a temporary file in the same directory as a specified file.
  73. This is useful if you have a file that you want to overwrite, but don't
  74. want to harm the original file if the write operation fails. You can
  75. use this to create a temporary file next to the target file, then
  76. write to the temporary file, and finally use overwriteTargetFileWithTemporary()
  77. to replace the target file with the one you've just written.
  78. This class won't create any files until you actually write to them. And remember
  79. that when this object is deleted, the temporary file will also be deleted!
  80. @param targetFile the file that you intend to overwrite - the temporary
  81. file will be created in the same directory as this
  82. @param optionFlags a combination of the values listed in the OptionFlags enum
  83. */
  84. TemporaryFile (const File& targetFile,
  85. int optionFlags = 0);
  86. /** Destructor.
  87. When this object is deleted it will make sure that its temporary file is
  88. also deleted! If the operation fails, it'll throw an assertion in debug
  89. mode.
  90. */
  91. ~TemporaryFile();
  92. //==============================================================================
  93. /** Returns the temporary file. */
  94. const File getFile() const { return temporaryFile; }
  95. /** Returns the target file that was specified in the constructor. */
  96. const File getTargetFile() const { return targetFile; }
  97. /** Tries to move the temporary file to overwrite the target file that was
  98. specified in the constructor.
  99. If you used the constructor that specified a target file, this will attempt
  100. to replace that file with the temporary one.
  101. Before calling this, make sure:
  102. - that you've actually written to the temporary file
  103. - that you've closed any open streams that you were using to write to it
  104. - and that you don't have any streams open to the target file, which would
  105. prevent it being overwritten
  106. If the file move succeeds, this returns false, and the temporary file will
  107. have disappeared. If it fails, the temporary file will probably still exist,
  108. but will be deleted when this object is destroyed.
  109. */
  110. bool overwriteTargetFileWithTemporary() const;
  111. /** Attempts to delete the temporary file, if it exists.
  112. @returns true if the file is successfully deleted (or if it didn't exist).
  113. */
  114. bool deleteTemporaryFile() const;
  115. //==============================================================================
  116. juce_UseDebuggingNewOperator
  117. private:
  118. //==============================================================================
  119. File temporaryFile, targetFile;
  120. void createTempFile (const File& parentDirectory, String name, const String& suffix, int optionFlags);
  121. TemporaryFile (const TemporaryFile&);
  122. TemporaryFile& operator= (const TemporaryFile&);
  123. };
  124. #endif // __JUCE_TEMPORARYFILE_JUCEHEADER__