-
Notifications
You must be signed in to change notification settings - Fork 24
Add ipr::input::SourceFile
#307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // -*- C++ -*- | ||
| // | ||
| // This file is part of The Pivot framework. | ||
| // Written by Gabriel Dos Reis. | ||
| // See LICENSE for copyright and license notices. | ||
| // | ||
|
|
||
| #include <cstddef> | ||
| #include <span> | ||
| #include <filesystem> | ||
|
|
||
| namespace ipr::input { | ||
| // Type for the error code values used by the host OS. | ||
| #ifdef _WIN32 | ||
| using ErrorCode = DWORD; | ||
| #else | ||
| using ErrorCode = int; | ||
| #endif | ||
|
|
||
| // String type preferred by the host OS to specify pathnames. | ||
| using SystemPath = std::filesystem::path::string_type; | ||
|
|
||
| // Exception type used to signal inability of the host OS to access a file. | ||
| struct AccessError { | ||
| SystemPath path; | ||
| ErrorCode error_code; | ||
| }; | ||
|
|
||
| // Exception type used to signal the file designated by th `path` is not a regular file. | ||
| struct RegularFileError { | ||
| SystemPath path; | ||
| }; | ||
|
|
||
| // Exception type used to signal inability of the host OS to memory-map a file. | ||
| struct FileMappingError { | ||
| SystemPath path; | ||
| ErrorCode error_code; | ||
| }; | ||
|
|
||
| // Input source file mapped to memory as sequence of raw bytes. | ||
| struct SourceFile { | ||
| using View = std::span<const std::byte>; | ||
|
|
||
| explicit SourceFile(const SystemPath&); | ||
| SourceFile(SourceFile&&); | ||
| ~SourceFile(); | ||
| View bytes() const { return view; } | ||
| private: | ||
| View view; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // | ||
| // This file is part of The Pivot framework. | ||
| // Written by Gabriel Dos Reis. | ||
| // See LICENSE for copright and license notices. | ||
| // | ||
|
|
||
| #ifdef _WIN32 | ||
| # include <windows.h> | ||
| #else | ||
| # include <sys/stat.h> | ||
| # include <sys/mman.h> | ||
| # include <fcntl.h> | ||
| # include <unistd.h> | ||
| #endif | ||
|
|
||
| #include <ipr/input> | ||
|
|
||
| namespace ipr::input { | ||
| #ifdef _WIN32 | ||
| // Helper type for automatically closing a handle on scope exit. | ||
| struct SystemHandle { | ||
| SystemHandle(HANDLE h) : handle{h} { } | ||
| bool valid() const { return handle != INVALID_HANDLE_VALUE; } | ||
| auto get_handle() const { return handle; } | ||
| ~SystemHandle() | ||
| { | ||
| if (valid()) | ||
|
||
| CloseHandle(handle); | ||
| } | ||
| private: | ||
| HANDLE handle; | ||
| }; | ||
| #endif | ||
|
|
||
| SourceFile::SourceFile(const SystemPath& path) | ||
| { | ||
| #ifdef _WIN32 | ||
| // FIXME: Handle the situation of large files in a 32-bit program. | ||
| static_assert(sizeof(LARGE_INTEGER) == sizeof(std::size_t)); | ||
|
|
||
| SystemHandle file = CreateFileW(path.c_str(), GENERIC_READ, 0, nullptr, | ||
| OPEN_EXISTING, | ||
| FILE_ATTRIBUTE_NORMAL, nullptr); | ||
| if (not file.valid()) | ||
| throw AccessError{ path, GetLastError() }; | ||
| LARGE_INTEGER s { }; | ||
| if (not GetFileSizeEx(file.get_handle(), &s)) | ||
| throw AccessError{ path, GetLastError() }; | ||
| if (s.QuadPart) | ||
| return; | ||
| SystemHandle mapping = CreateFileMapping(file.get_handle(), nullptr, PAGE_READONLY, 0, 0, nullptr); | ||
| if (mapping.get_handle() == nullptr) | ||
| throw FileMappingError{ path, GetLastError() }; | ||
| auto start = MapViewOfFile(mapping.get_handle(), FILE_MAP_READ, 0, 0, 0); | ||
| view = { reinterpret_cast<const std::byte*>(start), static_cast<View::size_type>(s.QuadPart) }; | ||
|
||
| #else | ||
| struct stat s { }; | ||
| errno = 0; | ||
| if (stat(path.c_str(), &s) < 0) | ||
| throw AccessError{ path, errno }; | ||
| else if (not S_ISREG(s.st_mode)) | ||
| throw RegularFileError{ path }; | ||
|
|
||
| // Don't labor too hard with empty files. | ||
| if (s.st_size == 0) | ||
| return; | ||
|
|
||
| auto fd = open(path.c_str(), O_RDONLY); | ||
| if (fd < 0) | ||
| throw AccessError{ path, errno }; | ||
| auto start = mmap(nullptr, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0); | ||
| close(fd); | ||
| if (start == MAP_FAILED) | ||
| throw FileMappingError{ path }; | ||
| view = { reinterpret_cast<std::byte*>(start), static_cast<View::size_type>(s.st_size) }; | ||
| #endif | ||
| } | ||
|
|
||
| SourceFile::SourceFile(SourceFile&& src) : view{src.view} | ||
Check warningCode scanning / PREfast This kind of function should not throw. Declare it 'noexcept' (f.6). Warning
This kind of function should not throw. Declare it 'noexcept' (f.6).
|
||
| { | ||
| src.view = { }; | ||
| } | ||
|
|
||
| SourceFile::~SourceFile() | ||
| { | ||
| if (not view.empty()) | ||
| { | ||
| #ifdef _WIN32 | ||
| UnmapViewOfFile(view.data()); | ||
| #else | ||
| munmap(const_cast<std::byte*>(view.data()), view.size()); | ||
| #endif | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.