#include "io/FileReader.h" #include #include "utils/Logger.h" Core::FileReader::FileReader() : file(nullptr) { } Core::FileReader::FileReader(FileReader&& other) : FileReader() { swap(other); } Core::FileReader::~FileReader() { if(file != nullptr) { int r = fclose(static_cast(file)); if(r != 0) { CORE_LOG_WARNING("Cannot close file #: #", path, r); } file = nullptr; } } Core::FileReader& Core::FileReader::operator=(FileReader&& other) { swap(other); return *this; } const Core::Path& Core::FileReader::getPath() const { return path; } Core::Error Core::FileReader::open(const Path& p) { if(file != nullptr) { return Error::INVALID_STATE; } path = p; file = fopen(path, "rb"); return file != nullptr ? Error::NONE : Error::CANNOT_OPEN_FILE; } Core::Error Core::FileReader::open(const char* p) { if(file != nullptr) { return Error::INVALID_STATE; } CORE_RETURN_ERROR(path.append(p)); file = fopen(path, "rb"); return file != nullptr ? Error::NONE : Error::CANNOT_OPEN_FILE; } Core::Error Core::FileReader::readChar(int& c) { if(file == nullptr) { return Error::INVALID_STATE; } c = fgetc(static_cast(file)); return c == EOF ? Error::END_OF_FILE : Error::NONE; } Core::Error Core::FileReader::readChars(char* buffer, int bufferSize) { if(file == nullptr) { return Error::INVALID_STATE; } else if(bufferSize <= 0) { return Error::INVALID_ARGUMENT; } size_t size = static_cast(bufferSize - 1); size_t readBytes = fread(buffer, 1, size, static_cast(file)); buffer[readBytes] = '\0'; return readBytes != size ? Error::END_OF_FILE : Error::NONE; } void Core::FileReader::swap(FileReader& other) { Core::swap(file, other.file); Core::swap(path, other.path); }