Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
MappedFile.cpp
Go to the documentation of this file.
1/*
2 * MappedFile.cpp
3 *
4 * Created on: Jan 21, 2014
5 * Author: Manuel Sascha Weiand
6 */
7
9
10#include <fcntl.h>
11#include <cstring>
12#include <fstream>
13
14#include <boost/integer/integer_mask.hpp>
15
17#include "storm/io/file.h"
19
20namespace storm {
21namespace parser {
22
23MappedFile::MappedFile(const char* filename) {
24 STORM_LOG_THROW(storm::io::fileExistsAndIsReadable(filename), storm::exceptions::FileIoException,
25 "Error while reading " << filename << ": The file does not exist or is not readable.");
26
27#if defined LINUX || defined MACOSX
28
29 // Do file mapping for reasonable systems.
30 // stat64(), open(), mmap()
31
32#ifdef MACOSX
33 if (stat(filename, &(this->st)) != 0) {
34#else
35 if (stat64(filename, &(this->st)) != 0) {
36#endif
37 STORM_LOG_THROW(false, storm::exceptions::FileIoException, "Error in stat(" << filename << "): Probably, this file does not exist.");
38 }
39 this->file = open(filename, O_RDONLY);
40
41 STORM_LOG_THROW(this->file >= 0, storm::exceptions::FileIoException, "Error in open(" << filename << "): Probably, we may not read this file.");
42
43 this->data = static_cast<char*>(mmap(NULL, this->st.st_size, PROT_READ, MAP_PRIVATE, this->file, 0));
44 if (this->data == MAP_FAILED) {
45 close(this->file);
46 STORM_LOG_ERROR("Error in mmap(" << filename << "): " << std::strerror(errno));
47 throw exceptions::FileIoException() << "MappedFile Error in mmap(): " << std::strerror(errno);
48 }
49 this->dataEnd = this->data + this->st.st_size;
50#elif defined WINDOWS
51
52 // Do file mapping for windows.
53 // _stat64(), CreateFile(), CreateFileMapping(), MapViewOfFile()
54
55 if (_stat64(filename, &(this->st)) != 0) {
56 STORM_LOG_ERROR("Error in _stat(" << filename << "): Probably, this file does not exist.");
57 throw exceptions::FileIoException("MappedFile Error in stat(): Probably, this file does not exist.");
58 }
59
60 this->file = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
61 if (this->file == INVALID_HANDLE_VALUE) {
62 STORM_LOG_ERROR("Error in CreateFileA(" << filename << "): Probably, we may not read this file.");
63 throw exceptions::FileIoException("MappedFile Error in CreateFileA(): Probably, we may not read this file.");
64 }
65
66 this->mapping = CreateFileMappingA(this->file, NULL, PAGE_READONLY, (DWORD)(st.st_size >> 32), (DWORD)st.st_size, NULL);
67 if (this->mapping == NULL) {
68 CloseHandle(this->file);
69 STORM_LOG_ERROR("Error in CreateFileMappingA(" << filename << ").");
70 throw exceptions::FileIoException("MappedFile Error in CreateFileMappingA().");
71 }
72
73 this->data = static_cast<char*>(MapViewOfFile(this->mapping, FILE_MAP_READ, 0, 0, this->st.st_size));
74 if (this->data == NULL) {
75 CloseHandle(this->mapping);
76 CloseHandle(this->file);
77 STORM_LOG_ERROR("Error in MapViewOfFile(" << filename << ").");
78 throw exceptions::FileIoException("MappedFile Error in MapViewOfFile().");
79 }
80 this->dataEnd = this->data + this->st.st_size;
81#endif
82}
83
85#if defined LINUX || defined MACOSX
86 munmap(this->data, this->st.st_size);
87 close(this->file);
88#elif defined WINDOWS
89 CloseHandle(this->mapping);
90 CloseHandle(this->file);
91#endif
92}
93
94char const* MappedFile::getData() const {
95 return data;
96}
97
98char const* MappedFile::getDataEnd() const {
99 return dataEnd;
100}
101
102std::size_t MappedFile::getDataSize() const {
103 return this->getDataEnd() - this->getData();
104}
105
106} // namespace parser
107} // namespace storm
char const * getData() const
Returns a pointer to the beginning of the mapped file data.
~MappedFile()
Destructs a MappedFile.
char const * getDataEnd() const
Returns a pointer to the end of the mapped file data.
std::size_t getDataSize() const
Returns the size of the mapped file data.
MappedFile(const char *filename)
Constructs a MappedFile.
#define STORM_LOG_ERROR(message)
Definition logging.h:31
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
bool fileExistsAndIsReadable(std::string const &filename)
Tests whether the given file exists and is readable.
Definition file.h:66
LabParser.cpp.
Definition cli.cpp:18