diff --git a/include/gemmi/third_party/tao/NOTES b/include/gemmi/third_party/tao/NOTES index f1dea94b..15b8b130 100644 --- a/include/gemmi/third_party/tao/NOTES +++ b/include/gemmi/third_party/tao/NOTES @@ -1,8 +1,11 @@ This directory contains files from PEGTL 2.8.3: - LICENSE and include/tao/* except include/tao/pegtl/contrib -with minor local modifications (in position.hpp and input_error.hpp): + LICENSE and include/tao/pegtl* except contrib/ and file_mapper_win32.hpp +with minor modifications (in position.hpp, input_error.hpp, file_input.hpp): - changed formatting of error messages, - using std::system_error instead of pegtl::input_error. +- avoiding mmap on Windows (until we upgrade to PEGTL v3), + because PEGTL v2 API doesn't support Unicode filenames + (and also, including has side-effects) PEGTL can be updated to v3 only when gemmi drops support for C++11/14. https://github.com/project-gemmi/gemmi/issues/212 diff --git a/include/gemmi/third_party/tao/pegtl/file_input.hpp b/include/gemmi/third_party/tao/pegtl/file_input.hpp index b2c66d9b..248e7039 100644 --- a/include/gemmi/third_party/tao/pegtl/file_input.hpp +++ b/include/gemmi/third_party/tao/pegtl/file_input.hpp @@ -12,7 +12,7 @@ #include // Required for _POSIX_MAPPED_FILES #endif -#if defined( _POSIX_MAPPED_FILES ) || defined( _WIN32 ) +#if defined( _POSIX_MAPPED_FILES ) #include "mmap_input.hpp" #else #include "read_input.hpp" @@ -22,7 +22,7 @@ namespace tao { namespace TAO_PEGTL_NAMESPACE { -#if defined( _POSIX_MAPPED_FILES ) || defined( _WIN32 ) +#if defined( _POSIX_MAPPED_FILES ) template< tracking_mode P = tracking_mode::eager, typename Eol = eol::lf_crlf > struct file_input : mmap_input< P, Eol > diff --git a/include/gemmi/third_party/tao/pegtl/internal/file_mapper_win32.hpp b/include/gemmi/third_party/tao/pegtl/internal/file_mapper_win32.hpp deleted file mode 100644 index a8cc1a4e..00000000 --- a/include/gemmi/third_party/tao/pegtl/internal/file_mapper_win32.hpp +++ /dev/null @@ -1,226 +0,0 @@ -// Copyright (c) 2014-2020 Dr. Colin Hirsch and Daniel Frey -// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/ - -#ifndef TAO_PEGTL_INTERNAL_FILE_MAPPER_WIN32_HPP -#define TAO_PEGTL_INTERNAL_FILE_MAPPER_WIN32_HPP - -#if !defined( NOMINMAX ) -#define NOMINMAX -#define TAO_PEGTL_NOMINMAX_WAS_DEFINED -#endif - -#if !defined( WIN32_LEAN_AND_MEAN ) -#define WIN32_LEAN_AND_MEAN -#define TAO_PEGTL_WIN32_LEAN_AND_MEAN_WAS_DEFINED -#endif - -#include - -#if defined( TAO_PEGTL_NOMINMAX_WAS_DEFINED ) -#undef NOMINMAX -#undef TAO_PEGTL_NOMINMAX_WAS_DEFINED -#endif - -#if defined( TAO_PEGTL_WIN32_LEAN_AND_MEAN_WAS_DEFINED ) -#undef WIN32_LEAN_AND_MEAN -#undef TAO_PEGTL_WIN32_LEAN_AND_MEAN_WAS_DEFINED -#endif - -#include "../config.hpp" -#include "../input_error.hpp" - -namespace tao -{ - namespace TAO_PEGTL_NAMESPACE - { - namespace internal - { - struct win32_file_opener - { - explicit win32_file_opener( const char* filename ) - : m_source( filename ), - m_handle( open() ) - { - } - - win32_file_opener( const win32_file_opener& ) = delete; - win32_file_opener( win32_file_opener&& ) = delete; - - ~win32_file_opener() noexcept - { - ::CloseHandle( m_handle ); - } - - void operator=( const win32_file_opener& ) = delete; - void operator=( win32_file_opener&& ) = delete; - - std::size_t size() const - { - LARGE_INTEGER size; - if( !::GetFileSizeEx( m_handle, &size ) ) { - TAO_PEGTL_THROW_INPUT_WIN32_ERROR( "unable to GetFileSizeEx() file " << m_source << " handle " << m_handle ); - } - return std::size_t( size.QuadPart ); - } - - const char* const m_source; - const HANDLE m_handle; - - private: - HANDLE open() const - { - SetLastError( 0 ); - std::wstring ws( m_source, m_source + strlen( m_source ) ); - -#if( _WIN32_WINNT >= 0x0602 ) - const HANDLE handle = ::CreateFile2( ws.c_str(), - GENERIC_READ, - FILE_SHARE_READ, - OPEN_EXISTING, - nullptr ); - if( handle != INVALID_HANDLE_VALUE ) { - return handle; - } - TAO_PEGTL_THROW_INPUT_WIN32_ERROR( "CreateFile2() failed opening file " << m_source << " for reading" ); -#else - const HANDLE handle = ::CreateFileW( ws.c_str(), - GENERIC_READ, - FILE_SHARE_READ, - nullptr, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - nullptr ); - if( handle != INVALID_HANDLE_VALUE ) { - return handle; - } - TAO_PEGTL_THROW_INPUT_WIN32_ERROR( "CreateFileW() failed opening file " << m_source << " for reading" ); -#endif - } - }; - - struct win32_file_mapper - { - explicit win32_file_mapper( const char* filename ) - : win32_file_mapper( win32_file_opener( filename ) ) - { - } - - explicit win32_file_mapper( const win32_file_opener& reader ) - : m_size( reader.size() ), - m_handle( open( reader ) ) - { - } - - win32_file_mapper( const win32_file_mapper& ) = delete; - win32_file_mapper( win32_file_mapper&& ) = delete; - - ~win32_file_mapper() noexcept - { - ::CloseHandle( m_handle ); - } - - void operator=( const win32_file_mapper& ) = delete; - void operator=( win32_file_mapper&& ) = delete; - - const size_t m_size; - const HANDLE m_handle; - - private: - HANDLE open( const win32_file_opener& reader ) const - { - const uint64_t file_size = reader.size(); - SetLastError( 0 ); - // Use `CreateFileMappingW` because a) we're not specifying a - // mapping name, so the character type is of no consequence, and - // b) it's defined in `memoryapi.h`, unlike - // `CreateFileMappingA`(?!) - const HANDLE handle = ::CreateFileMappingW( reader.m_handle, - nullptr, - PAGE_READONLY, - DWORD( file_size >> 32 ), - DWORD( file_size & 0xffffffff ), - nullptr ); - if( handle != NULL || file_size == 0 ) { - return handle; - } - TAO_PEGTL_THROW_INPUT_WIN32_ERROR( "unable to CreateFileMappingW() file " << reader.m_source << " for reading" ); - } - }; - - class file_mapper - { - public: - explicit file_mapper( const char* filename ) - : file_mapper( win32_file_mapper( filename ) ) - { - } - - explicit file_mapper( const win32_file_mapper& mapper ) - : m_size( mapper.m_size ), - m_data( static_cast< const char* >( ::MapViewOfFile( mapper.m_handle, - FILE_MAP_READ, - 0, - 0, - 0 ) ) ) - { - if( ( m_size != 0 ) && ( intptr_t( m_data ) == 0 ) ) { - TAO_PEGTL_THROW_INPUT_WIN32_ERROR( "unable to MapViewOfFile() file mapping object with handle " << mapper.m_handle ); - } - } - - file_mapper( const file_mapper& ) = delete; - file_mapper( file_mapper&& ) = delete; - - ~file_mapper() noexcept - { - ::UnmapViewOfFile( LPCVOID( m_data ) ); - } - - void operator=( const file_mapper& ) = delete; - void operator=( file_mapper&& ) = delete; - - bool empty() const noexcept - { - return m_size == 0; - } - - std::size_t size() const noexcept - { - return m_size; - } - - using iterator = const char*; - using const_iterator = const char*; - - iterator data() const noexcept - { - return m_data; - } - - iterator begin() const noexcept - { - return m_data; - } - - iterator end() const noexcept - { - return m_data + m_size; - } - - std::string string() const - { - return std::string( m_data, m_size ); - } - - private: - const std::size_t m_size; - const char* const m_data; - }; - - } // namespace internal - - } // namespace TAO_PEGTL_NAMESPACE - -} // namespace tao - -#endif