2022-08-20 00:01:36 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2023-05-11 10:51:12 +00:00
|
|
|
* with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-08-20 00:01:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <kiplatform/io.h>
|
|
|
|
|
|
|
|
#include <wx/string.h>
|
2023-05-11 10:51:12 +00:00
|
|
|
#include <wx/wxcrt.h>
|
2022-08-20 00:01:36 +00:00
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
2023-05-11 10:51:12 +00:00
|
|
|
// Define USE_MSYS2_FALlBACK if the code for _MSC_VER does not compile on msys2
|
|
|
|
//#define USE_MSYS2_FALLBACK
|
2022-08-20 00:01:36 +00:00
|
|
|
|
2023-05-11 10:51:12 +00:00
|
|
|
FILE* KIPLATFORM::IO::SeqFOpen( const wxString& aPath, const wxString& aMode )
|
2022-08-20 00:01:36 +00:00
|
|
|
{
|
2023-05-11 10:51:12 +00:00
|
|
|
#if defined( _MSC_VER ) || !defined( USE_MSYS2_FALLBACK )
|
2022-08-20 00:01:36 +00:00
|
|
|
// We need to use the win32 api to setup a file handle with sequential scan flagged
|
|
|
|
// and pass it up the chain to create a normal FILE stream
|
|
|
|
HANDLE hFile = INVALID_HANDLE_VALUE;
|
|
|
|
hFile = CreateFileW( aPath.wc_str(),
|
|
|
|
GENERIC_READ,
|
|
|
|
FILE_SHARE_READ,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_FLAG_SEQUENTIAL_SCAN,
|
|
|
|
NULL );
|
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = _open_osfhandle( reinterpret_cast<intptr_t>( hFile ), 0 );
|
|
|
|
|
|
|
|
if( fd == -1 )
|
|
|
|
{
|
|
|
|
// close the handle manually as the ownership didnt transfer
|
|
|
|
CloseHandle( hFile );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-05-11 10:51:12 +00:00
|
|
|
FILE* fp = _fdopen( fd, aMode.c_str() );
|
2022-08-20 00:01:36 +00:00
|
|
|
|
|
|
|
if( !fp )
|
|
|
|
{
|
|
|
|
// close the file descriptor manually as the ownership didnt transfer
|
|
|
|
_close( fd );
|
|
|
|
}
|
|
|
|
|
|
|
|
return fp;
|
|
|
|
#else
|
|
|
|
// Fallback for MSYS2
|
|
|
|
return wxFopen( aPath, aMode );
|
|
|
|
#endif
|
|
|
|
}
|