1
0
Fork 0

Spectre/System/File: add getErrorMessage()

This commit is contained in:
Henrik Hautakoski 2020-01-18 22:16:47 +01:00
parent 59a4f1347a
commit dc04a12922
No known key found for this signature in database
GPG key ID: 96765B12FEAC4745
2 changed files with 21 additions and 1 deletions

View file

@ -1,6 +1,7 @@
#include <string.h>
#include <stdio.h>
#include <system_error>
#include <Spectre/System/File.h>
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()