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.

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