From 408290d72b325dd4b62f5f94f465453e654d29d4 Mon Sep 17 00:00:00 2001 From: gereon Date: Wed, 21 Nov 2012 20:34:37 +0100 Subject: [PATCH] moved file mapping logic to parser.h --- src/parser/parser.h | 63 +++++++++++++++++++++++++++++++++----- src/parser/readLabFile.cpp | 29 ++---------------- src/parser/readTraFile.cpp | 36 ++-------------------- 3 files changed, 62 insertions(+), 66 deletions(-) diff --git a/src/parser/parser.h b/src/parser/parser.h index 2ebefdb9d..b36f8dc70 100644 --- a/src/parser/parser.h +++ b/src/parser/parser.h @@ -1,15 +1,64 @@ /* * parser.h * - * Created on: 12.09.2012 - * Author: Thomas Heinemann + * Created on: 21.11.2012 + * Author: Gereon Kremer */ -#ifndef PARSER_H_ -#define PARSER_H_ +#pragma once -#include "boost/integer/integer_mask.hpp" +#include +#include +#include +#include -const uint_fast32_t BUFFER_SIZE=1024; +#include +#include "src/exceptions/file_IO_exception.h" -#endif /* PARSER_H_ */ +namespace mrmc +{ +namespace parser +{ + + class MappedFile + { + private: + int file; + struct stat st; + + public: + char* data; + + MappedFile(const char* filename) + { + if (stat(filename, &(this->st)) != 0) + { + pantheios::log_ERROR("Could not stat ", filename, ". Does it exist? Is it readable?"); + throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in stat()"); + } + + this->file = open(filename, O_RDONLY); + if (this->file < 0) + { + pantheios::log_ERROR("Could not open ", filename, ". Does it exist? Is it readable?"); + throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in open()"); + } + + this->data = (char*) mmap(NULL, this->st.st_size, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, this->file, 0); + if (this->data == (char*)-1) + { + close(this->file); + pantheios::log_ERROR("Could not mmap ", filename, "."); + throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in mmap()"); + } + } + + ~MappedFile() + { + munmap(this->data, this->st.st_size); + close(this->file); + } + }; + +} +} \ No newline at end of file diff --git a/src/parser/readLabFile.cpp b/src/parser/readLabFile.cpp index c09fd2db5..15b66aaba 100644 --- a/src/parser/readLabFile.cpp +++ b/src/parser/readLabFile.cpp @@ -42,30 +42,9 @@ namespace parser { */ mrmc::models::AtomicPropositionsLabeling * readLabFile(int node_count, const char * filename) { - /*! - * open file and map to memory - */ - struct stat st; - int f = open(filename, O_RDONLY); - if ((f < 0) || (stat(filename, &st) != 0)) { - /*! - * - */ - pantheios::log_ERROR("File could not be opened."); - throw mrmc::exceptions::file_IO_exception(); - return NULL; - } - - char* data = (char*) mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, f, 0); - if (data == (char*)-1) - { - pantheios::log_ERROR("File could not be mapped. Something went wrong with mmap."); - close(f); - throw mrmc::exceptions::file_IO_exception(); - return NULL; - } + MappedFile file(filename); - char* buf = data; + char* buf = file.data; char sep[] = " \n\t"; uint_fast32_t proposition_count = 0; size_t cnt = 0; @@ -83,7 +62,7 @@ mrmc::models::AtomicPropositionsLabeling * readLabFile(int node_count, const cha mrmc::models::AtomicPropositionsLabeling* result = new mrmc::models::AtomicPropositionsLabeling(node_count, proposition_count); char proposition[128]; - buf = data; + buf = file.data; cnt = 0; do { buf += cnt; @@ -113,8 +92,6 @@ mrmc::models::AtomicPropositionsLabeling * readLabFile(int node_count, const cha buf += cnt; } - munmap(data, st.st_size); - close(f); return result; } diff --git a/src/parser/readTraFile.cpp b/src/parser/readTraFile.cpp index af5350693..c11dfdf9a 100644 --- a/src/parser/readTraFile.cpp +++ b/src/parser/readTraFile.cpp @@ -99,42 +99,17 @@ sparse::StaticSparseMatrix * readTraFile(const char * filename) { */ setlocale( LC_NUMERIC, "C" ); - /*! - * open file and map to memory - */ - struct stat st; - int f = open(filename, O_RDONLY); - if((f < 0) || (stat(filename, &st) != 0)) { - /*! - * stat() or open() failed - */ - pantheios::log_ERROR("File ", filename, " was not readable (Does it exist?)"); - throw exceptions::file_IO_exception("mrmc::read_tra_file: Error opening file! (Does it exist?)"); - return NULL; - } - char *data = (char*)mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, f, 0); - if (data == (char*)-1) - { - /*! - * mmap() failed - */ - pantheios::log_ERROR("Could not map the file to memory. Something went wrong with mmap."); - throw exceptions::file_IO_exception("mrmc::read_tra_file: Error mapping file to memory"); - close(f); - return NULL; - } + MappedFile file(filename); /*! * perform first pass, i.e. count entries that are not zero and not on the diagonal */ - uint_fast32_t non_zero = makeFirstPass(data); + uint_fast32_t non_zero = makeFirstPass(file.data); if (non_zero == 0) { /*! * first pass returned zero, this means the file format was wrong */ - close(f); - munmap(data, st.st_size); throw mrmc::exceptions::wrong_file_format(); return NULL; } @@ -144,7 +119,7 @@ sparse::StaticSparseMatrix * readTraFile(const char * filename) { * * from here on, we already know that the file header is correct */ - char* buf = data; + char* buf = file.data; uint_fast32_t rows; sparse::StaticSparseMatrix *sp = NULL; @@ -172,8 +147,6 @@ sparse::StaticSparseMatrix * readTraFile(const char * filename) { /*! * creating the matrix failed */ - close(f); - munmap(data, st.st_size); throw std::bad_alloc(); return NULL; } @@ -201,9 +174,6 @@ sparse::StaticSparseMatrix * readTraFile(const char * filename) { /*! * clean up */ - close(f); - munmap(data, st.st_size); - pantheios::log_DEBUG("Finalizing Matrix"); sp->finalize(); return sp;