VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / modules / juce_core / files / juce_TemporaryFile.cpp
blobc50f2381b7224e0193b67110894743666a105ae0
1 /*
2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
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.
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.
20 ==============================================================================
23 namespace juce
26 // Using Random::getSystemRandom() can be a bit dangerous in multithreaded contexts!
27 class LockedRandom
29 public:
30 int nextInt()
32 const ScopedLock lock (mutex);
33 return random.nextInt();
36 private:
37 CriticalSection mutex;
38 Random random;
41 static LockedRandom lockedRandom;
43 static File createTempFile (const File& parentDirectory, String name,
44 const String& suffix, int optionFlags)
46 if ((optionFlags & TemporaryFile::useHiddenFile) != 0)
47 name = "." + name;
49 return parentDirectory.getNonexistentChildFile (name, suffix, (optionFlags & TemporaryFile::putNumbersInBrackets) != 0);
52 TemporaryFile::TemporaryFile (const String& suffix, const int optionFlags)
53 : temporaryFile (createTempFile (File::getSpecialLocation (File::tempDirectory),
54 "temp_" + String::toHexString (lockedRandom.nextInt()),
55 suffix, optionFlags)),
56 targetFile()
60 TemporaryFile::TemporaryFile (const File& target, const int optionFlags)
61 : temporaryFile (createTempFile (target.getParentDirectory(),
62 target.getFileNameWithoutExtension()
63 + "_temp" + String::toHexString (lockedRandom.nextInt()),
64 target.getFileExtension(), optionFlags)),
65 targetFile (target)
67 // If you use this constructor, you need to give it a valid target file!
68 jassert (targetFile != File());
71 TemporaryFile::TemporaryFile (const File& target, const File& temporary)
72 : temporaryFile (temporary), targetFile (target)
76 TemporaryFile::~TemporaryFile()
78 if (! deleteTemporaryFile())
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.
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.
88 jassertfalse;
92 //==============================================================================
93 bool TemporaryFile::overwriteTargetFileWithTemporary() const
95 // This method only works if you created this object with the constructor
96 // that takes a target file!
97 jassert (targetFile != File());
99 if (temporaryFile.exists())
101 // Have a few attempts at overwriting the file before giving up..
102 for (int i = 5; --i >= 0;)
104 if (temporaryFile.replaceFileIn (targetFile))
105 return true;
107 Thread::sleep (100);
110 else
112 // There's no temporary file to use. If your write failed, you should
113 // probably check, and not bother calling this method.
114 jassertfalse;
117 return false;
120 bool TemporaryFile::deleteTemporaryFile() const
122 // Have a few attempts at deleting the file before giving up..
123 for (int i = 5; --i >= 0;)
125 if (temporaryFile.isDirectory() ? temporaryFile.deleteRecursively() : temporaryFile.deleteFile())
126 return true;
128 Thread::sleep (50);
131 return false;
134 } // namespace juce