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.

120 lines
2.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 Cirilo Bernardo <cirilo.bernardo@gmail.com>
  5. * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation, either version 3 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * @file streamwrapper.cpp
  22. */
  23. #if !defined( _WIN32 ) || !defined( __GNUC__ )
  24. #error streamwrapper.cpp should not be included in this build
  25. #endif
  26. #include "streamwrapper.h"
  27. #include <wx/string.h>
  28. #include <iostream>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <fcntl.h>
  32. kicad::stream::stream()
  33. {
  34. m_buf = nullptr;
  35. m_stream = nullptr;
  36. }
  37. kicad::stream::~stream()
  38. {
  39. if( nullptr != m_stream )
  40. delete m_stream;
  41. if( nullptr != m_buf )
  42. {
  43. m_buf->close(); // ensure file is closed regardless of m_buf's destructor
  44. delete m_buf;
  45. }
  46. }
  47. std::iostream* kicad::stream::Open( const char* aFileName, std::ios_base::openmode aMode )
  48. {
  49. if( nullptr != m_stream )
  50. {
  51. delete m_stream;
  52. m_stream = nullptr;
  53. }
  54. if( nullptr != m_buf )
  55. {
  56. m_buf->close();
  57. delete m_buf;
  58. }
  59. int flags = 0;
  60. if( aMode & std::ios_base::app )
  61. flags |= _O_APPEND;
  62. if( aMode & std::ios_base::out && aMode & std::ios_base::in )
  63. flags |= _O_RDWR;
  64. else if( aMode & std::ios_base::out )
  65. flags |= _O_WRONLY;
  66. else if( aMode & std::ios_base::in )
  67. flags |= _O_RDONLY;
  68. if( aMode & std::ios_base::binary )
  69. flags |= _O_BINARY;
  70. if( aMode & std::ios_base::out && aMode & std::ios_base::trunc
  71. && !( aMode & std::ios_base::app ) && !( aMode & std::ios_base::ate ) )
  72. flags |= _O_TRUNC;
  73. if( aMode & std::ios_base::out )
  74. flags |= _O_CREAT;
  75. //int fd = open( "testfile.txt", flags, S_IRUSR | S_IWUSR );
  76. wxString lstr( wxString::FromUTF8Unchecked( aFileName ) );
  77. int fd = _wopen( lstr.wc_str(), flags, _S_IREAD | _S_IWRITE );
  78. if( fd >= 0 && aMode & std::ios_base::ate )
  79. lseek( fd, 0, SEEK_END );
  80. // NOTE: _O_RDONLY in Windows, O_RDONLY in Linux
  81. m_buf = new __gnu_cxx::stdio_filebuf<char>( fd, aMode );
  82. m_stream = new std::iostream( m_buf );
  83. return m_stream;
  84. }
  85. void kicad::stream::Close( void )
  86. {
  87. if( m_buf )
  88. m_buf->close();
  89. }
  90. std::iostream* kicad::stream::GetStream( void )
  91. {
  92. return m_stream;
  93. }