diff --git a/modules/juce_core/files/juce_TemporaryFile.cpp b/modules/juce_core/files/juce_TemporaryFile.cpp index b65ea149a1..51885490b9 100644 --- a/modules/juce_core/files/juce_TemporaryFile.cpp +++ b/modules/juce_core/files/juce_TemporaryFile.cpp @@ -23,34 +23,36 @@ ============================================================================== */ -TemporaryFile::TemporaryFile (const String& suffix, const int optionFlags) +static File createTempFile (const File& parentDirectory, String name, + const String& suffix, const int optionFlags) +{ + if ((optionFlags & TemporaryFile::useHiddenFile) != 0) + name = "." + name; + + return parentDirectory.getNonexistentChildFile (name, suffix, (optionFlags & TemporaryFile::putNumbersInBrackets) != 0); +} + +TemporaryFile::TemporaryFile (const String& suffix, const int optionFlags) + : temporaryFile (createTempFile (File::getSpecialLocation (File::tempDirectory), + "temp_" + String::toHexString (Random::getSystemRandom().nextInt()), + suffix, optionFlags)) { - createTempFile (File::getSpecialLocation (File::tempDirectory), - "temp_" + String::toHexString (Random::getSystemRandom().nextInt()), - suffix, - optionFlags); } TemporaryFile::TemporaryFile (const File& target, const int optionFlags) - : targetFile (target) + : temporaryFile (createTempFile (target.getParentDirectory(), + target.getFileNameWithoutExtension() + + "_temp" + String::toHexString (Random::getSystemRandom().nextInt()), + target.getFileExtension(), optionFlags)), + targetFile (target) { // If you use this constructor, you need to give it a valid target file! jassert (targetFile != File::nonexistent); - - createTempFile (targetFile.getParentDirectory(), - targetFile.getFileNameWithoutExtension() - + "_temp" + String::toHexString (Random::getSystemRandom().nextInt()), - targetFile.getFileExtension(), - optionFlags); } -void TemporaryFile::createTempFile (const File& parentDirectory, String name, - const String& suffix, const int optionFlags) +TemporaryFile::TemporaryFile (const File& target, const File& temporary) + : temporaryFile (temporary), targetFile (target) { - if ((optionFlags & useHiddenFile) != 0) - name = "." + name; - - temporaryFile = parentDirectory.getNonexistentChildFile (name, suffix, (optionFlags & putNumbersInBrackets) != 0); } TemporaryFile::~TemporaryFile() diff --git a/modules/juce_core/files/juce_TemporaryFile.h b/modules/juce_core/files/juce_TemporaryFile.h index d3992e2650..0cc5bcb4ff 100644 --- a/modules/juce_core/files/juce_TemporaryFile.h +++ b/modules/juce_core/files/juce_TemporaryFile.h @@ -109,6 +109,16 @@ public: TemporaryFile (const File& targetFile, int optionFlags = 0); + /** Creates a temporary file using an explicit filename. + The other constructors are a better choice than this one, unless for some reason + you need to explicitly specify the temporary file you want to use. + + @param targetFile the file that you intend to overwrite + @param temporaryFile the temporary file to be used + */ + TemporaryFile (const File& targetFile, + const File& temporaryFile); + /** Destructor. When this object is deleted it will make sure that its temporary file is @@ -119,10 +129,10 @@ public: //============================================================================== /** Returns the temporary file. */ - const File& getFile() const { return temporaryFile; } + const File& getFile() const noexcept { return temporaryFile; } /** Returns the target file that was specified in the constructor. */ - const File& getTargetFile() const { return targetFile; } + const File& getTargetFile() const noexcept { return targetFile; } /** Tries to move the temporary file to overwrite the target file that was specified in the constructor. @@ -150,9 +160,7 @@ public: private: //============================================================================== - File temporaryFile, targetFile; - - void createTempFile (const File& parentDirectory, String name, const String& suffix, int optionFlags); + const File temporaryFile, targetFile; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TemporaryFile) };