Browse Source

moved file mapping logic to parser.h

tempestpy_adaptions
gereon 12 years ago
parent
commit
408290d72b
  1. 63
      src/parser/parser.h
  2. 29
      src/parser/readLabFile.cpp
  3. 36
      src/parser/readTraFile.cpp

63
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 <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
const uint_fast32_t BUFFER_SIZE=1024;
#include <pantheios/pantheios.hpp>
#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);
}
};
}
}

29
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;
}

36
src/parser/readTraFile.cpp

@ -99,42 +99,17 @@ sparse::StaticSparseMatrix<double> * 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<double> * 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<double> *sp = NULL;
@ -172,8 +147,6 @@ sparse::StaticSparseMatrix<double> * 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<double> * readTraFile(const char * filename) {
/*!
* clean up
*/
close(f);
munmap(data, st.st_size);
pantheios::log_DEBUG("Finalizing Matrix");
sp->finalize();
return sp;

Loading…
Cancel
Save