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
23 namespace juce
24{
25
26 static 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
35 TemporaryFile::TemporaryFile (const String& suffix, const int optionFlags)
36 : temporaryFile (createTempFile (File::getSpecialLocation (File::tempDirectory),
37 "temp_" + String::toHexString (Random::getSystemRandom().nextInt()),
38 suffix, optionFlags)),
39 targetFile()
40{
41}
42
43 TemporaryFile::TemporaryFile (const File& target, const int optionFlags)
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
54 TemporaryFile::TemporaryFile (const File& target, const File& temporary)
55 : temporaryFile (temporary), targetFile (target)
56{
57}
58
59 TemporaryFile::~TemporaryFile()
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//==============================================================================
76 bool TemporaryFile::overwriteTargetFileWithTemporary() const
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
103 bool TemporaryFile::deleteTemporaryFile() const
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
juce::File
Definition: juce_File.h:41
juce::File::getNonexistentChildFile
File getNonexistentChildFile(const String &prefix, const String &suffix, bool putNumbersInBrackets=true) const
Definition: juce_File.cpp:606
juce::File::deleteFile
bool deleteFile() const
juce::File::replaceFileIn
bool replaceFileIn(const File &targetLocation) const
Definition: juce_File.cpp:313
juce::File::exists
bool exists() const
juce::Random
Definition: juce_Random.h:35
juce::String
Definition: juce_String.h:39
juce::TemporaryFile::putNumbersInBrackets
@ putNumbersInBrackets
Definition: juce_TemporaryFile.h:72
juce::TemporaryFile::useHiddenFile
@ useHiddenFile
Definition: juce_TemporaryFile.h:70
juce::TemporaryFile::deleteTemporaryFile
bool deleteTemporaryFile() const
Definition: juce_TemporaryFile.cpp:103
juce::TemporaryFile::overwriteTargetFileWithTemporary
bool overwriteTargetFileWithTemporary() const
Definition: juce_TemporaryFile.cpp:76
juce::TemporaryFile::~TemporaryFile
~TemporaryFile()
Definition: juce_TemporaryFile.cpp:59
juce::TemporaryFile::TemporaryFile
TemporaryFile(const String &suffix=String(), int optionFlags=0)
Definition: juce_TemporaryFile.cpp:35
juce::Thread::sleep
static void JUCE_CALLTYPE sleep(int milliseconds)