|
|
@ -9,10 +9,15 @@ |
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include "src/exceptions/file_IO_exception.h"
|
|
|
|
#include "src/exceptions/wrong_file_format.h"
|
|
|
|
|
|
|
|
#include "log4cplus/logger.h"
|
|
|
|
#include "log4cplus/loggingmacros.h"
|
|
|
|
extern log4cplus::Logger logger; |
|
|
|
|
|
|
|
/*!
|
|
|
|
* Calls strtol() internally and checks if the new pointer is different |
|
|
|
* from the original one, i.e. if str != *end. If they are the same, a |
|
|
@ -24,7 +29,12 @@ |
|
|
|
uint_fast64_t mrmc::parser::checked_strtol(const char* str, char** end) |
|
|
|
{ |
|
|
|
uint_fast64_t res = strtol(str, end, 10); |
|
|
|
if (str == *end) throw mrmc::exceptions::wrong_file_format(); |
|
|
|
if (str == *end) |
|
|
|
{ |
|
|
|
LOG4CPLUS_ERROR(logger, "Error while parsing integer. Next input token is not a number."); |
|
|
|
LOG4CPLUS_ERROR(logger, "\tUpcoming input is: \"" << std::string(str, 0, 16) << "\""); |
|
|
|
throw mrmc::exceptions::wrong_file_format(); |
|
|
|
} |
|
|
|
return res; |
|
|
|
} |
|
|
|
|
|
|
@ -55,12 +65,14 @@ mrmc::parser::MappedFile::MappedFile(const char* filename) |
|
|
|
*/ |
|
|
|
if (stat64(filename, &(this->st)) != 0) |
|
|
|
{ |
|
|
|
LOG4CPLUS_ERROR(logger, "Error in stat(" << filename << ")."); |
|
|
|
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in stat()"); |
|
|
|
} |
|
|
|
this->file = open(filename, O_RDONLY); |
|
|
|
|
|
|
|
if (this->file < 0) |
|
|
|
{ |
|
|
|
LOG4CPLUS_ERROR(logger, "Error in open(" << filename << ")."); |
|
|
|
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in open()"); |
|
|
|
} |
|
|
|
|
|
|
@ -68,6 +80,7 @@ mrmc::parser::MappedFile::MappedFile(const char* filename) |
|
|
|
if (this->data == (char*)-1) |
|
|
|
{ |
|
|
|
close(this->file); |
|
|
|
LOG4CPLUS_ERROR(logger, "Error in mmap(" << filename << ")."); |
|
|
|
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in mmap()"); |
|
|
|
} |
|
|
|
this->dataend = this->data + this->st.st_size; |
|
|
@ -78,20 +91,23 @@ mrmc::parser::MappedFile::MappedFile(const char* filename) |
|
|
|
*/ |
|
|
|
if (_stat64(filename, &(this->st)) != 0) |
|
|
|
{ |
|
|
|
LOG4CPLUS_ERROR(logger, "Error in _stat(" << filename << ")."); |
|
|
|
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in stat()"); |
|
|
|
} |
|
|
|
|
|
|
|
this->file = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
|
|
|
if (this->file == INVALID_HANDLE_VALUE) |
|
|
|
{ |
|
|
|
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in CreateFile()"); |
|
|
|
LOG4CPLUS_ERROR(logger, "Error in CreateFileA(" << filename << ")."); |
|
|
|
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in CreateFileA()"); |
|
|
|
} |
|
|
|
|
|
|
|
this->mapping = CreateFileMappingA(this->file, NULL, PAGE_READONLY, (DWORD)(st.st_size >> 32), (DWORD)st.st_size, NULL); |
|
|
|
if (this->mapping == NULL) |
|
|
|
{ |
|
|
|
CloseHandle(this->file); |
|
|
|
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in CreateFileMapping()"); |
|
|
|
LOG4CPLUS_ERROR(logger, "Error in CreateFileMappingA(" << filename << ")."); |
|
|
|
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in CreateFileMappingA()"); |
|
|
|
} |
|
|
|
|
|
|
|
this->data = static_cast<char*>(MapViewOfFile(this->mapping, FILE_MAP_READ, 0, 0, this->st.st_size)); |
|
|
@ -99,6 +115,7 @@ mrmc::parser::MappedFile::MappedFile(const char* filename) |
|
|
|
{ |
|
|
|
CloseHandle(this->mapping); |
|
|
|
CloseHandle(this->file); |
|
|
|
LOG4CPLUS_ERROR(logger, "Error in MapViewOfFile(" << filename << ")."); |
|
|
|
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in MapViewOfFile()"); |
|
|
|
} |
|
|
|
this->dataend = this->data + this->st.st_size; |
|
|
|