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.

121 lines
2.8 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. *
  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. #if !defined( WIN32 ) || !defined( __GNUC__ )
  20. #error streamwrapper.cpp should not be included in this build
  21. #endif
  22. #include "streamwrapper.h"
  23. #include <wx/string.h>
  24. #include <iostream>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. kicad::stream::stream()
  29. {
  30. m_buf = NULL;
  31. m_stream = NULL;
  32. return;
  33. }
  34. kicad::stream::~stream()
  35. {
  36. if( NULL != m_stream )
  37. delete m_stream;
  38. if( NULL != m_buf )
  39. {
  40. m_buf->close(); // ensure file is closed regardless of m_buf's destructor
  41. delete m_buf;
  42. }
  43. return;
  44. }
  45. std::iostream* kicad::stream::Open( const char* aFileName, std::ios_base::openmode aMode )
  46. {
  47. if( NULL != m_stream )
  48. {
  49. delete m_stream;
  50. m_stream = NULL;
  51. }
  52. if( NULL != m_buf )
  53. {
  54. m_buf->close();
  55. delete m_buf;
  56. }
  57. int flags = 0;
  58. if( aMode & std::ios_base::app )
  59. flags |= _O_APPEND;
  60. if( aMode & std::ios_base::out && aMode & std::ios_base::in )
  61. flags |= _O_RDWR;
  62. else if( aMode & std::ios_base::out )
  63. flags |= _O_WRONLY;
  64. else if( aMode & std::ios_base::in )
  65. flags |= _O_RDONLY;
  66. if( aMode & std::ios_base::binary )
  67. flags |= _O_BINARY;
  68. if( aMode & std::ios_base::out && aMode & std::ios_base::trunc
  69. && !( aMode & std::ios_base::app ) && !( aMode & std::ios_base::ate ) )
  70. flags |= _O_TRUNC;
  71. if( aMode & std::ios_base::out )
  72. flags |= _O_CREAT;
  73. //int fd = open( "testfile.txt", flags, S_IRUSR | S_IWUSR );
  74. wxString lstr( wxString::FromUTF8Unchecked( aFileName ) );
  75. int fd = _wopen( lstr.wc_str(), flags, _S_IREAD | _S_IWRITE );
  76. if( fd >= 0 && aMode & std::ios_base::ate )
  77. lseek( fd, 0, SEEK_END );
  78. // NOTE: _O_RDONLY in Windows, O_RDONLY in Linux
  79. m_buf = new __gnu_cxx::stdio_filebuf<char>( fd, aMode );
  80. m_stream = new std::iostream( m_buf );
  81. return m_stream;
  82. }
  83. void kicad::stream::Close( void )
  84. {
  85. if( m_buf )
  86. m_buf->close();
  87. return;
  88. }
  89. std::iostream* kicad::stream::GetStream( void )
  90. {
  91. return m_stream;
  92. }