JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_TemporaryFile.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26// Using Random::getSystemRandom() can be a bit dangerous in multithreaded contexts!
28{
29public:
30 int nextInt()
31 {
32 const ScopedLock lock (mutex);
33 return random.nextInt();
34 }
35
36private:
37 CriticalSection mutex;
38 Random random;
39};
40
41static LockedRandom lockedRandom;
42
43static File createTempFile (const File& parentDirectory, String name,
44 const String& suffix, int optionFlags)
45{
47 name = "." + name;
48
49 return parentDirectory.getNonexistentChildFile (name, suffix, (optionFlags & TemporaryFile::putNumbersInBrackets) != 0);
50}
51
53 : temporaryFile (createTempFile (File::getSpecialLocation (File::tempDirectory),
54 "temp_" + String::toHexString (lockedRandom.nextInt()),
55 suffix, optionFlags)),
56 targetFile()
57{
58}
59
61 : temporaryFile (createTempFile (target.getParentDirectory(),
62 target.getFileNameWithoutExtension()
63 + "_temp" + String::toHexString (lockedRandom.nextInt()),
64 target.getFileExtension(), optionFlags)),
65 targetFile (target)
66{
67 // If you use this constructor, you need to give it a valid target file!
68 jassert (targetFile != File());
69}
70
72 : temporaryFile (temporary), targetFile (target)
73{
74}
75
77{
78 if (! deleteTemporaryFile())
79 {
80 /* Failed to delete our temporary file! The most likely reason for this would be
81 that you've not closed an output stream that was being used to write to file.
82
83 If you find that something beyond your control is changing permissions on
84 your temporary files and preventing them from being deleted, you may want to
85 call TemporaryFile::deleteTemporaryFile() to detect those error cases and
86 handle them appropriately.
87 */
89 }
90}
91
92//==============================================================================
94{
95 // This method only works if you created this object with the constructor
96 // that takes a target file!
97 jassert (targetFile != File());
98
99 if (temporaryFile.exists())
100 {
101 // Have a few attempts at overwriting the file before giving up..
102 for (int i = 5; --i >= 0;)
103 {
104 if (temporaryFile.replaceFileIn (targetFile))
105 return true;
106
107 Thread::sleep (100);
108 }
109 }
110 else
111 {
112 // There's no temporary file to use. If your write failed, you should
113 // probably check, and not bother calling this method.
115 }
116
117 return false;
118}
119
121{
122 // Have a few attempts at deleting the file before giving up..
123 for (int i = 5; --i >= 0;)
124 {
125 if (temporaryFile.isDirectory() ? temporaryFile.deleteRecursively() : temporaryFile.deleteFile())
126 return true;
127
128 Thread::sleep (50);
129 }
130
131 return false;
132}
133
134} // namespace juce
Represents a local file or directory.
Definition juce_File.h:45
bool isDirectory() const
Checks whether the file is a directory that exists.
bool deleteRecursively(bool followSymlinks=false) const
Deletes a file or directory and all its subdirectories.
bool deleteFile() const
Deletes a file.
bool replaceFileIn(const File &targetLocation) const
Replaces a file.
bool exists() const
Checks whether the file actually exists.
Automatically locks and unlocks a mutex object.
A random number generator.
Definition juce_Random.h:35
int nextInt() noexcept
Returns the next random 32 bit integer.
The JUCE String class!
Definition juce_String.h:53
@ putNumbersInBrackets
Indicates that when numbers are appended to make sure the file is unique, they should go in brackets ...
@ useHiddenFile
Indicates that the temporary file should be hidden - i.e.
bool deleteTemporaryFile() const
Attempts to delete the temporary file, if it exists.
bool overwriteTargetFileWithTemporary() const
Tries to move the temporary file to overwrite the target file that was specified in the constructor.
TemporaryFile(const String &suffix=String(), int optionFlags=0)
Creates a randomly-named temporary file in the default temp directory.
static void JUCE_CALLTYPE sleep(int milliseconds)
Suspends the execution of the current thread until the specified timeout period has elapsed (note tha...
#define jassert(expression)
Platform-independent assertion macro.
#define jassertfalse
This will always cause an assertion failure.
JUCE Namespace.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88