Remove-on-destroy option for simgear::Dir, to help with cleaning up temporary directories.

This commit is contained in:
James Turner 2011-10-21 09:36:10 +01:00
parent 63a8209a83
commit 43e13f0cf2
2 changed files with 29 additions and 5 deletions

View File

@ -47,22 +47,37 @@ using std::string;
namespace simgear
{
Dir::Dir()
Dir::Dir() :
_removeOnDestroy(false)
{
}
Dir::Dir(const SGPath& path) :
_path(path)
_path(path),
_removeOnDestroy(false)
{
_path.set_cached(false); // disable caching, so create/remove work
}
Dir::Dir(const Dir& rel, const SGPath& relPath) :
_path(rel.file(relPath.str()))
_path(rel.file(relPath.str())),
_removeOnDestroy(false)
{
_path.set_cached(false); // disable caching, so create/remove work
}
Dir::~Dir()
{
if (_removeOnDestroy) {
remove(true);
}
}
void Dir::setRemoveOnDestroy()
{
_removeOnDestroy = true;
}
Dir Dir::current()
{
#ifdef _WIN32
@ -79,7 +94,7 @@ Dir Dir::tempDir(const std::string& templ)
{
#ifdef HAVE_MKDTEMP
char buf[1024];
char* tempPath = ::getenv("TMPDIR");
const char* tempPath = ::getenv("TMPDIR");
if (!tempPath) {
tempPath = "/tmp/";
}

View File

@ -43,6 +43,14 @@ namespace simgear
{
public:
Dir();
~Dir();
/**
* when this directory object is destroyed, remove the corresponding
* diretory (and its contents) from the disk. Often used with temporary
* directories to ensure they are cleaned up.
*/
void setRemoveOnDestroy();
static Dir current();
@ -93,6 +101,7 @@ namespace simgear
Dir parent() const;
private:
mutable SGPath _path;
bool _removeOnDestroy;
};
} // of namespace simgear