dump: move openStdioFile() to util/dump_util.h

This commit is contained in:
Justin Viiret
2017-06-26 10:48:25 +10:00
committed by Matthew Barr
parent bc953717c1
commit 4edf1e4195
2 changed files with 27 additions and 20 deletions

View File

@@ -30,6 +30,8 @@
#define DUMP_UTIL
#include <cstdio>
#include <memory>
#include <string>
namespace ue2 {
@@ -38,6 +40,20 @@ namespace ue2 {
*/
FILE *fopen_or_throw(const char *path, const char *mode);
/**
* \brief Helper function: returns a C stdio FILE* handle wrapped in
* a unique_ptr that takes care of closing the file on destruction.
*
* If the file cannot be opened, throws an exception.
*/
inline
std::unique_ptr<FILE, decltype(&fclose)>
openStdioFile(const std::string &filename, const char *mode) {
return std::unique_ptr<FILE, decltype(&fclose)>(
fopen_or_throw(filename.c_str(), mode), &fclose);
}
} // namespace ue2
#endif