From f87ae01d3b409ddd4fb619bf3221bdcea7a77cf0 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 18 Feb 2015 21:18:20 -0500 Subject: [PATCH] Removed mmap.h dependency. Close #1. --- lint/mmap.h | 149 ------------------------------------------------ lint/peglint.cc | 23 ++++++-- peglib.h | 1 + 3 files changed, 18 insertions(+), 155 deletions(-) delete mode 100644 lint/mmap.h diff --git a/lint/mmap.h b/lint/mmap.h deleted file mode 100644 index e19333c..0000000 --- a/lint/mmap.h +++ /dev/null @@ -1,149 +0,0 @@ - -#ifndef _MMAP_H_ -#define _MMAP_H_ - -#if defined(_MSC_VER) -#include -#else -#include -#include -#include -#include -#endif - -class MemoryMappedFile -{ -public: - MemoryMappedFile(const char* path); - ~MemoryMappedFile(); - - bool is_open() const; - size_t size() const; - const char* data() const; - -private: - void cleanup(); - -#if defined(_MSC_VER) - HANDLE hFile_; - HANDLE hMapping_; -#else - int fd_; -#endif - size_t size_; - void* addr_; -}; - -#if defined(_MSC_VER) -#define MAP_FAILED NULL -#endif - -inline MemoryMappedFile::MemoryMappedFile(const char* path) -#if defined(_MSC_VER) - : hFile_(NULL) - , hMapping_(NULL) -#else - : fd_(-1) -#endif - , size_(0) - , addr_(MAP_FAILED) -{ -#if defined(_MSC_VER) - hFile_ = ::CreateFileA( - path, - GENERIC_READ, - FILE_SHARE_READ, - NULL, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - NULL); - - if (hFile_ == INVALID_HANDLE_VALUE) { - std::runtime_error(""); - } - - size_ = ::GetFileSize(hFile_, NULL); - - hMapping_ = ::CreateFileMapping(hFile_, NULL, PAGE_READONLY, 0, 0, NULL); - - if (hMapping_ == NULL) { - cleanup(); - std::runtime_error(""); - } - - addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0); -#else - fd_ = open(path, O_RDONLY); - if (fd_ == -1) { - std::runtime_error(""); - } - - struct stat sb; - if (fstat(fd_, &sb) == -1) { - cleanup(); - std::runtime_error(""); - } - size_ = sb.st_size; - - addr_ = mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0); -#endif - - if (addr_ == MAP_FAILED) { - cleanup(); - std::runtime_error(""); - } -} - -inline MemoryMappedFile::~MemoryMappedFile() -{ - cleanup(); -} - -inline bool MemoryMappedFile::is_open() const -{ - return addr_ != MAP_FAILED; -} - -inline size_t MemoryMappedFile::size() const -{ - return size_; -} - -inline const char* MemoryMappedFile::data() const -{ - return (const char*)addr_; -} - -inline void MemoryMappedFile::cleanup() -{ -#if defined(_MSC_VER) - if (addr_) { - ::UnmapViewOfFile(addr_); - addr_ = MAP_FAILED; - } - - if (hMapping_) { - ::CloseHandle(hMapping_); - hMapping_ = NULL; - } - - if (hFile_ != INVALID_HANDLE_VALUE) { - ::CloseHandle(hFile_); - hFile_ = INVALID_HANDLE_VALUE; - } -#else - if (addr_ != MAP_FAILED) { - munmap(addr_, size_); - addr_ = MAP_FAILED; - } - - if (fd_ != -1) { - close(fd_); - fd_ = -1; - } -#endif -} - -#endif // _MMAP_H_ - -// vim: et ts=4 sw=4 cin cino={1s ff=unix diff --git a/lint/peglint.cc b/lint/peglint.cc index 1436554..586c175 100644 --- a/lint/peglint.cc +++ b/lint/peglint.cc @@ -6,11 +6,22 @@ // #include -#include -#include "mmap.h" +#include using namespace std; +bool read_file(const char* path, vector& buff) +{ + ifstream ifs(path, ios::in | ios::binary); + if (ifs.fail()) { + return false; + } + + buff.resize(ifs.seekg(0, ios::end).tellg()); + ifs.seekg(0, ios::beg).read(&buff[0], static_cast(buff.size())); + return true; +} + int main(int argc, const char** argv) { if (argc < 2 || string("--help") == argv[1]) { @@ -21,8 +32,8 @@ int main(int argc, const char** argv) // Check PEG grammar auto syntax_path = argv[1]; - MemoryMappedFile syntax(syntax_path); - if (!syntax.is_open()) { + vector syntax; + if (!read_file(syntax_path, syntax)) { cerr << "can't open the grammar file." << endl; return -1; } @@ -42,8 +53,8 @@ int main(int argc, const char** argv) // Check source auto source_path = argv[2]; - MemoryMappedFile source(source_path); - if (!source.is_open()) { + vector source; + if (!read_file(source_path, source)) { cerr << "can't open the source file." << endl; return -1; } diff --git a/peglib.h b/peglib.h index 823e9b8..8114099 100644 --- a/peglib.h +++ b/peglib.h @@ -10,6 +10,7 @@ #include #include +#include #include #include #include