1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/*
* mptFileTemporary.h
* ------------------
* Purpose:
* Notes : Currently none.
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#pragma once
#include "openmpt/all/BuildSettings.hpp"
#include "mpt/base/namespace.hpp"
#if defined(MODPLUG_TRACKER) && MPT_OS_WINDOWS
#include "mpt/fs/common_directories.hpp"
#include "mpt/fs/fs.hpp"
#include "mpt/io_file_unique/unique_basename.hpp"
#include "mpt/io_file_unique/unique_tempfilename.hpp"
#include "mpt/uuid/uuid.hpp"
#include "mptPathString.h"
#include "mptRandom.h"
#if MPT_OS_WINDOWS
#include <windows.h>
#endif
#endif // MODPLUG_TRACKER && MPT_OS_WINDOWS
OPENMPT_NAMESPACE_BEGIN
namespace mpt
{
#if defined(MODPLUG_TRACKER) && MPT_OS_WINDOWS
// Returns a new unique absolute path.
class TemporaryPathname
{
private:
mpt::PathString m_Path;
public:
TemporaryPathname(const mpt::PathString &fileNameExtension = P_("tmp"))
{
mpt::PathString prefix;
#if defined(LIBOPENMPT_BUILD)
prefix = P_("libopenmpt");
#else
prefix = P_("OpenMPT");
#endif
m_Path = mpt::PathString::FromNative(mpt::IO::unique_tempfilename{mpt::IO::unique_basename{prefix, mpt::UUID::GenerateLocalUseOnly(mpt::global_prng())}, fileNameExtension});
}
public:
mpt::PathString GetPathname() const
{
return m_Path;
}
};
// Scoped temporary file guard. Deletes the file when going out of scope.
// The file itself is not created automatically.
class TempFileGuard
{
private:
const mpt::PathString filename;
public:
TempFileGuard(const mpt::TemporaryPathname &pathname = mpt::TemporaryPathname{})
: filename(pathname.GetPathname())
{
return;
}
mpt::PathString GetFilename() const
{
return filename;
}
~TempFileGuard()
{
if(!filename.empty())
{
DeleteFile(mpt::support_long_path(filename.AsNative()).c_str());
}
}
};
// Scoped temporary directory guard. Deletes the directory when going out of scope.
// The directory itself is created automatically.
class TempDirGuard
{
private:
mpt::PathString dirname;
public:
TempDirGuard(const mpt::TemporaryPathname &pathname = mpt::TemporaryPathname{})
: dirname(pathname.GetPathname().WithTrailingSlash())
{
if(dirname.empty())
{
return;
}
if(::CreateDirectory(mpt::support_long_path(dirname.AsNative()).c_str(), NULL) == 0)
{ // fail
dirname = mpt::PathString();
}
}
mpt::PathString GetDirname() const
{
return dirname;
}
~TempDirGuard()
{
if(!dirname.empty())
{
mpt::native_fs{}.delete_tree(dirname);
}
}
};
#endif // MODPLUG_TRACKER && MPT_OS_WINDOWS
} // namespace mpt
OPENMPT_NAMESPACE_END
|