OpenShot Library | libopenshot-audio 0.2.0
juce_TemporaryFile.h
1
2/** @weakgroup juce_core-files
3 * @{
4 */
5/*
6 ==============================================================================
7
8 This file is part of the JUCE library.
9 Copyright (c) 2017 - ROLI Ltd.
10
11 JUCE is an open source library subject to commercial or open-source
12 licensing.
13
14 The code included in this file is provided under the terms of the ISC license
15 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16 To use, copy, modify, and/or distribute this software for any purpose with or
17 without fee is hereby granted provided that the above copyright notice and
18 this permission notice appear in all copies.
19
20 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22 DISCLAIMED.
23
24 ==============================================================================
25*/
26
27namespace juce
28{
29
30//==============================================================================
31/**
32 Manages a temporary file, which will be deleted when this object is deleted.
33
34 This object is intended to be used as a stack based object, using its scope
35 to make sure the temporary file isn't left lying around.
36
37 For example:
38
39 @code
40 {
41 File myTargetFile ("~/myfile.txt");
42
43 // this will choose a file called something like "~/myfile_temp239348.txt"
44 // which definitely doesn't exist at the time the constructor is called.
45 TemporaryFile temp (myTargetFile);
46
47 // create a stream to the temporary file, and write some data to it...
48 if (auto out = std::unique_ptr<FileOutputStream> (temp.getFile().createOutputStream()))
49 {
50 out->write ( ...etc )
51 out.reset(); // (deletes the stream)
52
53 // ..now we've finished writing, this will rename the temp file to
54 // make it replace the target file we specified above.
55 bool succeeded = temp.overwriteTargetFileWithTemporary();
56 }
57
58 // ..and even if something went wrong and our overwrite failed,
59 // as the TemporaryFile object goes out of scope here, it'll make sure
60 // that the temp file gets deleted.
61 }
62 @endcode
63
64 @see File, FileOutputStream
65
66 @tags{Core}
67*/
69{
70public:
71 //==============================================================================
73 {
74 useHiddenFile = 1, /**< Indicates that the temporary file should be hidden -
75 i.e. its name should start with a dot. */
76 putNumbersInBrackets = 2 /**< Indicates that when numbers are appended to make sure
77 the file is unique, they should go in brackets rather
78 than just being appended (see File::getNonexistentSibling() )*/
79 };
80
81 //==============================================================================
82 /** Creates a randomly-named temporary file in the default temp directory.
83
84 @param suffix a file suffix to use for the file
85 @param optionFlags a combination of the values listed in the OptionFlags enum
86 The file will not be created until you write to it. And remember that when
87 this object is deleted, the file will also be deleted!
88 */
89 TemporaryFile (const String& suffix = String(),
90 int optionFlags = 0);
91
92 /** Creates a temporary file in the same directory as a specified file.
93
94 This is useful if you have a file that you want to overwrite, but don't
95 want to harm the original file if the write operation fails. You can
96 use this to create a temporary file next to the target file, then
97 write to the temporary file, and finally use overwriteTargetFileWithTemporary()
98 to replace the target file with the one you've just written.
99
100 This class won't create any files until you actually write to them. And remember
101 that when this object is deleted, the temporary file will also be deleted!
102
103 @param targetFile the file that you intend to overwrite - the temporary
104 file will be created in the same directory as this
105 @param optionFlags a combination of the values listed in the OptionFlags enum
106 */
107 TemporaryFile (const File& targetFile,
108 int optionFlags = 0);
109
110 /** Creates a temporary file using an explicit filename.
111 The other constructors are a better choice than this one, unless for some reason
112 you need to explicitly specify the temporary file you want to use.
113
114 @param targetFile the file that you intend to overwrite
115 @param temporaryFile the temporary file to be used
116 */
117 TemporaryFile (const File& targetFile,
118 const File& temporaryFile);
119
120 /** Destructor.
121
122 When this object is deleted it will make sure that its temporary file is
123 also deleted! If the operation fails, it'll throw an assertion in debug
124 mode.
125 */
127
128 //==============================================================================
129 /** Returns the temporary file. */
130 const File& getFile() const noexcept { return temporaryFile; }
131
132 /** Returns the target file that was specified in the constructor. */
133 const File& getTargetFile() const noexcept { return targetFile; }
134
135 /** Tries to move the temporary file to overwrite the target file that was
136 specified in the constructor.
137
138 If you used the constructor that specified a target file, this will attempt
139 to replace that file with the temporary one.
140
141 Before calling this, make sure:
142 - that you've actually written to the temporary file
143 - that you've closed any open streams that you were using to write to it
144 - and that you don't have any streams open to the target file, which would
145 prevent it being overwritten
146
147 If the file move succeeds, this returns true, and the temporary file will
148 have disappeared. If it fails, the temporary file will probably still exist,
149 but will be deleted when this object is destroyed.
150 */
151 bool overwriteTargetFileWithTemporary() const;
152
153 /** Attempts to delete the temporary file, if it exists.
154 @returns true if the file is successfully deleted (or if it didn't exist).
155 */
156 bool deleteTemporaryFile() const;
157
158
159private:
160 //==============================================================================
161 const File temporaryFile, targetFile;
162
163 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TemporaryFile)
164};
165
166} // namespace juce
167
168/** @}*/
Represents a local file or directory.
Definition juce_File.h:45
The JUCE String class!
Definition juce_String.h:43
Manages a temporary file, which will be deleted when this object is deleted.
const File & getTargetFile() const noexcept
Returns the target file that was specified in the constructor.
const File & getFile() const noexcept
Returns the temporary file.
#define JUCE_API
This macro is added to all JUCE public class declarations.