Browse Source

Added a new constructor to TemporaryFile to allow explicitly setting the temp file.

tags/2021-05-28
jules 13 years ago
parent
commit
76ae8fe523
2 changed files with 32 additions and 22 deletions
  1. +19
    -17
      modules/juce_core/files/juce_TemporaryFile.cpp
  2. +13
    -5
      modules/juce_core/files/juce_TemporaryFile.h

+ 19
- 17
modules/juce_core/files/juce_TemporaryFile.cpp View File

@@ -23,34 +23,36 @@
==============================================================================
*/
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()


+ 13
- 5
modules/juce_core/files/juce_TemporaryFile.h View File

@@ -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)
};


Loading…
Cancel
Save