1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #include "io/FileReader.h"
- #include <stdio.h>
- #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*>(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*>(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<size_t>(bufferSize - 1);
- size_t readBytes = fread(buffer, 1, size, static_cast<FILE*>(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);
- }
|