hecl/hecl: Add smart pointer variant of Fopen

Provides a wrapper API over Fopen in order to prevent resource leaks.
This commit is contained in:
Lioncash 2019-08-21 19:28:10 -04:00
parent bcfea9a09a
commit 9dcf7e7f08
1 changed files with 10 additions and 0 deletions

View File

@ -275,6 +275,16 @@ inline FILE* Fopen(const SystemChar* path, const SystemChar* mode, FileLockType
return fp;
}
struct UniqueFileDeleter {
void operator()(FILE* file) const noexcept { std::fclose(file); }
};
using UniqueFilePtr = std::unique_ptr<FILE, UniqueFileDeleter>;
inline UniqueFilePtr FopenUnique(const SystemChar* path, const SystemChar* mode,
FileLockType lock = FileLockType::None) {
return UniqueFilePtr{Fopen(path, mode, lock)};
}
inline int FSeek(FILE* fp, int64_t offset, int whence) {
#if _WIN32
return _fseeki64(fp, offset, whence);