support for easy creation of temporary files
parent
6e7ce77165
commit
ef0979b15e
|
@ -103,6 +103,9 @@ public:
|
|||
//! @{ \name Miscellaneous
|
||||
// =============================================================
|
||||
|
||||
/// Create a temporary file and return an associated FileStream
|
||||
static ref<FileStream> createTemporary();
|
||||
|
||||
/// Initialize the file I/O layer (unicode conversions etc.)
|
||||
static void staticInitialization();
|
||||
|
||||
|
|
|
@ -37,6 +37,7 @@ struct FileStream::FileStreamPrivate
|
|||
#endif
|
||||
bool write;
|
||||
bool read;
|
||||
bool deleteOnClose;
|
||||
FileStream::EFileMode mode;
|
||||
fs::path path;
|
||||
|
||||
|
@ -79,6 +80,7 @@ void FileStream::open(const fs::path &path, EFileMode mode) {
|
|||
d->mode = mode;
|
||||
d->write = true;
|
||||
d->read = true;
|
||||
d->deleteOnClose = false;
|
||||
|
||||
#if defined(__WINDOWS__)
|
||||
DWORD dwDesiredAccess = GENERIC_READ;
|
||||
|
@ -177,6 +179,14 @@ void FileStream::close() {
|
|||
}
|
||||
#endif
|
||||
d->file = 0;
|
||||
|
||||
if (d->deleteOnClose) {
|
||||
try {
|
||||
fs::remove(d->path);
|
||||
} catch (...) {
|
||||
Log(EWarn, "close(): Unable to delete file \"%s\"", d->path.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -360,6 +370,51 @@ bool FileStream::canWrite() const {
|
|||
return d->write;
|
||||
}
|
||||
|
||||
ref<FileStream> FileStream::createTemporary() {
|
||||
ref<FileStream> result = new FileStream();
|
||||
|
||||
#if defined(__WINDOWS__)
|
||||
WCHAR tempPath[MAX_PATH];
|
||||
WCHAR filename[MAX_PATH];
|
||||
|
||||
unsigned int ret = GetTempPathW(MAX_PATH, tempPath);
|
||||
if (ret == 0 || ret > MAX_PATH)
|
||||
Log(EError, "GetTempPath failed(): %s", lastErrorText().c_str());
|
||||
|
||||
ret = GetTempFileNameW(tempPath, TEXT("mitsuba"), 0, filename);
|
||||
if (ret == 0)
|
||||
Log(EError, "GetTempFileName failed(): %s", lastErrorText().c_str());
|
||||
|
||||
result->d->file = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE,
|
||||
0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
|
||||
if (result->d->file == INVALID_HANDLE_VALUE)
|
||||
Log(EError, "Error while trying to create temporary file \"%s\": %s",
|
||||
d->path.string().c_str(), lastErrorText().c_str());
|
||||
|
||||
result->d->path = fs::path(filename);
|
||||
#else
|
||||
char *path = strdup("/tmp/mitsuba_XXXXXX");
|
||||
int fd = mkstemp(path);
|
||||
if (fd == -1)
|
||||
Log(EError, "Unable to create temporary file (1): %s", strerror(errno));
|
||||
|
||||
result->d->file = fdopen(fd, "wb+");
|
||||
if (result->d->file == NULL)
|
||||
Log(EError, "Unable to create temporary file (2): %s", strerror(errno));
|
||||
|
||||
result->d->path = path;
|
||||
free(path);
|
||||
#endif
|
||||
|
||||
result->d->mode = ETruncReadWrite;
|
||||
result->d->write = true;
|
||||
result->d->read = true;
|
||||
result->d->deleteOnClose = true;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#if defined(__WINDOWS__)
|
||||
static boost::filesystem::detail::utf8_codecvt_facet *__facet = NULL;
|
||||
#endif
|
||||
|
|
|
@ -1093,7 +1093,9 @@ void export_core() {
|
|||
BP_RETURN_CONSTREF)
|
||||
.def("open", &FileStream::open)
|
||||
.def("close", &FileStream::close)
|
||||
.def("remove", &FileStream::remove);
|
||||
.def("remove", &FileStream::remove)
|
||||
.def("createTemporary", &FileStream::createTemporary)
|
||||
.staticmethod("createTemporary");
|
||||
|
||||
BP_CLASS(SocketStream, Stream, (bp::init<std::string, int>()))
|
||||
.def("getPeer", &SocketStream::getPeer, BP_RETURN_CONSTREF)
|
||||
|
|
Loading…
Reference in New Issue