You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 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 <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef WXSTREAM_HELPER_H
  20. #define WXSTREAM_HELPER_H
  21. #include <vector>
  22. #include <wx/log.h>
  23. #include <wx/wfstream.h>
  24. static bool CopyStreamData( wxInputStream& inputStream, wxOutputStream& outputStream,
  25. wxFileOffset size )
  26. {
  27. constexpr size_t bufSize = 128 * 1024;
  28. std::vector<wxChar> buf( bufSize );
  29. wxFileOffset copiedData = 0;
  30. wxFileOffset readSize = bufSize;
  31. for( ; ; )
  32. {
  33. if(size != -1 && copiedData + readSize > size )
  34. readSize = size - copiedData;
  35. inputStream.Read( buf.data(), readSize );
  36. size_t actuallyRead = inputStream.LastRead();
  37. outputStream.Write( buf.data(), actuallyRead );
  38. if( outputStream.LastWrite() != actuallyRead )
  39. {
  40. wxLogError( _("Failed to output data") );
  41. //return false;
  42. }
  43. if( size == -1 )
  44. {
  45. if( inputStream.Eof() )
  46. break;
  47. }
  48. else
  49. {
  50. copiedData += actuallyRead;
  51. if( copiedData >= size )
  52. break;
  53. }
  54. }
  55. return true;
  56. }
  57. #endif // WXSTREAM_HELPER_H