You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.7 KiB

  1. /*
  2. * MappedFile.cpp
  3. *
  4. * Created on: Jan 21, 2014
  5. * Author: Manuel Sascha Weiand
  6. */
  7. #include "src/parser/MappedFile.h"
  8. #include <fstream>
  9. #include <cstring>
  10. #include <fcntl.h>
  11. #include <boost/integer/integer_mask.hpp>
  12. #include "src/exceptions/FileIoException.h"
  13. #include "src/utility/macros.h"
  14. namespace storm {
  15. namespace parser {
  16. MappedFile::MappedFile(const char* filename) {
  17. #if defined LINUX || defined MACOSX
  18. // Do file mapping for reasonable systems.
  19. // stat64(), open(), mmap()
  20. #ifdef MACOSX
  21. if (stat(filename, &(this->st)) != 0) {
  22. #else
  23. if (stat64(filename, &(this->st)) != 0) {
  24. #endif
  25. STORM_LOG_ERROR("Error in stat(" << filename << "): Probably, this file does not exist.");
  26. throw exceptions::FileIoException() << "MappedFile Error in stat(): Probably, this file does not exist.";
  27. }
  28. this->file = open(filename, O_RDONLY);
  29. if (this->file < 0) {
  30. STORM_LOG_ERROR("Error in open(" << filename << "): Probably, we may not read this file.");
  31. throw exceptions::FileIoException() << "MappedFile Error in open(): Probably, we may not read this file.";
  32. }
  33. this->data = static_cast<char*>(mmap(NULL, this->st.st_size, PROT_READ, MAP_PRIVATE, this->file, 0));
  34. if (this->data == MAP_FAILED) {
  35. close(this->file);
  36. STORM_LOG_ERROR("Error in mmap(" << filename << "): " << std::strerror(errno));
  37. throw exceptions::FileIoException() << "MappedFile Error in mmap(): " << std::strerror(errno);
  38. }
  39. this->dataEnd = this->data + this->st.st_size;
  40. #elif defined WINDOWS
  41. // Do file mapping for windows.
  42. // _stat64(), CreateFile(), CreateFileMapping(), MapViewOfFile()
  43. if (_stat64(filename, &(this->st)) != 0) {
  44. STORM_LOG_ERROR("Error in _stat(" << filename << "): Probably, this file does not exist.");
  45. throw exceptions::FileIoException("MappedFile Error in stat(): Probably, this file does not exist.");
  46. }
  47. this->file = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  48. if (this->file == INVALID_HANDLE_VALUE) {
  49. STORM_LOG_ERROR("Error in CreateFileA(" << filename << "): Probably, we may not read this file.");
  50. throw exceptions::FileIoException("MappedFile Error in CreateFileA(): Probably, we may not read this file.");
  51. }
  52. this->mapping = CreateFileMappingA(this->file, NULL, PAGE_READONLY, (DWORD)(st.st_size >> 32), (DWORD)st.st_size, NULL);
  53. if (this->mapping == NULL) {
  54. CloseHandle(this->file);
  55. STORM_LOG_ERROR("Error in CreateFileMappingA(" << filename << ").");
  56. throw exceptions::FileIoException("MappedFile Error in CreateFileMappingA().");
  57. }
  58. this->data = static_cast<char*>(MapViewOfFile(this->mapping, FILE_MAP_READ, 0, 0, this->st.st_size));
  59. if (this->data == NULL) {
  60. CloseHandle(this->mapping);
  61. CloseHandle(this->file);
  62. STORM_LOG_ERROR("Error in MapViewOfFile(" << filename << ").");
  63. throw exceptions::FileIoException("MappedFile Error in MapViewOfFile().");
  64. }
  65. this->dataEnd = this->data + this->st.st_size;
  66. #endif
  67. }
  68. MappedFile::~MappedFile() {
  69. #if defined LINUX || defined MACOSX
  70. munmap(this->data, this->st.st_size);
  71. close(this->file);
  72. #elif defined WINDOWS
  73. CloseHandle(this->mapping);
  74. CloseHandle(this->file);
  75. #endif
  76. }
  77. bool MappedFile::fileExistsAndIsReadable(const char* filename) {
  78. // Test by opening an input file stream and testing the stream flags.
  79. std::ifstream fin(filename);
  80. return fin.good();
  81. }
  82. char const* MappedFile::getData() const {
  83. return data;
  84. }
  85. char const* MappedFile::getDataEnd() const {
  86. return dataEnd;
  87. }
  88. std::size_t MappedFile::getDataSize() const {
  89. return this->getDataEnd() - this->getData();
  90. }
  91. } // namespace parser
  92. } // namespace storm