ソースを参照

core: add FilesystemDisk::open_temporary()

Daniele Bartolini 1 年間 前
コミット
dd98569ba7

+ 14 - 0
src/core/filesystem/filesystem_disk.cpp

@@ -7,6 +7,7 @@
 #include "core/filesystem/file.h"
 #include "core/filesystem/filesystem_disk.h"
 #include "core/filesystem/path.h"
+#include "core/guid.h"
 #include "core/memory/temp_allocator.inl"
 #include "core/os.h"
 #include "core/strings/dynamic_string.inl"
@@ -251,6 +252,19 @@ File *FilesystemDisk::open(const char *path, FileOpenMode::Enum mode)
 	return file;
 }
 
+File *FilesystemDisk::open_temporary(DynamicString &absolute_path)
+{
+	TempAllocator256 ta;
+	DynamicString tmp_basename(ta);
+	tmp_basename.from_guid(guid::new_guid());
+	tmp_basename += ".tmp";
+	this->absolute_path(absolute_path, tmp_basename.c_str());
+
+	FileDisk *file = CE_NEW(*_allocator, FileDisk)();
+	file->open(absolute_path.c_str(), FileOpenMode::WRITE);
+	return file;
+}
+
 void FilesystemDisk::close(File &file)
 {
 	CE_DELETE(*_allocator, &file);

+ 4 - 0
src/core/filesystem/filesystem_disk.h

@@ -34,6 +34,10 @@ struct FilesystemDisk : public Filesystem
 	/// @copydoc Filesystem::open()
 	File *open(const char *path, FileOpenMode::Enum mode) override;
 
+	/// Opens a new temporary file for writing. It returns a pointer to the new
+	/// file and its @a absolute_path.
+	File *open_temporary(DynamicString &absolute_path);
+
 	/// @copydoc Filesystem::close()
 	void close(File &file) override;