1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-21 01:24:21 +00:00

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

This commit is contained in:
jules 2013-03-18 21:25:45 +00:00
parent 152d91fa9e
commit 76ae8fe523
2 changed files with 33 additions and 23 deletions

View file

@ -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()

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