kicad/include/wxstream_helper.h

72 lines
1.9 KiB
C
Raw Normal View History

2020-10-26 10:54:36 +00:00
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2020 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
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef WXSTREAM_HELPER_H
#define WXSTREAM_HELPER_H
#include <vector>
2020-10-26 10:54:36 +00:00
#include <wx/log.h>
#include <wx/wfstream.h>
static bool CopyStreamData( wxInputStream& inputStream, wxOutputStream& outputStream,
wxFileOffset size )
{
constexpr size_t bufSize = 128 * 1024;
std::vector<wxChar> buf( bufSize );
2020-10-26 10:54:36 +00:00
wxFileOffset copiedData = 0;
wxFileOffset readSize = bufSize;
2020-10-26 10:54:36 +00:00
for( ; ; )
{
if(size != -1 && copiedData + readSize > size )
readSize = size - copiedData;
inputStream.Read( buf.data(), readSize );
2020-10-26 10:54:36 +00:00
size_t actuallyRead = inputStream.LastRead();
outputStream.Write( buf.data(), actuallyRead );
2020-10-26 10:54:36 +00:00
if( outputStream.LastWrite() != actuallyRead )
{
wxLogError( _("Failed to output data") );
//return false;
}
if( size == -1 )
{
if( inputStream.Eof() )
break;
}
else
{
copiedData += actuallyRead;
2020-10-26 10:54:36 +00:00
if( copiedData >= size )
break;
}
}
return true;
}
#endif // WXSTREAM_HELPER_H