Browse Source
First part of the refactoring of Parser.h/.cpp
First part of the refactoring of Parser.h/.cpp
- Removed the RewardMatrixInformationStruct since it was only used as function argument.
|- Replaced its function by the respective results of the transition parser (for det/non-det)
- Moved the MappedFile class to separate file.
- Renamed the SupportedFileEndingsEnum enum to SupportedFileEndings.
- Whats left in Parser.*:
|- A bunch of free functions for manipulation of c-style strings, a function to read the model hint and the SupportedFileEndings enum with its recognition function.
Im not exactly sure what to do with this.
- I think the name Parser.* implies a parser base class, while it is not. Therefore: Renaming.
- The c-style string manipulation functions might also needed elsewhere. Therefore: Moving them into the utility folder/namespace.
- The model hint function would fit in the AutoParser, except for its signature (low level arguments vs. high level arguments of the AutoParser). Therefore: Either moving it to the Auto Parser or leave it as helper function with the others (c-style string handling and low level OS detection)
- The SupportedLineEndings and its assorted functions either belong to the string manipulation functions or to the MappedFile class. Therefor: Moving them accordingly.
- Once all things have moved the Parser.* will be empty. Therefore: Deletion.
Next up: Trying to decide what to do with the rest of the Parser.* and doing it.
Former-commit-id: aa7417e5e1
main
16 changed files with 679 additions and 623 deletions
-
5src/parser/AtomicPropositionLabelingParser.cpp
-
7src/parser/AutoParser.cpp
-
64src/parser/AutoParser.h
-
19src/parser/DeterministicModelParser.cpp
-
360src/parser/DeterministicSparseTransitionParser.cpp
-
14src/parser/DeterministicSparseTransitionParser.h
-
101src/parser/MappedFile.cpp
-
95src/parser/MappedFile.h
-
7src/parser/MarkovAutomatonSparseTransitionParser.cpp
-
4src/parser/MarkovAutomatonSparseTransitionParser.h
-
27src/parser/NondeterministicModelParser.cpp
-
241src/parser/NondeterministicSparseTransitionParser.cpp
-
26src/parser/NondeterministicSparseTransitionParser.h
-
132src/parser/Parser.cpp
-
199src/parser/Parser.h
-
1src/parser/SparseStateRewardParser.cpp
@ -0,0 +1,101 @@ |
|||||
|
/*
|
||||
|
* MappedFile.cpp |
||||
|
* |
||||
|
* Created on: Jan 21, 2014 |
||||
|
* Author: Manuel Sascha Weiand |
||||
|
*/ |
||||
|
|
||||
|
#include "src/parser/MappedFile.h"
|
||||
|
|
||||
|
#include <fstream>
|
||||
|
#include <cstring>
|
||||
|
#include <fcntl.h>
|
||||
|
|
||||
|
#include <iostream>
|
||||
|
#include <string>
|
||||
|
|
||||
|
#include <boost/integer/integer_mask.hpp>
|
||||
|
|
||||
|
#include "src/exceptions/FileIoException.h"
|
||||
|
|
||||
|
#include "log4cplus/logger.h"
|
||||
|
#include "log4cplus/loggingmacros.h"
|
||||
|
extern log4cplus::Logger logger; |
||||
|
|
||||
|
namespace storm { |
||||
|
namespace parser { |
||||
|
|
||||
|
MappedFile::MappedFile(const char* filename) { |
||||
|
#if defined LINUX || defined MACOSX
|
||||
|
/*
|
||||
|
* Do file mapping for reasonable systems. |
||||
|
* stat64(), open(), mmap() |
||||
|
*/ |
||||
|
#ifdef MACOSX
|
||||
|
if (stat(filename, &(this->st)) != 0) { |
||||
|
#else
|
||||
|
if (stat64(filename, &(this->st)) != 0) { |
||||
|
#endif
|
||||
|
LOG4CPLUS_ERROR(logger, "Error in stat(" << filename << "): Probably, this file does not exist."); |
||||
|
throw exceptions::FileIoException() << "MappedFile Error in stat(): Probably, this file does not exist."; |
||||
|
} |
||||
|
this->file = open(filename, O_RDONLY); |
||||
|
|
||||
|
if (this->file < 0) { |
||||
|
LOG4CPLUS_ERROR(logger, "Error in open(" << filename << "): Probably, we may not read this file."); |
||||
|
throw exceptions::FileIoException() << "MappedFile Error in open(): Probably, we may not read this file."; |
||||
|
} |
||||
|
|
||||
|
this->data = reinterpret_cast<char*>(mmap(NULL, this->st.st_size, PROT_READ, MAP_PRIVATE, this->file, 0)); |
||||
|
if (this->data == reinterpret_cast<char*>(-1)) { |
||||
|
close(this->file); |
||||
|
LOG4CPLUS_ERROR(logger, "Error in mmap(" << filename << "): " << std::strerror(errno)); |
||||
|
throw exceptions::FileIoException() << "MappedFile Error in mmap(): " << std::strerror(errno); |
||||
|
} |
||||
|
this->dataend = this->data + this->st.st_size; |
||||
|
#elif defined WINDOWS
|
||||
|
/*
|
||||
|
* Do file mapping for windows. |
||||
|
* _stat64(), CreateFile(), CreateFileMapping(), MapViewOfFile() |
||||
|
*/ |
||||
|
if (_stat64(filename, &(this->st)) != 0) { |
||||
|
LOG4CPLUS_ERROR(logger, "Error in _stat(" << filename << "): Probably, this file does not exist."); |
||||
|
throw exceptions::FileIoException("MappedFile Error in stat(): Probably, this file does not exist."); |
||||
|
} |
||||
|
|
||||
|
this->file = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
||||
|
if (this->file == INVALID_HANDLE_VALUE) { |
||||
|
LOG4CPLUS_ERROR(logger, "Error in CreateFileA(" << filename << "): Probably, we may not read this file."); |
||||
|
throw exceptions::FileIoException("MappedFile Error in CreateFileA(): Probably, we may not read this file."); |
||||
|
} |
||||
|
|
||||
|
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); |
||||
|
LOG4CPLUS_ERROR(logger, "Error in CreateFileMappingA(" << filename << ")."); |
||||
|
throw exceptions::FileIoException("MappedFile Error in CreateFileMappingA()."); |
||||
|
} |
||||
|
|
||||
|
this->data = static_cast<char*>(MapViewOfFile(this->mapping, FILE_MAP_READ, 0, 0, this->st.st_size)); |
||||
|
if (this->data == NULL) { |
||||
|
CloseHandle(this->mapping); |
||||
|
CloseHandle(this->file); |
||||
|
LOG4CPLUS_ERROR(logger, "Error in MapViewOfFile(" << filename << ")."); |
||||
|
throw exceptions::FileIoException("MappedFile Error in MapViewOfFile()."); |
||||
|
} |
||||
|
this->dataend = this->data + this->st.st_size; |
||||
|
#endif
|
||||
|
} |
||||
|
|
||||
|
MappedFile::~MappedFile() { |
||||
|
#if defined LINUX || defined MACOSX
|
||||
|
munmap(this->data, this->st.st_size); |
||||
|
close(this->file); |
||||
|
#elif defined WINDOWS
|
||||
|
CloseHandle(this->mapping); |
||||
|
CloseHandle(this->file); |
||||
|
#endif
|
||||
|
} |
||||
|
} // namespace parser
|
||||
|
} // namespace storm
|
||||
|
|
@ -0,0 +1,95 @@ |
|||||
|
/* |
||||
|
* MappedFile.h |
||||
|
* |
||||
|
* Created on: Jan 21, 2014 |
||||
|
* Author: Manuel Sascha Weiand |
||||
|
*/ |
||||
|
|
||||
|
#ifndef STORM_PARSER_MAPPEDFILE_H_ |
||||
|
#define STORM_PARSER_MAPPEDFILE_H_ |
||||
|
|
||||
|
#include <sys/stat.h> |
||||
|
|
||||
|
#include "src/utility/OsDetection.h" |
||||
|
|
||||
|
namespace storm { |
||||
|
namespace parser { |
||||
|
/*! |
||||
|
* @brief Opens a file and maps it to memory providing a char* |
||||
|
* containing the file content. |
||||
|
* |
||||
|
* This class is a very simple interface to read files efficiently. |
||||
|
* The given file is opened and mapped to memory using mmap(). |
||||
|
* The public member data is a pointer to the actual file content. |
||||
|
* Using this method, the kernel will take care of all buffering. This is |
||||
|
* most probably much more efficient than doing this manually. |
||||
|
*/ |
||||
|
|
||||
|
#if !defined LINUX && !defined MACOSX && !defined WINDOWS |
||||
|
#error Platform not supported |
||||
|
#endif |
||||
|
|
||||
|
class MappedFile { |
||||
|
|
||||
|
public: |
||||
|
|
||||
|
/*! |
||||
|
* Constructor of MappedFile. |
||||
|
* Will stat the given file, open it and map it to memory. |
||||
|
* If anything of this fails, an appropriate exception is raised |
||||
|
* and a log entry is written. |
||||
|
* @param filename file to be opened |
||||
|
*/ |
||||
|
MappedFile(const char* filename); |
||||
|
|
||||
|
/*! |
||||
|
* Destructor of MappedFile. |
||||
|
* Will unmap the data and close the file. |
||||
|
*/ |
||||
|
~MappedFile(); |
||||
|
|
||||
|
/*! |
||||
|
* @brief pointer to actual file content. |
||||
|
*/ |
||||
|
char* data; |
||||
|
|
||||
|
/*! |
||||
|
* @brief pointer to end of file content. |
||||
|
*/ |
||||
|
char* dataend; |
||||
|
|
||||
|
private: |
||||
|
|
||||
|
#if defined LINUX || defined MACOSX |
||||
|
/*! |
||||
|
* @brief file descriptor obtained by open(). |
||||
|
*/ |
||||
|
int file; |
||||
|
#elif defined WINDOWS |
||||
|
HANDLE file; |
||||
|
HANDLE mapping; |
||||
|
#endif |
||||
|
|
||||
|
#if defined LINUX |
||||
|
/*! |
||||
|
* @brief stat information about the file. |
||||
|
*/ |
||||
|
struct stat64 st; |
||||
|
#elif defined MACOSX |
||||
|
/*! |
||||
|
* @brief stat information about the file. |
||||
|
*/ |
||||
|
struct stat st; |
||||
|
#elif defined WINDOWS |
||||
|
/*! |
||||
|
* @brief stat information about the file. |
||||
|
*/ |
||||
|
struct __stat64 st; |
||||
|
#endif |
||||
|
}; |
||||
|
} // namespace parser |
||||
|
} // namespace storm |
||||
|
|
||||
|
|
||||
|
|
||||
|
#endif /* STORM_PARSER_MAPPEDFILE_H_ */ |
Reference in new issue
xxxxxxxxxx