OpenShot Audio Library | OpenShotAudio 0.3.2
Loading...
Searching...
No Matches
juce_TemporaryFile.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
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
26static File createTempFile (const File& parentDirectory, String name,
27 const String& suffix, int optionFlags)
28{
29 if ((optionFlags & TemporaryFile::useHiddenFile) != 0)
30 name = "." + name;
31
32 return parentDirectory.getNonexistentChildFile (name, suffix, (optionFlags & TemporaryFile::putNumbersInBrackets) != 0);
33}
34
36 : temporaryFile (createTempFile (File::getSpecialLocation (File::tempDirectory),
37 "temp_" + String::toHexString (Random::getSystemRandom().nextInt()),
39 targetFile()
40{
41}
42
44 : temporaryFile (createTempFile (target.getParentDirectory(),
45 target.getFileNameWithoutExtension()
46 + "_temp" + String::toHexString (Random::getSystemRandom().nextInt()),
47 target.getFileExtension(), optionFlags)),
48 targetFile (target)
49{
50 // If you use this constructor, you need to give it a valid target file!
51 jassert (targetFile != File());
52}
53
55 : temporaryFile (temporary), targetFile (target)
56{
57}
58
60{
61 if (! deleteTemporaryFile())
62 {
63 /* Failed to delete our temporary file! The most likely reason for this would be
64 that you've not closed an output stream that was being used to write to file.
65
66 If you find that something beyond your control is changing permissions on
67 your temporary files and preventing them from being deleted, you may want to
68 call TemporaryFile::deleteTemporaryFile() to detect those error cases and
69 handle them appropriately.
70 */
71 jassertfalse;
72 }
73}
74
75//==============================================================================
77{
78 // This method only works if you created this object with the constructor
79 // that takes a target file!
80 jassert (targetFile != File());
81
82 if (temporaryFile.exists())
83 {
84 // Have a few attempts at overwriting the file before giving up..
85 for (int i = 5; --i >= 0;)
86 {
87 if (temporaryFile.replaceFileIn (targetFile))
88 return true;
89
90 Thread::sleep (100);
91 }
92 }
93 else
94 {
95 // There's no temporary file to use. If your write failed, you should
96 // probably check, and not bother calling this method.
97 jassertfalse;
98 }
99
100 return false;
101}
102
104{
105 // Have a few attempts at deleting the file before giving up..
106 for (int i = 5; --i >= 0;)
107 {
108 if (temporaryFile.deleteFile())
109 return true;
110
111 Thread::sleep (50);
112 }
113
114 return false;
115}
116
117} // namespace juce
bool overwriteTargetFileWithTemporary() const
TemporaryFile(const String &suffix=String(), int optionFlags=0)
static void JUCE_CALLTYPE sleep(int milliseconds)