2 ==============================================================================
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017-2022 Filipe Coelho <falktx@falktx.com>
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 ==============================================================================
26 #ifndef WATER_TEMPORARYFILE_H_INCLUDED
27 #define WATER_TEMPORARYFILE_H_INCLUDED
29 #include "TemporaryFile.h"
34 //==============================================================================
36 Manages a temporary file, which will be deleted when this object is deleted.
38 This object is intended to be used as a stack based object, using its scope
39 to make sure the temporary file isn't left lying around.
45 File myTargetFile ("~/myfile.txt");
47 // this will choose a file called something like "~/myfile_temp239348.txt"
48 // which definitely doesn't exist at the time the constructor is called.
49 TemporaryFile temp (myTargetFile);
51 // create a stream to the temporary file, and write some data to it...
52 ScopedPointer<FileOutputStream> out (temp.getFile().createOutputStream());
57 out = nullptr; // (deletes the stream)
59 // ..now we've finished writing, this will rename the temp file to
60 // make it replace the target file we specified above.
61 bool succeeded = temp.overwriteTargetFileWithTemporary();
64 // ..and even if something went wrong and our overwrite failed,
65 // as the TemporaryFile object goes out of scope here, it'll make sure
66 // that the temp file gets deleted.
70 @see File, FileOutputStream
75 //==============================================================================
78 useHiddenFile
= 1, /**< Indicates that the temporary file should be hidden -
79 i.e. its name should start with a dot. */
80 putNumbersInBrackets
= 2 /**< Indicates that when numbers are appended to make sure
81 the file is unique, they should go in brackets rather
82 than just being appended (see File::getNonexistentSibling() )*/
85 //==============================================================================
86 /** Creates a randomly-named temporary file in the default temp directory.
88 @param suffix a file suffix to use for the file
89 @param optionFlags a combination of the values listed in the OptionFlags enum
90 The file will not be created until you write to it. And remember that when
91 this object is deleted, the file will also be deleted!
93 TemporaryFile (const String
& suffix
= String(),
96 /** Creates a temporary file in the same directory as a specified file.
98 This is useful if you have a file that you want to overwrite, but don't
99 want to harm the original file if the write operation fails. You can
100 use this to create a temporary file next to the target file, then
101 write to the temporary file, and finally use overwriteTargetFileWithTemporary()
102 to replace the target file with the one you've just written.
104 This class won't create any files until you actually write to them. And remember
105 that when this object is deleted, the temporary file will also be deleted!
107 @param targetFile the file that you intend to overwrite - the temporary
108 file will be created in the same directory as this
109 @param optionFlags a combination of the values listed in the OptionFlags enum
111 TemporaryFile (const File
& targetFile
,
112 int optionFlags
= 0);
114 /** Creates a temporary file using an explicit filename.
115 The other constructors are a better choice than this one, unless for some reason
116 you need to explicitly specify the temporary file you want to use.
118 @param targetFile the file that you intend to overwrite
119 @param temporaryFile the temporary file to be used
121 TemporaryFile (const File
& targetFile
,
122 const File
& temporaryFile
);
126 When this object is deleted it will make sure that its temporary file is
127 also deleted! If the operation fails, it'll throw an assertion in debug
132 //==============================================================================
133 /** Returns the temporary file. */
134 const File
& getFile() const noexcept
{ return temporaryFile
; }
136 /** Returns the target file that was specified in the constructor. */
137 const File
& getTargetFile() const noexcept
{ return targetFile
; }
139 /** Tries to move the temporary file to overwrite the target file that was
140 specified in the constructor.
142 If you used the constructor that specified a target file, this will attempt
143 to replace that file with the temporary one.
145 Before calling this, make sure:
146 - that you've actually written to the temporary file
147 - that you've closed any open streams that you were using to write to it
148 - and that you don't have any streams open to the target file, which would
149 prevent it being overwritten
151 If the file move succeeds, this returns false, and the temporary file will
152 have disappeared. If it fails, the temporary file will probably still exist,
153 but will be deleted when this object is destroyed.
155 bool overwriteTargetFileWithTemporary() const;
157 /** Attempts to delete the temporary file, if it exists.
158 @returns true if the file is successfully deleted (or if it didn't exist).
160 bool deleteTemporaryFile() const;
164 //==============================================================================
165 const File temporaryFile
, targetFile
;
167 CARLA_DECLARE_NON_COPYABLE (TemporaryFile
)
172 #endif // WATER_TEMPORARYFILE_H_INCLUDED