KiCad PCB EDA Suite
Loading...
Searching...
No Matches
common/io.cpp
Go to the documentation of this file.
1/*
2* This program source code file is part of KiCad, a free EDA CAD application.
3*
4* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5*
6* This program is free software: you can redistribute it and/or modify it
7* under the terms of the GNU General Public License as published by the
8* Free Software Foundation, either version 3 of the License, or (at your
9* option) any later version.
10*
11* This program is distributed in the hope that it will be useful, but
12* WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14* General Public License for more details.
15*
16* You should have received a copy of the GNU General Public License along
17* with this program. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20#include <kiplatform/io.h>
21
22#include <wx/crt.h>
23#include <wx/string.h>
24
25#include <stdexcept>
26#include <string>
27
28
29void KIPLATFORM::IO::MAPPED_FILE::readIntoBuffer( const wxString& aFileName )
30{
31 FILE* fp = wxFopen( aFileName, wxS( "rb" ) );
32
33 if( !fp )
34 throw std::runtime_error( std::string( "Cannot open file: " ) + aFileName.ToStdString() );
35
36 fseek( fp, 0, SEEK_END );
37 long len = ftell( fp );
38
39 if( len < 0 )
40 {
41 fclose( fp );
42 throw std::runtime_error( std::string( "Cannot determine file size: " )
43 + aFileName.ToStdString() );
44 }
45
46 m_fallbackBuffer.resize( static_cast<size_t>( len ) );
47 fseek( fp, 0, SEEK_SET );
48
49 size_t bytesRead = fread( m_fallbackBuffer.data(), 1, static_cast<size_t>( len ), fp );
50 fclose( fp );
51
52 if( bytesRead != static_cast<size_t>( len ) )
53 {
54 throw std::runtime_error( std::string( "Failed to read file: " )
55 + aFileName.ToStdString() );
56 }
57
58 m_data = m_fallbackBuffer.data();
59 m_size = m_fallbackBuffer.size();
60}
void readIntoBuffer(const wxString &aFileName)
Definition common/io.cpp:29
const uint8_t * m_data
Definition io.h:56
std::vector< uint8_t > m_fallbackBuffer
Definition io.h:66