From dc04a129229f5840bdb5629d186f64962a1838c1 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 18 Jan 2020 22:16:47 +0100 Subject: [PATCH] Spectre/System/File: add getErrorMessage() --- include/Spectre/System/File.h | 6 ++++++ source/System/File.cpp | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/include/Spectre/System/File.h b/include/Spectre/System/File.h index adfc611..5adb044 100644 --- a/include/Spectre/System/File.h +++ b/include/Spectre/System/File.h @@ -4,6 +4,7 @@ #include #include +#include namespace sp { @@ -54,6 +55,8 @@ public : // Close the file. void close(); + std::string getErrorMessage() const; + // Get the current position in the file. size_t pos(); @@ -103,6 +106,9 @@ public : private : FILE *m_handle; + + // Error + mutable std::error_condition m_error; }; } //namespace sp diff --git a/source/System/File.cpp b/source/System/File.cpp index acc6870..2cc24d3 100644 --- a/source/System/File.cpp +++ b/source/System/File.cpp @@ -1,6 +1,7 @@ #include #include +#include #include namespace sp { @@ -54,6 +55,7 @@ bool File::open(const std::string& filename, Access access, unsigned int mode) if (mode & CREATE && (access == Access::WRITE || access == Access::READ_WRITE)) { m_handle = fopen(filename.c_str(), "a"); if (!m_handle) { + m_error.assign(errno, std::generic_category()); return false; } fclose(m_handle); @@ -70,12 +72,18 @@ bool File::open(const std::string& filename, Access access, unsigned int mode) } return true; } + + m_error.assign(errno, std::generic_category()); return false; } bool File::isOpen() const { - return m_handle != NULL; + if (m_handle == NULL) { + m_error = std::errc::bad_file_descriptor; + return false; + } + return true; } void File::close() @@ -84,6 +92,12 @@ void File::close() fclose(m_handle); m_handle = NULL; } + m_error.clear(); +} + +std::string File::getErrorMessage() const +{ + return m_error.message(); } size_t File::pos()